-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcfn.go
More file actions
215 lines (194 loc) · 6.54 KB
/
Copy pathcfn.go
File metadata and controls
215 lines (194 loc) · 6.54 KB
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
package cfn
import (
"context"
"errors"
"io/ioutil"
"log"
"os"
"path"
"sync"
"time"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/credentials"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/handler"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/logging"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/metrics"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
const (
invalidRequestError = "InvalidRequest"
serviceInternalError = "ServiceInternal"
unmarshalingError = "UnmarshalingError"
marshalingError = "MarshalingError"
validationError = "Validation"
timeoutError = "Timeout"
sessionNotFoundError = "SessionNotFound"
)
const (
unknownAction = "UNKNOWN"
createAction = "CREATE"
readAction = "READ"
updateAction = "UPDATE"
deleteAction = "DELETE"
listAction = "LIST"
)
var once sync.Once
// Handler is the interface that all resource providers must implement
//
// Each method of Handler maps directly to a CloudFormation action.
// Every action must return a progress event containing details of
// any actions that were undertaken by the resource provider
// or of any error that occurred during operation.
type Handler interface {
Create(request handler.Request) handler.ProgressEvent
Read(request handler.Request) handler.ProgressEvent
Update(request handler.Request) handler.ProgressEvent
Delete(request handler.Request) handler.ProgressEvent
List(request handler.Request) handler.ProgressEvent
}
// Start is the entry point called from a resource's main function
//
// We define two lambda entry points; MakeEventFunc is the entry point to all
// invocations of a custom resource and MakeTestEventFunc is the entry point that
// allows the CLI's contract testing framework to invoke the resource's CRUDL handlers.
func Start(h Handler) {
defer func() {
if r := recover(); r != nil {
log.Printf("Handler panicked: %s", r)
panic(r) // Continue the panic
}
}()
log.Printf("Handler starting")
lambda.Start(makeEventFunc(h))
log.Printf("Handler finished")
}
// Tags are stored as key/value paired strings
type tags map[string]string
// eventFunc is the function signature required to execute an event from the Lambda SDK
type eventFunc func(ctx context.Context, event *event) (response, error)
// handlerFunc is the signature required for all actions
type handlerFunc func(request handler.Request) handler.ProgressEvent
// MakeEventFunc is the entry point to all invocations of a custom resource
func makeEventFunc(h Handler) eventFunc {
return func(ctx context.Context, event *event) (response, error) {
ps := credentials.SessionFromCredentialsProvider(&event.RequestData.ProviderCredentials)
m := metrics.New(cloudwatch.New(ps), event.ResourceType)
once.Do(func() {
l, err := logging.NewCloudWatchLogsProvider(
cloudwatchlogs.New(ps),
m,
event.RequestData.ProviderLogGroupName,
)
if err != nil {
log.Printf("Error: %v, Logging to Stdout", err)
m.PublishExceptionMetric(time.Now(), event.Action, err)
l = os.Stdout
}
// Set default logger to output to CWL in the provider account
logging.SetProviderLogOutput(l)
})
re := newReportErr(m)
if err := scrubFiles("/tmp"); err != nil {
log.Printf("Error: %v", err)
m.PublishExceptionMetric(time.Now(), event.Action, err)
}
handlerFn, err := router(event.Action, h)
log.Printf("Handler received the %s action", event.Action)
if err != nil {
return re.report(event, "router error", err, serviceInternalError)
}
if err := validateEvent(event); err != nil {
return re.report(event, "validation error", err, invalidRequestError)
}
rctx := handler.RequestContext{
StackID: event.StackID,
Region: event.Region,
AccountID: event.AWSAccountID,
StackTags: event.RequestData.StackTags,
SystemTags: event.RequestData.SystemTags,
NextToken: event.NextToken,
}
request := handler.NewRequest(
event.RequestData.LogicalResourceID,
event.CallbackContext,
rctx,
credentials.SessionFromCredentialsProvider(&event.RequestData.CallerCredentials),
event.RequestData.PreviousResourceProperties,
event.RequestData.ResourceProperties,
)
p := invoke(handlerFn, request, m, event.Action)
r, err := newResponse(&p, event.BearerToken)
if err != nil {
log.Printf("Error creating response: %v", err)
return re.report(event, "Response error", err, unmarshalingError)
}
if !isMutatingAction(event.Action) && r.OperationStatus == handler.InProgress {
return re.report(event, "Response error", errors.New("READ and LIST handlers must return synchronous"), invalidRequestError)
}
return r, nil
}
}
func scrubFiles(dir string) error {
names, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, entery := range names {
os.RemoveAll(path.Join([]string{dir, entery.Name()}...))
}
return nil
}
// router decides which handler should be invoked based on the action
// It will return a route or an error depending on the action passed in
func router(a string, h Handler) (handlerFunc, error) {
// Figure out which action was called and have a "catch-all"
switch a {
case createAction:
return h.Create, nil
case readAction:
return h.Read, nil
case updateAction:
return h.Update, nil
case deleteAction:
return h.Delete, nil
case listAction:
return h.List, nil
default:
// No action matched, we should fail and return an InvalidRequestErrorCode
return nil, cfnerr.New(invalidRequestError, "No action/invalid action specified", nil)
}
}
// Invoke handles the invocation of the handerFn.
func invoke(handlerFn handlerFunc, request handler.Request, metricsPublisher *metrics.Publisher, action string) handler.ProgressEvent {
// Create a channel to received a signal that work is done.
ch := make(chan handler.ProgressEvent, 1)
// Ask the goroutine to do some work for us.
go func() {
//start the timer
s := time.Now()
metricsPublisher.PublishInvocationMetric(time.Now(), string(action))
// Report the work is done.
pe := handlerFn(request)
log.Printf("Received event: %s\nMessage: %s\n",
pe.OperationStatus,
pe.Message,
)
e := time.Since(s)
metricsPublisher.PublishDurationMetric(time.Now(), string(action), e.Seconds()*1e3)
ch <- pe
}()
return <-ch
}
func isMutatingAction(action string) bool {
switch action {
case createAction:
return true
case updateAction:
return true
case deleteAction:
return true
}
return false
}