@@ -5,7 +5,10 @@ package eksdetector
5
5
6
6
import (
7
7
"context"
8
+ "encoding/base64"
9
+ "encoding/json"
8
10
"fmt"
11
+ "strings"
9
12
"sync"
10
13
11
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -15,6 +18,7 @@ import (
15
18
16
19
type Detector interface {
17
20
getConfigMap (namespace string , name string ) (map [string ]string , error )
21
+ getIssuer () (string , error )
18
22
}
19
23
20
24
type EksDetector struct {
55
59
}
56
60
57
61
// IsEKS checks if the agent is running on EKS. This is done by using the kubernetes API to determine if the aws-auth
58
- // configmap exists in the kube-system namespace
62
+ // configmap exists in the kube-system namespace or by extracting the "iss" field from the service account token and
63
+ // checking if it contains "eks" as a fall-back
59
64
IsEKS = func () IsEKSCache {
60
65
once .Do (func () {
61
66
var errors error
71
76
awsAuth , err := eksDetector .getConfigMap (authConfigNamespace , authConfigConfigMap )
72
77
if err == nil {
73
78
value = awsAuth != nil
79
+ } else {
80
+ issuer , err := eksDetector .getIssuer ()
81
+ if err == nil {
82
+ value = strings .Contains (strings .ToLower (issuer ), "eks" )
83
+ }
74
84
}
75
85
}
76
86
isEKSCacheSingleton = IsEKSCache {Value : value , Err : errors }
@@ -90,6 +100,41 @@ func (d *EksDetector) getConfigMap(namespace string, name string) (map[string]st
90
100
return configMap .Data , nil
91
101
}
92
102
103
+ // getIssuer retrieves the issuer ("iss") from the service account token
104
+ func (d * EksDetector ) getIssuer () (string , error ) {
105
+ conf , err := getInClusterConfig ()
106
+ if err != nil {
107
+ return "" , fmt .Errorf ("failed to get in-cluster config: %w" , err )
108
+ }
109
+
110
+ token := conf .BearerToken
111
+ if token == "" {
112
+ return "" , fmt .Errorf ("empty token in config" )
113
+ }
114
+
115
+ parts := strings .Split (token , "." )
116
+ if len (parts ) < 2 {
117
+ return "" , fmt .Errorf ("missing payload" )
118
+ }
119
+
120
+ decoded , err := base64 .RawURLEncoding .DecodeString (parts [1 ])
121
+ if err != nil {
122
+ return "" , fmt .Errorf ("failed to decode token payload: %w" , err )
123
+ }
124
+
125
+ var claims map [string ]interface {}
126
+ if err = json .Unmarshal (decoded , & claims ); err != nil {
127
+ return "" , fmt .Errorf ("failed to unmarshal token payload: %w" , err )
128
+ }
129
+
130
+ iss , ok := claims ["iss" ].(string )
131
+ if ! ok {
132
+ return "" , fmt .Errorf ("issuer field not found in token" )
133
+ }
134
+
135
+ return iss , nil
136
+ }
137
+
93
138
func getClient () (kubernetes.Interface , error ) {
94
139
//Get cluster config
95
140
confs , err := getInClusterConfig ()
0 commit comments