| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2022-08-04 20:19:09 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/apikey" | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route GET /auth/keys api_keys getAPIkeys
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Get auth keys.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will return auth keys.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:58:35 +08:00
										 |  |  | // Deprecated: true.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Deprecated. Please use GET /api/serviceaccounts and GET /api/serviceaccounts/{id}/tokens instead
 | 
					
						
							| 
									
										
										
										
											2024-08-30 17:37:36 +08:00
										 |  |  | // see https://grafana.com/docs/grafana/next/administration/service-accounts/migrate-api-keys/.
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:58:35 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: getAPIkeyResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) GetAPIKeys(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2023-08-28 16:42:24 +08:00
										 |  |  | 	query := apikey.GetApiKeysQuery{OrgID: c.SignedInUser.GetOrgID(), User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 20:26:33 +08:00
										 |  |  | 	keys, err := hs.apiKeyService.GetAPIKeys(c.Req.Context(), &query) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-08-28 16:42:24 +08:00
										 |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to list api keys", err) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +08:00
										 |  |  | 	ids := map[string]bool{} | 
					
						
							| 
									
										
										
										
											2023-03-21 20:26:33 +08:00
										 |  |  | 	result := make([]*dtos.ApiKeyDTO, len(keys)) | 
					
						
							|  |  |  | 	for i, t := range keys { | 
					
						
							| 
									
										
										
										
											2023-02-04 00:23:09 +08:00
										 |  |  | 		ids[strconv.FormatInt(t.ID, 10)] = true | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		var expiration *time.Time = nil | 
					
						
							|  |  |  | 		if t.Expires != nil { | 
					
						
							|  |  |  | 			v := time.Unix(*t.Expires, 0) | 
					
						
							|  |  |  | 			expiration = &v | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +08:00
										 |  |  | 		result[i] = &dtos.ApiKeyDTO{ | 
					
						
							| 
									
										
										
										
											2023-02-04 00:23:09 +08:00
										 |  |  | 			ID:         t.ID, | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 			Name:       t.Name, | 
					
						
							|  |  |  | 			Role:       t.Role, | 
					
						
							|  |  |  | 			Expiration: expiration, | 
					
						
							| 
									
										
										
										
											2023-03-09 22:16:42 +08:00
										 |  |  | 			LastUsedAt: t.LastUsedAt, | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-19 03:23:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-07 18:08:16 +08:00
										 |  |  | 	metadata := getMultiAccessControlMetadata(c, "apikeys:id", ids) | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +08:00
										 |  |  | 	if len(metadata) > 0 { | 
					
						
							|  |  |  | 		for _, key := range result { | 
					
						
							| 
									
										
										
										
											2023-02-04 00:23:09 +08:00
										 |  |  | 			key.AccessControl = metadata[strconv.FormatInt(key.ID, 10)] | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, result) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route DELETE /auth/keys/{id} api_keys deleteAPIkey
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Delete API key.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:58:35 +08:00
										 |  |  | // Deletes an API key.
 | 
					
						
							| 
									
										
										
										
											2024-08-30 17:37:36 +08:00
										 |  |  | // Deprecated. See: https://grafana.com/docs/grafana/next/administration/service-accounts/migrate-api-keys/.
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:58:35 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Deprecated: true
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // Responses:
 | 
					
						
							|  |  |  | // 200: okResponse
 | 
					
						
							|  |  |  | // 401: unauthorisedError
 | 
					
						
							|  |  |  | // 403: forbiddenError
 | 
					
						
							|  |  |  | // 404: notFoundError
 | 
					
						
							|  |  |  | // 500: internalServerError
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) DeleteAPIKey(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-01-15 00:55:57 +08: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 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-28 16:42:24 +08:00
										 |  |  | 	cmd := &apikey.DeleteCommand{ID: id, OrgID: c.SignedInUser.GetOrgID()} | 
					
						
							| 
									
										
										
										
											2022-08-02 22:55:19 +08:00
										 |  |  | 	err = hs.apiKeyService.DeleteApiKey(c.Req.Context(), cmd) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-04-28 19:30:09 +08:00
										 |  |  | 		var status int | 
					
						
							| 
									
										
										
										
											2022-08-04 20:19:09 +08:00
										 |  |  | 		if errors.Is(err, apikey.ErrNotFound) { | 
					
						
							| 
									
										
										
										
											2024-02-28 00:39:51 +08:00
										 |  |  | 			status = http.StatusNotFound | 
					
						
							| 
									
										
										
										
											2021-04-28 19:30:09 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2024-02-28 00:39:51 +08:00
										 |  |  | 			status = http.StatusInternalServerError | 
					
						
							| 
									
										
										
										
											2021-04-28 19:30:09 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return response.Error(status, "Failed to delete API key", err) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("API key deleted") | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // swagger:route POST /auth/keys api_keys addAPIkey
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Creates an API key.
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-09-12 15:40:35 +08:00
										 |  |  | // Will return details of the created API key.
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:58:35 +08:00
										 |  |  | // Deprecated: true
 | 
					
						
							|  |  |  | // Deprecated. Please use POST /api/serviceaccounts and POST /api/serviceaccounts/{id}/tokens
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2024-08-30 17:37:36 +08:00
										 |  |  | // see: https://grafana.com/docs/grafana/next/administration/service-accounts/migrate-api-keys/.
 | 
					
						
							| 
									
										
										
										
											2023-04-11 22:58:35 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | // Responses:
 | 
					
						
							| 
									
										
										
										
											2024-09-05 18:10:24 +08:00
										 |  |  | // 410: goneError
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) AddAPIKey(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2024-08-28 19:20:36 +08:00
										 |  |  | 	hs.log.Warn("Obsolete and Permanently moved API endpoint called", "path", c.Req.URL.Path) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-05 18:10:24 +08:00
										 |  |  | 	// Respond with a 410 Gone status code
 | 
					
						
							|  |  |  | 	return response.Error( | 
					
						
							|  |  |  | 		http.StatusGone, | 
					
						
							|  |  |  | 		"this endpoint has been removed, please use POST /api/serviceaccounts and POST /api/serviceaccounts/{id}/tokens instead", | 
					
						
							|  |  |  | 		nil, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-27 21:54:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // swagger:parameters getAPIkeys
 | 
					
						
							|  |  |  | type GetAPIkeysParams struct { | 
					
						
							|  |  |  | 	// Show expired keys
 | 
					
						
							|  |  |  | 	// in:query
 | 
					
						
							|  |  |  | 	// required:false
 | 
					
						
							|  |  |  | 	// default:false
 | 
					
						
							|  |  |  | 	IncludeExpired bool `json:"includeExpired"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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"` | 
					
						
							|  |  |  | } |