-
Notifications
You must be signed in to change notification settings - Fork 88
/
write.go
225 lines (185 loc) · 6.32 KB
/
write.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package upstream
import (
"bytes"
"encoding/base64"
"io/ioutil"
"os"
"path"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kots/pkg/crypto"
"github.com/replicatedhq/kots/pkg/upstream/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
serializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes/scheme"
)
func WriteUpstream(u *types.Upstream, options types.WriteOptions) error {
renderDir := options.RootDir
if options.CreateAppDir {
renderDir = path.Join(renderDir, u.Name)
}
renderDir = path.Join(renderDir, "upstream")
if options.IncludeAdminConsole {
adminConsoleFiles, err := GenerateAdminConsoleFiles(renderDir, options)
if err != nil {
return errors.Wrap(err, "failed to generate admin console")
}
u.Files = append(u.Files, adminConsoleFiles...)
}
var previousInstallationContent []byte
_, err := os.Stat(renderDir)
if err == nil {
_, err = os.Stat(path.Join(renderDir, "userdata", "installation.yaml"))
if err == nil {
c, err := ioutil.ReadFile(path.Join(renderDir, "userdata", "installation.yaml"))
if err != nil {
return errors.Wrap(err, "failed to read existing installation")
}
previousInstallationContent = c
}
if err := os.RemoveAll(renderDir); err != nil {
return errors.Wrap(err, "failed to remove previous content in upstream")
}
}
var prevInstallation *kotsv1beta1.Installation
if previousInstallationContent != nil {
decode := scheme.Codecs.UniversalDeserializer().Decode
prevObj, _, err := decode(previousInstallationContent, nil, nil)
if err != nil {
return errors.Wrap(err, "failed to decode previous installation")
}
prevInstallation = prevObj.(*kotsv1beta1.Installation)
}
encryptionKey, err := getEncryptionKey(prevInstallation)
if err != nil {
return errors.Wrap(err, "failed to get encryption key")
}
u.EncryptionKey = encryptionKey
for i, file := range u.Files {
fileRenderPath := path.Join(renderDir, file.Path)
d, _ := path.Split(fileRenderPath)
if _, err := os.Stat(d); os.IsNotExist(err) {
if err := os.MkdirAll(d, 0744); err != nil {
return errors.Wrap(err, "failed to mkdir")
}
}
if options.EncryptConfig {
configValues := contentToConfigValues(file.Content)
if configValues != nil {
content, err := encryptConfigValues(configValues, encryptionKey)
if err != nil {
return errors.Wrap(err, "failed to encrypt config values")
}
file.Content = content
u.Files[i] = file
}
}
identityConfig := contentToIdentityConfig(file.Content)
if identityConfig != nil {
content, err := maybeEncryptIdentityConfig(identityConfig, encryptionKey)
if err != nil {
return errors.Wrap(err, "failed to encrypt identity config")
}
file.Content = content
u.Files[i] = file
}
if err := ioutil.WriteFile(fileRenderPath, file.Content, 0644); err != nil {
return errors.Wrap(err, "failed to write upstream file")
}
}
channelID, channelName := "", ""
if prevInstallation != nil && options.PreserveInstallation {
channelID = prevInstallation.Spec.ChannelID
channelName = prevInstallation.Spec.ChannelName
} else {
channelID = u.ChannelID
channelName = u.ChannelName
}
installation := kotsv1beta1.Installation{
TypeMeta: metav1.TypeMeta{
APIVersion: "kots.io/v1beta1",
Kind: "Installation",
},
ObjectMeta: metav1.ObjectMeta{
Name: u.Name,
},
Spec: kotsv1beta1.InstallationSpec{
UpdateCursor: u.UpdateCursor,
ChannelID: channelID,
ChannelName: channelName,
VersionLabel: u.VersionLabel,
ReleaseNotes: u.ReleaseNotes,
EncryptionKey: encryptionKey,
},
}
if u.ReleasedAt != nil {
releasedAt := metav1.NewTime(*u.ReleasedAt)
installation.Spec.ReleasedAt = &releasedAt
}
if _, err := os.Stat(path.Join(renderDir, "userdata")); os.IsNotExist(err) {
if err := os.MkdirAll(path.Join(renderDir, "userdata"), 0755); err != nil {
return errors.Wrap(err, "failed to create userdata dir")
}
}
err = ioutil.WriteFile(path.Join(renderDir, "userdata", "installation.yaml"), mustMarshalInstallation(&installation), 0644)
if err != nil {
return errors.Wrap(err, "failed to write installation")
}
return nil
}
func getEncryptionKey(prevInstallation *kotsv1beta1.Installation) (string, error) {
if prevInstallation == nil {
cipher, err := crypto.NewAESCipher()
if err != nil {
return "", errors.Wrap(err, "failed to create new AES cipher")
}
return cipher.ToString(), nil
}
return prevInstallation.Spec.EncryptionKey, nil
}
func mustMarshalInstallation(installation *kotsv1beta1.Installation) []byte {
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(installation, &b); err != nil {
panic(err)
}
return b.Bytes()
}
func encryptConfigValues(configValues *kotsv1beta1.ConfigValues, encryptionKey string) ([]byte, error) {
cipher, err := crypto.AESCipherFromString(encryptionKey)
if err != nil {
return nil, errors.Wrap(err, "failed to load encryption cipher")
}
for k, v := range configValues.Spec.Values {
if v.ValuePlaintext == "" {
continue
}
v.Value = base64.StdEncoding.EncodeToString(cipher.Encrypt([]byte(v.ValuePlaintext)))
v.ValuePlaintext = ""
configValues.Spec.Values[k] = v
}
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(configValues, &b); err != nil {
return nil, errors.Wrap(err, "failed to encode config values")
}
return b.Bytes(), nil
}
func maybeEncryptIdentityConfig(identityConfig *kotsv1beta1.IdentityConfig, encryptionKey string) ([]byte, error) {
cipher, err := crypto.AESCipherFromString(encryptionKey)
if err != nil {
return nil, errors.Wrap(err, "failed to load encryption cipher")
}
identityConfig.Spec.ClientSecret.EncryptValue(*cipher)
if identityConfig.Spec.Storage.PostgresConfig != nil {
identityConfig.Spec.Storage.PostgresConfig.Password.EncryptValue(*cipher)
}
identityConfig.Spec.DexConnectors.EncryptValue(*cipher)
s := serializer.NewYAMLSerializer(serializer.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var b bytes.Buffer
if err := s.Encode(identityConfig, &b); err != nil {
return nil, errors.Wrap(err, "failed to encode identity config")
}
return b.Bytes(), nil
}