def test_create_aws_account(self):
        from security_monkey.account_manager import account_registry

        for name, account_manager in list(account_registry.items()):
            manager.add_command("add_account_%s" % name.lower(),
                                AddAccount(account_manager()))

        manager.handle("manage.py", [
            "add_account_aws", "-n", "test", "--active", "--id", "99999999999",
            "--canonical_id", "bcaf1ffd86f41161ca5fb16fd081034f",
            "--role_name", "SecurityMonkey"
        ])

        account = Account.query.filter(Account.name == "test").first()
        assert account
        assert account.identifier == "99999999999"
        assert account.active
        assert len(account.custom_fields) == 4

        # Get the canonical ID field:
        c_id = AccountTypeCustomValues.query.filter(
            AccountTypeCustomValues.name == "canonical_id",
            AccountTypeCustomValues.account_id == account.id).first()

        assert c_id
        assert c_id.value == "bcaf1ffd86f41161ca5fb16fd081034f"

        # Already exists:
        assert manager.handle("manage.py", [
            "add_account_aws", "-n", "test", "--active", "--id", "99999999999",
            "--canonical_id", "bcaf1ffd86f41161ca5fb16fd081034f",
            "--role_name", "SecurityMonkey"
        ]) == -1
    def test_create_account_with_canonical(self):
        from security_monkey.account_manager import account_registry

        for name, account_manager in account_registry.items():
            manager.add_command("add_account_%s" % name.lower(), AddAccount(account_manager()))

        manager.handle("manage.py", ["add_account_aws", "-n", "test", "--active", "--id", "99999999999",
                                     "--canonical_id", "bcaf1ffd86f41161ca5fb16fd081034f", "--s3_name", "test",
                                     "--role_name", "SecurityMonkey"])

        account = Account.query.filter(Account.name == "test").first()
        assert account
        assert account.identifier == "99999999999"
        assert account.active
        assert len(account.custom_fields) == 3

        # Get the canonical ID field:
        c_id = AccountTypeCustomValues.query.filter(AccountTypeCustomValues.name == "canonical_id",
                                                    AccountTypeCustomValues.account_id == account.id).first()

        assert c_id
        assert c_id.value == "bcaf1ffd86f41161ca5fb16fd081034f"

        assert manager.handle("manage.py", ["add_account_aws", "-n", "test", "--active", "--id", "99999999999",
                                            "--canonical_id", "bcaf1ffd86f41161ca5fb16fd081034f", "--s3_name", "test",
                                            "--role_name", "SecurityMonkey"]) == -1
Esempio n. 3
0
    def test_update_account_with_canonical(self):
        from security_monkey.account_manager import account_registry

        for name, account_manager in list(account_registry.items()):
            manager.add_command("add_account_%s" % name.lower(),
                                AddAccount(account_manager()))

        # Update:
        manager.handle("manage.py", [
            "add_account_aws", "-n", "account0", "--active", "--id",
            "012345678910", "--canonical_id",
            "bcaf1ffd86f41161ca5fb16fd081034f", "--s3_name", "test",
            "--role_name", "SecurityMonkey", "--update-existing"
        ])

        account = Account.query.filter(Account.name == "account0").first()
        assert account
        assert account.identifier == "012345678910"
        assert account.active
        assert len(account.custom_fields) == 4

        # Get the canonical ID field:
        c_id = AccountTypeCustomValues.query.filter(
            AccountTypeCustomValues.name == "canonical_id",
            AccountTypeCustomValues.account_id == account.id).first()

        assert c_id
        assert c_id.value == "bcaf1ffd86f41161ca5fb16fd081034f"
Esempio n. 4
0
 def __sync_networks(networks, additional_args=None):
     if additional_args is None:
         additional_args = []
     with tempfile.NamedTemporaryFile() as tfile:
         json.dump(networks, tfile)
         tfile.seek(0)
         manager.handle(
             'manage.py',
             ['sync_networks', '-i', tfile.name] + additional_args,
         )
Esempio n. 5
0
    def test_create_user(self, prompt_pass_function):
        email = "*****@*****.**"
        manager.handle("manage.py", ["create_user", email, "View"])

        user = User.query.filter(User.email == email).one()
        assert user
        assert user.email == "*****@*****.**"
        assert user.role == "View"

        # Update existing user:
        manager.handle("manage.py", ["create_user", email, "Comment"])
        user = User.query.filter(User.email == email).one()
        assert user
        assert user.role == "Comment"
Esempio n. 6
0
    def test_create_user(self, prompt_pass_function):
        email = "*****@*****.**"
        manager.handle("manage.py", ["create_user", email, "View"])

        user = User.query.filter(User.email == email).one()
        assert user
        assert user.email == "*****@*****.**"
        assert user.role == "View"

        # Update existing user:
        manager.handle("manage.py", ["create_user", email, "Comment"])
        user = User.query.filter(User.email == email).one()
        assert user
        assert user.role == "Comment"
Esempio n. 7
0
 def test_add_whitelist_entries_with_s3(self):
     # this test exhausts the code path for the s3.get_object() call. The
     # tests below are agnostic to storage, so it's just more convenient
     # to use local files.
     mock_s3().start()
     s3 = boto3.client('s3')
     s3.create_bucket(Bucket='testBucket')
     s3.put_object(
         Bucket='testBucket',
         Key='networks.json',
         Body=json.dumps(self.TEST_NETWORKS),
     )
     manager.handle(
         'manage.py',
         ['sync_networks', '-i', 'networks.json', '-b', 'testBucket'],
     )
     mock_s3().stop()
     for name, cidr in self.TEST_NETWORKS.items():
         entry = NetworkWhitelistEntry.query.filter(
             NetworkWhitelistEntry.name == name).first()
         assert entry is not None
         assert entry.cidr == cidr
Esempio n. 8
0
    def test_toggle_active_user(self):
        test_user = User(email='*****@*****.**')
        test_user.role = 'View'
        test_user.active = False
        db.session.add(test_user)
        db.session.commit()

        manager.handle('manage.py', [
            'toggle_active_user', '--email', '*****@*****.**', '--active',
            'True'
        ])
        assert User.query.filter(
            User.email == '*****@*****.**').first().active

        manager.handle('manage.py',
                       ['toggle_active_user', '--email', '*****@*****.**'])
        assert not User.query.filter(
            User.email == '*****@*****.**').first().active

        with pytest.raises(SystemExit):
            manager.handle('manage.py',
                           ['toggle_active_user', '--email', 'notauser'])