Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serviceaccount to prod GCS buckets #5997

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 54 additions & 3 deletions infra/gcp/bash/ensure-prod-storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ readonly ALL_PROD_PROJECTS
# This is a list of all prod GCS buckets, but only their trailing "name". Each
# name will get a GCS bucket called "k8s-artifacts-${name}", and write access
# will be granted to the group "k8s-infra-push-${name}@kubernetes.io", which
# must already exist.
# must already exist. Additionally, a service account "k8s-infra-push-bot" will
# be created which can be used for automation purposes.
#
ALL_PROD_BUCKETS=(
"csi"
Expand Down Expand Up @@ -250,13 +251,63 @@ function ensure_all_prod_projects() {

# Create all prod GCS buckets.
function ensure_all_prod_buckets() {
local sa_name="k8s-infra-push-bot"
local sa_email="${sa_name}@${PROD_PROJECT}.iam.gserviceaccount.com"
local principal="serviceAccount:${sa_email}"
local pool="k8s-infra-push-pool"
local provider="k8s-infra-push-provider"

color 6 "Ensuring ${sa_email} exists and can write to prod buckets in project: ${PROD_PROJECT}"
ensure_service_account \
"${PROD_PROJECT}" \
"${sa_name}" \
"used by automation to push artifacts to prod buckets in ${PROD_PROJECT}"
Comment on lines +261 to +264
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ameukam @xmudrii is there any automation required to get the service account private keys? I assume we manually create them and push them into the 1Password vault, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prevent private keys creation. Workload Identity or Service Account impersonation are the only things allowed now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to workload identity as described in https://github.com/google-github-actions/auth


color 6 "Creating workload identity pool: ${pool}"
gcloud iam workload-identity-pools create "${pool}" \
--project="${PROD_PROJECT}" \
--location="global" \
--display-name="${pool}"

color 6 "Creating workload identity provider: ${provider}"
gcloud iam workload-identity-pools providers create-oidc "${provider}" \
--project="${PROD_PROJECT}" \
--location="global" \
--workload-identity-pool="${pool}" \
--display-name="${provider}" \
--attribute-mapping="google.subject=assertion.sub,attribute.actor=assertion.actor,attribute.repository=assertion.repository" \
--issuer-uri="https://token.actions.githubusercontent.com"

pool_id=$(gcloud iam workload-identity-pools describe "${pool}" --project="${PROD_PROJECT}" --location="global" --format="value(name)")

for repo in "kubernetes/release" "kubernetes-sigs/cri-tools"; do
color 6 "Allow '${sa_email}' as workload identity user in repository: ${repo}"
gcloud iam service-accounts add-iam-policy-binding "${sa_email}" \
--project="${PROD_PROJECT}" \
--role="roles/iam.workloadIdentityUser" \
--member="principalSet://iam.googleapis.com/${pool_id}/attribute.repository/${repo}"
done

# `workload_identity_provider` value in GitHub Actions YAML
color 6 "Extracting workload identity resource name:"
gcloud iam workload-identity-pools providers describe "${provider}" \
--project="${PROD_PROJECT}" \
--location="global" \
--workload-identity-pool="${pool}" \
--format="value(name)"

for sfx in "${ALL_PROD_BUCKETS[@]}"; do
color 6 "Ensuring the GCS bucket: gs://k8s-artifacts-${sfx}"
bucket="gs://k8s-artifacts-${sfx}"

color 6 "Ensuring the GCS bucket: ${bucket}"
ensure_prod_gcs_bucket \
"${PROD_PROJECT}" \
"gs://k8s-artifacts-${sfx}" \
"${bucket}" \
"k8s-infra-push-${sfx}@kubernetes.io" \
| indent

ensure_gcs_role_binding "${bucket}" "${principal}" "objectCreator"
ensure_gcs_role_binding "${bucket}" "${principal}" "objectViewer"
done
}

Expand Down