forked from theaaf/cloudformation-media-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
226 lines (203 loc) · 6.15 KB
/
main.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
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"runtime/debug"
"strconv"
"strings"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
)
type CustomResourceRequest struct {
RequestType string `json:"RequestType"`
ResponseURL string `json:"ResponseURL"`
StackId string `json:"StackId"`
RequestId string `json:"RequestId"`
ResourceType string `json:"ResourceType"`
LogicalResourceId string `json:"LogicalResourceId"`
PhysicalResourceId string `json:"PhysicalResourceId"`
ResourceProperties map[string]interface{} `json:"ResourceProperties"`
OldResourceProperties map[string]interface{} `json:"OldResourceProperties"`
}
type CustomResourceResponse struct {
Status string `json:"Status"`
Reason string `json:"Reason"`
PhysicalResourceId string `json:"PhysicalResourceId"`
StackId string `json:"StackId"`
RequestId string `json:"RequestId"`
LogicalResourceId string `json:"LogicalResourceId"`
NoEcho bool `json:"NoEcho,omitempty"`
Data map[string]interface{} `json:"Data,omitempty"`
}
type Success struct {
PhysicalResourceId string `json:"PhysicalResourceId"`
Data map[string]interface{} `json:"Data,omitempty"`
}
func ReshapeProps(in map[string]interface{}, out interface{}) error {
if len(in) > 0 {
filtered := make(map[string]interface{})
for k, v := range in {
if k != "ServiceToken" {
filtered[k] = v
}
}
in = filtered
}
return reshape(in, reflect.ValueOf(out), "")
}
func reshape(in interface{}, out reflect.Value, path string) error {
if out.Kind() == reflect.Ptr {
if out.IsNil() {
out.Set(reflect.New(out.Type().Elem()))
}
return reshape(in, out.Elem(), path)
}
switch in := in.(type) {
case bool:
out.SetBool(in)
case int:
switch out.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
out.SetInt(int64(in))
}
case string:
switch out.Kind() {
case reflect.Bool:
switch in {
case "true":
out.SetBool(true)
case "false":
out.SetBool(false)
default:
return fmt.Errorf("Invalid boolean value: %s", in)
}
case reflect.String:
out.SetString(in)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if n, err := strconv.ParseInt(in, 10, 64); err == nil {
out.SetInt(n)
}
}
case []interface{}:
if out.Kind() == reflect.Slice {
slice := reflect.MakeSlice(out.Type(), len(in), len(in))
for i, v := range in {
if err := reshape(v, slice.Index(i), fmt.Sprintf("%s[%d]", path, i)); err != nil {
return err
}
}
out.Set(slice)
}
case map[string]interface{}:
for k, v := range in {
if field := out.FieldByName(k); field.IsValid() {
if err := reshape(v, field, path+"."+k); err != nil {
return err
}
} else if path == "" {
return fmt.Errorf("Unsupported property: %s", k)
} else {
return fmt.Errorf("Unsupported property: %s in %s", k, path)
}
}
}
return nil
}
var customResourceTypes = map[string]func(*CustomResourceRequest, aws.Config) (*Success, error){}
func RegisterType(name string, f func(*CustomResourceRequest, aws.Config) (*Success, error)) {
customResourceTypes[name] = f
}
func Handler(request *CustomResourceRequest) {
logrus.WithFields(logrus.Fields{
"request_type": request.RequestType,
"response_url": request.ResponseURL,
"stack_id": request.StackId,
"request_id": request.RequestId,
"logical_resource_id": request.LogicalResourceId,
"physical_resource_id": request.PhysicalResourceId,
}).Info("request received")
var success *Success
var err error
defer func() {
resp := &CustomResourceResponse{
StackId: request.StackId,
RequestId: request.RequestId,
LogicalResourceId: request.LogicalResourceId,
PhysicalResourceId: request.PhysicalResourceId,
}
if r := recover(); r != nil {
logrus.Error(r)
logrus.Info(string(debug.Stack()))
resp.Status = "FAILED"
resp.Reason = "Handler panicked. See logs for details."
} else if err != nil {
resp.Status = "FAILED"
resp.Reason = err.Error()
} else {
resp.Status = "SUCCESS"
if success != nil {
resp.PhysicalResourceId = success.PhysicalResourceId
resp.Data = success.Data
}
}
if request.RequestType == "Create" && resp.Status == "FAILED" && resp.PhysicalResourceId == "" {
buf := make([]byte, 16)
rand.Read(buf)
resp.PhysicalResourceId = "failed/" + hex.EncodeToString(buf)
}
logrus.WithFields(logrus.Fields{
"status": resp.Status,
"reason": resp.Reason,
}).Info("sending response")
if b, err := json.Marshal(resp); err != nil {
logrus.Error(err)
} else if req, err := http.NewRequest("PUT", request.ResponseURL, bytes.NewReader(b)); err != nil {
logrus.Error(err)
} else {
req.ContentLength = int64(len(b))
if resp, err := http.DefaultClient.Do(req); err != nil {
logrus.Error(err)
} else {
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
}
}
}()
if strings.HasPrefix(request.PhysicalResourceId, "failed/") {
return
}
cfg, err := external.LoadDefaultAWSConfig()
if err == nil {
if f, ok := customResourceTypes[request.ResourceType]; ok {
success, err = f(request, cfg)
} else {
err = fmt.Errorf("Invalid custom resource type.")
}
}
}
func main() {
if !terminal.IsTerminal(unix.Stdout) {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
flags := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
if err := flags.Parse(os.Args[1:]); err != nil {
if err == flag.ErrHelp {
// Exit with no error if --help was given. This is used to test the build.
os.Exit(0)
}
logrus.Fatal(err)
}
lambda.Start(Handler)
}