Skip to content

Commit

Permalink
Mask raw token in debug logs
Browse files Browse the repository at this point in the history
Configure extended expiration time through flag
  • Loading branch information
dekiel committed Oct 25, 2024
1 parent d23cdfd commit 2e20017
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
23 changes: 12 additions & 11 deletions cmd/oidc-token-verifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type Logger interface {
}

type options struct {
token string
clientID string
outputPath string
publicKeyPath string
newPublicKeysVarName string
trustedWorkflows []string
debug bool
token string
clientID string
outputPath string
publicKeyPath string
newPublicKeysVarName string
trustedWorkflows []string
debug bool
oidcTokenExpirationTime int // OIDC token expiration time in minutes
}

var (
Expand All @@ -53,6 +54,7 @@ func NewRootCmd() *cobra.Command {
rootCmd.PersistentFlags().StringVarP(&opts.clientID, "client-id", "c", "image-builder", "OIDC token client ID, this is used to verify the audience claim in the token. The value should be the same as the audience claim value in the token.")
rootCmd.PersistentFlags().StringVarP(&opts.publicKeyPath, "public-key-path", "p", "", "Path to the cached public keys directory")
rootCmd.PersistentFlags().BoolVarP(&opts.debug, "debug", "d", false, "Enable debug mode")
rootCmd.PersistentFlags().IntVarP(&opts.oidcTokenExpirationTime, "oidc-token-expiration-time", "e", 10, "OIDC token expiration time in minutes")
return rootCmd
}

Expand Down Expand Up @@ -165,7 +167,8 @@ func (opts *options) extractClaims() error {

token, err = verifier.VerifyToken(ctx, opts.token)
if errors.As(err, &tokenExpiredError) {
err = verifier.VerifyExtendedExpiration(err.(tioidc.TokenExpiredError).Expiry, 5)
// Verify the token expiration time using the extended expiration time.
err = verifier.VerifyExtendedExpiration(err.(tioidc.TokenExpiredError).Expiry, opts.oidcTokenExpirationTime)
if err != nil {
return err
}
Expand All @@ -181,10 +184,8 @@ func (opts *options) extractClaims() error {
// claims will store the extracted claim values from the token.
claims := tioidc.NewClaims(logger)
logger.Infow("Verifying token claims")
// Verifies the token and check if the claims have expected values.
// Verifies custom claim values too.
// Verifies if custom claims has expected values.
// Extract the claim values from the token into the claims struct.
// It provides a final result if the token is valid and the claims have expected values.
err = tokenProcessor.ValidateClaims(ctx, &claims)
if err != nil {
return err
Expand Down
15 changes: 11 additions & 4 deletions pkg/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ type TokenVerifier struct {
Logger LoggerInterface
}

func maskToken(token string) string {
if len(token) < 15 {
return "********"
}
return token[:2] + "********" + token[len(token)-2:]
}

// NewVerifierConfig creates a new VerifierConfig.
// It verifies the clientID is not empty.
func NewVerifierConfig(logger LoggerInterface, clientID string, options ...VerifierConfigOption) (VerifierConfig, error) {
Expand Down Expand Up @@ -201,7 +208,7 @@ func NewVerifierConfig(logger LoggerInterface, clientID string, options ...Verif
func (tokenVerifier *TokenVerifier) Verify(ctx context.Context, rawToken string) (Token, error) {
logger := tokenVerifier.Logger
logger.Debugw("Verifying token")
logger.Debugw("Got raw token value", "rawToken", rawToken)
logger.Debugw("Got raw token value", "rawToken", maskToken(rawToken))
idToken, err := tokenVerifier.Verifier.Verify(ctx, rawToken)
if err != nil {
token := Token{}
Expand Down Expand Up @@ -241,7 +248,7 @@ func NewClaims(logger LoggerInterface) Claims {
}
}

// ValidateExpectations validates the claims against the trusted issuer expected values.
// validateExpectations validates the claims against the trusted issuer expected values.
// It checks audience, issuer, and job_workflow_ref claims.
func (claims *Claims) validateExpectations(issuer Issuer) error {
logger := claims.LoggerInterface
Expand Down Expand Up @@ -300,7 +307,7 @@ func NewTokenProcessor(
tokenProcessor.logger = logger

tokenProcessor.rawToken = rawToken
logger.Debugw("Added raw token to token processor", "rawToken", rawToken)
logger.Debugw("Added raw token to token processor", "rawToken", maskToken(rawToken))

tokenProcessor.verifierConfig = config
logger.Debugw("Added Verifier config to token processor",
Expand Down Expand Up @@ -391,7 +398,7 @@ func (tokenProcessor *TokenProcessor) Issuer() string {
return tokenProcessor.issuer.IssuerURL
}

// VerifyAndExtractClaims verify and parse the token to get the token claims.
// ValidateClaims verify and parse the token to get the token claims.
// It uses the provided verifier to verify the token signature and expiration time.
// It verifies if the token claims have expected values.
// It unmarshal the claims into the provided claims struct.
Expand Down

0 comments on commit 2e20017

Please sign in to comment.