Skip to content

Commit

Permalink
Align with the code structure and test update
Browse files Browse the repository at this point in the history
revert to Token
  • Loading branch information
akiioto committed Oct 25, 2024
1 parent 2e20017 commit ccdef29
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
23 changes: 13 additions & 10 deletions cmd/oidc-token-verifier/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/coreos/go-oidc/v3/oidc"
"github.com/kyma-project/test-infra/pkg/logging"
tioidc "github.com/kyma-project/test-infra/pkg/oidc"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -107,8 +109,8 @@ func (opts *options) extractClaims() error {
var (
zapLogger *zap.Logger
err error
tokenExpiredError *tioidc.TokenExpiredError
token Token
tokenExpiredError *oidc.TokenExpiredError
token tioidc.TokenInterface
)
if opts.debug {
zapLogger, err = zap.NewDevelopment()
Expand Down Expand Up @@ -165,28 +167,29 @@ func (opts *options) extractClaims() error {
verifier := provider.NewVerifier(logger, verifyConfig)
logger.Infow("New verifier created")

token, err = verifier.VerifyToken(ctx, opts.token)
// Verify the token
token, err = verifier.Verify(ctx, opts.token)

Check failure on line 171 in cmd/oidc-token-verifier/main.go

View workflow job for this annotation

GitHub Actions / unittest

cannot use verifier.Verify(ctx, opts.token) (value of type "github.com/kyma-project/test-infra/pkg/oidc".Token) as "github.com/kyma-project/test-infra/pkg/oidc".TokenInterface value in assignment: "github.com/kyma-project/test-infra/pkg/oidc".Token does not implement "github.com/kyma-project/test-infra/pkg/oidc".TokenInterface (method Claims has pointer receiver)
if errors.As(err, &tokenExpiredError) {
// Verify the token expiration time using the extended expiration time.
err = verifier.VerifyExtendedExpiration(err.(tioidc.TokenExpiredError).Expiry, opts.oidcTokenExpirationTime)
err = verifier.VerifyExtendedExpiration(err.(*oidc.TokenExpiredError).Expiry, opts.oidcTokenExpirationTime)
if err != nil {
return err
}
verifyConfig.SkipExpiryCheck = false
verifierWithoutExpiration := provider.NewVerifier(logger, verifyConfig)
token, err = verifierWithoutExpiration.VerifyToken(ctx, opts.token)
token, err = verifierWithoutExpiration.Verify(ctx, opts.token)

Check failure on line 179 in cmd/oidc-token-verifier/main.go

View workflow job for this annotation

GitHub Actions / unittest

cannot use verifierWithoutExpiration.Verify(ctx, opts.token) (value of type "github.com/kyma-project/test-infra/pkg/oidc".Token) as "github.com/kyma-project/test-infra/pkg/oidc".TokenInterface value in assignment: "github.com/kyma-project/test-infra/pkg/oidc".Token does not implement "github.com/kyma-project/test-infra/pkg/oidc".TokenInterface (method Claims has pointer receiver)
}
if err != nil {
return err
}
logger.Infow("Token verified successfully")

// claims will store the extracted claim values from the token.
// Create claims
claims := tioidc.NewClaims(logger)
logger.Infow("Verifying token claims")
// Verifies if custom claims has expected values.
// Extract the claim values from the token into the claims struct.
err = tokenProcessor.ValidateClaims(ctx, &claims)

// Pass the token to ValidateClaims
err = tokenProcessor.ValidateClaims(token, &claims)

Check failure on line 191 in cmd/oidc-token-verifier/main.go

View workflow job for this annotation

GitHub Actions / unittest

cannot use token (variable of type "github.com/kyma-project/test-infra/pkg/oidc".TokenInterface) as "github.com/kyma-project/test-infra/pkg/oidc".ClaimsInterface value in argument to tokenProcessor.ValidateClaims: "github.com/kyma-project/test-infra/pkg/oidc".TokenInterface does not implement "github.com/kyma-project/test-infra/pkg/oidc".ClaimsInterface (missing method validateExpectations)

Check failure on line 191 in cmd/oidc-token-verifier/main.go

View workflow job for this annotation

GitHub Actions / unittest

cannot use &claims (value of type *"github.com/kyma-project/test-infra/pkg/oidc".Claims) as "github.com/kyma-project/test-infra/pkg/oidc".Token value in argument to tokenProcessor.ValidateClaims

if err != nil {
return err
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (tokenVerifier *TokenVerifier) VerifyExtendedExpiration(expirationTimestamp
logger.Debugw("Verifying token expiration time", "expirationTimestamp", expirationTimestamp, "gracePeriodMinutes", gracePeriodMinutes)
now := time.Now()
elapsed := now.Sub(expirationTimestamp)
gracePeriod := *time.Minute
gracePeriod := time.Minute
if elapsed <= gracePeriod {
return nil
}
Expand Down Expand Up @@ -402,15 +402,21 @@ func (tokenProcessor *TokenProcessor) Issuer() string {
// 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.
func (tokenProcessor *TokenProcessor) ValidateClaims(claims ClaimsInterface) error {
func (tokenProcessor *TokenProcessor) ValidateClaims(claims ClaimsInterface, token Token) error {
logger := tokenProcessor.logger

// Ensure that the token is initialized
if token.Token == nil {
return fmt.Errorf("failed to verify token: token validation failed")
}

logger.Debugw("Getting claims from token")
err = token.Claims(claims)
err := token.Claims(claims)
if err != nil {
return fmt.Errorf("failed to get claims from token: %w", err)
}
logger.Debugw("Got claims from token", "claims", fmt.Sprintf("%+v", claims))

err = claims.validateExpectations(tokenProcessor.issuer)
if err != nil {
return fmt.Errorf("failed to validate claims: %w", err)
Expand Down
23 changes: 9 additions & 14 deletions pkg/oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package oidc_test
import (
"errors"
"fmt"

// "time"

// "fmt"
"os"

// "time"

"github.com/coreos/go-oidc/v3/oidc"
"github.com/go-jose/go-jose/v4/jwt"
tioidc "github.com/kyma-project/test-infra/pkg/oidc"
Expand All @@ -22,8 +21,8 @@ import (

var _ = Describe("OIDC", func() {
var (
err error
ctx context.Context
err error

logger *zap.SugaredLogger
trustedIssuers map[string]tioidc.Issuer
rawToken []byte
Expand Down Expand Up @@ -90,7 +89,6 @@ var _ = Describe("OIDC", func() {
JWKSURL: "https://fakedings.dev-gcp.nais.io/fake/jwks",
},
}
ctx = context.Background()
})
When("issuer is trusted", func() {
It("should return a new TokenProcessor", func() {
Expand Down Expand Up @@ -176,8 +174,6 @@ var _ = Describe("OIDC", func() {
Expect(err).NotTo(HaveOccurred())
Expect(tokenProcessor).NotTo(BeNil())

ctx = context.Background()

trustedIssuers = map[string]tioidc.Issuer{
"https://fakedings.dev-gcp.nais.io/fake": {
Name: "github",
Expand Down Expand Up @@ -215,7 +211,7 @@ var _ = Describe("OIDC", func() {
verifier.On("Verify", mock.AnythingOfType("backgroundCtx"), string(rawToken)).Return(token, nil)

// Run
err = tokenProcessor.VerifyAndExtractClaims(ctx, verifier, &claims)
err = tokenProcessor.ValidateClaims(&claims, token)

// Verify
Expect(err).NotTo(HaveOccurred())
Expand All @@ -239,17 +235,17 @@ var _ = Describe("OIDC", func() {
verifier.On("Verify", mock.AnythingOfType("backgroundCtx"), string(rawToken)).Return(token, nil)

// Run
err = tokenProcessor.VerifyAndExtractClaims(ctx, verifier, &claims)
err = tokenProcessor.ValidateClaims(&claims, token)

// Verify
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError("failed to validate claims: job_workflow_ref claim expected value validation failed, expected: kyma-project/test-infra/.github/workflows/unexpected.yml@refs/heads/main, provided: kyma-project/test-infra/.github/workflows/verify-oidc-token.yml@refs/heads/main"))
})
It("should return an error when token was not verified", func() {
verifier.On("Verify", mock.AnythingOfType("backgroundCtx"), string(rawToken)).Return(token, fmt.Errorf("token validation failed"))
verifier.On("Verify", mock.AnythingOfType("backgroundCtx"), string(rawToken)).Return(tioidc.Token{}, fmt.Errorf("token validation failed"))

// Run
err = tokenProcessor.VerifyAndExtractClaims(ctx, verifier, &claims)
err = tokenProcessor.ValidateClaims(&claims, tioidc.Token{})

// Verify
Expect(err).To(HaveOccurred())
Expand All @@ -263,7 +259,7 @@ var _ = Describe("OIDC", func() {
Token.On("Claims", &claims).Return(fmt.Errorf("claims are not set"))

// Run
err = tokenProcessor.VerifyAndExtractClaims(ctx, verifier, &claims)
err = tokenProcessor.ValidateClaims(&claims, token)

// Verify
Expect(err).To(HaveOccurred())
Expand Down Expand Up @@ -311,7 +307,6 @@ var _ = Describe("OIDC", func() {
provider = tioidc.Provider{
VerifierProvider: oidcProvider,
}
ctx = context.Background()
verifierConfig, err = tioidc.NewVerifierConfig(logger, clientID)
Expect(err).NotTo(HaveOccurred())
})
Expand Down

0 comments on commit ccdef29

Please sign in to comment.