-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
80 lines (69 loc) · 2.3 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
/*
Copyright 2020 Amazon.com, Inc. or its affiliates. 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.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file 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 (
"os"
"github.com/aws-samples/amazon-ecr-repository-compliance-webhook/pkg/function"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-xray-sdk-go/xray"
"github.com/aws/aws-xray-sdk-go/xraylog"
log "github.com/sirupsen/logrus"
)
func init() {
xraylvl, loglvl := logLevels(os.Getenv("LOG_LEVEL"))
log.SetFormatter(new(log.JSONFormatter))
log.Infof("Got log levels [%s, %s]", xraylvl, loglvl)
log.SetLevel(loglvl)
xray.SetLogger(xraylog.NewDefaultLogger(os.Stdout, xraylvl))
}
var (
sess = xray.AWSSession(session.Must(session.NewSession()))
svc = ecr.New(sess, &aws.Config{Region: getRegistryRegion()})
// Handler is the handler for the validating webhook.
Handler = function.NewContainer(svc).Handler().WithLogging().WithProxiedResponse()
// Version is the shortened git hash of the binary's source code.
// It is injected using the -X linker flag when running `make`
Version string
)
func main() {
log.Infof("Starting function version: %s", Version)
lambda.Start(Handler)
}
func logLevels(lvl string) (xraylog.LogLevel, log.Level) {
loglvl, err := log.ParseLevel(lvl)
if err != nil {
return xraylog.LogLevelInfo, log.InfoLevel
}
var xraylvl xraylog.LogLevel
switch lvl {
case "DEBUG":
xraylvl = xraylog.LogLevelDebug
case "INFO":
xraylvl = xraylog.LogLevelInfo
case "WARN":
xraylvl = xraylog.LogLevelWarn
case "ERROR":
xraylvl = xraylog.LogLevelError
default:
xraylvl = xraylog.LogLevelInfo
}
return xraylvl, loglvl
}
func getRegistryRegion() *string {
if value, ok := os.LookupEnv("REGISTRY_REGION"); ok {
return aws.String(value)
}
return aws.String(os.Getenv("AWS_REGION"))
}