forked from kubernetes-retired/kube-aws
/
credential.go
173 lines (148 loc) · 4.88 KB
/
credential.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package config
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
)
const CacheFileExtension = "enc"
const FingerprintFileExtension = "fingerprint"
type RawCredentialOnDisk struct {
content []byte
filePath string
}
// The fact KMS encryption produces different ciphertexts for the same plaintext had been
// causing unnecessary node replacements(https://github.com/kubernetes-incubator/kube-aws/issues/107)
// Persist encrypted assets for caching purpose so that we can avoid that.
type EncryptedCredentialOnDisk struct {
content []byte
filePath string
fingerprintFilePath string
fingerprint string
}
type CachedEncryptor struct {
bytesEncryptionService bytesEncryptionService
}
func (e CachedEncryptor) EncryptedBytes(raw []byte) ([]byte, error) {
return e.bytesEncryptionService.Encrypt(raw)
}
func (e CachedEncryptor) EncryptedCredentialFromPath(filePath string, defaultValue *string) (*EncryptedCredentialOnDisk, error) {
raw, err := RawCredentialFileFromPath(filePath, defaultValue)
if err != nil {
return nil, err
}
cache, err := EncryptedCredentialCacheFromPath(filePath)
if err != nil {
cache, err = EncryptedCredentialCacheFromRawCredential(raw, e.bytesEncryptionService)
if err != nil {
return nil, err
}
fmt.Printf("INFO: generated \"%s\" by encrypting \"%s\"\n", cache.filePath, raw.filePath)
} else if raw.Fingerprint() != cache.Fingerprint() {
fmt.Printf("INFO: \"%s\" is not up-to-date. kube-aws is regenerating it from \"%s\"\n", cache.filePath, raw.filePath)
cache, err = EncryptedCredentialCacheFromRawCredential(raw, e.bytesEncryptionService)
if err != nil {
return nil, err
}
}
return cache, nil
}
func EncryptedCredentialCacheFromRawCredential(raw *RawCredentialOnDisk, bytesEncryptionService bytesEncryptionService) (*EncryptedCredentialOnDisk, error) {
encrypted, err := bytesEncryptionService.Encrypt(raw.content)
if err != nil {
return nil, err
}
cache := EncryptedCredentialOnDisk{
filePath: cacheFilePath(raw.filePath),
fingerprintFilePath: fingerprintFilePath(raw.filePath),
content: encrypted,
fingerprint: raw.Fingerprint(),
}
if err := cache.Persist(); err != nil {
return nil, err
}
return &cache, nil
}
func RawCredentialFileFromPath(filePath string, defaultValue *string) (*RawCredentialOnDisk, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) && defaultValue != nil {
if err := ioutil.WriteFile(filePath, []byte(*defaultValue), 0644); err != nil {
return nil, err
}
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return &RawCredentialOnDisk{
filePath: filePath,
content: content,
}, nil
}
func (c *RawCredentialOnDisk) Fingerprint() string {
return calculateFingerprint(c.content)
}
func (c *RawCredentialOnDisk) Persist() error {
if err := ioutil.WriteFile(c.filePath, c.content, 0600); err != nil {
return err
}
return nil
}
func (c *RawCredentialOnDisk) String() string {
return string(c.content)
}
func cacheFilePath(rawCredFilePath string) string {
return fmt.Sprintf("%s.%s", rawCredFilePath, CacheFileExtension)
}
func fingerprintFilePath(rawCredFilePath string) string {
return fmt.Sprintf("%s.%s", rawCredFilePath, FingerprintFileExtension)
}
func EncryptedCredentialCacheFromPath(filePath string) (*EncryptedCredentialOnDisk, error) {
cachePath := cacheFilePath(filePath)
credential, cacheErr := ioutil.ReadFile(cachePath)
if cacheErr != nil {
return nil, cacheErr
}
fingerprintPath := fingerprintFilePath(filePath)
fingerprint, fingerprintErr := loadFingerprint(fingerprintPath)
if fingerprintErr != nil {
fmt.Printf("WARNING: \"%s\" does not exist. Did you explicitly removed it or upgrading from old kube-aws? Anyway, kube-aws is generating one for you from \"%s\" to automatically detect updates to it and recreate \"%s\" if necessary\n", fingerprintPath, filePath, cachePath)
raw, rawErr := RawCredentialFileFromPath(filePath, nil)
if rawErr != nil {
return nil, rawErr
}
fingerprint = raw.Fingerprint()
}
return &EncryptedCredentialOnDisk{
filePath: cachePath,
fingerprintFilePath: fingerprintPath,
content: credential,
fingerprint: fingerprint,
}, nil
}
func (c *EncryptedCredentialOnDisk) Fingerprint() string {
return c.fingerprint
}
func (c *EncryptedCredentialOnDisk) Persist() error {
if err := ioutil.WriteFile(c.filePath, c.content, 0600); err != nil {
return err
}
if err := ioutil.WriteFile(c.fingerprintFilePath, []byte(c.fingerprint), 0600); err != nil {
return err
}
return nil
}
func (c *EncryptedCredentialOnDisk) String() string {
return string(c.content)
}
func loadFingerprint(file string) (string, error) {
p, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(p), nil
}
func calculateFingerprint(content []byte) string {
h := sha256.New()
h.Write(content)
return fmt.Sprintf("%x", h.Sum(nil))
}