2015-02-13 08:55:32 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2021-04-28 06:30:09 -05:00
|
|
|
"context"
|
2022-04-29 08:30:24 -05:00
|
|
|
"fmt"
|
2015-02-13 08:55:32 -06:00
|
|
|
"testing"
|
2019-06-26 01:47:03 -05:00
|
|
|
"time"
|
2020-04-29 14:37:21 -05:00
|
|
|
|
2022-05-16 05:45:41 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-04-29 08:30:24 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-04-29 14:37:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-05-16 05:45:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2015-02-13 08:55:32 -06:00
|
|
|
)
|
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationApiKeyDataAccess(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2019-06-26 01:47:03 -05:00
|
|
|
mockTimeNow()
|
|
|
|
defer resetTimeNow()
|
2015-02-13 08:55:32 -06:00
|
|
|
|
2019-06-26 01:47:03 -05:00
|
|
|
t.Run("Testing API Key data access", func(t *testing.T) {
|
2021-11-18 02:50:03 -06:00
|
|
|
ss := InitTestDB(t)
|
2015-02-13 08:55:32 -06:00
|
|
|
|
2019-06-26 01:47:03 -05:00
|
|
|
t.Run("Given saved api key", func(t *testing.T) {
|
|
|
|
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "hello", Key: "asd"}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
2015-02-13 08:55:32 -06:00
|
|
|
|
2019-06-26 01:47:03 -05:00
|
|
|
t.Run("Should be able to get key by name", func(t *testing.T) {
|
|
|
|
query := models.GetApiKeyByNameQuery{KeyName: "hello", OrgId: 1}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.GetApiKeyByName(context.Background(), &query)
|
2015-02-13 08:55:32 -06:00
|
|
|
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, query.Result)
|
2015-02-13 08:55:32 -06:00
|
|
|
})
|
2022-05-23 06:14:38 -05:00
|
|
|
|
|
|
|
t.Run("Should be able to get key by hash", func(t *testing.T) {
|
|
|
|
key, err := ss.GetAPIKeyByHash(context.Background(), cmd.Key)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, key)
|
|
|
|
})
|
2015-02-13 08:55:32 -06:00
|
|
|
})
|
2019-06-26 01:47:03 -05:00
|
|
|
|
|
|
|
t.Run("Add non expiring key", func(t *testing.T) {
|
2022-02-28 04:30:45 -06:00
|
|
|
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "non-expiring", Key: "asd1", SecondsToLive: 0}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
query := models.GetApiKeyByNameQuery{KeyName: "non-expiring", OrgId: 1}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.GetApiKeyByName(context.Background(), &query)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Nil(t, query.Result.Expires)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Add an expiring key", func(t *testing.T) {
|
2020-09-22 09:22:19 -05:00
|
|
|
// expires in one hour
|
2019-06-26 01:47:03 -05:00
|
|
|
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "expiring-in-an-hour", Key: "asd2", SecondsToLive: 3600}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
query := models.GetApiKeyByNameQuery{KeyName: "expiring-in-an-hour", OrgId: 1}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.GetApiKeyByName(context.Background(), &query)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.True(t, *query.Result.Expires >= timeNow().Unix())
|
|
|
|
|
2021-10-11 07:35:31 -05:00
|
|
|
// timeNow() has been called twice since creation; once by AddAPIKey and once by GetApiKeyByName
|
2020-04-29 14:37:21 -05:00
|
|
|
// therefore two seconds should be subtracted by next value returned by timeNow()
|
2019-06-26 01:47:03 -05:00
|
|
|
// that equals the number by which timeSeed has been advanced
|
|
|
|
then := timeNow().Add(-2 * time.Second)
|
|
|
|
expected := then.Add(1 * time.Hour).UTC().Unix()
|
|
|
|
assert.Equal(t, *query.Result.Expires, expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Add a key with negative lifespan", func(t *testing.T) {
|
2020-09-22 09:22:19 -05:00
|
|
|
// expires in one day
|
2019-06-26 01:47:03 -05:00
|
|
|
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "key-with-negative-lifespan", Key: "asd3", SecondsToLive: -3600}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.EqualError(t, err, models.ErrInvalidApiKeyExpiration.Error())
|
|
|
|
|
|
|
|
query := models.GetApiKeyByNameQuery{KeyName: "key-with-negative-lifespan", OrgId: 1}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.GetApiKeyByName(context.Background(), &query)
|
2020-11-05 06:07:06 -06:00
|
|
|
assert.EqualError(t, err, "invalid API key")
|
2019-06-26 01:47:03 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Add keys", func(t *testing.T) {
|
2020-09-22 09:22:19 -05:00
|
|
|
// never expires
|
2019-06-26 01:47:03 -05:00
|
|
|
cmd := models.AddApiKeyCommand{OrgId: 1, Name: "key1", Key: "key1", SecondsToLive: 0}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
// expires in 1s
|
2019-06-26 01:47:03 -05:00
|
|
|
cmd = models.AddApiKeyCommand{OrgId: 1, Name: "key2", Key: "key2", SecondsToLive: 1}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
// expires in one hour
|
2019-06-26 01:47:03 -05:00
|
|
|
cmd = models.AddApiKeyCommand{OrgId: 1, Name: "key3", Key: "key3", SecondsToLive: 3600}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.AddAPIKey(context.Background(), &cmd)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// advance mocked getTime by 1s
|
|
|
|
timeNow()
|
|
|
|
|
2022-05-16 05:45:41 -05:00
|
|
|
testUser := &models.SignedInUser{
|
|
|
|
OrgId: 1,
|
|
|
|
Permissions: map[int64]map[string][]string{
|
|
|
|
1: {accesscontrol.ActionAPIKeyRead: []string{accesscontrol.ScopeAPIKeysAll}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
query := models.GetApiKeysQuery{OrgId: 1, IncludeExpired: false, User: testUser}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.GetAPIKeys(context.Background(), &query)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for _, k := range query.Result {
|
|
|
|
if k.Name == "key2" {
|
|
|
|
t.Fatalf("key2 should not be there")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 05:45:41 -05:00
|
|
|
query = models.GetApiKeysQuery{OrgId: 1, IncludeExpired: true, User: testUser}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.GetAPIKeys(context.Background(), &query)
|
2019-06-26 01:47:03 -05:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, k := range query.Result {
|
|
|
|
if k.Name == "key2" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.True(t, found)
|
|
|
|
})
|
2015-02-13 08:55:32 -06:00
|
|
|
})
|
|
|
|
}
|
2019-07-11 03:20:34 -05:00
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationApiKeyErrors(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2019-07-11 03:20:34 -05:00
|
|
|
mockTimeNow()
|
|
|
|
defer resetTimeNow()
|
|
|
|
|
2021-04-28 06:30:09 -05:00
|
|
|
t.Run("Testing API Key errors", func(t *testing.T) {
|
2021-11-18 02:50:03 -06:00
|
|
|
ss := InitTestDB(t)
|
2019-07-11 03:20:34 -05:00
|
|
|
|
2021-04-28 06:30:09 -05:00
|
|
|
t.Run("Delete non-existing key should return error", func(t *testing.T) {
|
|
|
|
cmd := models.DeleteApiKeyCommand{Id: 1}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.DeleteApiKey(context.Background(), &cmd)
|
2021-04-28 06:30:09 -05:00
|
|
|
|
|
|
|
assert.EqualError(t, err, models.ErrApiKeyNotFound.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Testing API Duplicate Key Errors", func(t *testing.T) {
|
|
|
|
t.Run("Given saved api key", func(t *testing.T) {
|
2019-07-11 03:20:34 -05:00
|
|
|
cmd := models.AddApiKeyCommand{OrgId: 0, Name: "duplicate", Key: "asd"}
|
2021-11-18 02:50:03 -06:00
|
|
|
err := ss.AddAPIKey(context.Background(), &cmd)
|
2021-04-28 06:30:09 -05:00
|
|
|
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"}
|
2021-11-18 02:50:03 -06:00
|
|
|
err = ss.AddAPIKey(context.Background(), &cmd)
|
2021-04-28 06:30:09 -05:00
|
|
|
assert.EqualError(t, err, models.ErrDuplicateApiKey.Error())
|
|
|
|
})
|
2019-07-11 03:20:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-04-29 08:30:24 -05:00
|
|
|
|
|
|
|
type getApiKeysTestCase struct {
|
|
|
|
desc string
|
|
|
|
user *models.SignedInUser
|
|
|
|
expectedNumKeys int
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:03 -05:00
|
|
|
func TestIntegrationSQLStore_GetAPIKeys(t *testing.T) {
|
2022-06-10 10:46:21 -05:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration test")
|
|
|
|
}
|
2022-04-29 08:30:24 -05:00
|
|
|
tests := []getApiKeysTestCase{
|
|
|
|
{
|
|
|
|
desc: "expect all keys for wildcard scope",
|
|
|
|
user: &models.SignedInUser{OrgId: 1, Permissions: map[int64]map[string][]string{
|
|
|
|
1: {"apikeys:read": {"apikeys:*"}},
|
|
|
|
}},
|
|
|
|
expectedNumKeys: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "expect only api keys that user have scopes for",
|
|
|
|
user: &models.SignedInUser{OrgId: 1, Permissions: map[int64]map[string][]string{
|
|
|
|
1: {"apikeys:read": {"apikeys:id:1", "apikeys:id:3"}},
|
|
|
|
}},
|
|
|
|
expectedNumKeys: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "expect no keys when user have no scopes",
|
|
|
|
user: &models.SignedInUser{OrgId: 1, Permissions: map[int64]map[string][]string{
|
|
|
|
1: {"apikeys:read": {}},
|
|
|
|
}},
|
|
|
|
expectedNumKeys: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
2022-05-16 05:45:41 -05:00
|
|
|
store := InitTestDB(t, InitTestDBOpt{})
|
2022-04-29 08:30:24 -05:00
|
|
|
seedApiKeys(t, store, 10)
|
|
|
|
|
|
|
|
query := &models.GetApiKeysQuery{OrgId: 1, User: tt.user}
|
|
|
|
err := store.GetAPIKeys(context.Background(), query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, query.Result, tt.expectedNumKeys)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func seedApiKeys(t *testing.T, store *SQLStore, num int) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
err := store.AddAPIKey(context.Background(), &models.AddApiKeyCommand{
|
|
|
|
Name: fmt.Sprintf("key:%d", i),
|
|
|
|
Key: fmt.Sprintf("key:%d", i),
|
|
|
|
OrgId: 1,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|