Skip to content

Commit be013d7

Browse files
zhaohuabingzirain
authored andcommitted
verify service account
Signed-off-by: Huabing (Robin) Zhao <[email protected]>
1 parent 5833bb7 commit be013d7

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

internal/xds/cache/snapshotcache.go

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type SnapshotCacheWithCallbacks interface {
4848
serverv3.Callbacks
4949
GenerateNewSnapshot(string, types.XdsResources) error
5050
SnapshotHasIrKey(string) bool
51+
GetIrKeys() []string
5152
}
5253

5354
type snapshotMap map[string]*cachev3.Snapshot
@@ -377,3 +378,15 @@ func (s *snapshotCache) SnapshotHasIrKey(irKey string) bool {
377378

378379
return false
379380
}
381+
382+
func (s *snapshotCache) GetIrKeys() []string {
383+
s.mu.Lock()
384+
defer s.mu.Unlock()
385+
386+
var irKeys []string
387+
for key := range s.lastSnapshot {
388+
irKeys = append(irKeys, key)
389+
}
390+
391+
return irKeys
392+
}

internal/xds/server/kubejwt/tokenreview.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"context"
1010
"fmt"
1111
"slices"
12+
"strings"
1213

14+
"github.com/envoyproxy/gateway/internal/envoygateway/config"
15+
"github.com/envoyproxy/gateway/internal/utils"
1316
authenticationv1 "k8s.io/api/authentication/v1"
1417
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1518
"k8s.io/apiserver/pkg/authentication/serviceaccount"
@@ -64,5 +67,25 @@ func (i *JWTAuthInterceptor) validateKubeJWT(ctx context.Context, token, nodeID
6467
}
6568
}
6669

67-
return nil
70+
// Check if the service account name in the JWT token exists in the cache to verify that the token
71+
// is valid for an Envoy proxy managed by Envoy Gateway.
72+
// example: "system:serviceaccount:default:envoy-default-eg-e41e7b31"
73+
parts:=strings.Split(tokenReview.Status.User.Username, ":")
74+
if len(parts) != 4 {
75+
return fmt.Errorf("invalid username format: %s", tokenReview.Status.User.Username)
76+
}
77+
sa := parts[3]
78+
79+
irKeys:=i.cache.GetIrKeys()
80+
for _, irKey := range irKeys {
81+
if irKey2ServiceAccountName(irKey) == sa {
82+
return nil
83+
}
84+
}
85+
return fmt.Errorf("Envoy service account %s not found in the cache", sa)
86+
}
87+
88+
func irKey2ServiceAccountName(irKey string) string {
89+
hashedName := utils.GetHashedName(irKey, 48)
90+
return fmt.Sprintf("%s-%s", config.EnvoyPrefix, hashedName)
6891
}

0 commit comments

Comments
 (0)