def get_header(self) -> str: workflow_name = self.request.session.get('workflow_name', None) next_step = self.request.session.get('workflow_ui_step', None) header = self.header if workflow_name is not None: workflow_skillet_dict = snippet_utils.load_snippet_with_name(workflow_name, self.app_dir) if workflow_skillet_dict is not None: header = workflow_skillet_dict.get('label', self.header) if next_step is None: return header else: return f"Step {next_step}: {header}"
def get_context_data(self, **kwargs): hostname = self.kwargs['hostname'] minion = self.kwargs['minion'] service = snippet_utils.load_snippet_with_name('delete_single_vm', self.app_dir) jinja_context = dict() for v in service['variables']: if kwargs.get(v['name']): jinja_context[v['name']] = kwargs.get(v['name']) template = snippet_utils.render_snippet_template( service, self.app_dir, jinja_context) salt_util = salt_utils.SaltUtil() res = salt_util.deploy_template(template) print(res) print('deleting hostname %s' % hostname) context = dict() context['base_html'] = self.base_html context['results'] = res return context
def get(self, request, *args, **kwargs) -> Any: """ """ mode = self.get_value_from_workflow('mode', 'online') workflow_name = self.request.session.get('workflow_name', False) if mode == 'online' and workflow_name: if {'TARGET_IP', 'TARGET_USERNAME', 'TARGET_PASSWORD'}.issubset(self.get_workflow().keys()): print('Skipping validation input as we already have this information cached') snippet = self.get_value_from_workflow('snippet_name', None) self.meta = snippet_utils.load_snippet_with_name(snippet, self.app_dir) target_ip = self.get_value_from_workflow('TARGET_IP', '') # target_port = self.get_value_from_workflow('TARGET_PORT', 443) target_username = self.get_value_from_workflow('TARGET_USERNAME', '') target_password = self.get_value_from_workflow('TARGET_PASSWORD', '') data = {'TARGET_IP': target_ip, 'TARGET_USERNAME': target_username, 'TARGET_PASSWORD': target_password} form = self.generate_dynamic_form(data=data) # this is part of a workflow, and we already have this information in the context return self.form_valid(form) return super().get(request, *args, **kwargs)
def form_valid(self, form): """ form_valid is always called on a blank / new form, so this is essentially going to get called on every POST self.request.POST should contain all the variables defined in the service identified by the hidden field 'service_id' :param form: blank form data from request :return: render of a success template after service is provisioned """ snippet_name = self.get_value_from_workflow('snippet_name', '') mode = self.get_value_from_workflow('mode', 'online') if snippet_name == '': print('Could not find a valid meta-cnc def') raise SnippetRequiredException meta = snippet_utils.load_snippet_with_name(snippet_name, self.app_dir) context = dict() self.header = 'Validation Results' context['header'] = self.header context['title'] = meta['label'] context['base_html'] = self.base_html context['app_dir'] = self.app_dir context['view'] = self # Always grab all the default values, then update them based on user input in the workflow jinja_context = dict() if 'variables' in meta and type(meta['variables']) is list: for snippet_var in meta['variables']: jinja_context[snippet_var['name']] = snippet_var['default'] # let's grab the current workflow values (values saved from ALL forms in this app jinja_context.update(self.get_workflow()) if mode == 'online': # if we are in a workflow, then the input form was skipped and we are using the # values previously saved! workflow_name = self.request.session.get('workflow_name', False) if workflow_name: target_ip = self.get_value_from_workflow('TARGET_IP', '') # target_port = self.get_value_from_workflow('TARGET_PORT', 443) target_username = self.get_value_from_workflow('TARGET_USERNAME', '') target_password = self.get_value_from_workflow('TARGET_PASSWORD', '') else: # Grab the values from the form, this is always hard-coded in this class target_ip = self.request.POST.get('TARGET_IP', None) # target_port = self.request.POST.get('TARGET_IP', 443) target_username = self.request.POST.get('TARGET_USERNAME', None) target_password = self.request.POST.get('TARGET_PASSWORD', None) self.save_value_to_workflow('TARGET_IP', target_ip) # self.save_value_to_workflow('TARGET_PORT', target_port) self.save_value_to_workflow('TARGET_USERNAME', target_username) err_condition = False if target_ip is None or target_ip == '': form.add_error('TARGET_IP', 'Host entry cannot be blank') err_condition = True if target_username is None or target_username == '': form.add_error('TARGET_USERNAME', 'Username cannot be blank') err_condition = True if target_password is None or target_password == '': form.add_error('TARGET_PASSWORD', 'Password cannot be blank') err_condition = True if err_condition: return self.form_invalid(form) try: print(f'checking {target_ip} {target_username}') panoply = Panos(hostname=target_ip, api_username=target_username, api_password=target_password, debug=True) except LoginException as le: print(le) form.add_error('TARGET_USERNAME', 'Invalid Credentials, ensure your username and password are correct') form.add_error('TARGET_PASSWORD', 'Invalid Credentials, ensure your username and password are correct') return self.form_invalid(form) except PanoplyException: form.add_error('TARGET_IP', 'Connection Refused Error, check the IP and try again') return self.form_invalid(form) if not panoply.connected: form.add_error('TARGET_IP', 'Connection Refused Error, check the IP and try again') return self.form_invalid(form) else: config = self.request.POST.get('config', '') self.save_value_to_workflow('config', config) panoply = None jinja_context['config'] = config try: skillet = PanValidationSkillet(meta, panoply) results = list() skillet_output = skillet.execute(jinja_context) validation_output = skillet_output.get('pan_validation', dict()) for snippet in skillet.snippet_stack: name = snippet.get('name', '') cmd = snippet.get('cmd', '') if cmd != 'validate': print('skipping non-validation snippet') continue result_object = dict() if snippet['name'] in validation_output: result_object['name'] = name result_object['results'] = validation_output[name] else: result_object['name'] = name result_object['results'] = {} results.append(result_object) context['results'] = results except SkilletLoaderException: print(f"Could not load it for some reason") return render(self.request, 'pan_cnc/results.html', context) return render(self.request, 'panhandler/validation-results.html', context)
def form_valid(self, form): print('Here we go deploying %s' % self.app_dir) jinja_context = dict() service = snippet_utils.load_snippet_with_name('provision_firewall', self.app_dir) for v in service['variables']: if self.request.POST.get(v['name']): jinja_context[v['name']] = self.request.POST.get(v['name']) template = snippet_utils.render_snippet_template( service, self.app_dir, jinja_context) print(template) salt_util = salt_utils.SaltUtil() res = salt_util.deploy_template(template) print(res) context = dict() context['base_html'] = self.base_html context['title'] = 'Deploy Next Generation Firewall' context['header'] = 'Deployment Results' try: results_json = json.loads(res) except ValueError as ve: print('Could not load results from provisioner!') print(ve) context['results'] = 'Error deploying VM!' return render(self.request, 'pan_cnc/results.html', context=context) except TypeError as te: print('Could not load results from provisioner!') print(te) context['results'] = 'Error deploying VM!' return render(self.request, 'pan_cnc/results.html', context=context) if 'minion' not in jinja_context: context[ 'results'] = 'Error deploying VM! No compute node found in response' return render(self.request, 'pan_cnc/results.html', context=context) minion = jinja_context['minion'] if 'return' in results_json and minion in results_json['return'][0]: r = results_json['return'][0][minion] steps = r.keys() for step in steps: step_detail = r[step] if step_detail['result'] is not True: context[ 'results'] = 'Error deploying VM! Not all steps completed successfully!\n\n' context['results'] += step_detail['comment'] return render(self.request, 'pan_cnc/results.html', context=context) context['results'] = 'VM Deployed Successfully on CPE: %s' % minion return render(self.request, 'pan_cnc/results.html', context=context)