mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 11:44:26 -06:00
6dbb6408d4
* Add extra fields to OSS types to support enterprise * Create a service account at the same time as the API key * Use service account credentials when accessing API with APIkey * Add GetRole to service, merge RoleDTO and Role structs This patch merges the identical OSS and Enterprise data structures, which improves the code for two reasons: 1. Makes switching between OSS and Enterprise easier 2. Reduces the chance of incompatibilities developing between the same functions in OSS and Enterprise * If API key is not linked to a service account, continue login as usual * Fallback to old auth if no service account linked to key * Add CloneUserToServiceAccount * Adding LinkAPIKeyToServiceAccount * Handle api key link error * Better error messages for OSS accesscontrol * Set an invalid user id as default * Re-arrange field names * ServiceAccountId is integer * Better error messages Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com> Co-authored-by: Eric Leijonmarck <eric.leijonmarck@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> Co-authored-by: Gabriel MABILLE <gamab@users.noreply.github.com>
112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/components/apikeygen"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
)
|
|
|
|
// GetAPIKeys returns a list of API keys
|
|
func GetAPIKeys(c *models.ReqContext) response.Response {
|
|
query := models.GetApiKeysQuery{OrgId: c.OrgId, IncludeExpired: c.QueryBool("includeExpired")}
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil {
|
|
return response.Error(500, "Failed to list api keys", err)
|
|
}
|
|
|
|
result := make([]*models.ApiKeyDTO, len(query.Result))
|
|
for i, t := range query.Result {
|
|
var expiration *time.Time = nil
|
|
if t.Expires != nil {
|
|
v := time.Unix(*t.Expires, 0)
|
|
expiration = &v
|
|
}
|
|
result[i] = &models.ApiKeyDTO{
|
|
Id: t.Id,
|
|
Name: t.Name,
|
|
Role: t.Role,
|
|
Expiration: expiration,
|
|
}
|
|
}
|
|
|
|
return response.JSON(200, result)
|
|
}
|
|
|
|
// DeleteAPIKey deletes an API key
|
|
func DeleteAPIKey(c *models.ReqContext) response.Response {
|
|
id := c.ParamsInt64(":id")
|
|
|
|
cmd := &models.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId}
|
|
|
|
err := bus.DispatchCtx(c.Req.Context(), cmd)
|
|
if err != nil {
|
|
var status int
|
|
if errors.Is(err, models.ErrApiKeyNotFound) {
|
|
status = 404
|
|
} else {
|
|
status = 500
|
|
}
|
|
return response.Error(status, "Failed to delete API key", err)
|
|
}
|
|
|
|
return response.Success("API key deleted")
|
|
}
|
|
|
|
// AddAPIKey adds an API key
|
|
func (hs *HTTPServer) AddAPIKey(c *models.ReqContext, cmd models.AddApiKeyCommand) response.Response {
|
|
if !cmd.Role.IsValid() {
|
|
return response.Error(400, "Invalid role specified", nil)
|
|
}
|
|
|
|
if hs.Cfg.ApiKeyMaxSecondsToLive != -1 {
|
|
if cmd.SecondsToLive == 0 {
|
|
return response.Error(400, "Number of seconds before expiration should be set", nil)
|
|
}
|
|
if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive {
|
|
return response.Error(400, "Number of seconds before expiration is greater than the global limit", nil)
|
|
}
|
|
}
|
|
cmd.OrgId = c.OrgId
|
|
var err error
|
|
var serviceAccount *models.User = &models.User{Id: -1}
|
|
if hs.Cfg.FeatureToggles["service-accounts"] {
|
|
if cmd.CreateNewServiceAccount {
|
|
serviceAccount, err = hs.AccessControl.CloneUserToServiceAccount(c.Req.Context(), c.SignedInUser)
|
|
if err != nil {
|
|
return response.Error(500, "Unable to clone user to service account", err)
|
|
}
|
|
cmd.ServiceAccountId = serviceAccount.Id
|
|
}
|
|
}
|
|
|
|
newKeyInfo, err := apikeygen.New(cmd.OrgId, cmd.Name)
|
|
if err != nil {
|
|
return response.Error(500, "Generating API key failed", err)
|
|
}
|
|
|
|
cmd.Key = newKeyInfo.HashedKey
|
|
|
|
if err := bus.DispatchCtx(c.Req.Context(), &cmd); err != nil {
|
|
if errors.Is(err, models.ErrInvalidApiKeyExpiration) {
|
|
return response.Error(400, err.Error(), nil)
|
|
}
|
|
if errors.Is(err, models.ErrDuplicateApiKey) {
|
|
return response.Error(409, err.Error(), nil)
|
|
}
|
|
return response.Error(500, "Failed to add API Key", err)
|
|
}
|
|
|
|
result := &dtos.NewApiKeyResult{
|
|
ID: cmd.Result.Id,
|
|
Name: cmd.Result.Name,
|
|
Key: newKeyInfo.ClientSecret,
|
|
}
|
|
|
|
return response.JSON(200, result)
|
|
}
|