Skip to content

Commit 00310c3

Browse files
Adding GSM and synk step (#499)
* Adding GSM and synk step * required updates for publishing Co-authored-by: Dustin Van Buskirk <[email protected]>
1 parent 78eaec7 commit 00310c3

File tree

6 files changed

+200
-0
lines changed

6 files changed

+200
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Step to Fetch Secret from Google Secret Manager
2+
3+
PreReqs:
4+
5+
1. [Hybrid Codefresh Runner](https://codefresh.io/docs/docs/administration/codefresh-runner/) on GKE
6+
7+
1. GKE w/ [Workload Identity Enabled](https://cloud.google.com/kubernetes-engine/docs/how-to/workload-identity)
8+
9+
1. GKE w/ [Config Connector Enabled](https://cloud.google.com/config-connector/docs/how-to/getting-started)
10+
11+
1. Create IAM Policy Binding between GCP SA and GKE SA.
12+
13+
```
14+
gcloud iam service-accounts add-iam-policy-binding <gcp-sa-name>@<gcp-project-name>.iam.gserviceaccount.com \
15+
--role roles/iam.workloadIdentityUser \
16+
--member "serviceAccount:<gcp-project-name>.svc.id.goog[<runner-namespace>/default]"
17+
```
18+
19+
1. Hybrid Codefresh Runner's Service Account `default` in the Runner namepsace must be properly annotated with a GSM Service Account that has access to Google Secret Manager to read the Secret.
20+
21+
Example of the annotation required.
22+
```
23+
apiVersion: v1
24+
kind: ServiceAccount
25+
metadata:
26+
annotations:
27+
iam.gke.io/gcp-service-account: <gcp-sa-name>@<gcp-project-name>.iam.gserviceaccount.com
28+
name: default
29+
namespace: codefresh
30+
secrets:
31+
- name: default-token
32+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# syntax=docker/dockerfile:1
2+
3+
FROM python:3.11.0-alpine3.16
4+
5+
COPY src/ /
6+
7+
RUN pip3 install -r requirements.txt
8+
9+
CMD [ "python3" , "get-secrets.py"]
10+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Import the Secret Manager client library.
2+
from google.cloud import secretmanager
3+
4+
import google_crc32c
5+
6+
import os, subprocess
7+
8+
9+
def access_secret_version(project_id, secret_id, version_id):
10+
11+
# Create the Secret Manager client.
12+
client = secretmanager.SecretManagerServiceClient()
13+
14+
# Build the resource name of the secret version.
15+
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
16+
17+
18+
# Access the secret version.
19+
response = client.access_secret_version(request={"name": name})
20+
21+
# Verify payload checksum.
22+
crc32c = google_crc32c.Checksum()
23+
crc32c.update(response.payload.data)
24+
if response.payload.data_crc32c != int(crc32c.hexdigest(), 16):
25+
print("Data corruption detected.")
26+
27+
payload = response.payload.data.decode("UTF-8")
28+
29+
env_file = open('/meta/env_vars_to_export', 'w')
30+
31+
subprocess.call(["echo", f"{env_var_key}={payload}"], stdout=env_file)
32+
33+
if __name__ == "__main__":
34+
35+
project_id = os.getenv('GCP_PROJECT_ID')
36+
37+
secret_id = os.getenv('GCP_SECRET_ID')
38+
39+
version_id= os.getenv('GCP_SECRET_VERSION')
40+
41+
env_var_key = os.getenv('ENV_VAR_KEY')
42+
43+
access_secret_version(project_id, secret_id, version_id)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
auth==0.5.3
2+
beautifulsoup4==4.11.1
3+
blinker==1.5
4+
cachetools==5.2.0
5+
certifi==2022.9.24
6+
charset-normalizer==2.1.1
7+
click==8.1.3
8+
dnspython==2.2.1
9+
eventlet==0.33.1
10+
falcon==3.1.0
11+
Flask==2.2.2
12+
google==3.0.0
13+
google-api-core==2.10.2
14+
google-auth==2.14.1
15+
google-cloud-secret-manager==2.12.6
16+
google-crc32c==1.5.0
17+
googleapis-common-protos==1.57.0
18+
greenlet==2.0.1
19+
grpc-google-iam-v1==0.12.4
20+
grpcio==1.50.0
21+
grpcio-status==1.50.0
22+
gunicorn==20.1.0
23+
idna==3.4
24+
importlib-metadata==5.0.0
25+
install==1.3.5
26+
itsdangerous==2.1.2
27+
Jinja2==3.1.2
28+
MarkupSafe==2.1.1
29+
mongoengine==0.24.2
30+
proto-plus==1.22.1
31+
protobuf==4.21.9
32+
pyasn1==0.4.8
33+
pyasn1-modules==0.2.8
34+
pymongo==4.3.2
35+
requests==2.28.1
36+
rsa==4.9
37+
six==1.16.0
38+
soupsieve==2.3.2.post1
39+
typing_extensions==4.3.0
40+
urllib3==1.26.12
41+
Werkzeug==2.2.2
42+
zipp==3.8.1
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
version: '1.0'
2+
kind: step-type
3+
metadata:
4+
name: google-secret-manager
5+
version: 0.0.1
6+
title: Fetch secrets from Google Secret Manager
7+
isPublic: true
8+
description: Read secrets from Google Secret Manager inside a Codefresh pipeline.
9+
sources:
10+
- 'https://github.com/codefresh-contrib/google-secret-manager/tree/main/step'
11+
maintainers:
12+
- name: Anthony Rozario
13+
official: true
14+
icon:
15+
type: image
16+
url: 'https://cdn.jsdelivr.net/gh/codefresh-contrib/google-secret-manager@main/step/secret_manager.png'
17+
background: '#f4f4f4'
18+
examples:
19+
- description: fetch-secret-from-gsm
20+
workflow:
21+
get-secret:
22+
title: Importing GSM Secret
23+
type: google-secret-manager
24+
arguments:
25+
GCP_PROJECT_ID: '${{PROJECT_ID}}'
26+
GCP_SECRET_ID: '${{SECRET_ID}}'
27+
GCP_SECRET_VERSION: '${{SECRET_VERSION}}'
28+
ENV_VAR_KEY: '${{ENV_VAR}}'
29+
spec:
30+
arguments: |-
31+
{
32+
"definitions": {},
33+
"$schema": "http://json-schema.org/draft-07/schema#",
34+
"type": "object",
35+
"additionalProperties": false,
36+
"patterns": [],
37+
"required": [
38+
"GCP_PROJECT_ID",
39+
"GCP_SECRET_ID",
40+
"GCP_SECRET_VERSION",
41+
"ENV_VAR_KEY"
42+
],
43+
"properties": {
44+
"GCP_PROJECT_ID": {
45+
"type": "string",
46+
"description": "Name of the Secret's GCP Project"
47+
},
48+
"GCP_SECRET_ID": {
49+
"type": "string",
50+
"description": "Name of the Secret"
51+
},
52+
"GCP_SECRET_VERSION": {
53+
"type": "string",
54+
"description": "Version of the Secret"
55+
},
56+
"ENV_VAR_KEY": {
57+
"type": "string",
58+
"description": "Environment variable key to store the Secret's value"
59+
}
60+
}
61+
}
62+
steps:
63+
main:
64+
name: fetch-google-secret
65+
image: codefreshplugins/google-secret-manager:0.0.1
66+
working_directory: /
67+
environment:
68+
- 'GCP_PROJECT_ID=${{GCP_PROJECT_ID}}'
69+
- 'GCP_SECRET_ID=${{GCP_SECRET_ID}}'
70+
- 'GCP_SECRET_VERSION=${{GCP_SECRET_VERSION}}'
71+
- 'ENV_VAR_KEY=${{ENV_VAR_KEY}}'
72+
commands:
73+
- python3 /get-secrets.py
3.36 KB
Loading

0 commit comments

Comments
 (0)