def test_get(self):
        # add system-config-inputs
        sc = SystemConfigurationInput().query.all()
        self.assertListEqual(sc, [])
        sc1 = create_sysConfig(label='sc1',
                               nixConfigFilename='foo.nix',
                               nixConfig='{ config: nix }',
                               workflowId=1)
        sc2 = create_sysConfig(label='sc2',
                               nixConfigFilename='bar.nix',
                               nixConfig='{ nix: config }',
                               workflowId=2)

        r = SystemConfigurationInput().query.all()
        self.assertEqual(len(r), 2)

        # get them individually
        response = self.client.get(f'{self.BASE_ENDPOINT}/{sc1.sysConfigId}')
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.get_data(as_text=True))
        self.assertEqual(json_response['label'], 'sc1')
        response = self.client.get(f'{self.BASE_ENDPOINT}/{sc2.sysConfigId}')
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.get_data(as_text=True))
        self.assertEqual(json_response['label'], 'sc2')

        # get them all
        response = self.client.get(self.BASE_ENDPOINT)
        self.assertEqual(response.status_code, 200)
        json_response = json.loads(response.get_data(as_text=True))
        self.assertEqual(len(json_response), 2)
    def test_update_label(self):
        wf = create_workflow(label='test-wf')

        sc = SystemConfigurationInput().query.all()
        self.assertListEqual(sc, [])
        sc = create_sysConfig(label='sc1',
                              nixConfigFilename='foo.nix',
                              nixConfig='{ config: nix }',
                              workflowId=wf.workflowId)

        self.assertEqual(len(SystemConfigurationInput().query.all()), 1)

        response = self.client.put(
            f'{self.BASE_ENDPOINT}/{sc.sysConfigId}',
            headers=DEFAULT_HEADERS,
            data=json.dumps(
                dict(sysConfigId=sc.sysConfigId,
                     label=sc.label + '-NEW',
                     nixConfigFilename=sc.nixConfigFilename,
                     nixConfig=sc.nixConfig,
                     workflowId=sc.workflowId)))

        self.assertEqual(response.status_code, 200)
        updated_sysconfig = SystemConfigurationInput.query.filter_by(
            label='sc1-NEW').first()
        self.assertIsNotNone(updated_sysconfig)
    def test_update_with_missing_data(self):
        sc = SystemConfigurationInput().query.all()
        self.assertListEqual(sc, [])
        sc = create_sysConfig(label='sc1',
                              nixConfigFilename='foo.nix',
                              nixConfig='{ config: nix }',
                              workflowId=1)

        self.assertEqual(len(SystemConfigurationInput().query.all()), 1)

        response = self.client.put(f'{self.BASE_ENDPOINT}/{sc.sysConfigId}',
                                   headers=DEFAULT_HEADERS,
                                   data=json.dumps(dict()))

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.get_json(), {
                'errors': {
                    'label': "'label' is a required property",
                    'nixConfig': "'nixConfig' is a required property",
                    'nixConfigFilename':
                    "'nixConfigFilename' is a required property",
                    'workflowId': "'workflowId' is a required property",
                },
                'message': 'Input payload validation failed'
            })
    def test_create(self):
        test_workflow_label = 'TEST WORKFLOW'
        wf = create_workflow(label=test_workflow_label)

        self.assertIsNotNone(wf.workflowId)
        self.assertIsNone(wf.updatedAt)

        r = SystemConfigurationInput().query.all()
        self.assertListEqual(r, [])

        label = f'created system-config-input {datetime.utcnow()}'
        response = self.client.post(self.BASE_ENDPOINT,
                                    headers=DEFAULT_HEADERS,
                                    data=json.dumps(
                                        dict(label=label,
                                             nixConfigFilename='foo.nix',
                                             nixConfig='{ nix: config }',
                                             workflowId=1)))

        self.assertEqual(response.status_code, 200)
        created_sysconfig = SystemConfigurationInput.query.filter_by(
            label=label).first()
        self.assertIsNotNone(created_sysconfig)

        updated_wf = Workflow.query.filter_by(workflowId=wf.workflowId).first()
        self.assertIsNotNone(updated_wf.updatedAt)
예제 #5
0
def create_sysConfig(**kwargs) -> SystemConfigurationInput:
    sc = SystemConfigurationInput(
        label=kwargs['label'],
        nixConfigFilename=kwargs['nixConfigFilename'],
        nixConfig=kwargs['nixConfig'],
        workflowId=kwargs['workflowId'])
    add_to_db(sc)
    return sc
예제 #6
0
    def get(self, workflowId):
        """
            clone a workflow based on the Id passed in
        """
        current_app.logger.debug(f'cloning workflow({workflowId})')
        workflow = Workflow.query.get_or_404(workflowId)

        new_workflow = Workflow(label=f'COPY - {workflow.label}')
        db.session.add(new_workflow)
        db.session.commit()

        if workflow.systemConfigurationInput is not None:
            sysconfig = SystemConfigurationInput(
                label=f'COPY - {workflow.systemConfigurationInput.label}',
                nixConfigFilename=workflow.systemConfigurationInput.
                nixConfigFilename,
                nixConfig=workflow.systemConfigurationInput.nixConfig,
                workflowId=new_workflow.workflowId)
            db.session.add(sysconfig)

        if workflow.testgenConfigInput is not None:
            testgenconfig = TestgenConfigInput(
                label=f'COPY - {workflow.testgenConfigInput.label}',
                configInput=workflow.testgenConfigInput.configInput,
                workflowId=new_workflow.workflowId)
            db.session.add(testgenconfig)

        if workflow.vulnerabilityConfigurationInput is not None:
            existing_feat_model = workflow.vulnerabilityConfigurationInput.featureModel
            feat_model = FeatureModel(
                label=f'COPY - {existing_feat_model.label}',
                uid=str(
                    uuid4()
                ),  # TODO: the model should really handle UID generation so we don't have this duplication
                filename=existing_feat_model.filename,
                source=existing_feat_model.source,
                conftree=existing_feat_model.conftree,
                hash=existing_feat_model.hash,
                configs=existing_feat_model.configs)
            db.session.add(feat_model)

            vuln_config = VulnerabilityConfigurationInput(
                label=
                f'COPY - {workflow.vulnerabilityConfigurationInput.label}',
                vulnClass=workflow.vulnerabilityConfigurationInput.vulnClass,
                featureModelUid=feat_model.uid,
                workflowId=new_workflow.workflowId)
            db.session.add(vuln_config)

        db.session.commit()

        return new_workflow
    def test_update(self):
        test_workflow_label = 'TEST WORKFLOW'
        wf = create_workflow(label=test_workflow_label)

        self.assertIsNotNone(wf.workflowId)
        self.assertIsNone(wf.updatedAt)

        sc = SystemConfigurationInput().query.all()
        self.assertListEqual(sc, [])
        sc = create_sysConfig(label='sc1',
                              nixConfigFilename='foo.nix',
                              nixConfig='{ config: nix }',
                              workflowId=wf.workflowId)

        self.assertEqual(len(SystemConfigurationInput().query.all()), 1)

        label = f'{sc.label}-{datetime.now()}'
        config = f'{sc.nixConfig}-{datetime.now()}'
        filename = f'new-{sc.nixConfigFilename}'
        response = self.client.put(f'{self.BASE_ENDPOINT}/{sc.sysConfigId}',
                                   headers=DEFAULT_HEADERS,
                                   data=json.dumps(
                                       dict(sysConfigId=sc.sysConfigId,
                                            label=label,
                                            nixConfigFilename=filename,
                                            nixConfig=config,
                                            workflowId=sc.workflowId)))

        self.assertEqual(response.status_code, 200)
        updated_sysconfig = SystemConfigurationInput.query.filter_by(
            label=label).first()
        self.assertIsNotNone(updated_sysconfig)
        self.assertEqual(updated_sysconfig.label, label)
        self.assertEqual(updated_sysconfig.nixConfig, config)
        self.assertEqual(updated_sysconfig.nixConfigFilename, filename)
        self.assertNotEqual(updated_sysconfig.updatedAt, db.null())

        updated_wf = Workflow.query.filter_by(workflowId=wf.workflowId).first()
        self.assertIsNotNone(updated_wf.updatedAt)
예제 #8
0
    def post(self):
        sysconfig_input = json.loads(request.data)
        workflow = Workflow.query.get(sysconfig_input['workflowId'])

        if workflow is None:
            return abort(400, 'Unable to find given workflow', workflowId=sysconfig_input['workflowId'])

        new_sysconfig_input = SystemConfigurationInput(
            label=sysconfig_input['label'],
            nixConfigFilename=sysconfig_input['nixConfigFilename'],
            nixConfig=sysconfig_input['nixConfig'],
            workflowId=workflow.workflowId
        )
        workflow.updatedAt = datetime.now()
        db.session.add(new_sysconfig_input)
        db.session.add(workflow)
        db.session.commit()

        return new_sysconfig_input
    def test_create_with_missing_data(self):
        r = SystemConfigurationInput().query.all()
        self.assertListEqual(r, [])

        response = self.client.post(self.BASE_ENDPOINT,
                                    headers=DEFAULT_HEADERS,
                                    data=json.dumps(dict()))

        self.assertEqual(response.status_code, 400)
        self.assertEqual(
            response.get_json(), {
                'errors': {
                    'label': "'label' is a required property",
                    'nixConfig': "'nixConfig' is a required property",
                    'nixConfigFilename':
                    "'nixConfigFilename' is a required property",
                    'workflowId': "'workflowId' is a required property",
                },
                'message': 'Input payload validation failed'
            })