2015-01-27 01:26:11 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2019-10-23 03:40:12 -05:00
|
|
|
"time"
|
|
|
|
|
2015-02-26 10:23:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2015-02-26 10:23:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/apikeygen"
|
2019-06-26 01:47:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-08-04 07:19:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/apikey"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-01-27 01:26:11 -06:00
|
|
|
)
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route GET /auth/keys api_keys getAPIkeys
|
|
|
|
//
|
|
|
|
// Get auth keys.
|
|
|
|
//
|
|
|
|
// Will return auth keys.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getAPIkeyResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2022-02-07 07:14:54 -06:00
|
|
|
func (hs *HTTPServer) GetAPIKeys(c *models.ReqContext) response.Response {
|
2022-08-11 06:28:55 -05:00
|
|
|
query := apikey.GetApiKeysQuery{OrgId: c.OrgID, User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")}
|
2015-01-27 01:26:11 -06:00
|
|
|
|
2022-08-02 09:55:19 -05:00
|
|
|
if err := hs.apiKeyService.GetAPIKeys(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to list api keys", err)
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
|
|
|
|
2022-04-29 08:30:24 -05:00
|
|
|
ids := map[string]bool{}
|
|
|
|
result := make([]*dtos.ApiKeyDTO, len(query.Result))
|
2015-01-27 01:26:11 -06:00
|
|
|
for i, t := range query.Result {
|
2022-04-29 08:30:24 -05:00
|
|
|
ids[strconv.FormatInt(t.Id, 10)] = true
|
2019-06-26 01:47:03 -05:00
|
|
|
var expiration *time.Time = nil
|
|
|
|
if t.Expires != nil {
|
|
|
|
v := time.Unix(*t.Expires, 0)
|
|
|
|
expiration = &v
|
|
|
|
}
|
2022-04-29 08:30:24 -05:00
|
|
|
result[i] = &dtos.ApiKeyDTO{
|
2019-06-26 01:47:03 -05:00
|
|
|
Id: t.Id,
|
|
|
|
Name: t.Name,
|
|
|
|
Role: t.Role,
|
|
|
|
Expiration: expiration,
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
|
|
|
}
|
2015-05-18 14:23:40 -05:00
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
metadata := hs.getMultiAccessControlMetadata(c, c.OrgID, "apikeys:id", ids)
|
2022-04-29 08:30:24 -05:00
|
|
|
if len(metadata) > 0 {
|
|
|
|
for _, key := range result {
|
|
|
|
key.AccessControl = metadata[strconv.FormatInt(key.Id, 10)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, result)
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route DELETE /auth/keys/{id} api_keys deleteAPIkey
|
|
|
|
//
|
|
|
|
// Delete API key.
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: okResponse
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 404: notFoundError
|
|
|
|
// 500: internalServerError
|
2022-02-07 07:14:54 -06:00
|
|
|
func (hs *HTTPServer) DeleteAPIKey(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2015-01-27 01:26:11 -06:00
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd := &apikey.DeleteCommand{Id: id, OrgId: c.OrgID}
|
2022-08-02 09:55:19 -05:00
|
|
|
err = hs.apiKeyService.DeleteApiKey(c.Req.Context(), cmd)
|
2015-01-27 01:26:11 -06:00
|
|
|
if err != nil {
|
2021-04-28 06:30:09 -05:00
|
|
|
var status int
|
2022-08-04 07:19:09 -05:00
|
|
|
if errors.Is(err, apikey.ErrNotFound) {
|
2021-04-28 06:30:09 -05:00
|
|
|
status = 404
|
|
|
|
} else {
|
|
|
|
status = 500
|
|
|
|
}
|
|
|
|
return response.Error(status, "Failed to delete API key", err)
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("API key deleted")
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
|
|
|
|
2022-07-27 08:54:37 -05:00
|
|
|
// swagger:route POST /auth/keys api_keys addAPIkey
|
|
|
|
//
|
|
|
|
// Creates an API key.
|
|
|
|
//
|
|
|
|
// Will return details of the created API key
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: postAPIkeyResponse
|
|
|
|
// 400: badRequestError
|
|
|
|
// 401: unauthorisedError
|
|
|
|
// 403: forbiddenError
|
|
|
|
// 409: conflictError
|
|
|
|
// 500: internalServerError
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AddAPIKey(c *models.ReqContext) response.Response {
|
2022-08-04 07:19:09 -05:00
|
|
|
cmd := apikey.AddCommand{}
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2015-01-27 01:26:11 -06:00
|
|
|
if !cmd.Role.IsValid() {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Invalid role specified", nil)
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
2022-04-13 11:11:03 -05:00
|
|
|
if !c.OrgRole.Includes(cmd.Role) {
|
|
|
|
return response.Error(http.StatusForbidden, "Cannot assign a role higher than user's role", nil)
|
|
|
|
}
|
2015-01-27 01:26:11 -06:00
|
|
|
|
2019-06-26 01:47:03 -05:00
|
|
|
if hs.Cfg.ApiKeyMaxSecondsToLive != -1 {
|
|
|
|
if cmd.SecondsToLive == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Number of seconds before expiration should be set", nil)
|
2019-06-26 01:47:03 -05:00
|
|
|
}
|
|
|
|
if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Number of seconds before expiration is greater than the global limit", nil)
|
2019-06-26 01:47:03 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 07:51:54 -06:00
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
cmd.OrgId = c.OrgID
|
2015-02-26 10:23:28 -06:00
|
|
|
|
2019-10-23 03:40:12 -05:00
|
|
|
newKeyInfo, err := apikeygen.New(cmd.OrgId, cmd.Name)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Generating API key failed", err)
|
2019-10-23 03:40:12 -05:00
|
|
|
}
|
|
|
|
|
2015-02-26 10:23:28 -06:00
|
|
|
cmd.Key = newKeyInfo.HashedKey
|
2022-08-02 09:55:19 -05:00
|
|
|
if err := hs.apiKeyService.AddAPIKey(c.Req.Context(), &cmd); err != nil {
|
2022-08-04 07:19:09 -05:00
|
|
|
if errors.Is(err, apikey.ErrInvalidExpiration) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, err.Error(), nil)
|
2019-06-26 01:47:03 -05:00
|
|
|
}
|
2022-08-04 07:19:09 -05:00
|
|
|
if errors.Is(err, apikey.ErrDuplicate) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(409, err.Error(), nil)
|
2019-07-11 03:20:34 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to add API Key", err)
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
|
|
|
|
2015-02-26 10:23:28 -06:00
|
|
|
result := &dtos.NewApiKeyResult{
|
2020-09-07 10:06:11 -05:00
|
|
|
ID: cmd.Result.Id,
|
2015-01-27 01:26:11 -06:00
|
|
|
Name: cmd.Result.Name,
|
2020-09-07 10:06:11 -05:00
|
|
|
Key: newKeyInfo.ClientSecret,
|
|
|
|
}
|
2015-01-27 01:26:11 -06:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, result)
|
2015-01-27 01:26:11 -06:00
|
|
|
}
|
2022-07-27 08:54:37 -05:00
|
|
|
|
|
|
|
// swagger:parameters getAPIkeys
|
|
|
|
type GetAPIkeysParams struct {
|
|
|
|
// Show expired keys
|
|
|
|
// in:query
|
|
|
|
// required:false
|
|
|
|
// default:false
|
|
|
|
IncludeExpired bool `json:"includeExpired"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters addAPIkey
|
|
|
|
type AddAPIkeyParams struct {
|
|
|
|
// in:body
|
|
|
|
// required:true
|
2022-08-04 07:19:09 -05:00
|
|
|
Body apikey.AddCommand
|
2022-07-27 08:54:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:parameters deleteAPIkey
|
|
|
|
type DeleteAPIkeyParams struct {
|
|
|
|
// in:path
|
|
|
|
// required:true
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response getAPIkeyResponse
|
|
|
|
type GetAPIkeyResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body []*dtos.ApiKeyDTO `json:"body"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:response postAPIkeyResponse
|
|
|
|
type PostAPIkeyResponse struct {
|
|
|
|
// The response message
|
|
|
|
// in: body
|
|
|
|
Body dtos.NewApiKeyResult `json:"body"`
|
|
|
|
}
|