aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/server.go
blob: 82e0f7520d936f3b67b8717c7473751eb3e87a02 (plain)
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
package web

import (
	"context"
	"io/fs"
	"log"
	"os"
	"time"

	"code.crute.us/mcrute/cloud-identity-broker/app"
	"code.crute.us/mcrute/cloud-identity-broker/app/controllers"
	"code.crute.us/mcrute/cloud-identity-broker/app/middleware"
	"code.crute.us/mcrute/cloud-identity-broker/app/models"
	"code.crute.us/mcrute/cloud-identity-broker/auth"
	"code.crute.us/mcrute/cloud-identity-broker/auth/github"

	"code.crute.us/mcrute/golib/cli"
	"code.crute.us/mcrute/golib/clients/autocert/v2"
	"code.crute.us/mcrute/golib/clients/netbox/v3"
	"code.crute.us/mcrute/golib/db/mongodb/v2"
	glecho "code.crute.us/mcrute/golib/echo"
	glmw "code.crute.us/mcrute/golib/echo/middleware"
	"code.crute.us/mcrute/golib/secrets"
	echomw "github.com/labstack/echo/v4/middleware"

	"github.com/labstack/echo/v4"
	"github.com/spf13/cobra"
	"golang.org/x/time/rate"
)

func Register(root *cobra.Command, embeddedTemplates fs.FS, appVersion string) {
	webCmd := &cobra.Command{
		Use:   "web [options]",
		Short: "Run web server",
		Run: func(c *cobra.Command, args []string) {
			cfg := app.Config{}
			cli.MustGetConfig(c, &cfg)
			webMain(cfg, embeddedTemplates, appVersion)
		},
	}
	cli.AddFlags(webCmd, &app.Config{}, app.DefaultConfig, "web")
	root.AddCommand(webCmd)
}

// webMain does the pretty standard golib/Echo setup
func webMain(cfg app.Config, embeddedTemplates fs.FS, version string) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	s, err := glecho.NewEchoWrapper(ctx, cfg.Debug)
	if err != nil {
		log.Fatalf("Error building echo: %s", err)
	}

	vc, err := glecho.MakeVaultSecretsClient(ctx)
	if err != nil {
		log.Fatalf("Error making vault client %s", err)
	}

	if err = s.Configure(glecho.EchoConfig{
		ApplicationName:      "cloud-identity-broker",
		ApplicationVersion:   version,
		BindAddresses:        cfg.Bind,
		DiskTemplates:        os.DirFS("templates/"),
		EmbeddedTemplates:    embeddedTemplates,
		TrustedProxyIPRanges: cfg.TrustedIPRanges,
		CombinedHostLogFile:  cfg.LogFile,
		ContentSecurityPolicy: &glmw.ContentSecurityPolicyConfig{
			DefaultSrc: []glmw.CSPDirective{
				glmw.CSPSelf,
				glmw.CSPUnsafeInline,
			},
			StyleSrc: []glmw.CSPDirective{
				glmw.CSPSelf,
				glmw.CSPData,
				glmw.CSPUnsafeInline,
			},
		},
		Autocert: autocert.MustNewAutocertWrapper(ctx, autocert.AutocertConfig{
			ApiKey:   secrets.MustGetApiKey(vc, ctx, cfg.DNSApiKeyVaultPath).Key,
			Hosts:    cfg.Hostnames,
			Email:    cfg.AutocertEmail,
			CertHost: cfg.AutocertHost,
		}),
		NetboxClient: &netbox.BasicNetboxClient{
			Endpoint: cfg.NetboxHost,
			ApiKey:   secrets.MustGetApiKey(vc, ctx, cfg.NetboxApiKeyVaultPath).Key,
		},
	}); err != nil {
		log.Fatalf("Error building echo: %s", err)
	}
	glecho.AttachSecretsClient(vc, cancel, s.Runner(), s.Logger)

	mongo, err := mongodb.Connect(ctx, cfg.MongoDbUri, vc)
	if err != nil {
		log.Fatalf("Error connecting to mongodb: %s", err)
	}

	setupApplication(ctx, cfg, s, mongo, vc)

	s.RunForever(!cfg.DisableBackgroundJobs)
}

// setupApplication does the setup work that's specific to this
// application
func setupApplication(ctx context.Context, cfg app.Config, s *glecho.EchoWrapper, mongo *mongodb.Mongo, vc secrets.Client) {
	rateLimit := echomw.RateLimiter(
		echomw.NewRateLimiterMemoryStoreWithConfig(
			echomw.RateLimiterMemoryStoreConfig{
				Rate:      rate.Every(cfg.RateLimit),
				Burst:     cfg.RateLimitBurst,
				ExpiresIn: time.Hour,
			},
		),
	)

	// These admin prefixed stores have unsafe-by-default behavior that's
	// necessary for some admin workflows. They should never be used in
	// non-admin workflows.
	adminAccountStore := &models.MongoDbAccountStore{
		Db:            mongo,
		ReturnDeleted: true,
	}
	adminUserStore := &models.MongoDbUserStore{
		Db:            mongo,
		ReturnDeleted: true,
	}

	// Use these for non-admin workflows
	as := &models.MongoDbAccountStore{Db: mongo}
	us := &models.MongoDbUserStore{Db: mongo}

	aws := &controllers.AWSAPI{
		Store:   as,
		Secrets: vc,
	}

	if errs := aws.Preload(ctx); len(errs) > 0 {
		for _, err := range errs {
			log.Printf("Error preloading AWS accounts: %s", err)
		}
		log.Fatalf("Could not preload all AWS accounts")
	}

	ghCred := &app.GitHubOauthCreds{}
	if _, err := vc.Secret(ctx, cfg.GitHubOauthCreds, &ghCred); err != nil {
		log.Fatalf("Error retrieving GitHub credentials: %s", err)
	}

	am := &middleware.AuthenticationMiddleware{
		Store:          us,
		CookieDuration: cfg.AuthCookieDuration,
		GitHub: &github.GitHubAuthenticator{
			ClientId:     ghCred.ClientId,
			ClientSecret: ghCred.ClientSecret,
		},
		JWTManager: &auth.JWTManager{
			Store:        us,
			Audience:     cfg.JWTAudience,
			TokenExpires: cfg.AuthCookieDuration,
		},
	}
	am.RegisterUrls(s)

	api := s.Group("/api")
	api.Use(glmw.VaryCookie())
	api.Use(glmw.CacheNeverMiddleware)
	api.Use(am.Middleware)
	{
		api.GET("", controllers.APIIndexHandler)

		account := api.Group("/account")
		{
			account.GET("", controllers.NewAPIAccountListHandler(as))
			account.GET(
				"/:provider/:account/credentials",
				controllers.NewAPIRegionListHandler(aws),
			)
			account.GET(
				"/:provider/:account/console",
				controllers.NewAPIConsoleRedirectHandler(aws, cfg.IssuerEndpoint),
				rateLimit,
			)
			account.GET(
				"/:provider/:account/credentials/:region",
				controllers.NewAPICredentialsHandler(aws),
				rateLimit,
			)
			(&controllers.APIAccountHandler{
				Store:      as,
				AdminStore: adminAccountStore,
			}).Register("/:provider/:account", "", account)
		}

		user := api.Group("/user")
		user.Use(middleware.RequireAdminPrivileges)
		{
			user.GET("", controllers.NewAPIUserListHandler(us))
			(&controllers.APIUserHandler{
				Store: adminUserStore,
			}).Register("/:user", "", user)
		}
	}

	s.GET("/favicon.ico", echo.NotFoundHandler)
	s.GET("/logout", controllers.LogoutHandler)
	s.CachedStaticRoute("/assets", "assets")
	s.GET("/", controllers.IndexHandler, am.Middleware)
}