| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/apikeygen" | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | // GetAPIKeys returns a list of API keys
 | 
					
						
							| 
									
										
										
										
											2022-02-07 21:14:54 +08:00
										 |  |  | func (hs *HTTPServer) GetAPIKeys(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +08:00
										 |  |  | 	query := models.GetApiKeysQuery{OrgId: c.OrgId, User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 21:14:54 +08:00
										 |  |  | 	if err := hs.SQLStore.GetAPIKeys(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "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{} | 
					
						
							|  |  |  | 	result := make([]*dtos.ApiKeyDTO, len(query.Result)) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	for i, t := range query.Result { | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +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{ | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 			Id:         t.Id, | 
					
						
							|  |  |  | 			Name:       t.Name, | 
					
						
							|  |  |  | 			Role:       t.Role, | 
					
						
							|  |  |  | 			Expiration: expiration, | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-19 03:23:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-29 21:30:24 +08:00
										 |  |  | 	metadata := hs.getMultiAccessControlMetadata(c, c.OrgId, "apikeys:id", ids) | 
					
						
							|  |  |  | 	if len(metadata) > 0 { | 
					
						
							|  |  |  | 		for _, key := range result { | 
					
						
							|  |  |  | 			key.AccessControl = metadata[strconv.FormatInt(key.Id, 10)] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, result) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | // DeleteAPIKey deletes an API key
 | 
					
						
							| 
									
										
										
										
											2022-02-07 21:14:54 +08:00
										 |  |  | func (hs *HTTPServer) DeleteAPIKey(c *models.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	cmd := &models.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2022-02-07 21:14:54 +08:00
										 |  |  | 	err = hs.SQLStore.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 | 
					
						
							|  |  |  | 		if errors.Is(err, models.ErrApiKeyNotFound) { | 
					
						
							|  |  |  | 			status = 404 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			status = 500 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | // AddAPIKey adds an API key
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) AddAPIKey(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := models.AddApiKeyCommand{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	if !cmd.Role.IsValid() { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(400, "Invalid role specified", nil) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-04-14 00:11:03 +08: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 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	if hs.Cfg.ApiKeyMaxSecondsToLive != -1 { | 
					
						
							|  |  |  | 		if cmd.SecondsToLive == 0 { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(400, "Number of seconds before expiration should be set", nil) | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(400, "Number of seconds before expiration is greater than the global limit", nil) | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-02-07 21:51:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	newKeyInfo, err := apikeygen.New(cmd.OrgId, cmd.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Generating API key failed", err) | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	cmd.Key = newKeyInfo.HashedKey | 
					
						
							| 
									
										
										
										
											2022-02-07 21:14:54 +08:00
										 |  |  | 	if err := hs.SQLStore.AddAPIKey(c.Req.Context(), &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrInvalidApiKeyExpiration) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(400, err.Error(), nil) | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-11-19 20:34:28 +08:00
										 |  |  | 		if errors.Is(err, models.ErrDuplicateApiKey) { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 			return response.Error(409, err.Error(), nil) | 
					
						
							| 
									
										
										
										
											2019-07-11 16:20:34 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to add API Key", err) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	result := &dtos.NewApiKeyResult{ | 
					
						
							| 
									
										
										
										
											2020-09-07 23:06:11 +08:00
										 |  |  | 		ID:   cmd.Result.Id, | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 		Name: cmd.Result.Name, | 
					
						
							| 
									
										
										
										
											2020-09-07 23:06:11 +08:00
										 |  |  | 		Key:  newKeyInfo.ClientSecret, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, result) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } |