Skip to content

Commit f51a7a8

Browse files
authored
Merge pull request #124 from supabase/chore/tidy_operator_token
Chore/tidy operator token
2 parents 8a49414 + 8bcc505 commit f51a7a8

4 files changed

Lines changed: 29 additions & 46 deletions

File tree

api/api.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,23 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
170170
})
171171
})
172172

173-
if globalConfig.MultiInstanceMode {
174-
// Operator microservice API
175-
r.WithBypass(logger).With(api.verifyOperatorRequest).Get("/", api.GetAppManifest)
176-
r.Route("/instances", func(r *router) {
177-
r.UseBypass(logger)
178-
r.Use(api.verifyOperatorRequest)
179-
180-
r.Post("/", api.CreateInstance)
181-
r.Route("/{instance_id}", func(r *router) {
182-
r.Use(api.loadInstance)
183-
184-
r.Get("/", api.GetInstance)
185-
r.Put("/", api.UpdateInstance)
186-
r.Delete("/", api.DeleteInstance)
187-
})
188-
})
189-
}
173+
// if globalConfig.MultiInstanceMode {
174+
// // Operator microservice API
175+
// r.WithBypass(logger).With(api.verifyOperatorRequest).Get("/", api.GetAppManifest)
176+
// r.Route("/instances", func(r *router) {
177+
// r.UseBypass(logger)
178+
// r.Use(api.verifyOperatorRequest)
179+
180+
// r.Post("/", api.CreateInstance)
181+
// r.Route("/{instance_id}", func(r *router) {
182+
// r.Use(api.loadInstance)
183+
184+
// r.Get("/", api.GetInstance)
185+
// r.Put("/", api.UpdateInstance)
186+
// r.Delete("/", api.DeleteInstance)
187+
// })
188+
// })
189+
// }
190190

191191
corsHandler := cors.New(cors.Options{
192192
AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete},

api/external.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (a *API) ExternalProviderRedirect(w http.ResponseWriter, r *http.Request) e
7272
InviteToken: inviteToken,
7373
Referrer: redirectURL,
7474
})
75-
tokenString, err := token.SignedString([]byte(a.config.OperatorToken))
75+
tokenString, err := token.SignedString([]byte(config.JWT.Secret))
7676
if err != nil {
7777
return internalServerError("Error creating state").WithInternalError(err)
7878
}
@@ -310,10 +310,11 @@ func (a *API) processInvite(ctx context.Context, tx *storage.Connection, userDat
310310
}
311311

312312
func (a *API) loadExternalState(ctx context.Context, state string) (context.Context, error) {
313+
config := a.getConfig(ctx)
313314
claims := ExternalProviderClaims{}
314315
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
315316
_, err := p.ParseWithClaims(state, &claims, func(token *jwt.Token) (interface{}, error) {
316-
return []byte(a.config.OperatorToken), nil
317+
return []byte(config.JWT.Secret), nil
317318
})
318319
if err != nil || claims.Provider == "" {
319320
return nil, badRequestError("OAuth state is invalid: %v", err)

api/middleware.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (a *API) loadJWSSignatureHeader(w http.ResponseWriter, r *http.Request) (co
8282

8383
func (a *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (context.Context, error) {
8484
ctx := r.Context()
85+
config := a.getConfig(ctx)
8586

8687
signature := getSignature(ctx)
8788
if signature == "" {
@@ -91,7 +92,7 @@ func (a *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (contex
9192
claims := NetlifyMicroserviceClaims{}
9293
p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}}
9394
_, err := p.ParseWithClaims(signature, &claims, func(token *jwt.Token) (interface{}, error) {
94-
return []byte(a.config.OperatorToken), nil
95+
return []byte(config.JWT.Secret), nil
9596
})
9697
if err != nil {
9798
return nil, badRequestError("Operator microservice signature is invalid: %v", err)
@@ -115,7 +116,7 @@ func (a *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (contex
115116
return nil, internalServerError("Database error loading instance").WithInternalError(err)
116117
}
117118

118-
config, err := instance.Config()
119+
config, err = instance.Config()
119120
if err != nil {
120121
return nil, internalServerError("Error loading environment config").WithInternalError(err)
121122
}
@@ -150,38 +151,19 @@ func (a *API) limitHandler(lmt *limiter.Limiter) middlewareHandler {
150151
}
151152
}
152153

153-
func (a *API) verifyOperatorRequest(w http.ResponseWriter, req *http.Request) (context.Context, error) {
154-
c, _, err := a.extractOperatorRequest(w, req)
155-
return c, err
156-
}
157-
158-
func (a *API) extractOperatorRequest(w http.ResponseWriter, req *http.Request) (context.Context, string, error) {
159-
token, err := a.extractBearerToken(w, req)
160-
if err != nil {
161-
return nil, token, err
162-
}
163-
if token == "" || token != a.config.OperatorToken {
164-
return nil, token, unauthorizedError("Request does not include an Operator token")
165-
}
166-
return withAdminUser(req.Context(), &models.User{ID: uuid.Nil, Email: "operator@netlify.com"}), token, nil
167-
}
168-
169154
func (a *API) requireAdminCredentials(w http.ResponseWriter, req *http.Request) (context.Context, error) {
170-
c, t, err := a.extractOperatorRequest(w, req)
171-
if err == nil {
172-
return c, nil
173-
}
174-
175-
if t == "" {
155+
ctx := req.Context()
156+
t, err := a.extractBearerToken(w, req)
157+
if err != nil || t == "" {
176158
return nil, err
177159
}
178160

179-
c, err = a.parseJWTClaims(t, req, w)
161+
ctx, err = a.parseJWTClaims(t, req, w)
180162
if err != nil {
181163
return nil, err
182164
}
183165

184-
return a.requireAdmin(c, w, req)
166+
return a.requireAdmin(ctx, w, req)
185167
}
186168

187169
func (a *API) requireEmailProvider(w http.ResponseWriter, req *http.Request) (context.Context, error) {

conf/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type GlobalConfiguration struct {
6363
DB DBConfiguration
6464
External ProviderConfiguration
6565
Logging LoggingConfig `envconfig:"LOG"`
66-
OperatorToken string `split_words:"true" required:"true"`
66+
OperatorToken string `split_words:"true" required:"false"`
6767
MultiInstanceMode bool
6868
Tracing TracingConfig
6969
SMTP SMTPConfiguration

0 commit comments

Comments
 (0)