Example #1
0
def _setup_service_account_to_google_bucket_access_group(db_session):
    """
    Setup some testing data.
    """
    cloud_provider = CloudProvider(
        name="test_provider",
        endpoint="https://test.com",
        backend="test_backend",
        description="description",
        service="service",
    )
    db_session.add(cloud_provider)

    db_session.add(
        UserServiceAccount(
            google_unique_id="test_id1",
            email="*****@*****.**",
            google_project_id="efewf444",
        )
    )
    db_session.add(
        UserServiceAccount(
            google_unique_id="test_id2",
            email="*****@*****.**",
            google_project_id="edfwf444",
        )
    )
    db_session.commit()

    bucket1 = Bucket(name="test_bucket1", provider_id=cloud_provider.id)
    db_session.add(bucket1)
    db_session.commit()

    db_session.add(
        GoogleBucketAccessGroup(
            bucket_id=bucket1.id,
            email="*****@*****.**",
            privileges=["read-storage", "write-storage"],
        )
    )
    db_session.add(
        GoogleBucketAccessGroup(
            bucket_id=bucket1.id,
            email="*****@*****.**",
            privileges=["read-storage"],
        )
    )
    db_session.commit()
Example #2
0
def test_service_account_relationsips(db_session):
    """
    test service account tables have proper relationships/fields
    """
    project = Project(id=1)
    bucket = Bucket(id=1)
    user_sa = UserServiceAccount(
        id=1,
        google_unique_id="guid",
        email="*****@*****.**",
        google_project_id="gpid",
    )
    sa_access_privilege = ServiceAccountAccessPrivilege(id=1,
                                                        project_id=1,
                                                        service_account_id=1)
    gbag = GoogleBucketAccessGroup(id=1, bucket_id=1, email="*****@*****.**")
    sa_to_gbag = ServiceAccountToGoogleBucketAccessGroup(id=1,
                                                         service_account_id=1,
                                                         expires=0,
                                                         access_group_id=1)
    db_session.add(project)
    db_session.add(bucket)
    db_session.add(user_sa)
    db_session.add(sa_access_privilege)
    db_session.add(gbag)
    db_session.add(sa_to_gbag)
    db_session.commit()
    assert project.sa_access_privileges[
        0].__class__ == ServiceAccountAccessPrivilege
    assert project.sa_access_privileges[0].id == 1
    assert sa_access_privilege.project.__class__ == Project
    assert sa_access_privilege.project.id == 1
    assert sa_access_privilege.service_account.__class__ == UserServiceAccount
    assert sa_access_privilege.service_account.id == 1
    assert user_sa.access_privileges[
        0].__class__ == ServiceAccountAccessPrivilege
    assert user_sa.access_privileges[0].id == 1
    assert (user_sa.to_access_groups[0].__class__ ==
            ServiceAccountToGoogleBucketAccessGroup)
    assert user_sa.to_access_groups[0].id == 1
    assert sa_to_gbag.service_account.__class__ == UserServiceAccount
    assert sa_to_gbag.service_account.id == 1
    assert sa_to_gbag.access_group.__class__ == GoogleBucketAccessGroup
    assert sa_to_gbag.access_group.id == 1
    assert gbag.to_access_groups[
        0].__class__ == ServiceAccountToGoogleBucketAccessGroup
    assert gbag.to_access_groups[0].id == 1
Example #3
0
    def _register_new_service_account(self, sa):
        """
        Add service account and related entries to database and add
        service account to google bucket access groups

        WARNING: this assumes that the project_access provided are all
        valid Project.auth_ids, currently checked before this is called
        in validity checking

        Args:
            sa (
                fence.resources.google.service_account.GoogleServiceAccountRegistration
            ): the service account object with its email, project_access, a google project,
               and optionally a user who is attempting to modify/add

        Return:
            (dict): dictionary representing service account object
        """
        with GoogleCloudManager(sa.google_project_id) as google_project:
            g_service_account = google_project.get_service_account(sa.email)

        db_service_account = UserServiceAccount(
            google_unique_id=g_service_account.get("uniqueId"),
            email=g_service_account.get("email"),
            google_project_id=sa.google_project_id,
        )

        current_session.add(db_service_account)
        current_session.commit()

        project_ids = get_project_ids_from_project_auth_ids(
            current_session, sa.project_access)

        add_user_service_account_to_db(current_session, project_ids,
                                       db_service_account)

        add_user_service_account_to_google(current_session, project_ids,
                                           sa.google_project_id,
                                           db_service_account)

        return {
            "service_account_email": g_service_account.get("email"),
            "google_project_id": g_service_account.get("projectId"),
            "project_access": sa.project_access,
        }
Example #4
0
def force_add_service_accounts_to_access(
    service_account_emails, google_project_id, project_access, db=None
):
    """
    service_account_emails(list(str)): list of account emails
    google_project_id(str):  google project id
    project_access(list(str)): list of projects
    db(str): db connection string
    """
    session = get_db_session(db)

    with GoogleCloudManager(google_project_id) as google_project:
        for service_account_email in service_account_emails:
            g_service_account = google_project.get_service_account(
                service_account_email
            )
            sa = (
                session.query(UserServiceAccount)
                .filter_by(email=service_account_email)
                .first()
            )
            if not sa:
                sa = UserServiceAccount(
                    google_unique_id=g_service_account.get("uniqueId"),
                    email=service_account_email,
                    google_project_id=google_project_id,
                )
                session.add(sa)
                session.commit()

            project_ids = set()
            for project in project_access:
                project_db = session.query(Project).filter_by(auth_id=project).first()
                if project_db:
                    project_ids.add(project_db.id)

            add_user_service_account_to_db(session, project_ids, sa)

            add_user_service_account_to_google(
                session, project_ids, google_project_id, sa
            )
Example #5
0
def setup_test_data(db_session):
    cp = CloudProvider(name="test", endpoint="http://test.endpt")

    proxy_group_list = [
        {
            "id": "group1",
            "email": "*****@*****.**"
        },
        {
            "id": "group2",
            "email": "*****@*****.**"
        },
    ]
    user_account_list = [
        {
            "google_unique_id": "test_id1",
            "email": "*****@*****.**",
            "google_project_id": "test",
        },
        {
            "google_unique_id": "test_id2",
            "email": "*****@*****.**",
            "google_project_id": "test",
        },
    ]

    proxy_groups = []
    for group in proxy_group_list:
        proxy_groups.append(GoogleProxyGroup(**group))
        db_session.add(proxy_groups[-1])

    user_service_accounts = []
    for user in user_account_list:
        user_service_accounts.append(UserServiceAccount(**user))
        db_session.add(user_service_accounts[-1])

    db_session.commit()

    bucket1 = Bucket(name="bucket1", provider_id=cp.id)
    bucket2 = Bucket(name="bucket2", provider_id=cp.id)
    bucket3 = Bucket(name="bucket3", provider_id=cp.id)
    db_session.add(bucket1)
    db_session.add(bucket2)
    db_session.add(bucket3)
    db_session.commit()

    access_grp1 = GoogleBucketAccessGroup(bucket_id=bucket1.id,
                                          email="*****@*****.**")
    db_session.add(access_grp1)
    db_session.commit()

    db_session.add(
        GoogleProxyGroupToGoogleBucketAccessGroup(
            proxy_group_id=proxy_groups[0].id, access_group_id=access_grp1.id))

    db_session.add(
        ServiceAccountToGoogleBucketAccessGroup(
            service_account_id=user_service_accounts[0].id,
            access_group_id=access_grp1.id,
        ))

    db_session.commit()
Example #6
0
def invalid_service_account_not_exist(db_session):
    invalid_service_account = "*****@*****.**"
    user = UserServiceAccount(
        google_unique_id="invalid_test_id",
        email=invalid_service_account,
        google_project_id="test",
    )
    db_session.add(user)
    db_session.commit()

    cp = db_session.query(CloudProvider).filter_by(name="test").first()
    if not cp:
        cp = CloudProvider(name="test", endpoint="http://test.endpt")
        db_session.add(cp)
        db_session.commit()

    bucket1 = db_session.query(Bucket).filter_by(name="bucket1").first()
    if not bucket1:
        bucket1 = Bucket(name="bucket1", provider_id=cp.id)
        db_session.add(bucket1)
        db_session.commit()

    project1 = db_session.query(Project).filter_by(name="test_1").first()
    if not project1:
        project1 = Project(name="test_1", auth_id="test_auth_1")
        db_session.add(project1)
        db_session.commit()

    access_grp1 = (
        db_session.query(GoogleBucketAccessGroup)
        .filter_by(email="*****@*****.**")
        .first()
    )
    if not access_grp1:
        access_grp1 = GoogleBucketAccessGroup(
            bucket_id=bucket1.id, email="*****@*****.**"
        )
        db_session.add(access_grp1)
        db_session.commit()

    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project1.id, service_account_id=user.id
        )
    )
    db_session.commit()

    # expiration set to 0 for testing that it gets set
    current_time = 0
    service_account_grp1 = ServiceAccountToGoogleBucketAccessGroup(
        service_account_id=user.id, access_group_id=access_grp1.id, expires=current_time
    )
    db_session.add(service_account_grp1)
    db_session.commit()

    def mock_is_valid(sa_email, *args, **kwargs):
        if sa_email == invalid_service_account:
            validity = GoogleServiceAccountValidity("account_id", "project_id")
            # set overall validity to False
            # set policy_accessible to False so the SA is removed from the DB
            validity["policy_accessible"] = False
            validity._valid = False
            return validity
        return True

    patcher = patch(
        "fence.scripting.google_monitor._is_valid_service_account", mock_is_valid
    )

    patcher.start()
    yield {
        "service_account": user,
        "projects": [project1],
        "bucket_access_groups": [access_grp1],
    }
    patcher.stop()
Example #7
0
def register_user_service_account(db_session):
    cp = db_session.query(CloudProvider).filter_by(name="test").first()
    if not cp:
        cp = CloudProvider(name="test", endpoint="http://test.endpt")
        db_session.add(cp)
        db_session.commit()

    bucket1 = db_session.query(Bucket).filter_by(name="bucket1").first()
    if not bucket1:
        bucket1 = Bucket(name="bucket1", provider_id=cp.id)
        db_session.add(bucket1)
        db_session.commit()

    bucket2 = db_session.query(Bucket).filter_by(name="bucket2").first()
    if not bucket2:
        bucket2 = Bucket(name="bucket2", provider_id=cp.id)
        db_session.add(bucket2)
        db_session.commit()

    project1 = db_session.query(Project).filter_by(name="test_1").first()
    if not project1:
        project1 = Project(name="test_1", auth_id="test_auth_1")
        db_session.add(project1)
        db_session.commit()

    project2 = db_session.query(Project).filter_by(name="test_2").first()
    if not project2:
        project2 = Project(name="test_2", auth_id="test_auth_2")
        db_session.add(project2)
        db_session.commit()

    access_grp1 = (
        db_session.query(GoogleBucketAccessGroup)
        .filter_by(email="*****@*****.**")
        .first()
    )
    if not access_grp1:
        access_grp1 = GoogleBucketAccessGroup(
            bucket_id=bucket1.id, email="*****@*****.**"
        )
        db_session.add(access_grp1)
        db_session.commit()

    access_grp2 = (
        db_session.query(GoogleBucketAccessGroup)
        .filter_by(email="*****@*****.**")
        .first()
    )
    if not access_grp2:
        access_grp2 = GoogleBucketAccessGroup(
            bucket_id=bucket2.id, email="*****@*****.**"
        )
        db_session.add(access_grp2)
        db_session.commit()

    project_to_bucket1 = (
        db_session.query(ProjectToBucket).filter_by(project_id=project1.id).first()
    )
    if not project_to_bucket1:
        project_to_bucket1 = ProjectToBucket(
            project_id=project1.id, bucket_id=bucket1.id
        )
        db_session.add(project_to_bucket1)
        db_session.commit()

    project_to_bucket2 = (
        db_session.query(ProjectToBucket).filter_by(project_id=project2.id).first()
    )
    if not project_to_bucket2:
        project_to_bucket2 = ProjectToBucket(
            project_id=project2.id, bucket_id=bucket2.id
        )
        db_session.add(project_to_bucket2)
        db_session.commit()

    # new service account each time this is called
    random_string = "".join(
        random.choice(string.ascii_uppercase + string.digits) for _ in range(6)
    )
    user = UserServiceAccount(
        google_unique_id="{}".format(random_string),
        email="{}@test.iam.gserviceaccount.com".format(random_string),
        google_project_id="test",
    )
    db_session.add(user)
    db_session.commit()

    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project1.id, service_account_id=user.id
        )
    )
    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project2.id, service_account_id=user.id
        )
    )

    # expiration set to 0 for testing that it gets set
    current_time = 0
    service_account_grp1 = ServiceAccountToGoogleBucketAccessGroup(
        service_account_id=user.id, access_group_id=access_grp1.id, expires=current_time
    )
    service_account_grp2 = ServiceAccountToGoogleBucketAccessGroup(
        service_account_id=user.id, access_group_id=access_grp2.id, expires=current_time
    )
    db_session.add(service_account_grp1)
    db_session.add(service_account_grp2)
    db_session.commit()

    return {
        "service_account": user,
        "projects": [project1, project2],
        "buckets": [bucket1, bucket2],
        "bucket_access_groups": [access_grp1, access_grp2],
    }
Example #8
0
def setup_data(db_session):
    cp = CloudProvider(name="test", endpoint="http://test.endpt")
    user = UserServiceAccount(
        google_unique_id="test_id", email="*****@*****.**", google_project_id="test"
    )
    user_1 = UserServiceAccount(
        google_unique_id="test_id", email="*****@*****.**", google_project_id="test"
    )
    user_2 = UserServiceAccount(
        google_unique_id="test_id", email="*****@*****.**", google_project_id="test"
    )
    user_3 = UserServiceAccount(
        google_unique_id="test_id", email="*****@*****.**", google_project_id="test"
    )

    db_session.add(user)
    db_session.add(user_1)
    db_session.add(user_2)
    db_session.add(user_3)

    db_session.add(cp)
    db_session.commit()

    bucket = Bucket(name="bucket1", provider_id=cp.id)
    bucket2 = Bucket(name="bucket2", provider_id=cp.id)
    bucket3 = Bucket(name="bucket3", provider_id=cp.id)

    db_session.add(bucket)
    db_session.add(bucket2)
    db_session.add(bucket3)
    db_session.commit()

    project1 = Project(name="test_1", auth_id="test_auth_1")
    project2 = Project(name="test_2", auth_id="test_auth_2")
    project3 = Project(name="test_3", auth_id="test_auth_3")
    db_session.add(project1)
    db_session.add(project2)
    db_session.add(project3)
    db_session.commit()

    db_session.add(ProjectToBucket(project_id=project1.id, bucket_id=bucket.id))
    db_session.add(ProjectToBucket(project_id=project2.id, bucket_id=bucket2.id))
    db_session.add(ProjectToBucket(project_id=project3.id, bucket_id=bucket3.id))

    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project1.id, service_account_id=user.id
        )
    )
    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project2.id, service_account_id=user.id
        )
    )

    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project1.id, service_account_id=user_1.id
        )
    )
    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project1.id, service_account_id=user_2.id
        )
    )
    db_session.add(
        ServiceAccountAccessPrivilege(
            project_id=project1.id, service_account_id=user_3.id
        )
    )

    access_grp = GoogleBucketAccessGroup(
        bucket_id=bucket.id, email="*****@*****.**"
    )

    access_grp2 = GoogleBucketAccessGroup(
        bucket_id=bucket2.id, email="*****@*****.**"
    )

    access_grp3 = GoogleBucketAccessGroup(
        bucket_id=bucket3.id, email="*****@*****.**"
    )

    db_session.add(access_grp)
    db_session.add(access_grp2)
    db_session.add(access_grp3)
    db_session.commit()

    service_account_grp1 = ServiceAccountToGoogleBucketAccessGroup(
        service_account_id=user.id, access_group_id=access_grp.id
    )

    service_account_grp2 = ServiceAccountToGoogleBucketAccessGroup(
        service_account_id=user.id, access_group_id=access_grp2.id
    )
    db_session.add(service_account_grp1)
    db_session.add(service_account_grp2)
    db_session.commit()
Example #9
0
def _setup_google_access(db_session,
                         access_1_expires=None,
                         access_2_expires=None):
    """
    Setup some testing data.

    Args:
        access_1_expires (str, optional): expiration for the Proxy Group ->
            Google Bucket Access Group for user 1, defaults to None
        access_2_expires (str, optional): expiration for the Proxy Group ->
            Google Bucket Access Group for user 2, defaults to None
    """
    cloud_provider = CloudProvider(
        name="test_provider",
        endpoint="https://test.com",
        backend="test_backend",
        description="description",
        service="service",
    )
    db_session.add(cloud_provider)

    db_session.add(
        UserServiceAccount(
            google_unique_id="test_id1",
            email="*****@*****.**",
            google_project_id="efewf444",
        ))
    db_session.add(
        UserServiceAccount(
            google_unique_id="test_id2",
            email="*****@*****.**",
            google_project_id="edfwf444",
        ))
    db_session.commit()

    bucket1 = Bucket(name="test_bucket1", provider_id=cloud_provider.id)
    db_session.add(bucket1)
    db_session.commit()

    gpg1 = GoogleProxyGroup(id=1, email="*****@*****.**")
    gpg2 = GoogleProxyGroup(id=2, email="*****@*****.**")
    db_session.add(gpg1)
    db_session.add(gpg2)
    db_session.commit()

    gbag1 = GoogleBucketAccessGroup(
        bucket_id=bucket1.id,
        email="*****@*****.**",
        privileges=["read-storage", "write-storage"],
    )
    gbag2 = GoogleBucketAccessGroup(
        bucket_id=bucket1.id,
        email="*****@*****.**",
        privileges=["read-storage"],
    )
    db_session.add(gbag1)
    db_session.add(gbag2)
    db_session.commit()

    db_session.add(
        GoogleProxyGroupToGoogleBucketAccessGroup(proxy_group_id=gpg1.id,
                                                  access_group_id=gbag1.id,
                                                  expires=access_1_expires))
    db_session.add(
        GoogleProxyGroupToGoogleBucketAccessGroup(proxy_group_id=gpg2.id,
                                                  access_group_id=gbag2.id,
                                                  expires=access_2_expires))
    db_session.commit()

    return {"google_proxy_group_ids": {"1": gpg1.id, "2": gpg2.id}}