Example #1
0
 def create_skillet(self, skillet_dict: dict) -> Skillet:
     skillet_type = skillet_dict['type']
     if skillet_type == 'panos' or skillet_type == 'panorama' or skillet_type == 'panorama-gpcs':
         from skilletlib.skillet.panos import PanosSkillet
         return PanosSkillet(skillet_dict)
     elif skillet_type == 'pan_validation':
         from skilletlib.skillet.pan_validation import PanValidationSkillet
         return PanValidationSkillet(skillet_dict)
     elif skillet_type == 'python3':
         from skilletlib.skillet.python3 import Python3Skillet
         return Python3Skillet(skillet_dict)
     elif skillet_type == 'template':
         from skilletlib.skillet.template import TemplateSkillet
         return TemplateSkillet(skillet_dict)
     elif skillet_type == 'docker':
         from skilletlib.skillet.docker import DockerSkillet
         return DockerSkillet(skillet_dict)
     elif skillet_type == 'rest':
         from skilletlib.skillet.rest import RestSkillet
         return RestSkillet(skillet_dict)
     elif skillet_type == 'workflow':
         from skilletlib.skillet.workflow import WorkflowSkillet
         return WorkflowSkillet(skillet_dict, self)
     else:
         raise SkilletLoaderException('Unknown Skillet Type!')
Example #2
0
    def create_skillet(self, skillet_dict: dict) -> Skillet:
        """
        Creates a Skillet object from the given skillet definition

        :param skillet_dict: Dictionary loaded from the skillet.yaml definition file
        :return: Skillet Object
        """
        skillet_type = skillet_dict["type"]

        if skillet_type == "panos" or skillet_type == "panorama" or skillet_type == "panorama-gpcs":
            from skilletlib.skillet.panos import PanosSkillet

            return PanosSkillet(skillet_dict)

        elif skillet_type == "pan_validation":
            from skilletlib.skillet.pan_validation import PanValidationSkillet

            return PanValidationSkillet(skillet_dict)

        elif skillet_type == "python3":
            from skilletlib.skillet.python3 import Python3Skillet

            return Python3Skillet(skillet_dict)

        elif skillet_type == "template":
            from skilletlib.skillet.template import TemplateSkillet

            return TemplateSkillet(skillet_dict)

        elif skillet_type == "docker":
            from skilletlib.skillet.docker import DockerSkillet

            return DockerSkillet(skillet_dict)

        elif skillet_type == "rest":
            from skilletlib.skillet.rest import RestSkillet

            return RestSkillet(skillet_dict)

        elif skillet_type == "workflow":
            from skilletlib.skillet.workflow import WorkflowSkillet

            return WorkflowSkillet(skillet_dict, self)

        elif skillet_type == "terraform":
            from skilletlib.skillet.terraform import TerraformSkillet

            return TerraformSkillet(skillet_dict)

        elif skillet_type == "app":
            from skilletlib.skillet.app import AppSkillet

            return AppSkillet(skillet_dict)

        else:
            raise SkilletLoaderException(
                f"Unknown Skillet Type: {skillet_type}!")
Example #3
0
    def create_skillet(self, skillet_dict: dict) -> Skillet:
        """
        Creates a Skillet object from the given skillet definition

        :param skillet_dict: Dictionary loaded from the skillet.yaml definition file
        :return: Skillet Object
        """
        skillet_type = skillet_dict['type']

        if skillet_type == 'panos' or skillet_type == 'panorama' or skillet_type == 'panorama-gpcs':
            from skilletlib.skillet.panos import PanosSkillet
            return PanosSkillet(skillet_dict)

        elif skillet_type == 'pan_validation':
            from skilletlib.skillet.pan_validation import PanValidationSkillet
            return PanValidationSkillet(skillet_dict)

        elif skillet_type == 'python3':
            from skilletlib.skillet.python3 import Python3Skillet
            return Python3Skillet(skillet_dict)

        elif skillet_type == 'template':
            from skilletlib.skillet.template import TemplateSkillet
            return TemplateSkillet(skillet_dict)

        elif skillet_type == 'docker':
            from skilletlib.skillet.docker import DockerSkillet
            return DockerSkillet(skillet_dict)

        elif skillet_type == 'rest':
            from skilletlib.skillet.rest import RestSkillet
            return RestSkillet(skillet_dict)

        elif skillet_type == 'workflow':
            from skilletlib.skillet.workflow import WorkflowSkillet
            return WorkflowSkillet(skillet_dict, self)

        elif skillet_type == 'terraform':
            from skilletlib.skillet.terraform import TerraformSkillet
            return TerraformSkillet(skillet_dict)

        elif skillet_type == 'app':
            from skilletlib.skillet.app import AppSkillet
            return AppSkillet(skillet_dict)

        else:
            raise SkilletLoaderException(
                f'Unknown Skillet Type: {skillet_type}!')
Example #4
0
    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 as pe:
                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)
# get the full contents of the environment to initialize the skillet context
try:

    context = dict()
    context['config'] = config

    # create the skillet definition from the 'skillet_content' dict we got from the environ
    skillet_dict_raw = oyaml.safe_load(skillet_content)

    # use skilletLoader to normalize the skillet definition and fix common config file errors
    skillet_dict = sl.normalize_skillet_dict(skillet_dict_raw)
    skillet_dict['snippet_path'] = '.'

    # create the skillet object from the skillet dict
    if 'pan_validation' in skillet_dict.get('type'):
        skillet: PanValidationSkillet = PanValidationSkillet(
            skillet_dict, device)
    elif 'pan' in skillet_dict.get('type'):
        skillet: PanosSkillet = PanosSkillet(skillet_dict, device)
    else:
        skillet: Skillet = sl.create_skillet(skillet_dict)

    # ensure all our variables from the environment / outer context is copied in and ready to go
    skillet.update_context(os.environ)
    # execute the skillet and return the results to us
    results = skillet.execute(context)

    print()
    print('=' * 137)
    print()
    print('Execution Results:')
    print()