Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CreateFakeJwks #47621

Merged
merged 8 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/kr/pretty v0.3.1
github.com/kylelemons/godebug v1.1.0
github.com/lestrrat-go/jwx v1.2.26
github.com/lestrrat-go/jwx/v2 v2.0.15
github.com/mattn/go-isatty v0.0.20
github.com/miekg/dns v1.1.56
github.com/mitchellh/copystructure v1.2.0
Expand Down Expand Up @@ -184,8 +185,9 @@ require (
github.com/klauspost/compress v1.17.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.4 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
Expand Down Expand Up @@ -217,6 +219,7 @@ require (
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.10.0 // indirect
Expand Down
10 changes: 9 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,19 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A=
github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y=
github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8=
github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
github.com/lestrrat-go/jwx v1.2.26 h1:4iFo8FPRZGDYe1t19mQP0zTRqA7n8HnJ5lkIiDvJcB0=
github.com/lestrrat-go/jwx v1.2.26/go.mod h1:MaiCdGbn3/cckbOFSCluJlJMmp9dmZm5hDuIkx8ftpQ=
github.com/lestrrat-go/jwx/v2 v2.0.15 h1:XvR2lQdX+mZechmqWxqQb2foU3hgAn5+Rj0ICa0I6sU=
github.com/lestrrat-go/jwx/v2 v2.0.15/go.mod h1:jBHyESp4e7QxfERM0UKkQ80/94paqNIEcdEfiUYz5zE=
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
Expand Down Expand Up @@ -733,6 +738,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down Expand Up @@ -1101,6 +1108,7 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
2 changes: 1 addition & 1 deletion istio.deps
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "PROXY_REPO_SHA",
"repoName": "proxy",
"file": "",
"lastStableSHA": "25b4a3908a0366e6779d7b1cd36279b70cc8e6aa"
"lastStableSHA": "7770bba582a02c45885ee8697c7b4453c3fd0f51"
},
{
"_comment": "",
Expand Down
21 changes: 21 additions & 0 deletions licenses/github.com/lestrrat-go/httprc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 lestrrat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions licenses/github.com/lestrrat-go/jwx/v2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 lestrrat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

35 changes: 23 additions & 12 deletions pilot/pkg/model/jwks_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package model

import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand All @@ -35,6 +36,7 @@ import (

core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_jwt "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/jwt_authn/v3"
"github.com/lestrrat-go/jwx/v2/jwk"

"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pkg/monitoring"
Expand Down Expand Up @@ -115,6 +117,17 @@ type jwtKey struct {
issuer string
}

type CreateFakeJwksFunc func() string

// CreateFakeJwks is a helper function to make a fake jwks when istiod failed to fetch it.
func CreateFakeJwks() string {
zirain marked this conversation as resolved.
Show resolved Hide resolved
raw, _ := rsa.GenerateKey(rand.Reader, 2048)
key, _ := jwk.FromRaw(raw)
rsaKey, _ := key.(jwk.RSAPrivateKey)
res, _ := json.Marshal(rsaKey)
return fmt.Sprintf(`{"keys":[ %s]}`, string(res))
}

// JwksResolver is resolver for jwksURI and jwt public key.
type JwksResolver struct {
// Callback function to invoke when detecting jwt public key change.
Expand Down Expand Up @@ -150,16 +163,21 @@ type JwksResolver struct {

// Whenever istiod fails to fetch the pubkey from jwksuri in main flow this variable becomes true for background trigger
jwksUribackgroundChannel bool

createFakeJwksFunc CreateFakeJwksFunc
}

// NewJwksResolver creates new instance of JwksResolver.
func NewJwksResolver(evictionDuration, refreshDefaultInterval, refreshIntervalOnFailure, retryInterval time.Duration) *JwksResolver {
func NewJwksResolver(evictionDuration, refreshDefaultInterval, refreshIntervalOnFailure, retryInterval time.Duration,
createFakeJwksFunc CreateFakeJwksFunc,
) *JwksResolver {
return newJwksResolverWithCABundlePaths(
evictionDuration,
refreshDefaultInterval,
refreshIntervalOnFailure,
retryInterval,
[]string{jwksExtraRootCABundlePath},
createFakeJwksFunc,
)
}

Expand All @@ -169,6 +187,7 @@ func newJwksResolverWithCABundlePaths(
refreshIntervalOnFailure,
retryInterval time.Duration,
caBundlePaths []string,
createFakeJwksFunc CreateFakeJwksFunc,
) *JwksResolver {
ret := &JwksResolver{
evictionDuration: evictionDuration,
Expand All @@ -183,6 +202,7 @@ func newJwksResolverWithCABundlePaths(
DisableKeepAlives: true,
},
},
createFakeJwksFunc: createFakeJwksFunc,
}

caCertPool, err := x509.SystemCertPool()
Expand Down Expand Up @@ -283,7 +303,7 @@ func (r *JwksResolver) BuildLocalJwks(jwksURI, jwtIssuer, jwtPubKey string) *env
log.Infof("The JWKS key is not yet fetched for issuer %s (%s), using a fake JWKS for now", jwtIssuer, jwksURI)
// This is a temporary workaround to reject a request with JWT token by using a fake jwks when istiod failed to fetch it.
// TODO(xulingqing): Find a better way to reject the request without using the fake jwks.
jwtPubKey = CreateFakeJwks(jwksURI)
jwtPubKey = r.createFakeJwksFunc()
}
}
return &envoy_jwt.JwtProvider_LocalJwks{
Expand All @@ -295,15 +315,6 @@ func (r *JwksResolver) BuildLocalJwks(jwksURI, jwtIssuer, jwtPubKey string) *env
}
}

// CreateFakeJwks is a helper function to make a fake jwks when istiod failed to fetch it.
func CreateFakeJwks(jwksURI string) string {
// Create a fake jwksURI
fakeJwksURI := "Error-IstiodFailedToFetchJwksUri-" + jwksURI
// Encode jwksURI with base64 to make dynamic n in jwks
encodedString := base64.RawURLEncoding.EncodeToString([]byte(fakeJwksURI))
return fmt.Sprintf(`{"keys":[ {"e":"AQAB","kid":"abc","kty":"RSA","n":"%s"}]}`, encodedString)
}

// Resolve jwks_uri through openID discovery.
func (r *JwksResolver) resolveJwksURIUsingOpenID(issuer string) (string, error) {
// Try to get jwks_uri through OpenID Discovery.
Expand Down
28 changes: 22 additions & 6 deletions pilot/pkg/model/jwks_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package model

import (
"encoding/base64"
"fmt"
"sync/atomic"
"testing"
Expand All @@ -27,8 +28,16 @@ import (

const testRetryInterval = time.Millisecond * 10

func testCreateFakeJwks() string {
// Create a fake jwksURI
fakeJwksURI := "Error-IstiodFailedToFetchJwksUri"
// Encode jwksURI with base64 to make dynamic n in jwks
encodedString := base64.RawURLEncoding.EncodeToString([]byte(fakeJwksURI))
return fmt.Sprintf(`{"keys":[ {"e":"AQAB","kid":"abc","kty":"RSA","n":"%s"}]}`, encodedString)
}

func TestResolveJwksURIUsingOpenID(t *testing.T) {
r := NewJwksResolver(JwtPubKeyEvictionDuration, JwtPubKeyRefreshInterval, JwtPubKeyRefreshIntervalOnFailure, testRetryInterval)
r := NewJwksResolver(JwtPubKeyEvictionDuration, JwtPubKeyRefreshInterval, JwtPubKeyRefreshIntervalOnFailure, testRetryInterval, testCreateFakeJwks)
defer r.Close()

ms, err := test.StartNewServer()
Expand Down Expand Up @@ -80,7 +89,7 @@ func TestResolveJwksURIUsingOpenID(t *testing.T) {
}

func TestGetPublicKey(t *testing.T) {
r := NewJwksResolver(JwtPubKeyEvictionDuration, JwtPubKeyRefreshInterval, JwtPubKeyRefreshIntervalOnFailure, testRetryInterval)
r := NewJwksResolver(JwtPubKeyEvictionDuration, JwtPubKeyRefreshInterval, JwtPubKeyRefreshIntervalOnFailure, testRetryInterval, testCreateFakeJwks)
defer r.Close()

ms, err := test.StartNewServer()
Expand Down Expand Up @@ -121,7 +130,7 @@ func TestGetPublicKey(t *testing.T) {
}

func TestGetPublicKeyReorderedKey(t *testing.T) {
r := NewJwksResolver(JwtPubKeyEvictionDuration, testRetryInterval*20, testRetryInterval*10, testRetryInterval)
r := NewJwksResolver(JwtPubKeyEvictionDuration, testRetryInterval*20, testRetryInterval*10, testRetryInterval, testCreateFakeJwks)
defer r.Close()

ms, err := test.StartNewServer()
Expand Down Expand Up @@ -170,6 +179,7 @@ func TestGetPublicKeyUsingTLS(t *testing.T) {
JwtPubKeyRefreshIntervalOnFailure,
testRetryInterval,
[]string{"./test/testcert/cert.pem"},
testCreateFakeJwks,
)
defer r.Close()

Expand All @@ -196,6 +206,7 @@ func TestGetPublicKeyUsingTLSBadCert(t *testing.T) {
testRetryInterval,
testRetryInterval,
[]string{"./test/testcert/cert2.pem"},
testCreateFakeJwks,
)
defer r.Close()

Expand All @@ -219,6 +230,7 @@ func TestGetPublicKeyUsingTLSWithoutCABundles(t *testing.T) {
testRetryInterval,
testRetryInterval,
[]string{},
testCreateFakeJwks,
)
defer r.Close()

Expand All @@ -241,6 +253,7 @@ func TestJwtPubKeyEvictionForNotUsed(t *testing.T) {
2*time.Millisecond, /*RefreshInterval*/
2*time.Millisecond, /*RefreshIntervalOnFailure*/
testRetryInterval,
testCreateFakeJwks,
)
defer r.Close()

Expand Down Expand Up @@ -269,6 +282,7 @@ func TestJwtPubKeyEvictionForNotRefreshed(t *testing.T) {
10*time.Millisecond, /*RefreshInterval*/
10*time.Millisecond, /*RefreshIntervalOnFailure*/
testRetryInterval, /*RetryInterval*/
testCreateFakeJwks,
)
defer r.Close()

Expand Down Expand Up @@ -323,6 +337,7 @@ func TestJwtPubKeyLastRefreshedTime(t *testing.T) {
2*time.Millisecond, /*RefreshInterval*/
2*time.Millisecond, /*RefreshIntervalOnFailure*/
testRetryInterval, /*RetryInterval*/
testCreateFakeJwks,
)
defer r.Close()

Expand All @@ -343,6 +358,7 @@ func TestJwtPubKeyRefreshWithNetworkError(t *testing.T) {
time.Second, /*RefreshInterval*/
time.Second, /*RefreshIntervalOnFailure*/
testRetryInterval,
testCreateFakeJwks,
)
defer r.Close()

Expand All @@ -362,7 +378,7 @@ func TestJwtPubKeyRefreshWithNetworkError(t *testing.T) {
func TestJwtRefreshIntervalRecoverFromInitialFailOnFirstHit(t *testing.T) {
defaultRefreshInterval := 50 * time.Millisecond
refreshIntervalOnFail := 2 * time.Millisecond
r := NewJwksResolver(JwtPubKeyEvictionDuration, defaultRefreshInterval, refreshIntervalOnFail, 1*time.Millisecond)
r := NewJwksResolver(JwtPubKeyEvictionDuration, defaultRefreshInterval, refreshIntervalOnFail, 1*time.Millisecond, testCreateFakeJwks)

ms := startMockServer(t)
defer ms.Stop()
Expand Down Expand Up @@ -401,7 +417,7 @@ func TestJwtRefreshIntervalRecoverFromInitialFailOnFirstHit(t *testing.T) {
func TestJwtRefreshIntervalRecoverFromFail(t *testing.T) {
defaultRefreshInterval := 50 * time.Millisecond
refreshIntervalOnFail := 2 * time.Millisecond
r := NewJwksResolver(JwtPubKeyEvictionDuration, defaultRefreshInterval, refreshIntervalOnFail, 1*time.Millisecond)
r := NewJwksResolver(JwtPubKeyEvictionDuration, defaultRefreshInterval, refreshIntervalOnFail, 1*time.Millisecond, testCreateFakeJwks)

ms := startMockServer(t)
defer ms.Stop()
Expand Down Expand Up @@ -431,7 +447,7 @@ func TestJwtPubKeyMetric(t *testing.T) {
mt := monitortest.New(t)
defaultRefreshInterval := 50 * time.Millisecond
refreshIntervalOnFail := 2 * time.Millisecond
r := NewJwksResolver(JwtPubKeyEvictionDuration, defaultRefreshInterval, refreshIntervalOnFail, 1*time.Millisecond)
r := NewJwksResolver(JwtPubKeyEvictionDuration, defaultRefreshInterval, refreshIntervalOnFail, 1*time.Millisecond, testCreateFakeJwks)
defer r.Close()

ms := startMockServer(t)
Expand Down