50
50
defaultTimeBeforeExpiry = 15 * time .Minute // Default time before expiry
51
51
reconcileInterval time.Duration // Requeue interval (from env var)
52
52
timeBeforeExpiry time.Duration // Expiry threshold (from env var)
53
+ gitUsername = "not-used"
53
54
)
54
55
55
56
//+kubebuilder:rbac:groups=githubapp.samir.io,resources=githubapps,verbs=get;list;watch;create;update;patch;delete
@@ -129,11 +130,11 @@ func (r *GithubAppReconciler) checkExpiryAndUpdateAccessToken(ctx context.Contex
129
130
}
130
131
131
132
// Check if the accessToken field exists and is not empty
132
- var accessToken string
133
- accessToken = string (accessTokenSecret .Data ["token " ])
133
+ accessToken := string ( accessTokenSecret . Data [ "token" ])
134
+ username : = string (accessTokenSecret .Data ["username " ])
134
135
135
136
// Check if the access token is a valid github token via gh api auth
136
- if ! isAccessTokenValid (ctx , accessToken , req ) {
137
+ if ! isAccessTokenValid (ctx , username , accessToken , req ) {
137
138
// If accessToken is invalid, generate or update access token
138
139
return r .generateOrUpdateAccessToken (ctx , githubApp )
139
140
}
@@ -156,9 +157,19 @@ func (r *GithubAppReconciler) checkExpiryAndUpdateAccessToken(ctx context.Contex
156
157
}
157
158
158
159
// Function to check if the access token is valid by making a request to GitHub API
159
- func isAccessTokenValid (ctx context.Context , accessToken string , req ctrl.Request ) bool {
160
+ func isAccessTokenValid (ctx context.Context , username string , accessToken string , req ctrl.Request ) bool {
160
161
l := log .FromContext (ctx )
161
162
163
+ // If username has been modified, renew the secret
164
+ if username != gitUsername {
165
+ log .Log .Info (
166
+ "Username key is invalid, will renew" ,
167
+ "GithubApp" , req .Name ,
168
+ "Namespace" , req .Namespace ,
169
+ )
170
+ return false
171
+ }
172
+
162
173
// GitHub API endpoint for rate limit information
163
174
url := "https://api.github.com/rate_limit"
164
175
@@ -273,7 +284,7 @@ func (r *GithubAppReconciler) generateOrUpdateAccessToken(ctx context.Context, g
273
284
},
274
285
StringData : map [string ]string {
275
286
"token" : accessToken ,
276
- "username" : "not-used" , // username is ignored in github auth but required
287
+ "username" : gitUsername , // username is ignored in github auth but required
277
288
},
278
289
}
279
290
accessTokenSecretKey := client.ObjectKey {
@@ -328,7 +339,7 @@ func (r *GithubAppReconciler) generateOrUpdateAccessToken(ctx context.Context, g
328
339
}
329
340
existingSecret .StringData = map [string ]string {
330
341
"token" : accessToken ,
331
- "username" : "not-used" ,
342
+ "username" : gitUsername ,
332
343
}
333
344
if err := r .Update (ctx , existingSecret ); err != nil {
334
345
l .Error (err , "Failed to update existing Secret" )
0 commit comments