def test_recipe_update(self):
        "Test updating recipe through auth"

        changed = auth.create_analysis(project=self.project,
                                       json_text=self.recipe.json_text,
                                       template=self.recipe.template,
                                       uid=self.recipe.uid, update=True)

        self.assertEqual(changed.uid, self.recipe.uid)
Beispiel #2
0
    def handle(self, *args, **options):

        # Collect the parameters.
        id = options['id']
        uid = options['uid']
        recipe_uid = options["recipe_uid"]
        json = options['json']
        template = options['template']
        text = options['text']
        name = options['name']
        update = options["update"]
        image = options["image"]

        # Select project by id or uid.
        if id:
            project = Project.objects.get_all(id=id).first()
        else:
            project = Project.objects.get_all(uid=uid).first()

        # Get the project.
        recipe = Analysis.objects.get_all(uid=recipe_uid).first()

        # Project must exist if the recipe has not been created yet.
        if project is None and recipe is None:
            logger.error(f"Project not found! id={id} uid={uid}!")
            return

        # Check if recipe exists
        if recipe and not update:
            logger.error(
                f"Recipe already exists uid={uid}, add the --update flag to update."
            )
            return

        # Create the recipe
        json = open(json, "r").read() if json else ""
        template = open(template, "r").read() if template else ""
        project = recipe.project if recipe else project
        # Get the image stream
        stream = open(image, "rb") if image else None

        rec = auth.create_analysis(project=project,
                                   json_text=json,
                                   template=template,
                                   name=name,
                                   text=text,
                                   uid=recipe_uid,
                                   update=update,
                                   stream=stream)

        if update:
            print(f"*** Updated recipe uid={rec.uid} name={rec.name}")
        else:
            print(f"*** Created recipe uid={rec.uid} name={rec.name}")
Beispiel #3
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        # Set up generic owner
        self.owner = models.User.objects.create_user(username="******", email="*****@*****.**", is_staff=True)
        self.owner.set_password("test")
        self.factory = RequestFactory()

        self.project = auth.create_project(user=self.owner, name="test", text="Text", summary="summary",
                                           uid="testing")
        #Test data
        self.recipe = auth.create_analysis(project=self.project, json_text="{}", template="")
        self.recipe.save()
Beispiel #4
0
    def handle(self, *args, **options):

        # Collect the parameters.
        pid = options['pid']
        rid = options["rid"]
        json = options['json']
        template = options['template'] or replace_ext(json, "sh")
        image = options["image"] or replace_ext(json, "png")
        info = options['info']
        name = options['name']
        update = options["update"]

        # Select project
        project = Project.objects.filter(uid=pid).first() if pid else None

        # Project must exist if the recipe has not been created yet.
        if not project:
            logger.error(f"Project pid={pid} not found!")
            return

        # Get the project.
        recipe = Analysis.objects.filter(uid=rid).first() if rid else None

        # Check if recipe exists
        if recipe and not update:
            logger.error(
                f"Recipe already exists rid={rid}. Add the --update flag to update."
            )
            return

        # Create the recipe
        json = open(json, "r").read() if json else ""
        template = open(template, "r").read() if template else ""
        project = recipe.project if recipe else project

        # Get the image stream
        stream = open(image, "rb") if image else None

        rec = auth.create_analysis(project=project,
                                   json_text=json,
                                   template=template,
                                   name=name,
                                   text=info,
                                   uid=rid,
                                   update=update,
                                   stream=stream)

        if update:
            print(f"*** Updated recipe uid={rec.uid} name={rec.name}")
        else:
            print(f"*** Created recipe uid={rec.uid} name={rec.name}")
Beispiel #5
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        self.owner = models.User.objects.create(username="******", email="*****@*****.**")
        self.owner.set_password("testing")
        self.owner.save()

        self.project = auth.create_project(user=self.owner, name="Test project",
                                           privacy=models.Project.PUBLIC, uid="testing")
        data = auth.create_data(project=self.project, path=__file__)
        analysis = auth.create_analysis(project=self.project, json_text='{}', template="")
        self.job = auth.create_job(analysis=analysis)

        self.proj_params = dict(uid=self.project.uid)
        self.analysis_params = dict(uid=analysis.uid)
        self.data_params = dict(uid=data.uid)
        self.job_params = dict(uid=self.job.uid)
Beispiel #6
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        # Set up generic owner
        self.owner = models.User.objects.create_user(username="******",
                                                     email="*****@*****.**")
        self.owner.set_password("test")

        self.project = auth.create_project(user=self.owner,
                                           name="test",
                                           text="Text",
                                           summary="summary",
                                           uid="testing")

        self.recipe = auth.create_analysis(project=self.project,
                                           json_text="{}",
                                           template="",
                                           security=models.Analysis.AUTHORIZED)

        self.job = auth.create_job(analysis=self.recipe, user=self.owner)
        self.job.save()
Beispiel #7
0
    def handle(self, *args, **options):

        json = options['json']
        pid = options['id']
        template_fname = options['template']
        jobs = options['jobs']

        verbosity = int(options['verbosity'])

        if verbosity > 1:
            logger.setLevel(logging.DEBUG)
            logger.info(f"level={verbosity}")

        # Require JSON and templatates to exist.
        if not (json and template_fname):
            logger.error(
                "This command requires --json and a --template to be set")
            return

        # Get the target project.
        project = Project.objects.filter(id=pid).first()

        # Invalid project specified.
        if not project:
            logger.error(f'No project with id={pid}')
            return

        # JSON file does not exist.
        if not os.path.isfile(json):
            logger.error(f'No file found for --json={json}')
            return

        # Template file does not exist.
        if not os.path.isfile(template_fname):
            logger.error(f'No file found for --template={template_fname}')
            return

        try:
            # Parse the json_text into json_data
            json_text = open(json).read()
            json_path = os.path.dirname(json)
            json_data = hjson.loads(json_text)
        except Exception as exc:
            logger.exception(f"JSON exception in file: {json}\n{exc}")
            return

        try:
            # Read the specification
            template = open(template_fname).read()
        except Exception as exc:
            logger.exception(f"Template exception: {exc}")
            return

        try:
            name = json_data.get("settings", {}).get("name", "No name")
            text = json_data.get("settings", {}).get("help", "No help")
            uid = json_data.get("settings", {}).get("uid", "")
            image = json_data.get("settings", {}).get("image", "")
            text = textwrap.dedent(text)
            summary = json_data.get("settings",
                                    {}).get("summary", "No summary")

            # Create the analysis
            analysis = auth.create_analysis(project=project,
                                            uid=uid,
                                            json_text=json_text,
                                            summary=summary,
                                            template=template,
                                            name=name,
                                            text=text,
                                            security=Analysis.AUTHORIZED)

            # Load the image if specified.
            if image:
                image_path = os.path.join(json_path, image)
                if os.path.isfile(image_path):
                    stream = open(image_path, 'rb')
                    analysis.image.save(image, stream, save=True)
                    logger.info(f"Image path: {image_path}")
                else:
                    logger.error(f"Skipping invalid image path: {image_path}")

            if jobs:
                # When creating a job automatically for data in projects
                # it will try to match the value of the parameter to the data name.
                missing_name = ''
                for key, obj in json_data.items():
                    if obj.get("source") != "PROJECT":
                        continue

                    name = obj.get('value', '')
                    data = Data.objects.filter(project=project,
                                               name=name).first()

                    if not data:
                        missing_name = name
                        break

                    data.fill_dict(obj)

                if missing_name:
                    logger.error(
                        f"Job not created! Missing data:{missing_name} in analysis:{analysis.name}"
                    )
                else:
                    auth.create_job(analysis=analysis, json_data=json_data)

        except Exception as exc:
            logger.exception(f"Error: {exc}")
            return