| 
									
										
										
										
											2022-05-23 19:13:55 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2022-08-30 02:44:55 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-05-23 19:13:55 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											2022-05-23 19:13:55 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) AdminRotateDataEncryptionKeys(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-05-23 19:13:55 +08:00
										 |  |  | 	if err := hs.SecretsService.RotateDataKeys(c.Req.Context()); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to rotate data keys", err) | 
					
						
							| 
									
										
										
										
											2022-05-23 19:13:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.Respond(http.StatusNoContent, "") | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) AdminReEncryptEncryptionKeys(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 	if err := hs.SecretsService.ReEncryptDataKeys(c.Req.Context()); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to re-encrypt data keys", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.Respond(http.StatusOK, "Data encryption keys re-encrypted successfully") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) AdminReEncryptSecrets(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 	success, err := hs.secretsMigrator.ReEncryptSecrets(c.Req.Context()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to re-encrypt secrets", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !success { | 
					
						
							| 
									
										
										
										
											2023-06-16 23:46:47 +08:00
										 |  |  | 		return response.Error(http.StatusPartialContent, fmt.Sprintf("Something unexpected happened - %s", hs.Cfg.UserFacingDefaultError), err) | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.Respond(http.StatusOK, "Secrets re-encrypted successfully") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-27 15:50:36 +08:00
										 |  |  | func (hs *HTTPServer) AdminRollbackSecrets(c *contextmodel.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 	success, err := hs.secretsMigrator.RollBackSecrets(c.Req.Context()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to rollback secrets", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !success { | 
					
						
							| 
									
										
										
										
											2023-06-16 23:46:47 +08:00
										 |  |  | 		return response.Error(http.StatusPartialContent, fmt.Sprintf("Something unexpected happened - %s", hs.Cfg.UserFacingDefaultError), err) | 
					
						
							| 
									
										
										
										
											2022-07-18 14:57:58 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.Respond(http.StatusOK, "Secrets rolled back successfully") | 
					
						
							|  |  |  | } |