mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Automatically remove expired API keys from the setting
This commit is contained in:
parent
4c6b834ae5
commit
d30c4f4072
1 changed files with 35 additions and 0 deletions
|
@ -13,6 +13,7 @@ import (
|
||||||
|
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/config"
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portbase/rng"
|
"github.com/safing/portbase/rng"
|
||||||
|
@ -361,15 +362,26 @@ func updateAPIKeys(_ context.Context, _ interface{}) error {
|
||||||
delete(apiKeys, k)
|
delete(apiKeys, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// whether or not we found expired API keys that should be removed
|
||||||
|
// from the setting
|
||||||
|
hasExpiredKeys := false
|
||||||
|
|
||||||
|
// a list of valid API keys. Used when hasExpiredKeys is set to true.
|
||||||
|
// in that case we'll update the setting to only contain validAPIKeys
|
||||||
|
validAPIKeys := []string{}
|
||||||
|
|
||||||
// Parse new keys.
|
// Parse new keys.
|
||||||
for _, key := range configuredAPIKeys() {
|
for _, key := range configuredAPIKeys() {
|
||||||
u, err := url.Parse(key)
|
u, err := url.Parse(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("api: failed to parse configured API key %s: %s", key, err)
|
log.Errorf("api: failed to parse configured API key %s: %s", key, err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.Path == "" {
|
if u.Path == "" {
|
||||||
log.Errorf("api: malformed API key %s: missing path section", key)
|
log.Errorf("api: malformed API key %s: missing path section", key)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,11 +415,34 @@ func updateAPIKeys(_ context.Context, _ interface{}) error {
|
||||||
log.Errorf("api: invalid API key %s: %s", key, err)
|
log.Errorf("api: invalid API key %s: %s", key, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// continue to the next token if this one is already invalid
|
||||||
|
if time.Now().After(validUntil) {
|
||||||
|
// mark the key as expired so we'll remove it from the setting afterwards
|
||||||
|
hasExpiredKeys = true
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
token.ValidUntil = &validUntil
|
token.ValidUntil = &validUntil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save token.
|
// Save token.
|
||||||
apiKeys[u.Path] = token
|
apiKeys[u.Path] = token
|
||||||
|
validAPIKeys = append(validAPIKeys, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasExpiredKeys {
|
||||||
|
name := "api-key-cleanup"
|
||||||
|
module.StartLowPriorityMicroTask(&name, func(ctx context.Context) error {
|
||||||
|
if err := config.SetConfigOption(CfgAPIKeys, validAPIKeys); err != nil {
|
||||||
|
log.Errorf("api: failed to remove expired API keys: %s", err)
|
||||||
|
} else {
|
||||||
|
log.Infof("api: removed expired API keys from %s", CfgAPIKeys)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue