Skip to content

Commit 2c4875a

Browse files
authored
Fix/username secret (#15)
* fix: renew secret on username change
1 parent 1deb782 commit 2c4875a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

internal/controller/githubapp_controller.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ var (
5050
defaultTimeBeforeExpiry = 15 * time.Minute // Default time before expiry
5151
reconcileInterval time.Duration // Requeue interval (from env var)
5252
timeBeforeExpiry time.Duration // Expiry threshold (from env var)
53+
gitUsername = "not-used"
5354
)
5455

5556
//+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
129130
}
130131

131132
// 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"])
134135

135136
// 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) {
137138
// If accessToken is invalid, generate or update access token
138139
return r.generateOrUpdateAccessToken(ctx, githubApp)
139140
}
@@ -156,9 +157,19 @@ func (r *GithubAppReconciler) checkExpiryAndUpdateAccessToken(ctx context.Contex
156157
}
157158

158159
// 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 {
160161
l := log.FromContext(ctx)
161162

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+
162173
// GitHub API endpoint for rate limit information
163174
url := "https://api.github.com/rate_limit"
164175

@@ -273,7 +284,7 @@ func (r *GithubAppReconciler) generateOrUpdateAccessToken(ctx context.Context, g
273284
},
274285
StringData: map[string]string{
275286
"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
277288
},
278289
}
279290
accessTokenSecretKey := client.ObjectKey{
@@ -328,7 +339,7 @@ func (r *GithubAppReconciler) generateOrUpdateAccessToken(ctx context.Context, g
328339
}
329340
existingSecret.StringData = map[string]string{
330341
"token": accessToken,
331-
"username": "not-used",
342+
"username": gitUsername,
332343
}
333344
if err := r.Update(ctx, existingSecret); err != nil {
334345
l.Error(err, "Failed to update existing Secret")

0 commit comments

Comments
 (0)