Chore: Move api key models into apikey service package (#53241)

* Chore: move api key models into apikey service package

* force table name for api key
This commit is contained in:
Serge Zaitsev
2022-08-04 14:19:09 +02:00
committed by GitHub
parent af83a09a92
commit 191ab3bb01
21 changed files with 203 additions and 230 deletions
+3 -2
View File
@@ -10,6 +10,7 @@ import (
"github.com/grafana/grafana/pkg/api/response"
apikeygenprefix "github.com/grafana/grafana/pkg/components/apikeygenprefixed"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/apikey"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/services/serviceaccounts/database"
"github.com/grafana/grafana/pkg/web"
@@ -215,10 +216,10 @@ func (api *ServiceAccountsAPI) DeleteToken(c *models.ReqContext) response.Respon
if err = api.store.DeleteServiceAccountToken(c.Req.Context(), c.OrgId, saID, tokenID); err != nil {
status := http.StatusNotFound
if err != nil && !errors.Is(err, models.ErrApiKeyNotFound) {
if err != nil && !errors.Is(err, apikey.ErrNotFound) {
status = http.StatusInternalServerError
} else {
err = models.ErrApiKeyNotFound
err = apikey.ErrNotFound
}
return response.Error(status, failedToDeleteMsg, err)
+11 -10
View File
@@ -18,6 +18,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
"github.com/grafana/grafana/pkg/services/apikey"
"github.com/grafana/grafana/pkg/services/apikey/apikeyimpl"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/services/serviceaccounts/database"
@@ -33,7 +34,7 @@ const (
serviceaccountIDTokensDetailPath = "/api/serviceaccounts/%v/tokens/%v" // #nosec G101
)
func createTokenforSA(t *testing.T, store serviceaccounts.Store, keyName string, orgID int64, saID int64, secondsToLive int64) *models.ApiKey {
func createTokenforSA(t *testing.T, store serviceaccounts.Store, keyName string, orgID int64, saID int64, secondsToLive int64) *apikey.APIKey {
key, err := apikeygen.New(orgID, keyName)
require.NoError(t, err)
@@ -42,7 +43,7 @@ func createTokenforSA(t *testing.T, store serviceaccounts.Store, keyName string,
OrgId: orgID,
Key: key.HashedKey,
SecondsToLive: secondsToLive,
Result: &models.ApiKey{},
Result: &apikey.APIKey{},
}
err = store.AddServiceAccountToken(context.Background(), saID, &cmd)
@@ -148,7 +149,7 @@ func TestServiceAccountsAPI_CreateToken(t *testing.T) {
if actualCode == http.StatusOK {
assert.Equal(t, tc.body["name"], actualBody["name"])
query := models.GetApiKeyByNameQuery{KeyName: tc.body["name"].(string), OrgId: sa.OrgID}
query := apikey.GetByNameQuery{KeyName: tc.body["name"].(string), OrgId: sa.OrgID}
err = apiKeyService.GetApiKeyByName(context.Background(), &query)
require.NoError(t, err)
@@ -245,7 +246,7 @@ func TestServiceAccountsAPI_DeleteToken(t *testing.T) {
_ = json.Unmarshal(actual.Body.Bytes(), &actualBody)
require.Equal(t, tc.expectedCode, actualCode, endpoint, actualBody)
query := models.GetApiKeyByNameQuery{KeyName: tc.keyName, OrgId: sa.OrgID}
query := apikey.GetByNameQuery{KeyName: tc.keyName, OrgId: sa.OrgID}
err := apiKeyService.GetApiKeyByName(context.Background(), &query)
if actualCode == http.StatusOK {
require.Error(t, err)
@@ -258,10 +259,10 @@ func TestServiceAccountsAPI_DeleteToken(t *testing.T) {
type saStoreMockTokens struct {
serviceaccounts.Store
saAPIKeys []*models.ApiKey
saAPIKeys []*apikey.APIKey
}
func (s *saStoreMockTokens) ListTokens(ctx context.Context, orgID, saID int64) ([]*models.ApiKey, error) {
func (s *saStoreMockTokens) ListTokens(ctx context.Context, orgID, saID int64) ([]*apikey.APIKey, error) {
return s.saAPIKeys, nil
}
@@ -272,7 +273,7 @@ func TestServiceAccountsAPI_ListTokens(t *testing.T) {
type testCreateSAToken struct {
desc string
tokens []*models.ApiKey
tokens []*apikey.APIKey
expectedHasExpired bool
expectedResponseBodyField string
expectedCode int
@@ -286,7 +287,7 @@ func TestServiceAccountsAPI_ListTokens(t *testing.T) {
testCases := []testCreateSAToken{
{
desc: "should be able to list serviceaccount with no expiration date",
tokens: []*models.ApiKey{{
tokens: []*apikey.APIKey{{
Id: 1,
OrgId: 1,
ServiceAccountId: &saId,
@@ -306,7 +307,7 @@ func TestServiceAccountsAPI_ListTokens(t *testing.T) {
},
{
desc: "should be able to list serviceaccount with secondsUntilExpiration",
tokens: []*models.ApiKey{{
tokens: []*apikey.APIKey{{
Id: 1,
OrgId: 1,
ServiceAccountId: &saId,
@@ -326,7 +327,7 @@ func TestServiceAccountsAPI_ListTokens(t *testing.T) {
},
{
desc: "should be able to list serviceaccount with expired token",
tokens: []*models.ApiKey{{
tokens: []*apikey.APIKey{{
Id: 1,
OrgId: 1,
ServiceAccountId: &saId,