| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-06-15 01:07:33 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2015-01-16 14:45:37 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:28:16 +08:00
										 |  |  | 	"xorm.io/xorm" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | func (ss *SQLStore) addAPIKeysQueryAndCommandHandlers() { | 
					
						
							| 
									
										
										
										
											2021-12-28 23:08:07 +08:00
										 |  |  | 	bus.AddHandler("sql", ss.GetAPIKeys) | 
					
						
							|  |  |  | 	bus.AddHandler("sql", ss.GetApiKeyById) | 
					
						
							|  |  |  | 	bus.AddHandler("sql", ss.GetApiKeyByName) | 
					
						
							|  |  |  | 	bus.AddHandler("sql", ss.DeleteApiKey) | 
					
						
							|  |  |  | 	bus.AddHandler("sql", ss.AddAPIKey) | 
					
						
							| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | // GetAPIKeys queries the database based
 | 
					
						
							|  |  |  | // on input on GetApiKeysQuery
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | func (ss *SQLStore) GetAPIKeys(ctx context.Context, query *models.GetApiKeysQuery) error { | 
					
						
							|  |  |  | 	return ss.WithDbSession(ctx, func(dbSession *DBSession) error { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | 		var sess *xorm.Session | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if query.IncludeExpired { | 
					
						
							|  |  |  | 			sess = dbSession.Limit(100, 0). | 
					
						
							|  |  |  | 				Where("org_id=?", query.OrgId). | 
					
						
							|  |  |  | 				Asc("name") | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			sess = dbSession.Limit(100, 0). | 
					
						
							|  |  |  | 				Where("org_id=? and ( expires IS NULL or expires >= ?)", query.OrgId, timeNow().Unix()). | 
					
						
							|  |  |  | 				Asc("name") | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | 		query.Result = make([]*models.ApiKey, 0) | 
					
						
							|  |  |  | 		return sess.Find(&query.Result) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:28:16 +08:00
										 |  |  | // GetAPIKeys queries the database based
 | 
					
						
							|  |  |  | // on input on GetApiKeysQuery
 | 
					
						
							|  |  |  | func (ss *SQLStore) GetNonServiceAccountAPIKeys(ctx context.Context) []*models.ApiKey { | 
					
						
							|  |  |  | 	result := make([]*models.ApiKey, 0) | 
					
						
							|  |  |  | 	err := ss.WithDbSession(ctx, func(dbSession *DBSession) error { | 
					
						
							|  |  |  | 		sess := dbSession. //CHECK how many API keys do our clients have?  Can we load them all?
 | 
					
						
							|  |  |  | 					Where("(expires IS NULL OR expires >= ?) AND service_account_id < 1 ", timeNow().Unix()).Asc("name") | 
					
						
							|  |  |  | 		return sess.Find(&result) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		ss.log.Warn("API key not loaded", "err", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | func (ss *SQLStore) DeleteApiKey(ctx context.Context, cmd *models.DeleteApiKeyCommand) error { | 
					
						
							|  |  |  | 	return ss.WithDbSession(ctx, func(sess *DBSession) error { | 
					
						
							| 
									
										
										
										
											2021-03-18 21:27:59 +08:00
										 |  |  | 		return deleteAPIKey(sess, cmd.Id, cmd.OrgId) | 
					
						
							| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 21:27:59 +08:00
										 |  |  | func deleteAPIKey(sess *DBSession, id, orgID int64) error { | 
					
						
							|  |  |  | 	rawSQL := "DELETE FROM api_key WHERE id=? and org_id=?" | 
					
						
							| 
									
										
										
										
											2021-04-28 19:30:09 +08:00
										 |  |  | 	result, err := sess.Exec(rawSQL, id, orgID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	n, err := result.RowsAffected() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} else if n == 0 { | 
					
						
							|  |  |  | 		return models.ErrApiKeyNotFound | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2021-03-18 21:27:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:35:31 +08:00
										 |  |  | // AddAPIKey adds the API key to the database.
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | func (ss *SQLStore) AddAPIKey(ctx context.Context, cmd *models.AddApiKeyCommand) error { | 
					
						
							|  |  |  | 	return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error { | 
					
						
							| 
									
										
										
										
											2019-07-11 16:20:34 +08:00
										 |  |  | 		key := models.ApiKey{OrgId: cmd.OrgId, Name: cmd.Name} | 
					
						
							|  |  |  | 		exists, _ := sess.Get(&key) | 
					
						
							|  |  |  | 		if exists { | 
					
						
							|  |  |  | 			return models.ErrDuplicateApiKey | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		updated := timeNow() | 
					
						
							|  |  |  | 		var expires *int64 = nil | 
					
						
							|  |  |  | 		if cmd.SecondsToLive > 0 { | 
					
						
							|  |  |  | 			v := updated.Add(time.Second * time.Duration(cmd.SecondsToLive)).Unix() | 
					
						
							|  |  |  | 			expires = &v | 
					
						
							|  |  |  | 		} else if cmd.SecondsToLive < 0 { | 
					
						
							|  |  |  | 			return models.ErrInvalidApiKeyExpiration | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-10-20 20:36:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		t := models.ApiKey{ | 
					
						
							| 
									
										
										
										
											2021-10-20 20:36:11 +08:00
										 |  |  | 			OrgId:            cmd.OrgId, | 
					
						
							|  |  |  | 			Name:             cmd.Name, | 
					
						
							|  |  |  | 			Role:             cmd.Role, | 
					
						
							|  |  |  | 			Key:              cmd.Key, | 
					
						
							|  |  |  | 			Created:          updated, | 
					
						
							|  |  |  | 			Updated:          updated, | 
					
						
							|  |  |  | 			Expires:          expires, | 
					
						
							|  |  |  | 			ServiceAccountId: cmd.ServiceAccountId, | 
					
						
							| 
									
										
										
										
											2015-01-14 16:33:34 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if _, err := sess.Insert(&t); err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cmd.Result = &t | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:28:16 +08:00
										 |  |  | // UpdateApikeyServiceAccount sets a service account for an existing API key
 | 
					
						
							|  |  |  | func (ss *SQLStore) UpdateApikeyServiceAccount(ctx context.Context, apikeyId int64, saccountId int64) error { | 
					
						
							|  |  |  | 	return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error { | 
					
						
							|  |  |  | 		key := models.ApiKey{Id: apikeyId} | 
					
						
							|  |  |  | 		exists, err := sess.Get(&key) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			ss.log.Warn("API key not loaded", "err", err) | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !exists { | 
					
						
							|  |  |  | 			ss.log.Warn("API key not found", "err", err) | 
					
						
							|  |  |  | 			return models.ErrApiKeyNotFound | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		key.ServiceAccountId = saccountId | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if _, err := sess.ID(key.Id).Update(&key); err != nil { | 
					
						
							|  |  |  | 			ss.log.Warn("Could not update api key", "err", err) | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | func (ss *SQLStore) GetApiKeyById(ctx context.Context, query *models.GetApiKeyByIdQuery) error { | 
					
						
							|  |  |  | 	return ss.WithDbSession(ctx, func(sess *DBSession) error { | 
					
						
							|  |  |  | 		var apikey models.ApiKey | 
					
						
							|  |  |  | 		has, err := sess.ID(query.ApiKeyId).Get(&apikey) | 
					
						
							| 
									
										
										
										
											2015-04-08 14:59:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} else if !has { | 
					
						
							|  |  |  | 			return models.ErrInvalidApiKey | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-08 14:59:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | 		query.Result = &apikey | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-04-08 14:59:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | func (ss *SQLStore) GetApiKeyByName(ctx context.Context, query *models.GetApiKeyByNameQuery) error { | 
					
						
							|  |  |  | 	return ss.WithDbSession(ctx, func(sess *DBSession) error { | 
					
						
							|  |  |  | 		var apikey models.ApiKey | 
					
						
							|  |  |  | 		has, err := sess.Where("org_id=? AND name=?", query.OrgId, query.KeyName).Get(&apikey) | 
					
						
							| 
									
										
										
										
											2015-01-16 23:15:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} else if !has { | 
					
						
							|  |  |  | 			return models.ErrInvalidApiKey | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-16 23:15:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 16:50:03 +08:00
										 |  |  | 		query.Result = &apikey | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-01-16 23:15:35 +08:00
										 |  |  | } |