-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkms.go
71 lines (59 loc) · 1.37 KB
/
kms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
)
type KMSProvider interface {
Decrypt([]byte) ([]byte, error)
}
type AWSConfig struct {
SharedCredsFileName string
Profile string
AccessKeyID string
SecretAccessKey string
SessionToken string
Region string
}
type AWSKMS struct {
Client *kms.KMS
}
func NewAWSKMS(c *AWSConfig) (KMSProvider, error) {
providers := []credentials.Provider{
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{
Profile: c.Profile,
Filename: c.SharedCredsFileName,
},
&credentials.StaticProvider{
Value: credentials.Value{
AccessKeyID: c.AccessKeyID,
SecretAccessKey: c.SecretAccessKey,
SessionToken: c.SessionToken,
},
},
}
creds := credentials.NewChainCredentials(providers)
sess, err := session.NewSession(&aws.Config{
Credentials: creds,
Region: aws.String(c.Region),
})
if err != nil {
return nil, err
}
kms := &AWSKMS{
Client: kms.New(sess),
}
return kms, nil
}
func (k *AWSKMS) Decrypt(cipher []byte) ([]byte, error) {
input := &kms.DecryptInput{
CiphertextBlob: cipher,
}
output, err := k.Client.Decrypt(input)
if err != nil {
return nil, err
}
return output.Plaintext, nil
}