Skip to content

Commit

Permalink
fix jwt service auth config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
piax93 committed Mar 5, 2025
1 parent 3ca3e7c commit ab4c419
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
8 changes: 8 additions & 0 deletions pkg/configstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,11 @@ func (s *Store) Load(key string, dst interface{}) (bool, error) {
}
return true, mapstructure.Decode(val, dst)
}

// AddHint inserts a new hint to find keys among config files
func (s *Store) AddHint(key string, filename string) {
if s.Hints == nil {
s.Hints = map[string]string{}
}
s.Hints[key] = filename
}
17 changes: 7 additions & 10 deletions pkg/volumes/service_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
authenticatingServicesConfigPath = "/nail/etc/services/authenticating.yaml"
jwtServiceAuthConfigName = "jwt_service_auth"
jwtServiceAuthConfigKey = "service_auth_token_settings"
authenticatingServicesCacheDuration = 10 * time.Minute
defaultTokenExpirationSeconds = 3600
)
Expand All @@ -23,13 +24,9 @@ type authenticatingServicesConfig struct {
}

type jwtServiceAuthTokenSettings struct {
Audience string `json:"audience"`
ContainerPath string `json:"container_path"`
ExpirationSeconds int64 `json:"expiration_seconds"`
}

type jwtServiceAuthConfig struct {
TokenSettings jwtServiceAuthTokenSettings `json:"service_auth_token_settings"`
Audience string `json:"audience" mapstructure:"audience"`
ContainerPath string `json:"container_path" mapstructure:"container_path"`
ExpirationSeconds int64 `json:"expiration_seconds" mapstructure:"expiration_seconds"`
}

var authenticatingServicesCache map[string]bool
Expand Down Expand Up @@ -106,14 +103,14 @@ func GetProjectedServiceAccountVolume(audience string, path string, expirationSe
}

func GetServiceAuthenticationTokenVolume(configStore *configstore.Store) (corev1.VolumeMount, corev1.Volume, error) {
var jwtServiceAuth jwtServiceAuthConfig
if ok, err := configStore.Load(jwtServiceAuthConfigName, &jwtServiceAuth); !ok || err != nil {
var tokenSettings jwtServiceAuthTokenSettings
configStore.AddHint(jwtServiceAuthConfigKey, jwtServiceAuthConfigName)
if ok, err := configStore.Load(jwtServiceAuthConfigKey, &tokenSettings); !ok || err != nil {
if err == nil {
err = fmt.Errorf("%s configuration not found", jwtServiceAuthConfigName)
}
return corev1.VolumeMount{}, corev1.Volume{}, err
}
tokenSettings := jwtServiceAuth.TokenSettings
if tokenSettings.Audience == "" || tokenSettings.ContainerPath == "" {
return corev1.VolumeMount{}, corev1.Volume{}, fmt.Errorf("Missing token settings in %s configuration", jwtServiceAuthConfigName)
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/volumes/service_authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ func TestGetServiceAuthenticationTokenVolume(t *testing.T) {
}
mockJwtAuthConfig := &sync.Map{}
mockJwtAuthConfig.Store(
jwtServiceAuthConfigName,
jwtServiceAuthConfig{
TokenSettings: jwtServiceAuthTokenSettings{
Audience: "foo.yelp.com",
ContainerPath: "/var/secret/serviceaccount/foo",
},
jwtServiceAuthConfigKey,
jwtServiceAuthTokenSettings{
Audience: "foo.yelp.com",
ContainerPath: "/var/secret/serviceaccount/foo",
},
)
configReader := &configstore.Store{Data: mockJwtAuthConfig}
Expand Down

0 comments on commit ab4c419

Please sign in to comment.