mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add context apikey (#41698)
* Add context apikey * Rename DeleteApiKeyCtx to DeleteApiKey
This commit is contained in:
parent
7a3b52783c
commit
f40c1d0808
@ -9,18 +9,18 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandlerCtx("sql", GetAPIKeys)
|
||||
bus.AddHandler("sql", GetApiKeyById)
|
||||
bus.AddHandler("sql", GetApiKeyByName)
|
||||
bus.AddHandlerCtx("sql", DeleteApiKeyCtx)
|
||||
bus.AddHandlerCtx("sql", AddAPIKey)
|
||||
func (ss *SQLStore) addAPIKeysQueryAndCommandHandlers() {
|
||||
bus.AddHandlerCtx("sql", ss.GetAPIKeys)
|
||||
bus.AddHandlerCtx("sql", ss.GetApiKeyById)
|
||||
bus.AddHandlerCtx("sql", ss.GetApiKeyByName)
|
||||
bus.AddHandlerCtx("sql", ss.DeleteApiKey)
|
||||
bus.AddHandlerCtx("sql", ss.AddAPIKey)
|
||||
}
|
||||
|
||||
// GetAPIKeys queries the database based
|
||||
// on input on GetApiKeysQuery
|
||||
func GetAPIKeys(ctx context.Context, query *models.GetApiKeysQuery) error {
|
||||
return withDbSession(ctx, x, func(dbSession *DBSession) error {
|
||||
func (ss *SQLStore) GetAPIKeys(ctx context.Context, query *models.GetApiKeysQuery) error {
|
||||
return ss.WithDbSession(ctx, func(dbSession *DBSession) error {
|
||||
var sess *xorm.Session
|
||||
|
||||
if query.IncludeExpired {
|
||||
@ -38,8 +38,8 @@ func GetAPIKeys(ctx context.Context, query *models.GetApiKeysQuery) error {
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteApiKeyCtx(ctx context.Context, cmd *models.DeleteApiKeyCommand) error {
|
||||
return withDbSession(ctx, x, func(sess *DBSession) error {
|
||||
func (ss *SQLStore) DeleteApiKey(ctx context.Context, cmd *models.DeleteApiKeyCommand) error {
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
return deleteAPIKey(sess, cmd.Id, cmd.OrgId)
|
||||
})
|
||||
}
|
||||
@ -60,8 +60,8 @@ func deleteAPIKey(sess *DBSession, id, orgID int64) error {
|
||||
}
|
||||
|
||||
// AddAPIKey adds the API key to the database.
|
||||
func AddAPIKey(ctx context.Context, cmd *models.AddApiKeyCommand) error {
|
||||
return inTransactionCtx(ctx, func(sess *DBSession) error {
|
||||
func (ss *SQLStore) AddAPIKey(ctx context.Context, cmd *models.AddApiKeyCommand) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
key := models.ApiKey{OrgId: cmd.OrgId, Name: cmd.Name}
|
||||
exists, _ := sess.Get(&key)
|
||||
if exists {
|
||||
@ -96,30 +96,34 @@ func AddAPIKey(ctx context.Context, cmd *models.AddApiKeyCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func GetApiKeyById(query *models.GetApiKeyByIdQuery) error {
|
||||
var apikey models.ApiKey
|
||||
has, err := x.Id(query.ApiKeyId).Get(&apikey)
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrInvalidApiKey
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrInvalidApiKey
|
||||
}
|
||||
|
||||
query.Result = &apikey
|
||||
return nil
|
||||
query.Result = &apikey
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func GetApiKeyByName(query *models.GetApiKeyByNameQuery) error {
|
||||
var apikey models.ApiKey
|
||||
has, err := x.Where("org_id=? AND name=?", query.OrgId, query.KeyName).Get(&apikey)
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrInvalidApiKey
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrInvalidApiKey
|
||||
}
|
||||
|
||||
query.Result = &apikey
|
||||
return nil
|
||||
query.Result = &apikey
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -17,16 +17,16 @@ func TestApiKeyDataAccess(t *testing.T) {
|
||||
defer resetTimeNow()
|
||||
|
||||
t.Run("Testing API Key data access", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
ss := InitTestDB(t)
|
||||
|
||||
t.Run("Given saved api key", func(t *testing.T) {
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "hello", Key: "asd"}
|
||||
err := AddAPIKey(context.Background(), &cmd)
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Should be able to get key by name", func(t *testing.T) {
|
||||
query := models.GetApiKeyByNameQuery{KeyName: "hello", OrgId: 1}
|
||||
err = GetApiKeyByName(&query)
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, query.Result)
|
||||
@ -35,11 +35,11 @@ func TestApiKeyDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Add non expiring key", func(t *testing.T) {
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "non-expiring", Key: "asd1", SecondsToLive: 0}
|
||||
err := AddAPIKey(context.Background(), &cmd)
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
query := models.GetApiKeyByNameQuery{KeyName: "non-expiring", OrgId: 1}
|
||||
err = GetApiKeyByName(&query)
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Nil(t, query.Result.Expires)
|
||||
@ -48,11 +48,11 @@ func TestApiKeyDataAccess(t *testing.T) {
|
||||
t.Run("Add an expiring key", func(t *testing.T) {
|
||||
// expires in one hour
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "expiring-in-an-hour", Key: "asd2", SecondsToLive: 3600}
|
||||
err := AddAPIKey(context.Background(), &cmd)
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
query := models.GetApiKeyByNameQuery{KeyName: "expiring-in-an-hour", OrgId: 1}
|
||||
err = GetApiKeyByName(&query)
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.True(t, *query.Result.Expires >= timeNow().Unix())
|
||||
@ -68,35 +68,35 @@ func TestApiKeyDataAccess(t *testing.T) {
|
||||
t.Run("Add a key with negative lifespan", func(t *testing.T) {
|
||||
// expires in one day
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "key-with-negative-lifespan", Key: "asd3", SecondsToLive: -3600}
|
||||
err := AddAPIKey(context.Background(), &cmd)
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.EqualError(t, err, models.ErrInvalidApiKeyExpiration.Error())
|
||||
|
||||
query := models.GetApiKeyByNameQuery{KeyName: "key-with-negative-lifespan", OrgId: 1}
|
||||
err = GetApiKeyByName(&query)
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.EqualError(t, err, "invalid API key")
|
||||
})
|
||||
|
||||
t.Run("Add keys", func(t *testing.T) {
|
||||
// never expires
|
||||
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "key1", Key: "key1", SecondsToLive: 0}
|
||||
err := AddAPIKey(context.Background(), &cmd)
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// expires in 1s
|
||||
cmd = models.AddApiKeyCommand{OrgId: 1, Name: "key2", Key: "key2", SecondsToLive: 1}
|
||||
err = AddAPIKey(context.Background(), &cmd)
|
||||
err = ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// expires in one hour
|
||||
cmd = models.AddApiKeyCommand{OrgId: 1, Name: "key3", Key: "key3", SecondsToLive: 3600}
|
||||
err = AddAPIKey(context.Background(), &cmd)
|
||||
err = ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// advance mocked getTime by 1s
|
||||
timeNow()
|
||||
|
||||
query := models.GetApiKeysQuery{OrgId: 1, IncludeExpired: false}
|
||||
err = GetAPIKeys(context.Background(), &query)
|
||||
err = ss.GetAPIKeys(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
for _, k := range query.Result {
|
||||
@ -106,7 +106,7 @@ func TestApiKeyDataAccess(t *testing.T) {
|
||||
}
|
||||
|
||||
query = models.GetApiKeysQuery{OrgId: 1, IncludeExpired: true}
|
||||
err = GetAPIKeys(context.Background(), &query)
|
||||
err = ss.GetAPIKeys(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
found := false
|
||||
@ -125,11 +125,11 @@ func TestApiKeyErrors(t *testing.T) {
|
||||
defer resetTimeNow()
|
||||
|
||||
t.Run("Testing API Key errors", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
ss := InitTestDB(t)
|
||||
|
||||
t.Run("Delete non-existing key should return error", func(t *testing.T) {
|
||||
cmd := models.DeleteApiKeyCommand{Id: 1}
|
||||
err := DeleteApiKeyCtx(context.Background(), &cmd)
|
||||
err := ss.DeleteApiKey(context.Background(), &cmd)
|
||||
|
||||
assert.EqualError(t, err, models.ErrApiKeyNotFound.Error())
|
||||
})
|
||||
@ -137,12 +137,12 @@ func TestApiKeyErrors(t *testing.T) {
|
||||
t.Run("Testing API Duplicate Key Errors", func(t *testing.T) {
|
||||
t.Run("Given saved api key", func(t *testing.T) {
|
||||
cmd := models.AddApiKeyCommand{OrgId: 0, Name: "duplicate", Key: "asd"}
|
||||
err := AddAPIKey(context.Background(), &cmd)
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Add API Key with existing Org ID and Name", func(t *testing.T) {
|
||||
cmd := models.AddApiKeyCommand{OrgId: 0, Name: "duplicate", Key: "asd"}
|
||||
err = AddAPIKey(context.Background(), &cmd)
|
||||
err = ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.EqualError(t, err, models.ErrDuplicateApiKey.Error())
|
||||
})
|
||||
})
|
||||
|
@ -120,6 +120,7 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, bus bu
|
||||
ss.addAlertQueryAndCommandHandlers()
|
||||
ss.addTempUserQueryAndCommandHandlers()
|
||||
ss.addDashboardVersionQueryAndCommandHandlers()
|
||||
ss.addAPIKeysQueryAndCommandHandlers()
|
||||
|
||||
// if err := ss.Reset(); err != nil {
|
||||
// return nil, err
|
||||
|
@ -19,7 +19,7 @@ func TestTransaction(t *testing.T) {
|
||||
|
||||
cmd := &models.AddApiKeyCommand{Key: "secret-key", Name: "key", OrgId: 1}
|
||||
t.Run("can update key", func(t *testing.T) {
|
||||
err := AddAPIKey(context.Background(), cmd)
|
||||
err := ss.AddAPIKey(context.Background(), cmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
err = ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
|
||||
@ -29,12 +29,12 @@ func TestTransaction(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
|
||||
query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
|
||||
err = GetApiKeyById(query)
|
||||
err = ss.GetApiKeyById(context.Background(), query)
|
||||
require.Equal(t, models.ErrInvalidApiKey, err)
|
||||
})
|
||||
|
||||
t.Run("won't update if one handler fails", func(t *testing.T) {
|
||||
err := AddAPIKey(context.Background(), cmd)
|
||||
err := ss.AddAPIKey(context.Background(), cmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
err = ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
|
||||
@ -49,7 +49,7 @@ func TestTransaction(t *testing.T) {
|
||||
require.Equal(t, ErrProvokedError, err)
|
||||
|
||||
query := &models.GetApiKeyByIdQuery{ApiKeyId: cmd.Result.Id}
|
||||
err = GetApiKeyById(query)
|
||||
err = ss.GetApiKeyById(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, cmd.Result.Id, query.Result.Id)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user