Skip to content

Commit df78aff

Browse files
committed
feat(gateway): disable api key requirement by default
1 parent 2643655 commit df78aff

6 files changed

Lines changed: 18 additions & 5 deletions

File tree

gateway/cmd/serve.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func Serve(cfg *config.Config) error {
7979
connectionService,
8080
promptService,
8181
apikeyService,
82+
cfg.App.Authentication.Enabled,
8283
)
8384

8485
if err := server.Serve(ctx, logger, cfg.App, deps); err != nil {

gateway/internal/api/deps.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Deps struct {
1919
ConnectionService *connection.Service
2020
PromptService *prompt.Service
2121
APIKeyService *apikey.Service
22+
AuthEnabled bool
2223
}
2324

2425
func NewDeps(
@@ -29,6 +30,7 @@ func NewDeps(
2930
cs *connection.Service,
3031
pms *prompt.Service,
3132
aks *apikey.Service,
33+
authEnabled bool,
3234
) *Deps {
3335
return &Deps{
3436
Logger: logger,
@@ -38,5 +40,6 @@ func NewDeps(
3840
ConnectionService: cs,
3941
PromptService: pms,
4042
APIKeyService: aks,
43+
AuthEnabled: authEnabled,
4144
}
4245
}

gateway/internal/api/v1/v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func Register(d *api.Deps) (http.Handler, error) {
5555
stdInterceptors := []connect.Interceptor{
5656
validateInterceptor,
5757
otelconnectInterceptor,
58-
interceptor.NewAPIKeyInterceptor(d.Logger, d.APIKeyService),
58+
interceptor.NewAPIKeyInterceptor(d.Logger, d.APIKeyService, d.AuthEnabled),
5959
interceptor.HeadersInterceptor(),
6060
interceptor.RateLimiterInterceptor(d.RateLimiter),
6161
interceptor.RetryInterceptor(),

gateway/internal/errors/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var (
1111
ErrProviderHeaderNotExit = errors.NewBadRequest(fmt.Sprintf("%s header is required", constants.XMSProvider))
1212
ErrRequiredHeaderNotExit = errors.NewBadRequest(fmt.Sprintf("either %s or %s header is required", constants.XMSProvider, constants.XMSConfig))
1313
ErrRateLimitExceeded = errors.NewForbidden("rate limit exceeded")
14-
ErrUnauthenticated = errors.NewUnauthorized("unauthenticated")
14+
ErrUnauthenticated = errors.NewUnauthorized("you are not authorized to access APIs")
1515
ErrProviderNotFound = errors.NewNotFound("provider is not found")
1616
ErrRouterConfigNotValid = errors.NewNotFound("router config is not valid")
1717
)

gateway/internal/interceptor/auth.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import (
1111
)
1212

1313
// NewAPIKeyInterceptor returns interceptor which is checking if api key exits
14-
func NewAPIKeyInterceptor(logger *slog.Logger, aks *apikey.Service) connect.UnaryInterceptorFunc {
14+
func NewAPIKeyInterceptor(logger *slog.Logger, aks *apikey.Service, authEnabled bool) connect.UnaryInterceptorFunc {
1515
return connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
1616
return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
17+
if !authEnabled {
18+
return next(ctx, req)
19+
}
20+
1721
if authenticationSkipList[req.Spec().Procedure] {
1822
return next(ctx, req)
1923
}

gateway/internal/server/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package server
22

33
type Config struct {
4-
Host string `yaml:"host" json:"host,omitempty" mapstructure:"host" default:"0.0.0.0"`
5-
Port int `yaml:"port" json:"port,omitempty" mapstructure:"port" default:"8080"`
4+
Host string `yaml:"host" json:"host,omitempty" mapstructure:"host" default:"0.0.0.0"`
5+
Port int `yaml:"port" json:"port,omitempty" mapstructure:"port" default:"8080"`
6+
Authentication AuthenticationConfig `yaml:"authentication" mapstructure:"authentication"`
7+
}
8+
9+
type AuthenticationConfig struct {
10+
Enabled bool `yaml:"enabled" json:"enabled,omitempty" mapstructure:"enabled" default:"false"`
611
}

0 commit comments

Comments
 (0)