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

use workload identity for external secret instead of service key. #233

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions terraform/gcp/modules/external_secrets/external_secrets.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/


locals {
namespace = "external-secrets"
k8s_sa = "external-secrets"
}

// External-Secrets
resource "helm_release" "external_secrets" {
name = "external-secrets"
namespace = "external-secrets"
namespace = local.namespace
create_namespace = true
chart = "external-secrets"
repository = "https://charts.external-secrets.io"
Expand All @@ -42,10 +47,17 @@ resource "google_project_iam_member" "external_secrets_binding" {
depends_on = [google_service_account.external_secrets_sa]
}

resource "google_project_iam_member" "external_secrets_binding_token" {
project = var.project_id
role = "roles/iam.serviceAccountTokenCreator"

Check warning

Code scanning / tfsec

Users should not be granted service account access at the project level

Service account access is granted to a user at project level.
Copy link

Choose a reason for hiding this comment

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

Post submit feedback @k4leung4 : can this be updated to grant the role on the specific service accounts(s) that need impersonating? This is a very powerful grant since the external-secrets SA can use access of any other service accounts in the project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@var-sdk good point. created #264

member = "serviceAccount:${google_service_account.external_secrets_sa.email}"
depends_on = [google_service_account.external_secrets_sa]
}

resource "google_service_account_iam_member" "gke_sa_iam_member_external_secrets" {
service_account_id = google_service_account.external_secrets_sa.name
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[external-secrets/external-secrets]"
member = "serviceAccount:${var.project_id}.svc.id.goog[${local.namespace}/${local.k8s_sa}]"
depends_on = [google_service_account.external_secrets_sa]
}

Expand All @@ -54,27 +66,6 @@ resource "google_service_account_key" "external_secrets_key" {
service_account_id = google_service_account.external_secrets_sa.name
}

resource "kubernetes_secret_v1" "gcpsm_secret" {
metadata {
name = "gcpsm-secret"
namespace = "external-secrets"
annotations = {}
labels = {}
}

binary_data = {
secret-access-credentials = google_service_account_key.external_secrets_key.private_key
}

type = "Opaque"

depends_on = [
google_service_account.external_secrets_sa,
google_service_account_key.external_secrets_key,
helm_release.external_secrets
]
}

resource "kubectl_manifest" "secretstore_gcp_backend" {
yaml_body = <<YAML
apiVersion: external-secrets.io/v1alpha1
Copy link
Member

Choose a reason for hiding this comment

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

maybe we can bump to apiVersion: external-secrets.io/v1beta1 and use the new version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we are still on 0.4.4, i dont think it supports v1beta1

though we should upgrade external secrets to 0.5x
filed https://github.com/sigstore/public-good-instance/issues/480

Copy link
Member

Choose a reason for hiding this comment

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

i think we don't use anything that breaks, but i will double check

Expand All @@ -84,19 +75,18 @@ metadata:
spec:
provider:
gcpsm:
auth:
secretRef:
secretAccessKeySecretRef:
name: gcpsm-secret
key: secret-access-credentials
namespace: "external-secrets"
projectID: "${var.project_id}"
auth:
workloadIdentity:
clusterLocation: "${var.cluster_location}"
clusterName: "${var.cluster_name}"
clusterProjectID: "${var.project_id}"
serviceAccountRef:
name: local.k8s_sa
namespace: local.namespace
YAML

depends_on = [
helm_release.external_secrets,
kubernetes_secret_v1.gcpsm_secret
]
depends_on = [helm_release.external_secrets]
}

resource "kubectl_manifest" "trillian_namespace" {
Expand Down
10 changes: 10 additions & 0 deletions terraform/gcp/modules/external_secrets/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ variable "mysql_dbname" {
description = "Name of MySQL database."
default = "trillian"
}

variable "cluster_name" {
type = string
description = "Name of the cluster in GCP"
}

variable "cluster_location" {
type = string
description = "Location of the cluster in GCP"
}