Skip to content

Commit 9e74944

Browse files
committed
fix!: add audience validation for jwt tokens
1 parent a6a7a90 commit 9e74944

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

docs/auth.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ JWT allow granting access to a state for a given time (the token lifetime). The
4141
NOTE: `state` value can be set to `*` to allow accessing all project states
4242

4343
### Config
44-
| Environment Variable | Type | Example | Description |
45-
|--------------------------|------|----------------------------------------------|-----------------------------------------------------------------------------------|
46-
| AUTH_JWT_OIDC_ISSUER_URL | bool | `https://vault.example.com/v1/identity/oidc` | Issuer URL which is used to validate token (if not defined, JWT auth is disabled) |
44+
| Environment Variable | Type | Example | Description |
45+
|--------------------------|--------|----------------------------------------------|-----------------------------------------------------------------------------------|
46+
| AUTH_JWT_OIDC_ISSUER_URL | bool | `https://vault.example.com/v1/identity/oidc` | Issuer URL which is used to validate token (if not defined, JWT auth is disabled) |
47+
| AUTH_JWT_OIDC_CLIENT_ID | string | `terraform-backend` (Default) | Client ID (string or URI) used for validating token audience claim |
4748

4849

4950
**Example Terraform backend configuration**

pkg/auth/auth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ func Authenticate(req *http.Request, s *terraform.State) (ok bool, err error) {
3232
authenticator = basic.NewBasicAuth()
3333
case jwt.Name:
3434
issuerURL := viper.GetString("auth_jwt_oidc_issuer_url")
35+
viper.SetDefault("auth_jwt_oidc_client_id", "terraform-backend")
36+
clientID := viper.GetString("auth_jwt_oidc_client_id")
3537
if addr := viper.GetString("vault_addr"); issuerURL != "" && addr != "" {
3638
issuerURL = fmt.Sprintf("%s/v1/identity/oidc", addr)
3739
} else {
3840
return false, fmt.Errorf("jwt auth is not enabled")
3941
}
40-
authenticator = jwt.NewJWTAuth(issuerURL)
42+
authenticator = jwt.NewJWTAuth(issuerURL, clientID)
4143
default:
4244
err = fmt.Errorf("backend is not implemented")
4345
}

pkg/auth/jwt/jwt.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ const Name = "jwt"
1212

1313
type JWTAuth struct {
1414
issuerURL string
15+
clientID string
1516
}
1617

17-
func NewJWTAuth(issuerURL string) *JWTAuth {
18+
func NewJWTAuth(issuerURL string, clientID string) *JWTAuth {
1819
return &JWTAuth{
1920
issuerURL: issuerURL,
21+
clientID: clientID,
2022
}
2123
}
2224

@@ -31,7 +33,7 @@ func (b *JWTAuth) Authenticate(secret string, s *terraform.State) (bool, error)
3133
}
3234

3335
verifier := provider.Verifier(&oidc.Config{
34-
SkipClientIDCheck: true,
36+
ClientID: b.clientID,
3537
})
3638

3739
token, err := verifier.Verify(context.Background(), secret)

0 commit comments

Comments
 (0)