Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6e63daa
xds: read JWT credentials from file as per A97
dimpavloff Jun 14, 2025
3268ea5
remove example
dimpavloff Jul 6, 2025
b18a1f5
refactor test creation
dimpavloff Jul 6, 2025
eb391af
refactor token string padding
dimpavloff Jul 26, 2025
d43893a
remove example; mark as experimental
dimpavloff Jul 29, 2025
167b86e
reorganise struct attributes
dimpavloff Jul 29, 2025
439d28c
rename methods with Locked suffix
dimpavloff Jul 29, 2025
b36d4b6
remove context param from refreshTokenSync
dimpavloff Jul 29, 2025
26e0451
reformat comments; remove redundant cachedErrorTime field
dimpavloff Jul 29, 2025
da2de8c
add defaultTestTimeout const
dimpavloff Jul 29, 2025
51ce34c
refactor test to use wantErr string only
dimpavloff Jul 29, 2025
f87f1f2
fix punctuation
dimpavloff Jul 29, 2025
15dd057
less prosaic subtest names
dimpavloff Jul 29, 2025
54cbbcb
remove unit test
dimpavloff Jul 30, 2025
9c5035d
rename preemptiveRefresh to forceRefresh
dimpavloff Jul 31, 2025
ec915dc
remove unused context param
dimpavloff Jul 31, 2025
1d95fa2
rename files
dimpavloff Aug 21, 2025
a797ed9
use cond variable
dimpavloff Aug 21, 2025
fd388d1
refactor to no longer need cond
dimpavloff Aug 21, 2025
790a2d9
fix docstring comment
dimpavloff Aug 21, 2025
6713190
cache authorization header instead of token
dimpavloff Aug 21, 2025
3f563eb
remove internal/ and xds/ changes
dimpavloff Aug 21, 2025
a38573b
remove xds/bootstrap
dimpavloff Aug 21, 2025
12fedd5
fix comment docstrings
dimpavloff Aug 21, 2025
52445c7
remove newJWTFileReader
dimpavloff Aug 26, 2025
1678016
make ReadToken private method
dimpavloff Aug 26, 2025
1be843b
use subtests
dimpavloff Aug 26, 2025
8ac3296
use writeTempFile
dimpavloff Aug 26, 2025
b0bdc70
add comment about RPC queue behaviour
dimpavloff Aug 26, 2025
e4f955c
remove needsPreemptiveRefreshLocked method
dimpavloff Aug 26, 2025
f78178c
split NewTokenFileCallCredentials tests
dimpavloff Aug 26, 2025
bbeb759
remove leftover os.MkdirTemp
dimpavloff Aug 26, 2025
bba5d34
remove audience parameter and do not set it at all for test tokens
dimpavloff Aug 26, 2025
607868b
test for grpc codes in TestTokenFileCallCreds_GetRequestMetadata
dimpavloff Aug 26, 2025
bc2d327
use cmp.Diff in TestTokenFileCallCreds_TokenCaching
dimpavloff Aug 26, 2025
330d9a8
fix createTestJWT docstring
dimpavloff Aug 29, 2025
774d83e
refactor readToken() and tests to use error values
dimpavloff Sep 1, 2025
b9dcfcb
remove errJWTFormat in favour of validation error
dimpavloff Sep 3, 2025
6ee5ba7
error wrapping
dimpavloff Sep 3, 2025
14c5ccd
subtests with underscores only
dimpavloff Sep 3, 2025
ca8227d
change credentials.CheckSecurityLevel error mgs; success path identation
dimpavloff Sep 3, 2025
ff50123
re-order assertions
dimpavloff Sep 3, 2025
c05da9f
remove string comparisons
dimpavloff Sep 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions credentials/jwt/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* Copyright 2025 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Package jwt implements JWT token file-based call credentials.
//
// This package provides support for A97 JWT Call Credentials, allowing gRPC
// clients to authenticate using JWT tokens read from files. While originally
// designed for xDS environments, these credentials are general-purpose.
//
// The credentials can be used directly in gRPC clients or configured via xDS.
//
// # Token Requirements
//
// JWT tokens must:
// - Be valid, well-formed JWT tokens with header, payload, and signature
// - Include an "exp" (expiration) claim
// - Be readable from the specified file path
//
// # Considerations
//
// - Tokens are cached until expiration to avoid excessive file I/O
// - Transport security is required (RequireTransportSecurity returns true)
// - Errors in reading tokens or parsing JWTs will result in RPC UNAVAILALBE or
// UNAUTHENTICATED errors. The errors are cached and retried with exponential
// backoff.
//
// This implementation is originally intended for use in service mesh
// environments like Istio where JWT tokens are provisioned and rotated by the
// infrastructure.
//
// # Experimental
//
// Notice: All APIs in this package are experimental and may be removed in a
// later release.
package jwt
102 changes: 102 additions & 0 deletions credentials/jwt/jwt_file_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
*
* Copyright 2025 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package jwt

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
)

var (
errTokenFileAccess = errors.New("token file access error")
errJWTValidation = errors.New("invalid JWT")
)

// jwtClaims represents the JWT claims structure for extracting expiration time.
type jwtClaims struct {
Exp int64 `json:"exp"`
}

// jWTFileReader handles reading and parsing JWT tokens from files.
type jWTFileReader struct {
tokenFilePath string
}

// readToken reads and parses a JWT token from the configured file.
// Returns the token string, expiration time, and any error encountered.
func (r *jWTFileReader) readToken() (string, time.Time, error) {
tokenBytes, err := os.ReadFile(r.tokenFilePath)
if err != nil {
return "", time.Time{}, fmt.Errorf("%v: %w", err, errTokenFileAccess)
}

token := strings.TrimSpace(string(tokenBytes))
if token == "" {
return "", time.Time{}, fmt.Errorf("token file %q is empty: %w", r.tokenFilePath, errJWTValidation)
}

exp, err := r.extractExpiration(token)
if err != nil {
return "", time.Time{}, fmt.Errorf("%q: %w", r.tokenFilePath, err)
}

return token, exp, nil
}

// extractExpiration parses the JWT token to extract the expiration time.
func (r *jWTFileReader) extractExpiration(token string) (time.Time, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return time.Time{}, fmt.Errorf("expected 3 parts, got %d: %w", len(parts), errJWTValidation)
}

payload := parts[1]
// Add padding if necessary for base64 decoding.
if m := len(payload) % 4; m != 0 {
payload += strings.Repeat("=", 4-m)
}

payloadBytes, err := base64.URLEncoding.DecodeString(payload)
if err != nil {
return time.Time{}, fmt.Errorf("decode error: %v: %w", err, errJWTValidation)
}

var claims jwtClaims
if err := json.Unmarshal(payloadBytes, &claims); err != nil {
return time.Time{}, fmt.Errorf("unmarshal error: %v: %w", err, errJWTValidation)
}

if claims.Exp == 0 {
return time.Time{}, fmt.Errorf("no expiration claims: %w", errJWTValidation)
}

expTime := time.Unix(claims.Exp, 0)

// Check if token is already expired.
if expTime.Before(time.Now()) {
return time.Time{}, fmt.Errorf("expired token: %w", errJWTValidation)
}

return expTime, nil
}
178 changes: 178 additions & 0 deletions credentials/jwt/jwt_file_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
*
* Copyright 2025 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package jwt

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"

"google.golang.org/grpc/internal/grpctest"
)

func TestJWTFileReader(t *testing.T) {
grpctest.RunSubTests(t, s{})
}

func (s) TestJWTFileReader_ReadToken_FileErrors(t *testing.T) {
tests := []struct {
name string
create bool
contents string
wantErr error
}{
{
name: "nonexistent_file",
create: false,
contents: "",
wantErr: errTokenFileAccess,
},
{
name: "empty_file",
create: true,
contents: "",
wantErr: errJWTValidation,
},
{
name: "file_with_whitespace_only",
create: true,
contents: " \n\t ",
wantErr: errJWTValidation,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var tokenFile string
if !tt.create {
tokenFile = "/does-not-exist"
} else {
tokenFile = writeTempFile(t, "token", tt.contents)
}

reader := jWTFileReader{tokenFilePath: tokenFile}
if _, _, err := reader.readToken(); err == nil {
t.Fatal("ReadToken() expected error, got nil")
} else if !errors.Is(err, tt.wantErr) {
t.Fatalf("ReadToken() error = %v, want error %v", err, tt.wantErr)
}
})
}
}

func (s) TestJWTFileReader_ReadToken_InvalidJWT(t *testing.T) {
now := time.Now().Truncate(time.Second)
tests := []struct {
name string
tokenContent string
wantErr error
}{
{
name: "valid_token_without_expiration",
tokenContent: createTestJWT(t, time.Time{}),
wantErr: errJWTValidation,
},
{
name: "expired_token",
tokenContent: createTestJWT(t, now.Add(-time.Hour)),
wantErr: errJWTValidation,
},
{
name: "malformed_JWT_not_enough_parts",
tokenContent: "invalid.jwt",
wantErr: errJWTValidation,
},
{
name: "malformed_JWT_invalid_base64",
tokenContent: "header.invalid_base64!@#.signature",
wantErr: errJWTValidation,
},
{
name: "malformed_JWT_invalid_JSON",
tokenContent: createInvalidJSONJWT(t),
wantErr: errJWTValidation,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tokenFile := writeTempFile(t, "token", tt.tokenContent)

reader := jWTFileReader{tokenFilePath: tokenFile}
if _, _, err := reader.readToken(); err == nil {
t.Fatal("ReadToken() expected error, got nil")
} else if !errors.Is(err, tt.wantErr) {
t.Fatalf("ReadToken() error = %v, want error %v", err, tt.wantErr)
}
})
}
}

func (s) TestJWTFileReader_ReadToken_ValidToken(t *testing.T) {
now := time.Now().Truncate(time.Second)
tokenExp := now.Add(time.Hour)
token := createTestJWT(t, tokenExp)
tokenFile := writeTempFile(t, "token", token)

reader := jWTFileReader{tokenFilePath: tokenFile}
readToken, expiry, err := reader.readToken()
if err != nil {
t.Fatalf("ReadToken() unexpected error: %v", err)
}

if readToken != token {
t.Errorf("ReadToken() token = %q, want %q", readToken, token)
}

if !expiry.Equal(tokenExp) {
t.Errorf("ReadToken() expiry = %v, want %v", expiry, tokenExp)
}
}

// createInvalidJSONJWT creates a JWT with invalid JSON in the payload.
func createInvalidJSONJWT(t *testing.T) string {
t.Helper()

header := map[string]any{
"typ": "JWT",
"alg": "HS256",
}

headerBytes, err := json.Marshal(header)
if err != nil {
t.Fatalf("Failed to marshal header: %v", err)
}

headerB64 := base64.URLEncoding.EncodeToString(headerBytes)
headerB64 = strings.TrimRight(headerB64, "=")

// Create invalid JSON payload
invalidJSON := "invalid json content"
payloadB64 := base64.URLEncoding.EncodeToString([]byte(invalidJSON))
payloadB64 = strings.TrimRight(payloadB64, "=")

signature := base64.URLEncoding.EncodeToString([]byte("fake_signature"))
signature = strings.TrimRight(signature, "=")

return fmt.Sprintf("%s.%s.%s", headerB64, payloadB64, signature)
}
Loading
Loading