-
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
Signed-off-by: Mateusz Hromada <[email protected]>
- Loading branch information
Showing
6 changed files
with
115 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" | ||
|
||
"github.com/helmfile/vals/pkg/api" | ||
"github.com/helmfile/vals/pkg/log" | ||
"gopkg.in/yaml.v3" | ||
|
||
kms "cloud.google.com/go/kms/apiv1" | ||
kmspb "cloud.google.com/go/kms/apiv1/kmspb" | ||
) | ||
|
||
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