-
Notifications
You must be signed in to change notification settings - Fork 6
/
aws.go
50 lines (42 loc) · 1.3 KB
/
aws.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
// Package cfg :: aws.go - extended AWS SDK functions
package cfg
import (
"encoding/base64"
"log"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
)
var (
_kmsService KMSDecryptInterface = kms.New(session.New(),
&aws.Config{Region: aws.String(os.Getenv("AWS_DEFAULT_REGION"))})
)
// KMSDecryptInterface interface
type KMSDecryptInterface interface {
Decrypt(input *kms.DecryptInput) (*kms.DecryptOutput, error)
}
// Decrypt tries to decrypt a text
func Decrypt(text string) string {
log.Println("[aws] decrypting:", text)
inputBlob := []byte(text)
if base64Blob, err := base64.StdEncoding.DecodeString(text); err == nil {
inputBlob = base64Blob
}
deInput := &kms.DecryptInput{CiphertextBlob: inputBlob}
if decrypt, err := _kmsService.Decrypt(deInput); err == nil {
return string(decrypt.Plaintext)
}
return text
}
// DecryptKeyTextByKMS checks possible encrypted KMS key/value and retruns decrypted text
func DecryptKeyTextByKMS(key, text string) string {
keyLowers := strings.ToLower(key)
keyOrPass := strings.Contains(keyLowers, "api_key") || strings.Contains(keyLowers, "password")
encrypted := len(text) > 128 && !strings.Contains(text, " ")
if keyOrPass && encrypted {
return Decrypt(text)
}
return text
}