Skip to content
This repository was archived by the owner on Aug 11, 2025. It is now read-only.

Commit 6d0c0e8

Browse files
authored
take multi-token auth config instead of individual variables (#117)
* take multi-token auth config instead of individual variables * simplify conditionorc environment variables
1 parent ce8c4a6 commit 6d0c0e8

File tree

5 files changed

+31
-61
lines changed

5 files changed

+31
-61
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ multistage-image:
9292
--build-arg BUILD_DATE=$(BUILD_DATE) --label org.label-schema.schema-version=1.0 \
9393
--label org.label-schema.vcs-ref=$(GIT_COMMIT_FULL) --label=org.label-schema.vcs-url=$(REPO)
9494

95+
push-ms-devel: multistage-image
96+
docker tag ${DOCKER_IMAGE}:latest localhost:5001/conditionorc:latest
97+
docker push localhost:5001/conditionorc:latest
98+
kind load docker-image localhost:5001/conditionorc:latest
9599

96100
# https://gist.github.com/prwhite/8168133
97101
# COLORS

cmd/server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
"github.com/metal-toolbox/conditionorc/internal/server"
1616
"github.com/metal-toolbox/conditionorc/internal/store"
1717
"github.com/spf13/cobra"
18+
"github.com/spf13/viper"
1819
"go.hollow.sh/toolbox/events"
20+
"go.hollow.sh/toolbox/ginjwt"
1921
)
2022

2123
var shutdownTimeout = 10 * time.Second
@@ -56,7 +58,13 @@ var cmdServer = &cobra.Command{
5658
server.WithStore(repository),
5759
server.WithStreamBroker(streamBroker),
5860
server.WithConditionDefinitions(app.Config.ConditionDefinitions),
59-
server.WithAuthMiddlewareConfig(app.Config.APIServerJWTAuth),
61+
}
62+
63+
if viper.GetViper().GetBool("oidc.enabled") {
64+
app.Logger.Info("enabling OIDC")
65+
options = append(options, server.WithAuthMiddlewareConfig(app.Config.APIServerJWTAuth))
66+
} else {
67+
app.Logger.Info("OIDC disabled")
6068
}
6169

6270
srv := server.New(options...)
@@ -83,4 +91,6 @@ var cmdServer = &cobra.Command{
8391
// install command flags
8492
func init() {
8593
rootCmd.AddCommand(cmdServer)
94+
cmdServer.Flags().Bool("oidc", true, "use oidc auth")
95+
ginjwt.BindFlagFromViperInst(viper.GetViper(), "oidc.enabled", cmdServer.Flags().Lookup("oidc"))
8696
}

internal/app/config.go

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ type Configuration struct {
3939
StoreKind model.StoreKind `mapstructure:"store_kind"`
4040

4141
// APIServerJWTAuth sets the JWT verification configuration for the conditionorc API service.
42-
APIServerJWTAuth *ginjwt.AuthConfig `mapstructure:"ginjwt_auth"`
42+
APIServerJWTAuth []ginjwt.AuthConfig `mapstructure:"ginjwt_auth"`
4343

4444
// ConditionDefinitions holds one or more condition definitions the conditionorc API, orchestrator support.
4545
ConditionDefinitions rctypes.Definitions `mapstructure:"conditions"`
4646

47-
// APIOIDCOptions defines configuration to handle OIDC authn/authz for conditions API clients.
48-
APIOIDCOptions APIOIDCOptions `mapstructure:"api_server_oidc"`
49-
5047
// ServerserviceOptions defines the serverservice client configuration parameters
5148
//
5249
// This parameter is required when StoreKind is set to serverservice.
@@ -66,17 +63,6 @@ type Configuration struct {
6663
Notifications notify.Configuration `mapstructure:"notifications"`
6764
}
6865

69-
// APIOIDCOptions defines configuration to handle OIDC authn/authz for conditions API clients.
70-
type APIOIDCOptions struct {
71-
EnabledOAuth bool `mapstructure:"enable_oauth"`
72-
IssuerEndpoint string `mapstructure:"issuer_endpoint"`
73-
AudienceEndpoint string `mapstructure:"audience_endpoint"`
74-
JWKSURI string `mapstructure:"jwksuri"`
75-
RolesClaim string `mapstructure:"roles_claim"`
76-
UsernameClaim string `mapstructure:"username_claim"`
77-
}
78-
79-
// ServerserviceOptions defines configuration for the Serverservice client.
8066
// https://github.com/metal-toolbox/hollow-serverservice
8167
type ServerserviceOptions struct {
8268
EndpointURL *url.URL
@@ -173,41 +159,16 @@ var (
173159
)
174160

175161
func (a *App) apiServerJWTAuthParams() error {
176-
if !a.v.GetBool("api.oidc.enabled") {
162+
if !a.v.GetBool("oidc.enabled") {
177163
return nil
178164
}
179165

180-
errOIDCAuthParams := errors.New("conditions API OIDC Auth params not defined")
181-
182-
required := []string{
183-
"audience.endpoint",
184-
"issuer.endpoint",
185-
"jwksuri",
186-
"claims.roles",
187-
"claims.username",
188-
}
189-
190-
var unset []string
191-
192-
for _, k := range required {
193-
if a.v.GetString("api.oidc."+k) == "" {
194-
unset = append(unset, "api.oidc."+k)
195-
}
196-
}
197-
198-
if len(unset) > 0 {
199-
return errors.Wrap(errOIDCAuthParams, strings.Join(unset, ","))
200-
}
201-
202-
a.Config.APIServerJWTAuth = &ginjwt.AuthConfig{
203-
Enabled: true,
204-
Audience: a.v.GetString("api.oidc.audience.endpoint"),
205-
Issuer: a.v.GetString("api.oidc.issuer.endpoint"),
206-
JWKSURI: a.v.GetString("api.oidc.jwksuri"),
207-
LogFields: a.v.GetStringSlice("api.oidc.log"),
208-
RolesClaim: a.v.GetString("api.oidc.claims.roles"),
209-
UsernameClaim: a.v.GetString("api.oidc.claims.username"),
166+
cfgs, err := ginjwt.GetAuthConfigsFromFlags(a.v)
167+
if err != nil {
168+
return err
210169
}
170+
a.Logger.WithField("config.length", len(cfgs)).Debug("oidc configurations found")
171+
a.Config.APIServerJWTAuth = cfgs
211172

212173
return nil
213174
}

internal/server/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
// Server type holds attributes of the condition orc server
3232
type Server struct {
3333
// Logger is the app logger
34-
authMWConfig *ginjwt.AuthConfig
34+
authMWConfigs []ginjwt.AuthConfig
3535
logger *logrus.Logger
3636
streamBroker events.Stream
3737
listenAddress string
@@ -78,9 +78,9 @@ func WithConditionDefinitions(defs rctypes.Definitions) Option {
7878
}
7979

8080
// WithAuthMiddlewareConfig sets the auth middleware configuration.
81-
func WithAuthMiddlewareConfig(authMWConfig *ginjwt.AuthConfig) Option {
81+
func WithAuthMiddlewareConfig(authMWConfigs []ginjwt.AuthConfig) Option {
8282
return func(s *Server) {
83-
s.authMWConfig = authMWConfig
83+
s.authMWConfigs = authMWConfigs
8484
}
8585
}
8686

@@ -104,8 +104,8 @@ func New(opts ...Option) *http.Server {
104104
}
105105

106106
// add auth middleware
107-
if s.authMWConfig != nil && s.authMWConfig.Enabled {
108-
authMW, err := ginjwt.NewAuthMiddleware(*s.authMWConfig)
107+
if s.authMWConfigs != nil {
108+
authMW, err := ginjwt.NewMultiTokenMiddlewareFromConfigs(s.authMWConfigs...)
109109
if err != nil {
110110
s.logger.Fatal("failed to initialize auth middleware: ", "error", err)
111111
}

pkg/api/v1/routes/routes.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/pkg/errors"
1212
"github.com/sirupsen/logrus"
1313
"go.hollow.sh/toolbox/events"
14-
"go.hollow.sh/toolbox/ginjwt"
14+
"go.hollow.sh/toolbox/ginauth"
1515

1616
v1types "github.com/metal-toolbox/conditionorc/pkg/api/v1/types"
1717
rctypes "github.com/metal-toolbox/rivets/condition"
@@ -28,7 +28,7 @@ var ginNoOp = func(_ *gin.Context) {
2828

2929
// Routes type sets up the conditionorc API router routes.
3030
type Routes struct {
31-
authMW *ginjwt.Middleware
31+
authMW *ginauth.MultiTokenMiddleware
3232
repository store.Repository
3333
streamBroker events.Stream
3434
conditionDefinitions rctypes.Definitions
@@ -60,7 +60,7 @@ func WithLogger(logger *logrus.Logger) Option {
6060
}
6161

6262
// WithAuthMiddleware sets the auth middleware on the routes type.
63-
func WithAuthMiddleware(authMW *ginjwt.Middleware) Option {
63+
func WithAuthMiddleware(authMW *ginauth.MultiTokenMiddleware) Option {
6464
return func(r *Routes) {
6565
r.authMW = authMW
6666
}
@@ -115,15 +115,10 @@ func (r *Routes) composeAuthHandler(scopes []string) gin.HandlerFunc {
115115
if r.authMW == nil {
116116
return ginNoOp
117117
}
118-
return r.authMW.RequiredScopes(scopes)
118+
return r.authMW.AuthRequired(scopes)
119119
}
120120

121121
func (r *Routes) Routes(g *gin.RouterGroup) {
122-
// JWT token verification.
123-
if r.authMW != nil {
124-
g.Use(r.authMW.AuthRequired())
125-
}
126-
127122
servers := g.Group("/servers/:uuid")
128123
{
129124
// /servers/:uuid/state/:conditionState

0 commit comments

Comments
 (0)