-
Notifications
You must be signed in to change notification settings - Fork 62
/
formats.go
258 lines (207 loc) · 7.65 KB
/
formats.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Copyright 2015 Home Office All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
"github.com/golang/glog"
"gopkg.in/yaml.v2"
)
func writeIniFile(filename string, data map[string]interface{}, mode os.FileMode) error {
var buf bytes.Buffer
for key, val := range data {
buf.WriteString(fmt.Sprintf("%s = %v\n", key, val))
}
return writeFile(filename, buf.Bytes(), mode)
}
func writeCSVFile(filename string, data map[string]interface{}, mode os.FileMode) error {
var buf bytes.Buffer
for key, val := range data {
buf.WriteString(fmt.Sprintf("%s,%v\n", key, val))
}
return writeFile(filename, buf.Bytes(), mode)
}
func writeYAMLFile(filename string, data map[string]interface{}, mode os.FileMode) error {
// marshall the content to yaml
content, err := yaml.Marshal(data)
if err != nil {
return err
}
return writeFile(filename, content, mode)
}
func writeEnvFile(filename string, data map[string]interface{}, mode os.FileMode) error {
var buf bytes.Buffer
for key, val := range data {
buf.WriteString(fmt.Sprintf("%s='%v'\n", strings.ToUpper(key), val))
}
return writeFile(filename, buf.Bytes(), mode)
}
func writeCAChain(filename string, data map[string]interface{}, mode os.FileMode) error {
const element = "ca_chain"
const suffix = "ca"
// the chain should be a slice so assert that the type is []interface
chain, ok := data[element].([]interface{})
if !ok {
glog.Errorf("didn't find the certification option: %s", element)
return nil
}
name := fmt.Sprintf("%s.%s.%s", filename, element, suffix)
certChain := ""
for count, cert := range chain {
certChain += fmt.Sprintf("%s", cert)
// append a newline after each cert except last
if count < len(chain)-1 {
certChain += "\n"
}
}
if err := writeFile(name, []byte(fmt.Sprintf("%s", certChain)), mode); err != nil {
return fmt.Errorf("failed to write resource: %s, element: %s, filename: %s, error: %s", filename, suffix, name, err)
}
return nil
}
func writeCertificateFile(filename string, data map[string]interface{}, mode os.FileMode) error {
if err := writeCAChain(filename, data, mode); err != nil {
glog.Errorf("failed to write CA chain: %s", err)
}
files := map[string]string{
"certificate": "crt",
"issuing_ca": "ca",
"private_key": "key",
}
for key, suffix := range files {
name := fmt.Sprintf("%s.%s", filename, suffix)
content, found := data[key]
if !found {
glog.Errorf("didn't find the certification option: %s in the resource: %s", key, name)
continue
}
// step: write the file
if err := writeFile(name, []byte(fmt.Sprintf("%s", content)), mode); err != nil {
glog.Errorf("failed to write resource: %s, element: %s, filename: %s, error: %s", filename, suffix, name, err)
continue
}
}
return nil
}
func writeCertificateBundleFile(filename string, data map[string]interface{}, mode os.FileMode) error {
bundleFile := fmt.Sprintf("%s-bundle.pem", filename)
keyFile := fmt.Sprintf("%s-key.pem", filename)
caFile := fmt.Sprintf("%s-ca.pem", filename)
certFile := fmt.Sprintf("%s.pem", filename)
bundle := fmt.Sprintf("%s\n\n%s\n\n%s", data["certificate"], data["issuing_ca"], data["private_key"])
key := fmt.Sprintf("%s\n", data["private_key"])
ca := fmt.Sprintf("%s\n", data["issuing_ca"])
certificate := fmt.Sprintf("%s\n", data["certificate"])
if err := writeFile(bundleFile, []byte(bundle), mode); err != nil {
glog.Errorf("failed to write the bundled certificate file, error: %s", err)
return err
}
if err := writeFile(certFile, []byte(certificate), mode); err != nil {
glog.Errorf("failed to write the certificate file, errro: %s", err)
return err
}
if err := writeFile(caFile, []byte(ca), mode); err != nil {
glog.Errorf("failed to write the ca file, errro: %s", err)
return err
}
if err := writeFile(keyFile, []byte(key), mode); err != nil {
glog.Errorf("failed to write the key file, errro: %s", err)
return err
}
return nil
}
func writeCredentialFile(filename string, data map[string]interface{}, mode os.FileMode) error {
privateKeyData := fmt.Sprintf("%s", data["private_key_data"])
key, err := base64.StdEncoding.DecodeString(privateKeyData)
if err != nil {
glog.Errorf("failed to decode private key data, error: %s", err)
return err
}
if err := writeFile(filename, key, mode); err != nil {
glog.Errorf("failed to write the bundled certificate file, error: %s", err)
return err
}
return nil
}
func writeAwsCredentialFile(filename string, data map[string]interface{}, mode os.FileMode) error {
if err := writeFile(filename, generateAwsCredentialFile(data), mode); err != nil {
glog.Errorf("failed to write aws credentials file, error: %s", err)
return err
}
return nil
}
func generateAwsCredentialFile(data map[string]interface{}) []byte {
const profileName = "[default]"
accessKey := fmt.Sprintf("aws_access_key_id=%s", data["access_key"])
secretKey := fmt.Sprintf("aws_secret_access_key=%s", data["secret_key"])
// Credentials of type IAM User do not have a security token, and are returned as nil
if data["security_token"] != nil {
sessionToken := fmt.Sprintf("aws_session_token=%s", data["security_token"])
// Support clients that are using boto
securityToken := fmt.Sprintf("aws_security_token=%s", data["security_token"])
return []byte(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n",
profileName, accessKey, secretKey, securityToken, sessionToken))
}
return []byte(fmt.Sprintf("%s\n%s\n%s\n", profileName, accessKey, secretKey))
}
func writeTxtFile(filename string, data map[string]interface{}, mode os.FileMode) error {
keys := getKeys(data)
if len(keys) > 1 {
// step: for plain formats we need to iterate the keys and produce a file per key
for suffix, content := range data {
name := fmt.Sprintf("%s.%s", filename, suffix)
if err := writeFile(name, []byte(fmt.Sprintf("%v", content)), mode); err != nil {
glog.Errorf("failed to write resource: %s, elemment: %s, filename: %s, error: %s",
filename, suffix, name, err)
continue
}
}
return nil
}
// step: we only have the one key, so will write plain
value, _ := data[keys[0]]
content := []byte(fmt.Sprintf("%s", value))
return writeFile(filename, content, mode)
}
func writeJSONFile(filename string, data map[string]interface{}, mode os.FileMode) error {
content, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
return writeFile(filename, content, mode)
}
func writeTemplateFile(filename string, data map[string]interface{}, mode os.FileMode, templateFile string) error {
tpl := template.Must(template.ParseFiles(templateFile))
var templateOutput bytes.Buffer
if err := tpl.Execute(&templateOutput, data); err != nil {
return err
}
content := []byte(fmt.Sprintf("%s", templateOutput.String()))
return writeFile(filename, content, mode)
}
// writeFile writes the file to stdout or an actual file
func writeFile(filename string, content []byte, mode os.FileMode) error {
if options.dryRun {
glog.Infof("dry-run: filename: %s, content:", filename)
fmt.Printf("%s\n", string(content))
return nil
}
glog.V(3).Infof("saving the file: %s", filename)
return ioutil.WriteFile(filename, content, mode)
}