Re-mint watchpr's Gitea token when it expires
Vault-minted Gitea tokens last ~1h, far less than a watch, and every poll past expiry 401'd into a warning while watchpr looked healthy. - retry a rejected request once with a freshly minted token - abort the watch when the fresh token is rejected too - poll anonymously when no token can be minted, mint only on a real 401/403
This commit is contained in:
+19
-5
@@ -86,17 +86,31 @@ func AuthentikURL() string {
|
||||
}
|
||||
|
||||
var (
|
||||
tokenOnce sync.Once
|
||||
tokenValue string
|
||||
tokenErr error
|
||||
tokenMu sync.Mutex
|
||||
tokenMinted bool
|
||||
tokenValue string
|
||||
tokenErr error
|
||||
)
|
||||
|
||||
// GiteaToken returns a Gitea token, minting it via Vault AppRole on first call
|
||||
// and caching it in-process for the lifetime of the command.
|
||||
func GiteaToken() (string, error) {
|
||||
tokenOnce.Do(func() {
|
||||
tokenMu.Lock()
|
||||
defer tokenMu.Unlock()
|
||||
if !tokenMinted {
|
||||
tokenValue, tokenErr = fetchGiteaToken(VaultAddr(), RoleID(), GiteaCredsPath())
|
||||
})
|
||||
tokenMinted = true
|
||||
}
|
||||
return tokenValue, tokenErr
|
||||
}
|
||||
|
||||
// RefreshGiteaToken mints a fresh Gitea token and replaces the cached one, for
|
||||
// callers that outlive the ~1h token TTL.
|
||||
func RefreshGiteaToken() (string, error) {
|
||||
tokenMu.Lock()
|
||||
defer tokenMu.Unlock()
|
||||
tokenValue, tokenErr = fetchGiteaToken(VaultAddr(), RoleID(), GiteaCredsPath())
|
||||
tokenMinted = true
|
||||
return tokenValue, tokenErr
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user