forked from daidokoro/qaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
224 lines (177 loc) · 5.01 KB
/
functions.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
package commands
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"text/template"
"encoding/base64"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
)
// Common Functions - Both Deploy/Gen
var kmsEncrypt = func(kid string, text string) (string, error) {
sess, err := awsSession()
if err != nil {
Log(err.Error(), level.err)
return "", err
}
svc := kms.New(sess)
params := &kms.EncryptInput{
KeyId: aws.String(kid),
Plaintext: []byte(text),
}
resp, err := svc.Encrypt(params)
if err != nil {
Log(err.Error(), level.err)
return "", err
}
return base64.StdEncoding.EncodeToString(resp.CiphertextBlob), nil
}
var kmsDecrypt = func(cipher string) (string, error) {
sess, err := awsSession()
if err != nil {
Log(err.Error(), level.err)
return "", err
}
svc := kms.New(sess)
ciph, err := base64.StdEncoding.DecodeString(cipher)
if err != nil {
Log(err.Error(), level.err)
return "", err
}
params := &kms.DecryptInput{
CiphertextBlob: []byte(ciph),
}
resp, err := svc.Decrypt(params)
if err != nil {
Log(err.Error(), level.err)
return "", err
}
return string(resp.Plaintext), nil
}
var httpGet = func(url string) (interface{}, error) {
Log(fmt.Sprintln("Calling Template Function [GET] with arguments:", url), level.debug)
resp, err := Get(url)
if err != nil {
Log(err.Error(), level.err)
return "", err
}
return resp, nil
}
var s3Read = func(url string) (string, error) {
Log(fmt.Sprintln("Calling Template Function [S3Read] with arguments:", url), level.debug)
resp, err := S3Read(url)
if err != nil {
Log(err.Error(), level.err)
return "", err
}
return resp, nil
}
var lambdaInvoke = func(name string, payload string) (interface{}, error) {
f := function{name: name}
if payload != "" {
f.payload = []byte(payload)
}
sess, err := awsSession()
if err != nil {
Log(err.Error(), level.err)
return "", err
}
if err := f.Invoke(sess); err != nil {
Log(err.Error(), level.err)
return "", err
}
return f.response, nil
}
// template function maps
var genTimeFunctions = template.FuncMap{
// simple additon function useful for counters in loops
"add": func(a int, b int) int {
Log(fmt.Sprintln("Calling Template Function [Add] with arguments:", a, b), level.debug)
return a + b
},
// strip function for removing characters from text
"strip": func(s string, rmv string) string {
Log(fmt.Sprintln("Calling Template Function [Strip] with arguments:", s, rmv), level.debug)
return strings.Replace(s, rmv, "", -1)
},
// file function for reading text from a given file under the files folder
"file": func(filename string) (string, error) {
Log(fmt.Sprintln("Calling Template Function [File] with arguments:", filename), level.debug)
p := job.tplFiles[0]
f := filepath.Join(filepath.Dir(p), "..", "files", filename)
b, err := ioutil.ReadFile(f)
if err != nil {
Log(err.Error(), level.err)
return "", err
}
return string(b), nil
},
// Get get does an HTTP Get request of the given url and returns the output string
"GET": httpGet,
// S3Read reads content of file from s3 and returns string contents
"s3_read": s3Read,
// invoke - invokes a lambda function
"invoke": lambdaInvoke,
// kms-encrypt - Encrypts PlainText using KMS key
"kms_encrypt": kmsEncrypt,
// kms-decrypt - Descrypts CipherText
"kms_decrypt": kmsDecrypt,
}
var deployTimeFunctions = template.FuncMap{
// Fetching stackoutputs
"stack_output": func(target string) (string, error) {
Log(fmt.Sprintf("Deploy-Time function resolving: %s", target), level.debug)
req := strings.Split(target, "::")
sess, err := awsSession()
if err != nil {
Log(err.Error(), level.err)
return "", nil
}
s := stack{name: req[0]}
s.setStackName()
if err := s.outputs(sess); err != nil {
return "", err
}
for _, i := range s.output.Stacks {
for _, o := range i.Outputs {
if *o.OutputKey == req[1] {
return *o.OutputValue, nil
}
}
}
return "", fmt.Errorf("Stack Output Not found - Stack:%s | Output:%s", req[0], req[1])
},
"stack_output_ext": func(target string) (string, error) {
Log(fmt.Sprintf("Deploy-Time function resolving: %s", target), level.debug)
req := strings.Split(target, "::")
sess, err := awsSession()
if err != nil {
Log(err.Error(), level.err)
return "", nil
}
s := stack{stackname: req[0]}
if err := s.outputs(sess); err != nil {
return "", err
}
for _, i := range s.output.Stacks {
for _, o := range i.Outputs {
if *o.OutputKey == req[1] {
return *o.OutputValue, nil
}
}
}
return "", fmt.Errorf("Stack Output Not found - Stack:%s | Output:%s", req[0], req[1])
},
// Get get does an HTTP Get request of the given url and returns the output string
"GET": httpGet,
// S3Read reads content of file from s3 and returns string contents
"s3_read": s3Read,
// invoke - invokes a lambda function
"invoke": lambdaInvoke,
// kms-encrypt - Encrypts PlainText using KMS key
"kms_encrypt": kmsEncrypt,
// kms-decrypt - Descrypts CipherText
"kms_decrypt": kmsDecrypt,
}