-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
181 lines (160 loc) · 5.53 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/quintilesims/d.ims.io/auth"
"github.com/quintilesims/d.ims.io/config"
"github.com/quintilesims/d.ims.io/controllers"
"github.com/quintilesims/d.ims.io/controllers/proxy"
"github.com/quintilesims/d.ims.io/logging"
"github.com/quintilesims/d.ims.io/router"
"github.com/urfave/cli"
"github.com/zpatrick/fireball"
)
const (
SWAGGER_URL = "/api/"
SWAGGER_UI_PATH = "static/swagger-ui/dist"
)
func serveSwaggerUI(w http.ResponseWriter, r *http.Request) {
dir := http.Dir(SWAGGER_UI_PATH)
fileServer := http.FileServer(dir)
http.StripPrefix(SWAGGER_URL, fileServer).ServeHTTP(w, r)
}
func main() {
app := cli.NewApp()
app.Name = "d.ims.io"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "p, port",
Value: config.DEFAULT_PORT,
EnvVar: config.ENVVAR_PORT,
},
cli.BoolFlag{
Name: "d, debug",
EnvVar: config.ENVVAR_DEBUG,
},
cli.StringFlag{
Name: "aws-access-key",
EnvVar: config.ENVVAR_AWS_ACCESS_KEY,
},
cli.StringFlag{
Name: "aws-secret-key",
EnvVar: config.ENVVAR_AWS_SECRET_KEY,
},
cli.StringFlag{
Name: "aws-region",
Value: config.DEFAULT_AWS_REGION,
EnvVar: config.ENVVAR_AWS_REGION,
},
cli.StringFlag{
Name: "tokens-table",
Value: config.DEFAULT_TOKENS_TABLE,
EnvVar: config.ENVVAR_TOKENS_TABLE,
},
cli.StringFlag{
Name: "accounts-table",
Value: config.DEFAULT_ACCOUNTS_TABLE,
EnvVar: config.ENVVAR_ACCOUNTS_TABLE,
},
cli.StringFlag{
Name: "registry-endpoint",
EnvVar: config.ENVVAR_REGISTRY_ENDPOINT,
},
cli.StringFlag{
Name: "auth0-domain",
Value: config.DEFAULT_AUTH0_DOMAIN,
EnvVar: config.ENVVAR_AUTH0_DOMAIN,
},
cli.StringFlag{
Name: "auth0-client-id",
EnvVar: config.ENVVAR_AUTH0_CLIENT_ID,
},
cli.StringFlag{
Name: "auth0-connection",
EnvVar: config.ENVVAR_AUTH0_CONNECTION,
},
}
app.Before = func(c *cli.Context) error {
if err := validateConfig(c); err != nil {
return err
}
log.SetOutput(logging.NewLogWriter(c.Bool("debug")))
return nil
}
app.Action = func(c *cli.Context) error {
session := getAWSSession(c)
dynamodb := dynamodb.New(session)
ecr := ecr.New(session)
tokenManager := auth.NewDynamoTokenManager(c.String("tokens-table"), dynamodb)
accountManager := auth.NewDynamoAccountManager(c.String("accounts-table"), dynamodb)
auth0Authenticator := auth.NewAuth0Authenticator(
c.String("auth0-domain"),
c.String("auth0-client-id"),
c.String("auth0-connection"),
time.Second/2)
authenticator := auth.NewCompositeAuthenticator(tokenManager, auth0Authenticator)
proxy := proxy.NewECRProxy(c.String("registry-endpoint"))
rootController := controllers.NewRootController()
repositoryController := controllers.NewRepositoryController(ecr, accountManager)
accountController := controllers.NewAccountController(ecr, accountManager)
tokenController := controllers.NewTokenController(tokenManager)
proxyController := controllers.NewProxyController(ecr, proxy)
swaggerController := controllers.NewSwaggerController()
routes := rootController.Routes()
routes = append(routes, repositoryController.Routes()...)
routes = append(routes, accountController.Routes()...)
routes = append(routes, tokenController.Routes()...)
routes = append(routes, swaggerController.Routes()...)
routes = fireball.Decorate(routes,
controllers.AuthDecorator(authenticator),
controllers.LogDecorator())
routes = fireball.EnableCORS(routes)
fb := fireball.NewApp(routes)
// decorate proxy handler with auth
doProxy := controllers.AuthDecorator(authenticator)(proxyController.DoProxy)
fb.Router = router.NewRouter(routes, doProxy)
port := fmt.Sprintf(":%s", c.String("port"))
log.Printf("[INFO] Running on port %s\n", port)
http.Handle("/", fb)
http.HandleFunc(SWAGGER_URL, serveSwaggerUI)
return http.ListenAndServe(port, nil)
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func validateConfig(c *cli.Context) error {
vars := map[string]error{
"aws-access-key": fmt.Errorf("AWS Access Key not set! (EnvVar: %s)", config.ENVVAR_AWS_ACCESS_KEY),
"aws-secret-key": fmt.Errorf("AWS Secret Key not set! (EnvVar: %s)", config.ENVVAR_AWS_SECRET_KEY),
"aws-region": fmt.Errorf("AWS Region not set! (EnvVar: %s)", config.ENVVAR_AWS_REGION),
"tokens-table": fmt.Errorf("Tokens Table not set! (EnvVar: %s)", config.ENVVAR_TOKENS_TABLE),
"accounts-table": fmt.Errorf("Accounts Table not set! (EnvVar: %s)", config.ENVVAR_ACCOUNTS_TABLE),
"registry-endpoint": fmt.Errorf("Registry Endpoint not set! (EnvVar: %s)", config.ENVVAR_REGISTRY_ENDPOINT),
"auth0-domain": fmt.Errorf("Auth0 Domain not set! (EnvVar: %s)", config.ENVVAR_AUTH0_DOMAIN),
"auth0-client-id": fmt.Errorf("Auth0 Client ID not set! (EnvVar: %s)", config.ENVVAR_AUTH0_CLIENT_ID),
"auth0-connection": fmt.Errorf("Auth0 Connection not set! (EnvVar: %s)", config.ENVVAR_AUTH0_CONNECTION),
}
for name, err := range vars {
if c.String(name) == "" {
return err
}
}
return nil
}
func getAWSSession(c *cli.Context) *session.Session {
config := defaults.Get().Config
creds := credentials.NewStaticCredentials(c.String("aws-access-key"), c.String("aws-secret-key"), "")
config.WithCredentials(creds)
config.WithRegion(c.String("aws-region"))
return session.New(config)
}