Skip to content

Commit 34be55f

Browse files
authored
Added GCP KMS option for sops encryption (#38)
1 parent 1c0b237 commit 34be55f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Git as Terraform backend? Seriously? I know, might sound like a stupid idea at f
2424
- [`sops`](#sops)
2525
- [PGP](#pgp)
2626
- [AWS KMS](#aws-kms)
27+
- [GCP KMS](#gcp-kms)
2728
- [Hashicorp Vault](#hashicorp-vault)
2829
- [AES256](#aes256)
2930
- [Running backend remotely](#running-backend-remotely)
@@ -221,6 +222,7 @@ We are using [`sops`](https://github.com/mozilla/sops) as encryption abstraction
221222

222223
- PGP
223224
- AWS KMS
225+
- GCP KMS
224226
- Hashicorp Vault
225227

226228
Before we integrated with `sops` - we had a basic AES256 encryption via static passphrase. It is no longer recommended, although might be useful in some limited scenarios. Basic AES256 encryption is using one shared key, and it encrypts entire JSON state file that it can no longer be read as JSON. `sops` supports various encryption-as-service providers such as AWS KMS and Hashicorp Vault Transit - meaning encryption can be safely performed without revealing private key to the encryption clients. That means keys can be easily rotated, access can be easily revoked and generally it dramatically reduces chances of the key leaks.
@@ -237,6 +239,10 @@ Use `TF_BACKEND_HTTP_SOPS_PGP_FP` to provide a comma separated PGP key fingerpri
237239

238240
Use `TF_BACKEND_HTTP_SOPS_AWS_KMS_ARNS` to provide a comma separated list of KMS ARNs. AWS SDK will use standard [credentials provider chain](https://docs.aws.amazon.com/sdk-for-go/api/aws/credentials/) in order to automatically discover local credentials in standard `AWS_*` environment variables or `~/.aws`. You can optionally use `TF_BACKEND_HTTP_SOPS_AWS_PROFILE` to point it to a specific shared profile. You can also provide additional KMS encryption context using `TF_BACKEND_HTTP_SOPS_AWS_KMS_CONTEXT` - it is a comma separated list of `key=value` pairs.
239241

242+
##### GCP KMS
243+
244+
Use `TF_BACKEND_HTTP_SOPS_GCP_KMS_KEYS` to provide a comma separated list of GCP KMS IDs. Read [Encrypting using GCP KMS](https://github.com/getsops/sops#encrypting-using-gcp-kms) for further details.
245+
240246
##### Hashicorp Vault
241247

242248
Use `TF_BACKEND_HTTP_SOPS_HC_VAULT_URIS` to point it to the Vault Transit keys. It is a comma separated list of URLs in a form of `${VAULT_ADDR}/v1/transit/keys/key`, where `transit` is a name of Vault Transit mount and `key` is the name of the key in that mount. Under the hood Vault SDK is using standard credentials resolver to automatically discover Vault credentials in the environment, meaning you can either use `vault login` or set `VAULT_TOKEN` environment variable.

crypt/sops/gcp_kms.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package sops
2+
3+
import (
4+
"os"
5+
6+
sops "go.mozilla.org/sops/v3"
7+
"go.mozilla.org/sops/v3/gcpkms"
8+
)
9+
10+
func init() {
11+
Configs["gcp-kms"] = &GcpKmsConfig{}
12+
}
13+
14+
type GcpKmsConfig struct{}
15+
16+
func (c *GcpKmsConfig) IsActivated() bool {
17+
_, ok := os.LookupEnv("TF_BACKEND_HTTP_SOPS_GCP_KMS_KEYS")
18+
return ok
19+
}
20+
21+
func (c *GcpKmsConfig) KeyGroup() (sops.KeyGroup, error) {
22+
keys := os.Getenv("TF_BACKEND_HTTP_SOPS_GCP_KMS_KEYS")
23+
24+
var keyGroup sops.KeyGroup
25+
26+
for _, k := range gcpkms.MasterKeysFromResourceIDString(keys) {
27+
keyGroup = append(keyGroup, k)
28+
}
29+
30+
return keyGroup, nil
31+
}

0 commit comments

Comments
 (0)