From a21c4796a38458d808b488d027906b10b1f4c97f Mon Sep 17 00:00:00 2001 From: Brad Geesaman Date: Fri, 9 Aug 2019 16:25:58 -0400 Subject: [PATCH 1/2] TF 12, Vault 1.2.0, Jenkinsfile formatting --- Jenkinsfile | 151 ++++++++++++++++--------------------- README.md | 12 ++- k8s-manifests/sidecar.yaml | 2 +- scripts/auth-to-vault.sh | 3 + terraform/gcs.tf | 4 +- terraform/iam.tf | 8 +- terraform/main.tf | 51 +++++++------ terraform/network.tf | 38 +++++----- terraform/outputs.tf | 10 +-- terraform/provider.tf | 14 ++-- terraform/variables.tf | 10 +-- terraform/vault.tf | 8 +- 12 files changed, 154 insertions(+), 157 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 76f80bf..b6d91a1 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,4 +1,5 @@ #!/usr/bin/env groovy + /* Copyright 2018 Google LLC @@ -22,93 +23,75 @@ limitations under the License. // Reference: https://github.com/jenkinsci/kubernetes-plugin // set up pod label and GOOGLE_APPLICATION_CREDENTIALS (for Terraform) -def label = "k8s-infra" -def containerName = "k8s-node" -def GOOGLE_APPLICATION_CREDENTIALS = '/home/jenkins/dev/jenkins-deploy-dev-infra.json' +def containerName = "vault" +def GOOGLE_APPLICATION_CREDENTIALS = '/home/jenkins/dev/jenkins-deploy-dev-infra.json' // Tells the ./scripts/common.sh which VAULT_VERSION of the vault CLI binary to use -def VAULT_VERSION = '1.0.2' - -podTemplate(label: label, yaml: """ -apiVersion: v1 -kind: Pod -metadata: - labels: - jenkins: build-node -spec: - containers: - - name: ${containerName} - image: gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${env.CONTAINER_VERSION} - command: ['cat'] - tty: true - volumeMounts: - # Mount the dev service account key - - name: dev-key - mountPath: /home/jenkins/dev - # Mount the host /dev/urandom to /dev/random for entropy - - name: random - mountPath: /dev/random - volumes: - # Create a volume that contains the dev json key that was saved as a secret - - name: dev-key - secret: - secretName: jenkins-deploy-dev-infra - # Host /dev/urandom to allow for entropy access - - name: random - hostPath: - path: /dev/urandom -""" - ) { - node(label) { - try { - // Options covers all other job properties or wrapper functions that apply to entire Pipeline. - properties([disableConcurrentBuilds()]) - // set env variable GOOGLE_APPLICATION_CREDENTIALS for Terraform - env.GOOGLE_APPLICATION_CREDENTIALS=GOOGLE_APPLICATION_CREDENTIALS +def VAULT_VERSION = '1.2.0' +def jenkins_container_version = env.JENKINS_CONTAINER_VERSION - stage('Setup') { - container(containerName) { - // checkout code from scm i.e. commits related to the PR - checkout scm - - // Setup gcloud service account access - sh "gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS}" - sh "gcloud config set compute/zone ${env.ZONE}" - sh "gcloud config set core/project ${env.PROJECT_ID}" - sh "gcloud config set compute/region ${env.REGION}" - } - } - stage('Lint') { - container(containerName) { - sh "make lint" - } - } +podTemplate( + containers: [ + containerTemplate(name: "${containerName}", + image: "gcr.io/pso-helmsman-cicd/jenkins-k8s-node:${jenkins_container_version}", + command: 'tail -f /dev/null', + resourceRequestCpu: '1000m', + resourceLimitCpu: '2000m', + resourceRequestMemory: '1Gi', + resourceLimitMemory: '2Gi' + ) + ], + volumes: [secretVolume(mountPath: '/home/jenkins/dev', + secretName: 'jenkins-deploy-dev-infra' + ), + hostPathVolume(mountPath: '/dev/random', hostPath: '/dev/urandom') + ] +) { + node(POD_LABEL) { + try { + // Options covers all other job properties or wrapper functions that apply to entire Pipeline. + properties([disableConcurrentBuilds()]) + // set env variable GOOGLE_APPLICATION_CREDENTIALS for Terraform + env.GOOGLE_APPLICATION_CREDENTIALS = GOOGLE_APPLICATION_CREDENTIALS - stage('Create') { - container(containerName) { - sh "make create" - } - } + stage('Setup') { + container(containerName) { + // checkout code from scm i.e. commits related to the PR + checkout scm - stage('Validate') { - container(containerName) { - sh "make validate" + // Setup gcloud service account access + sh "gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS}" + sh "gcloud config set compute/zone ${env.ZONE}" + sh "gcloud config set core/project ${env.PROJECT_ID}" + sh "gcloud config set compute/region ${env.REGION}" + } + } + stage('Lint') { + container(containerName) { + sh "make lint" + } + } + stage('Create') { + container(containerName) { + sh "make create" + } + } + stage('Validate') { + container(containerName) { + sh "make validate" + } + } + } catch (err) { + // if any exception occurs, mark the build as failed + // and display a detailed message on the Jenkins console output + currentBuild.result = 'FAILURE' + echo "FAILURE caught echo ${err}" + throw err + } finally { + stage('Teardown') { + container(containerName) { + sh "make teardown" + } + } } } - - } - catch (err) { - // if any exception occurs, mark the build as failed - // and display a detailed message on the Jenkins console output - currentBuild.result = 'FAILURE' - echo "FAILURE caught echo ${err}" - throw err - } - finally { - stage('Teardown') { - container(containerName) { - sh "make teardown" - } - } - } - } -} +} \ No newline at end of file diff --git a/README.md b/README.md index decc95b..cc267b8 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ Recovery Seal Type shamir Sealed false Total Recovery Shares 1 Threshold 1 -Version 1.0.0 +Version 1.2.0 Cluster Name vault-cluster-be7094aa Cluster ID ac0d2d33-61db-a06a-77d0-eb9c1e87b236 HA Enabled true @@ -136,6 +136,12 @@ HA Cluster https://10.24.1.3:8201 HA Mode active ``` +Enable the `kv` store inside Vault: + +```console +vault secrets enable -path=secret/ kv +``` + Create a sample secret in Vault inside the custom `kv` path: ```console @@ -181,7 +187,7 @@ Recovery Seal Type shamir Sealed false Total Recovery Shares 1 Threshold 1 -Version 1.0.0 +Version 1.2.0 Cluster Name vault-cluster-be7094aa Cluster ID ac0d2d33-61db-a06a-77d0-eb9c1e87b236 HA Enabled true @@ -320,7 +326,7 @@ Recovery Seal Type shamir Sealed false Total Recovery Shares 1 Threshold 1 -Version 1.0.0 +Version 1.2.0 Cluster Name vault-cluster-be7094aa Cluster ID ac0d2d33-61db-a06a-77d0-eb9c1e87b236 HA Enabled true diff --git a/k8s-manifests/sidecar.yaml b/k8s-manifests/sidecar.yaml index 37dc0b5..51e95e4 100644 --- a/k8s-manifests/sidecar.yaml +++ b/k8s-manifests/sidecar.yaml @@ -51,7 +51,7 @@ spec: # The vault-authenticator container authenticates the container using the # kubernetes auth method and puts the resulting token on the filesystem. - name: vault-authenticator - image: registry.hub.docker.com/sethvargo/vault-kubernetes-authenticator:0.1.0 + image: registry.hub.docker.com/sethvargo/vault-kubernetes-authenticator:0.3.0 imagePullPolicy: Always volumeMounts: # The mount where the vault token will be written after login diff --git a/scripts/auth-to-vault.sh b/scripts/auth-to-vault.sh index 017d55b..c7d6cc9 100755 --- a/scripts/auth-to-vault.sh +++ b/scripts/auth-to-vault.sh @@ -99,6 +99,9 @@ vault write auth/kubernetes/config \ kubernetes_ca_cert="${K8S_CACERT}" \ token_reviewer_jwt="${TR_ACCOUNT_TOKEN}" +# Enable the KV secrets backend +vault secrets enable -path=secret/ kv + # Create a policy to be referenced by a role to access the kv location secret/myapp/* vault policy write myapp-kv-rw - < Date: Tue, 13 Aug 2019 14:41:05 -0400 Subject: [PATCH 2/2] fix IAM permissions and linting --- terraform/iam.tf | 10 +++++++++- terraform/main.tf | 7 ++++++- terraform/variables.tf | 12 ++++++++++++ test/make.sh | 6 ++++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/terraform/iam.tf b/terraform/iam.tf index 623b1a6..93f09a5 100644 --- a/terraform/iam.tf +++ b/terraform/iam.tf @@ -14,7 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -# Add user-specified roles +# Add user-specified roles to App SA +resource "google_project_iam_member" "app-service-account" { + count = length(var.app_service_account_roles) + project = var.project + role = element(var.app_service_account_roles, count.index) + member = format("serviceAccount:gke-vault-demo-app-cluster@%s.iam.gserviceaccount.com", var.project) +} + +# Add user-specified roles to Vault SA resource "google_project_iam_member" "service-account" { count = length(var.service_account_roles) project = var.project diff --git a/terraform/main.tf b/terraform/main.tf index 1d7825f..fcdd0a9 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -65,7 +65,12 @@ resource "google_container_cluster" "app" { service_account = google_service_account.app_cluster.email oauth_scopes = [ - "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/devstorage.read_only", + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/monitoring", + "https://www.googleapis.com/auth/servicecontrol", + "https://www.googleapis.com/auth/service.management.readonly", + "https://www.googleapis.com/auth/trace.append" ] # Set metadata on the VM to supply more entropy diff --git a/terraform/variables.tf b/terraform/variables.tf index 832bf54..da4f88e 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -178,3 +178,15 @@ List of roles to be granted to the vault-server SA in this application cluster project for managing SAs and SA Keys. EOF } + +variable "app_service_account_roles" { + type = "list" + + default = [ + "roles/editor" + ] + + description = <