-
Notifications
You must be signed in to change notification settings - Fork 828
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
ensure k8s-conform service accounts have objectAdmin rights #1890
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2279b36
infra/gcp/lib: K8S_INFRA_DEBUG will leave behind TMPDIR
spiffxp a0f8334
infra/gcp/lib_gsm: create lib_gsm
spiffxp 6262533
infra/gcp/lib_iam: handle empty iam policy
spiffxp ab6d28e
infra/gcp/lib_iam: mv ensure_service_account, support update
spiffxp 20ca96a
infra/gcp/conformance: reorganize and fix bugs
spiffxp 5890959
infra/gcp/lib: chmod -x all libs
spiffxp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,23 @@ | |
# limitations under the License. | ||
|
||
readonly TMPDIR=$(mktemp -d "/tmp/k8sio-infra-gcp-lib.XXXXX") | ||
trap 'rm -rf "${TMPDIR}"' EXIT | ||
function cleanup_tmpdir() { | ||
if [ "${K8S_INFRA_DEBUG:-"false"}" == "true" ]; then | ||
echo "K8S_INFRA_DEBUG mode, not removing tmpdir: ${TMPDIR}" | ||
ls -l "${TMPDIR}" | ||
else | ||
rm -rf "${TMPDIR}" | ||
fi | ||
} | ||
trap 'cleanup_tmpdir' EXIT | ||
|
||
# This is a library of functions used to create GCP stuff. | ||
|
||
. "$(dirname "${BASH_SOURCE[0]}")/lib_util.sh" | ||
. "$(dirname "${BASH_SOURCE[0]}")/lib_iam.sh" | ||
. "$(dirname "${BASH_SOURCE[0]}")/lib_gcr.sh" | ||
. "$(dirname "${BASH_SOURCE[0]}")/lib_gcs.sh" | ||
. "$(dirname "${BASH_SOURCE[0]}")/lib_gcr.sh" | ||
. "$(dirname "${BASH_SOURCE[0]}")/lib_gsm.sh" | ||
|
||
# The group that admins all GCR repos. | ||
GCR_ADMINS="[email protected]" | ||
|
@@ -434,29 +443,6 @@ function empower_artifact_auditor_invoker() { | |
--region=us-central1 | ||
} | ||
|
||
# Create a service account | ||
# $1: The GCP project | ||
# $2: The account name (e.g. "foo-manager") | ||
# $3: The account display-name (e.g. "Manages all foo") | ||
function ensure_service_account() { | ||
if [ $# != 3 -o -z "$1" -o -z "$2" -o -z "$3" ]; then | ||
echo "ensure_service_account(project, name, display_name) requires 3 arguments" >&2 | ||
return 1 | ||
fi | ||
local project="$1" | ||
local name="$2" | ||
local display_name="$3" | ||
|
||
local acct=$(svc_acct_email "${project}" "${name}") | ||
|
||
if ! gcloud --project "${project}" iam service-accounts describe "${acct}" >/dev/null 2>&1; then | ||
gcloud --project "${project}" \ | ||
iam service-accounts create \ | ||
"${name}" \ | ||
--display-name="${display_name}" | ||
fi | ||
} | ||
|
||
# Ensure that DNS managed zone exists, creating one if need. | ||
# $1 The GCP project | ||
# $2 The managed zone name (e.g. kubernetes-io) | ||
|
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2021 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Google Secret Manager (GSM) utility functions | ||
# | ||
# This MUST NOT be used directly. Source it via lib.sh instead. | ||
|
||
# Returns full name of a secret in the given project with the given secret id | ||
# Arguments: | ||
# $1: The project id hosting the secret (e.g. "k8s-infra-foo") | ||
# $2: The secret name (e.g. "my-secret") | ||
function secret_full_name() { | ||
if [ ! $# -eq 2 -o -z "$1" -o -z "$2" ]; then | ||
echo "secret_full_name(project, secret) requires 2 arguments" >&2 | ||
return 1 | ||
fi | ||
|
||
local project="${1}" | ||
local secret="${2}" | ||
|
||
# this command would take longer, require privileges, and fail if not found | ||
# gcloud secrets describe --projects ${project} ${name} --format='value(name)' | ||
echo "projects/${project}/secrets/${secret}" | ||
} | ||
|
||
# Ensures a secret exists in the given project with the given name | ||
# Arguments: | ||
# $1: The project id hosting the secret (e.g. "k8s-infra-foo") | ||
# $2: The secret name (e.g. "my-secret") | ||
function ensure_secret() { | ||
if [ ! $# -eq 2 -o -z "$1" -o -z "$2" ]; then | ||
echo "ensure_secret(project, secret) requires 2 arguments" >&2 | ||
return 1 | ||
fi | ||
|
||
local project="${1}" | ||
local secret="${2}" | ||
|
||
if ! gcloud secrets describe --project "${project}" "${secret}" > /dev/null; then | ||
gcloud secrets create --project "${project}" "${secret}" | ||
fi | ||
} | ||
|
||
# Ensures a secret exists in the given project with the given name. If the | ||
# secret does not exist, it is pre-populated with a newly created private key | ||
# for the given service-account | ||
# Arguments: | ||
# $1: The project id hosting the secret (e.g. "k8s-infra-foo") | ||
# $2: The secret name (e.g. "my-secret") | ||
# $3: The service-account (e.g. "[email protected]") | ||
function ensure_serviceaccount_key_secret() { | ||
if [ ! $# -eq 3 -o -z "$1" -o -z "$2" -o -z "$3" ]; then | ||
echo "ensure_serviceaccount_key_secret(project, secret, serviceaccountt) requires 3 arguments" >&2 | ||
return 1 | ||
fi | ||
|
||
local project="${1}" | ||
local secret="${2}" | ||
local serviceaccount="${3}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @spiffxp you mean |
||
|
||
local private_key_file="${TMPDIR}/key.json" | ||
|
||
if ! gcloud secrets describe --project "${project}" "${secret}" > /dev/null; then | ||
ensure_secret "${project}" "${secret}" | ||
|
||
gcloud iam service-accounts keys create "${private_key_file}" \ | ||
--project "${project}" \ | ||
--iam-account "${email}" | ||
|
||
gcloud secrets versions add "${secret}" \ | ||
--project "${project}" \ | ||
--data-file "${private_key_file}" | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
email
instead ofserviceaccountt