-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): add support for GCP KMS (#184)
* feat(providers): add support for GCP KMS Signed-off-by: Mateusz Hromada <[email protected]> * Fix linter errors Signed-off-by: Mateusz Hromada <[email protected]> * feat(providers): add basic test for GKMS Signed-off-by: Mateusz Hromada <[email protected]> # Conflicts: # pkg/stringprovider/stringprovider.go # vals.go --------- Signed-off-by: Mateusz Hromada <[email protected]>
- Loading branch information
Showing
7 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package gkms | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
|
||
kms "cloud.google.com/go/kms/apiv1" | ||
kmspb "cloud.google.com/go/kms/apiv1/kmspb" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/helmfile/vals/pkg/api" | ||
"github.com/helmfile/vals/pkg/log" | ||
) | ||
|
||
type provider struct { | ||
log *log.Logger | ||
Project string | ||
Location string | ||
Keyring string | ||
CryptoKey string | ||
} | ||
|
||
func New(l *log.Logger, cfg api.StaticConfig) *provider { | ||
p := &provider{ | ||
log: l, | ||
} | ||
p.Project = cfg.String("project") | ||
p.Location = cfg.String("location") | ||
p.Keyring = cfg.String("keyring") | ||
p.CryptoKey = cfg.String("crypto_key") | ||
return p | ||
} | ||
|
||
func (p *provider) GetString(key string) (string, error) { | ||
ctx := context.Background() | ||
value, err := p.getValue(ctx, key) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(value), nil | ||
} | ||
|
||
func (p *provider) GetStringMap(key string) (map[string]interface{}, error) { | ||
ctx := context.Background() | ||
value, err := p.getValue(ctx, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var valueMap map[string]interface{} | ||
if err := yaml.Unmarshal(value, &valueMap); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal value: %w", err) | ||
} | ||
return valueMap, nil | ||
} | ||
|
||
func (p *provider) getValue(ctx context.Context, key string) ([]byte, error) { | ||
c, err := kms.NewKeyManagementClient(ctx) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to connect: %s", err) | ||
return nil, err | ||
} | ||
defer func() { | ||
if err := c.Close(); err != nil { | ||
p.log.Debugf("gkms: %v", err) | ||
} | ||
}() | ||
blob, err := base64.URLEncoding.DecodeString(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := &kmspb.DecryptRequest{ | ||
Name: fmt.Sprintf("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", p.Project, p.Location, p.Keyring, p.CryptoKey), | ||
Ciphertext: blob, | ||
} | ||
|
||
resp, err := c.Decrypt(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.Plaintext, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package vals | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestValues_GKMS(t *testing.T) { | ||
// TODO | ||
// create gkms and encrypt test value | ||
// gcloud kms keyrings create "test" --location "global" | ||
// gcloud kms keys create "default" --location "global" --keyring "test" --purpose "encryption" | ||
// echo -n "test_value" \ | ||
// | gcloud kms encrypt \ | ||
// --location "global" \ | ||
// --keyring "test" \ | ||
// --key "default" \ | ||
// --plaintext-file - \ | ||
// --ciphertext-file - \ | ||
// | base64 -w0 \ | ||
// | tr '/+' '_-' | ||
// | ||
// run with: | ||
// | ||
// go test -run '^(TestValues_GKMS)$' | ||
|
||
type testcase struct { | ||
template map[string]interface{} | ||
expected map[string]interface{} | ||
} | ||
|
||
plain_value := "test_value" | ||
encrypted_value := "CiQAmPqoGAKT97oUK0DdiI_cLDm3j6iPDK4-TJ3yQII-snFHCckSMwAkTpnEoD5wOeRaZrt3eC1ewFMuw617fqqjTStrsar9ciGERzk5t6uMgA0HKzSxGMdjHQ==" | ||
|
||
project := "test-project" | ||
location := "global" | ||
keyring := "test" | ||
crypto_key := "default" | ||
|
||
testcases := []testcase{ | ||
{ | ||
template: map[string]interface{}{ | ||
"test_key": fmt.Sprintf("ref+gkms://%s?project=%s&location=%s&keyring=%s&crypto_key=%s", encrypted_value, project, location, keyring, crypto_key), | ||
}, | ||
expected: map[string]interface{}{ | ||
"test_key": plain_value, | ||
}, | ||
}, | ||
} | ||
|
||
for i := range testcases { | ||
tc := testcases[i] | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
vals, err := Eval(tc.template) | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
diff := cmp.Diff(tc.expected, vals) | ||
if diff != "" { | ||
t.Errorf("unexpected diff: %s", diff) | ||
} | ||
}) | ||
} | ||
} |