Example #1
0
    def test_Organisations(self):
        """ 
        This test creats new organisations from the org.json file and adds user from the user.json file to it.
        After all organisations and user are created the tests removes them and checks if they are correctly removed
        """

        ### Create organisations from org.json file

        org_list = readInFile("samples/org.json")
        tested_keys = [
            'name', 'description', 'nationality', 'sector', 'uuid', 'contacts'
        ]

        for item in org_list:
            org = MISPOrganisation()
            org.name = org_list[item]['name']
            org.description = org_list[item]['description']
            org.nationality = org_list[item]['nationality']
            org.sector = org_list[item]['sector']
            org.uuid = org_list[item]['uuid']
            org.contacts = org_list[item]['contacts']
            #org.local = org_list[item]['local']

            logging.info("OrgManagement - try to create organization \"" +
                         org_list[item]['name'] + "\"")
            response = self._misp.add_organisation(org, pythonify=True)

            self.assertTrue(
                org_list[item]['uuid'] in response.uuid,
                msg="The created organisation has no or a wrong UUID")
            self.assertTrue(
                org_list[item]['name'] in response.name,
                msg="The created organisation has no or a wrong name")
            self.assertTrue(
                org_list[item]['description'] in response.description,
                msg="The created organisation has no or a wrong description")
            self.assertTrue(
                org_list[item]['nationality'] in response.nationality,
                msg="The created organisation has no or a wrong nationality")
            self.assertTrue(
                response.local,
                msg=
                "The created organisation is not a local organisation but should be a local organisation"
            )
            self.assertTrue(
                org_list[item]['sector'] in response.sector,
                msg="The created organisation has no or a wrong sector")

        response = self._misp.organisations(scope="local", pythonify=True)
        logging.info(
            "OrgManagement - check if the admin and both test organisations exist"
        )
        self.assertGreaterEqual(
            len(response), 3,
            "MISP responded with less then 3 existing organisations - there shold exactly be 3"
        )

        ### Add new user from the user.json file
        list_user = readInFile("samples/user.json")
        users = self._misp.users(pythonify=True)
        for item in list_user:
            for org in response:
                if org.name in list_user[item]['org_id']:
                    logging.info("OrgManagement - try to add user \"" +
                                 list_user[item]['email'] + "\"")
                    usr = MISPUser()
                    usr.email = list_user[item]['email']
                    usr.org_id = org.id
                    usr.role_id = list_user[item]['role_id']

                    usr_response = self._misp.add_user(usr, pythonify=True)

                    # legnth of regular authkeys is 40 chars or longer
                    self.assertTrue(
                        usr_response.email in list_user[item]['email'],
                        msg="The created users has no or a wrong email")
                    self.assertTrue(
                        usr_response.role_id in list_user[item]['role_id'],
                        msg="The created users has no or a wrong role id")
                    self.assertGreaterEqual(
                        len(usr_response.authkey),
                        40,
                        msg=
                        "MISP responded with a wrong authkey - should be exactly 40 chars"
                    )

        ### An authentication test could be inserted here

        ### An user change role test could be inserted here

        logging.info(
            "OrgManagement - check if all user where created successfully")
        response = self._misp.users(pythonify=True)
        self.assertGreaterEqual(
            len(response),
            len(list_user),
            msg=
            "MISP responded with a wrong number of users - it seems that not all users could be created."
        )

        for item in response:
            if item.org_id not in '1' or item.id not in '1':
                logging.info("OrgManagement - try to delete user \"" +
                             item.email + "\"")
                usr_response = self._misp.delete_user(item)
                self.assertTrue("User deleted" in usr_response['message'],
                                msg="User could ne be deleted")
                pass

        logging.info(
            "OrgManagement - check if user list now only contains the admin user"
        )
        response = self._misp.users(pythonify=True)
        self.assertEqual(
            len(response), 1,
            "MISP responded with a wrong number of users - it seems that not all users could be deleted."
        )

        ### Remove organisations
        response = self._misp.organisations(pythonify=True)
        for item in response:
            if item.id not in "1":
                logging.info("Try to remove organization: \"" + item.name +
                             "\"")
                org_response = self._misp.delete_organisation(item)
                self.assertTrue(
                    'deleted' in org_response['message'],
                    msg="Organisations could not be deleted from MISP")
            pass

        response = self._misp.organisations(pythonify=True)
        logging.info("OrgManagement - check if only admin org exist")
        self.assertEqual(
            len(response),
            1,
            msg=
            "MISP responded with a wrong number of organisations - it seems that not all organisations could be deleted."
        )
Example #2
0
    misp = ExpandedPyMISP(misp_url, misp_key, misp_verifycert)

    # CSV format
    #   orgname,nationality,sector,type,contacts,uuid,local,sharingroup
    with open(args.csv_import) as csv_file:
        count_orgs = 0
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:

            org = MISPOrganisation()
            org.name = row[0]
            print("Process {}".format(org.name))
            org.nationality = row[1]
            org.sector = row[2]
            org.type = row[3]
            org.contacts = row[4]
            org.uuid = row[5]
            org.local = row[6]

            add_org = misp.add_organisation(org, pythonify=True)

            if 'errors' in add_org:
                print(add_org['errors'])
            else:
                count_orgs = count_orgs + 1
                org_uuid = add_org.uuid

                if org_uuid:
                    sharinggroup = MISPSharingGroup()
                    sharinggroup_uuid = row[7]