mirror of
https://github.com/grafana/grafana.git
synced 2026-08-15 15:45:00 -05:00
Auth: Add expiry date for service accounts access tokens (#58885)
* Add new configuration option for SA tokens * Add new expiry date option to frontend components * Add backend validation Co-authored-by: Gabriel MABILLE <gamab@users.noreply.github.com>
This commit is contained in:
co-authored by
Gabriel MABILLE
parent
c1eabb893f
commit
f8f61c1a69
@@ -198,9 +198,10 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
"unifiedAlerting": map[string]interface{}{
|
||||
"minInterval": hs.Cfg.UnifiedAlerting.MinInterval.String(),
|
||||
},
|
||||
"oauth": hs.getEnabledOAuthProviders(),
|
||||
"samlEnabled": hs.samlEnabled(),
|
||||
"samlName": hs.samlName(),
|
||||
"oauth": hs.getEnabledOAuthProviders(),
|
||||
"samlEnabled": hs.samlEnabled(),
|
||||
"samlName": hs.samlName(),
|
||||
"tokenExpirationDayLimit": hs.Cfg.SATokenExpirationDayLimit,
|
||||
}
|
||||
|
||||
if hs.ThumbService != nil {
|
||||
|
||||
@@ -160,6 +160,14 @@ func (api *ServiceAccountsAPI) CreateToken(c *models.ReqContext) response.Respon
|
||||
}
|
||||
}
|
||||
|
||||
if api.cfg.SATokenExpirationDayLimit > 0 {
|
||||
dayExpireLimit := time.Now().Add(time.Duration(api.cfg.SATokenExpirationDayLimit) * time.Hour * 24).Truncate(24 * time.Hour)
|
||||
expirationDate := time.Now().Add(time.Duration(cmd.SecondsToLive) * time.Second).Truncate(24 * time.Hour)
|
||||
if expirationDate.After(dayExpireLimit) {
|
||||
return response.Respond(http.StatusBadRequest, "The expiration date input exceeds the limit for service account access tokens expiration date")
|
||||
}
|
||||
}
|
||||
|
||||
newKeyInfo, err := apikeygenprefix.New(ServiceID)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusInternalServerError, "Generating service account token failed", err)
|
||||
|
||||
@@ -385,6 +385,9 @@ type Cfg struct {
|
||||
HiddenUsers map[string]struct{}
|
||||
CaseInsensitiveLogin bool // Login and Email will be considered case insensitive
|
||||
|
||||
// Service Accounts
|
||||
SATokenExpirationDayLimit int
|
||||
|
||||
// Annotations
|
||||
AnnotationCleanupJobBatchSize int64
|
||||
AnnotationMaximumTagsLength int64
|
||||
@@ -978,6 +981,9 @@ func (cfg *Cfg) Load(args CommandLineArgs) error {
|
||||
if err := readUserSettings(iniFile, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := readServiceAccountSettings(iniFile, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := readAuthSettings(iniFile, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1481,6 +1487,12 @@ func readUserSettings(iniFile *ini.File, cfg *Cfg) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readServiceAccountSettings(iniFile *ini.File, cfg *Cfg) error {
|
||||
serviceAccount := iniFile.Section("service_accounts")
|
||||
cfg.SATokenExpirationDayLimit = serviceAccount.Key("token_expiration_day_limit").MustInt(-1)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *Cfg) readRenderingSettings(iniFile *ini.File) error {
|
||||
renderSec := iniFile.Section("rendering")
|
||||
cfg.RendererUrl = valueAsString(renderSec, "server_url", "")
|
||||
|
||||
Reference in New Issue
Block a user