2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module GoogleCloud
|
|
|
|
##
|
|
|
|
# GCP keys used to store Google Cloud Service Accounts
|
|
|
|
GCP_KEYS = %w[GCP_PROJECT_ID GCP_SERVICE_ACCOUNT GCP_SERVICE_ACCOUNT_KEY].freeze
|
|
|
|
|
|
|
|
##
|
|
|
|
# This service deals with GCP Service Accounts in GitLab
|
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
class ServiceAccountsService < ::GoogleCloud::BaseService
|
2021-12-11 22:18:48 +05:30
|
|
|
##
|
|
|
|
# Find GCP Service Accounts in a GitLab project
|
|
|
|
#
|
|
|
|
# This method looks up GitLab project's CI vars
|
|
|
|
# and returns Google Cloud Service Accounts combinations
|
2022-05-07 20:08:51 +05:30
|
|
|
# aligning GitLab project and ref to GCP projects
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
def find_for_project
|
2022-08-13 15:12:31 +05:30
|
|
|
group_vars_by_environment(GCP_KEYS).map do |environment_scope, value|
|
2021-12-11 22:18:48 +05:30
|
|
|
{
|
2022-05-07 20:08:51 +05:30
|
|
|
ref: environment_scope,
|
2021-12-11 22:18:48 +05:30
|
|
|
gcp_project: value['GCP_PROJECT_ID'],
|
|
|
|
service_account_exists: value['GCP_SERVICE_ACCOUNT'].present?,
|
|
|
|
service_account_key_exists: value['GCP_SERVICE_ACCOUNT_KEY'].present?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
def add_for_project(ref, gcp_project_id, service_account, service_account_key, is_protected)
|
2022-08-13 15:12:31 +05:30
|
|
|
create_or_replace_project_vars(
|
2022-05-07 20:08:51 +05:30
|
|
|
ref,
|
2022-01-26 12:08:38 +05:30
|
|
|
'GCP_PROJECT_ID',
|
2022-03-02 08:16:31 +05:30
|
|
|
gcp_project_id,
|
|
|
|
is_protected
|
2022-01-26 12:08:38 +05:30
|
|
|
)
|
2022-08-13 15:12:31 +05:30
|
|
|
create_or_replace_project_vars(
|
2022-05-07 20:08:51 +05:30
|
|
|
ref,
|
2022-01-26 12:08:38 +05:30
|
|
|
'GCP_SERVICE_ACCOUNT',
|
2022-03-02 08:16:31 +05:30
|
|
|
service_account,
|
|
|
|
is_protected
|
2022-01-26 12:08:38 +05:30
|
|
|
)
|
2022-08-13 15:12:31 +05:30
|
|
|
create_or_replace_project_vars(
|
2022-05-07 20:08:51 +05:30
|
|
|
ref,
|
2022-01-26 12:08:38 +05:30
|
|
|
'GCP_SERVICE_ACCOUNT_KEY',
|
2022-03-02 08:16:31 +05:30
|
|
|
service_account_key,
|
|
|
|
is_protected
|
2022-01-26 12:08:38 +05:30
|
|
|
)
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|