Skip to content
This repository was archived by the owner on Nov 10, 2019. It is now read-only.

Commit 090ba8b

Browse files
Google KMS plugin
1 parent 9ddb0be commit 090ba8b

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ See each plugin readme for more info and usage instructions.
2020
| [Twistlock](plugins/cfstep-twistlock) | Security scanning of docker images using Twistlock | `security` |
2121
| [Clair](plugins/clair/README.md) | Security scanning of Docker images using Clair | `security` |
2222
| [Import Docker Images](plugins/import-docker-images/README.md) | Import Docker images metadata into Codefresh| `docker` `codefresh`|
23+
| [Google KMS](plugins/google-kms/README.md) | Encryption/Decryption with Google KMS| `KMS` `codefresh`|

plugins/google-kms/Dockerfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM google/cloud-sdk:alpine
2+
3+
WORKDIR /kms
4+
5+
RUN apk -U add jq bash
6+
ENV PATH=${PATH}:/kms
7+
8+
COPY google-kms.sh ./kms
9+
10+

plugins/google-kms/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
odefresh Google KMS plugin
2+
3+
This plugin facilitates work with Google Key Management Service for such operations like *encrypting* and *decrypting*
4+
5+
# Usage
6+
7+
kms [OPERATION] [VALUE_1] [VALUE_n...]
8+
9+
Set the plugin required environment variables for the pipeline and use the plugin as a freestyle step with a command like:
10+
11+
```yaml
12+
GoogleKMS:
13+
image: codefresh/google-kms
14+
commands:
15+
- kms encrypt VALUE_1 VALUE_n
16+
```
17+
where VALUE_1 and VALUE_n are the **names** of the environment variables containing the values you need to encrypt or decrypt.
18+
19+
The operation is mutable and when the step finishes the variables with the same names will contain encrypted values. For decryption the process is similar
20+
21+
# Required environment variables
22+
23+
- `KMS_PROJECT` - GCP project name in which your KMS entities are present
24+
- `KMS_LOCATION` - Google KMS location
25+
- `KMS_KEYRING` - Google KMS keyring
26+
- `KMS_KEY` - Google KMS key
27+
- `GCP_SA_KEY` - [Google Service Account Key (JSON)](https://cloud.google.com/iam/docs/creating-managing-service-account-keys)

plugins/google-kms/google-kms.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
for pluginVar in KMS_PROJECT KMS_LOCATION KMS_KEYRING KMS_KEY
4+
do
5+
if [ -z ${!pluginVar} ]; then echo $pluginVar is not set, stopping...; exit 1; fi
6+
done
7+
8+
echo $GCP_SA_KEY > google-app-creds.json
9+
export GOOGLE_APPLICATION_CREDENTIALS=$(realpath google-app-creds.json)
10+
operation=$1
11+
12+
13+
function encrypt () {
14+
15+
hashedtext=$(echo $2 | base64 | tr -d '\n')
16+
cf_export $1=$(curl -s -X POST "https://cloudkms.googleapis.com/v1/projects/$KMS_PROJECT/locations/$KMS_LOCATION/keyRings/$KMS_KEYRING/cryptoKeys/$KMS_KEY:encrypt" \
17+
-d "{\"plaintext\":\"$hashedtext\"}" \
18+
-H "Authorization:Bearer $(gcloud auth application-default print-access-token)" \
19+
-H "Content-Type:application/json" | jq '.ciphertext' --raw-output )
20+
21+
}
22+
23+
function decrypt {
24+
25+
cf_export $1=$(curl -s -X POST "https://cloudkms.googleapis.com/v1/projects/$KMS_PROJECT/locations/$KMS_LOCATION/keyRings/$KMS_KEYRING/cryptoKeys/$KMS_KEY:decrypt" \
26+
-d "{\"ciphertext\":\"$2\"}" \
27+
-H "Authorization:Bearer $(gcloud auth application-default print-access-token)" \
28+
-H "Content-Type:application/json" | jq '.plaintext' --raw-output | base64 -d)
29+
30+
}
31+
32+
for secret in "${@: 2}"
33+
do
34+
$operation $secret ${!secret}
35+
done

0 commit comments

Comments
 (0)