mirror of
https://github.com/grafana/grafana.git
synced 2025-09-23 01:43:36 +08:00
Chore: Remove Result field from serviceaccounts, ualert (#62476)
* Chore: Remove Result field from serviceaccounts * Chore: Remove Result field from ualert
This commit is contained in:
@ -47,7 +47,7 @@ type service interface {
|
|||||||
MigrateApiKey(ctx context.Context, orgID int64, keyId int64) error
|
MigrateApiKey(ctx context.Context, orgID int64, keyId int64) error
|
||||||
RevertApiKey(ctx context.Context, saId int64, keyId int64) error
|
RevertApiKey(ctx context.Context, saId int64, keyId int64) error
|
||||||
// Service account tokens
|
// Service account tokens
|
||||||
AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) error
|
AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) (*apikey.APIKey, error)
|
||||||
DeleteServiceAccountToken(ctx context.Context, orgID, serviceAccountID, tokenID int64) error
|
DeleteServiceAccountToken(ctx context.Context, orgID, serviceAccountID, tokenID int64) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ var _ service = new(fakeService)
|
|||||||
type fakeService struct {
|
type fakeService struct {
|
||||||
service
|
service
|
||||||
ExpectedErr error
|
ExpectedErr error
|
||||||
ExpectedApiKey *apikey.APIKey
|
ExpectedAPIKey *apikey.APIKey
|
||||||
ExpectedServiceAccountTokens []apikey.APIKey
|
ExpectedServiceAccountTokens []apikey.APIKey
|
||||||
ExpectedServiceAccount *serviceaccounts.ServiceAccountDTO
|
ExpectedServiceAccount *serviceaccounts.ServiceAccountDTO
|
||||||
ExpectedServiceAccountProfile *serviceaccounts.ServiceAccountProfileDTO
|
ExpectedServiceAccountProfile *serviceaccounts.ServiceAccountProfileDTO
|
||||||
@ -286,9 +286,8 @@ func (f *fakeService) UpdateServiceAccount(ctx context.Context, orgID, id int64,
|
|||||||
return f.ExpectedServiceAccountProfile, f.ExpectedErr
|
return f.ExpectedServiceAccountProfile, f.ExpectedErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeService) AddServiceAccountToken(ctx context.Context, id int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) error {
|
func (f *fakeService) AddServiceAccountToken(ctx context.Context, id int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) (*apikey.APIKey, error) {
|
||||||
cmd.Result = f.ExpectedApiKey
|
return f.ExpectedAPIKey, f.ExpectedErr
|
||||||
return f.ExpectedErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeService) DeleteServiceAccountToken(ctx context.Context, orgID, id, tokenID int64) error {
|
func (f *fakeService) DeleteServiceAccountToken(ctx context.Context, orgID, id, tokenID int64) error {
|
||||||
|
@ -175,7 +175,8 @@ func (api *ServiceAccountsAPI) CreateToken(c *contextmodel.ReqContext) response.
|
|||||||
|
|
||||||
cmd.Key = newKeyInfo.HashedKey
|
cmd.Key = newKeyInfo.HashedKey
|
||||||
|
|
||||||
if err := api.service.AddServiceAccountToken(c.Req.Context(), saID, &cmd); err != nil {
|
apiKey, err := api.service.AddServiceAccountToken(c.Req.Context(), saID, &cmd)
|
||||||
|
if err != nil {
|
||||||
if errors.Is(err, database.ErrInvalidTokenExpiration) {
|
if errors.Is(err, database.ErrInvalidTokenExpiration) {
|
||||||
return response.Error(http.StatusBadRequest, err.Error(), nil)
|
return response.Error(http.StatusBadRequest, err.Error(), nil)
|
||||||
}
|
}
|
||||||
@ -186,8 +187,8 @@ func (api *ServiceAccountsAPI) CreateToken(c *contextmodel.ReqContext) response.
|
|||||||
}
|
}
|
||||||
|
|
||||||
result := &dtos.NewApiKeyResult{
|
result := &dtos.NewApiKeyResult{
|
||||||
ID: cmd.Result.Id,
|
ID: apiKey.Id,
|
||||||
Name: cmd.Result.Name,
|
Name: apiKey.Name,
|
||||||
Key: newKeyInfo.ClientSecret,
|
Key: newKeyInfo.ClientSecret,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ func TestServiceAccountsAPI_CreateToken(t *testing.T) {
|
|||||||
permissions []accesscontrol.Permission
|
permissions []accesscontrol.Permission
|
||||||
tokenTTL int64
|
tokenTTL int64
|
||||||
expectedErr error
|
expectedErr error
|
||||||
expectedApiKey *apikey.APIKey
|
expectedAPIKey *apikey.APIKey
|
||||||
expectedCode int
|
expectedCode int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ func TestServiceAccountsAPI_CreateToken(t *testing.T) {
|
|||||||
body: `{"name": "test"}`,
|
body: `{"name": "test"}`,
|
||||||
tokenTTL: -1,
|
tokenTTL: -1,
|
||||||
permissions: []accesscontrol.Permission{{Action: serviceaccounts.ActionWrite, Scope: "serviceaccounts:id:1"}},
|
permissions: []accesscontrol.Permission{{Action: serviceaccounts.ActionWrite, Scope: "serviceaccounts:id:1"}},
|
||||||
expectedApiKey: &apikey.APIKey{},
|
expectedAPIKey: &apikey.APIKey{},
|
||||||
expectedCode: http.StatusOK,
|
expectedCode: http.StatusOK,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -111,7 +111,7 @@ func TestServiceAccountsAPI_CreateToken(t *testing.T) {
|
|||||||
a.cfg.ApiKeyMaxSecondsToLive = tt.tokenTTL
|
a.cfg.ApiKeyMaxSecondsToLive = tt.tokenTTL
|
||||||
a.service = &fakeService{
|
a.service = &fakeService{
|
||||||
ExpectedErr: tt.expectedErr,
|
ExpectedErr: tt.expectedErr,
|
||||||
ExpectedApiKey: tt.expectedApiKey,
|
ExpectedAPIKey: tt.expectedAPIKey,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req := server.NewRequest(http.MethodPost, fmt.Sprintf("/api/serviceaccounts/%d/tokens", tt.id), strings.NewReader(tt.body))
|
req := server.NewRequest(http.MethodPost, fmt.Sprintf("/api/serviceaccounts/%d/tokens", tt.id), strings.NewReader(tt.body))
|
||||||
|
@ -28,7 +28,7 @@ func TestStore_UsageStats(t *testing.T) {
|
|||||||
SecondsToLive: 0,
|
SecondsToLive: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
_, err = store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
stats, err := store.GetUsageMetrics(context.Background())
|
stats, err := store.GetUsageMetrics(context.Background())
|
||||||
|
@ -38,8 +38,10 @@ func (s *ServiceAccountsStoreImpl) ListTokens(
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceAccountsStoreImpl) AddServiceAccountToken(ctx context.Context, serviceAccountId int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) error {
|
func (s *ServiceAccountsStoreImpl) AddServiceAccountToken(ctx context.Context, serviceAccountId int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) (*apikey.APIKey, error) {
|
||||||
return s.sqlStore.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
var apiKey *apikey.APIKey
|
||||||
|
|
||||||
|
return apiKey, s.sqlStore.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||||
if _, err := s.RetrieveServiceAccount(ctx, cmd.OrgId, serviceAccountId); err != nil {
|
if _, err := s.RetrieveServiceAccount(ctx, cmd.OrgId, serviceAccountId); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -64,7 +66,7 @@ func (s *ServiceAccountsStoreImpl) AddServiceAccountToken(ctx context.Context, s
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Result = addKeyCmd.Result
|
apiKey = addKeyCmd.Result
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/apikeygen"
|
"github.com/grafana/grafana/pkg/components/apikeygen"
|
||||||
"github.com/grafana/grafana/pkg/services/apikey"
|
|
||||||
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||||||
"github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
|
"github.com/grafana/grafana/pkg/services/serviceaccounts/tests"
|
||||||
)
|
)
|
||||||
@ -35,17 +34,15 @@ func TestStore_AddServiceAccountToken(t *testing.T) {
|
|||||||
OrgId: user.OrgID,
|
OrgId: user.OrgID,
|
||||||
Key: key.HashedKey,
|
Key: key.HashedKey,
|
||||||
SecondsToLive: tc.secondsToLive,
|
SecondsToLive: tc.secondsToLive,
|
||||||
Result: &apikey.APIKey{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.AddServiceAccountToken(context.Background(), user.ID, &cmd)
|
newKey, err := store.AddServiceAccountToken(context.Background(), user.ID, &cmd)
|
||||||
if tc.secondsToLive < 0 {
|
if tc.secondsToLive < 0 {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
newKey := cmd.Result
|
|
||||||
require.Equal(t, t.Name(), newKey.Name)
|
require.Equal(t, t.Name(), newKey.Name)
|
||||||
|
|
||||||
// Verify against DB
|
// Verify against DB
|
||||||
@ -90,10 +87,9 @@ func TestStore_AddServiceAccountToken_WrongServiceAccount(t *testing.T) {
|
|||||||
OrgId: sa.OrgID,
|
OrgId: sa.OrgID,
|
||||||
Key: key.HashedKey,
|
Key: key.HashedKey,
|
||||||
SecondsToLive: 0,
|
SecondsToLive: 0,
|
||||||
Result: &apikey.APIKey{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.AddServiceAccountToken(context.Background(), sa.ID+1, &cmd)
|
_, err = store.AddServiceAccountToken(context.Background(), sa.ID+1, &cmd)
|
||||||
require.Error(t, err, "It should not be possible to add token to non-existing service account")
|
require.Error(t, err, "It should not be possible to add token to non-existing service account")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,12 +107,10 @@ func TestStore_RevokeServiceAccountToken(t *testing.T) {
|
|||||||
OrgId: sa.OrgID,
|
OrgId: sa.OrgID,
|
||||||
Key: key.HashedKey,
|
Key: key.HashedKey,
|
||||||
SecondsToLive: 0,
|
SecondsToLive: 0,
|
||||||
Result: &apikey.APIKey{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
newKey, err := store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
newKey := cmd.Result
|
|
||||||
|
|
||||||
// Revoke SAT
|
// Revoke SAT
|
||||||
err = store.RevokeServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.Id)
|
err = store.RevokeServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.Id)
|
||||||
@ -153,12 +147,10 @@ func TestStore_DeleteServiceAccountToken(t *testing.T) {
|
|||||||
OrgId: sa.OrgID,
|
OrgId: sa.OrgID,
|
||||||
Key: key.HashedKey,
|
Key: key.HashedKey,
|
||||||
SecondsToLive: 0,
|
SecondsToLive: 0,
|
||||||
Result: &apikey.APIKey{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
newKey, err := store.AddServiceAccountToken(context.Background(), sa.ID, &cmd)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
newKey := cmd.Result
|
|
||||||
|
|
||||||
// Delete key from wrong service account
|
// Delete key from wrong service account
|
||||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID+2, newKey.Id)
|
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID+2, newKey.Id)
|
||||||
|
@ -206,9 +206,9 @@ func (sa *ServiceAccountsService) ListTokens(ctx context.Context, query *service
|
|||||||
return sa.store.ListTokens(ctx, query)
|
return sa.store.ListTokens(ctx, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sa *ServiceAccountsService) AddServiceAccountToken(ctx context.Context, serviceAccountID int64, query *serviceaccounts.AddServiceAccountTokenCommand) error {
|
func (sa *ServiceAccountsService) AddServiceAccountToken(ctx context.Context, serviceAccountID int64, query *serviceaccounts.AddServiceAccountTokenCommand) (*apikey.APIKey, error) {
|
||||||
if err := validServiceAccountID(serviceAccountID); err != nil {
|
if err := validServiceAccountID(serviceAccountID); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
return sa.store.AddServiceAccountToken(ctx, serviceAccountID, query)
|
return sa.store.AddServiceAccountToken(ctx, serviceAccountID, query)
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ type FakeServiceAccountStore struct {
|
|||||||
ExpectedSearchServiceAccountQueryResult *serviceaccounts.SearchOrgServiceAccountsResult
|
ExpectedSearchServiceAccountQueryResult *serviceaccounts.SearchOrgServiceAccountsResult
|
||||||
ExpectedServiceAccountMigrationStatus *serviceaccounts.APIKeysMigrationStatus
|
ExpectedServiceAccountMigrationStatus *serviceaccounts.APIKeysMigrationStatus
|
||||||
ExpectedStats *serviceaccounts.Stats
|
ExpectedStats *serviceaccounts.Stats
|
||||||
ExpectedApiKeys []apikey.APIKey
|
ExpectedAPIKeys []apikey.APIKey
|
||||||
|
ExpectedAPIKey *apikey.APIKey
|
||||||
ExpectedError error
|
ExpectedError error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ func (f *FakeServiceAccountStore) RevertApiKey(ctx context.Context, saId int64,
|
|||||||
|
|
||||||
// ListTokens is a fake listing tokens.
|
// ListTokens is a fake listing tokens.
|
||||||
func (f *FakeServiceAccountStore) ListTokens(ctx context.Context, query *serviceaccounts.GetSATokensQuery) ([]apikey.APIKey, error) {
|
func (f *FakeServiceAccountStore) ListTokens(ctx context.Context, query *serviceaccounts.GetSATokensQuery) ([]apikey.APIKey, error) {
|
||||||
return f.ExpectedApiKeys, f.ExpectedError
|
return f.ExpectedAPIKeys, f.ExpectedError
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevokeServiceAccountToken is a fake revoking a service account token.
|
// RevokeServiceAccountToken is a fake revoking a service account token.
|
||||||
@ -94,8 +95,8 @@ func (f *FakeServiceAccountStore) RevokeServiceAccountToken(ctx context.Context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddServiceAccountToken is a fake adding a service account token.
|
// AddServiceAccountToken is a fake adding a service account token.
|
||||||
func (f *FakeServiceAccountStore) AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) error {
|
func (f *FakeServiceAccountStore) AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) (*apikey.APIKey, error) {
|
||||||
return f.ExpectedError
|
return f.ExpectedAPIKey, f.ExpectedError
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteServiceAccountToken is a fake deleting a service account token.
|
// DeleteServiceAccountToken is a fake deleting a service account token.
|
||||||
|
@ -33,7 +33,7 @@ type store interface {
|
|||||||
RevertApiKey(ctx context.Context, saId int64, keyId int64) error
|
RevertApiKey(ctx context.Context, saId int64, keyId int64) error
|
||||||
ListTokens(ctx context.Context, query *serviceaccounts.GetSATokensQuery) ([]apikey.APIKey, error)
|
ListTokens(ctx context.Context, query *serviceaccounts.GetSATokensQuery) ([]apikey.APIKey, error)
|
||||||
RevokeServiceAccountToken(ctx context.Context, orgId, serviceAccountId, tokenId int64) error
|
RevokeServiceAccountToken(ctx context.Context, orgId, serviceAccountId, tokenId int64) error
|
||||||
AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) error
|
AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *serviceaccounts.AddServiceAccountTokenCommand) (*apikey.APIKey, error)
|
||||||
DeleteServiceAccountToken(ctx context.Context, orgID, serviceAccountID, tokenID int64) error
|
DeleteServiceAccountToken(ctx context.Context, orgID, serviceAccountID, tokenID int64) error
|
||||||
GetUsageMetrics(ctx context.Context) (*serviceaccounts.Stats, error)
|
GetUsageMetrics(ctx context.Context) (*serviceaccounts.Stats, error)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
"github.com/grafana/grafana/pkg/services/apikey"
|
|
||||||
"github.com/grafana/grafana/pkg/services/org"
|
"github.com/grafana/grafana/pkg/services/org"
|
||||||
"github.com/grafana/grafana/pkg/services/user"
|
"github.com/grafana/grafana/pkg/services/user"
|
||||||
)
|
)
|
||||||
@ -76,7 +75,6 @@ type AddServiceAccountTokenCommand struct {
|
|||||||
OrgId int64 `json:"-"`
|
OrgId int64 `json:"-"`
|
||||||
Key string `json:"-"`
|
Key string `json:"-"`
|
||||||
SecondsToLive int64 `json:"secondsToLive"`
|
SecondsToLive int64 `json:"secondsToLive"`
|
||||||
Result *apikey.APIKey `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchOrgServiceAccountsQuery struct {
|
type SearchOrgServiceAccountsQuery struct {
|
||||||
|
@ -2,6 +2,8 @@ package serviceaccounts
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/services/apikey"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -17,5 +19,6 @@ type Service interface {
|
|||||||
RetrieveServiceAccountIdByName(ctx context.Context, orgID int64, name string) (int64, error)
|
RetrieveServiceAccountIdByName(ctx context.Context, orgID int64, name string) (int64, error)
|
||||||
UpdateServiceAccount(ctx context.Context, orgID, serviceAccountID int64,
|
UpdateServiceAccount(ctx context.Context, orgID, serviceAccountID int64,
|
||||||
saForm *UpdateServiceAccountForm) (*ServiceAccountProfileDTO, error)
|
saForm *UpdateServiceAccountForm) (*ServiceAccountProfileDTO, error)
|
||||||
AddServiceAccountToken(ctx context.Context, serviceAccountID int64, cmd *AddServiceAccountTokenCommand) error
|
AddServiceAccountToken(ctx context.Context, serviceAccountID int64,
|
||||||
|
cmd *AddServiceAccountTokenCommand) (*apikey.APIKey, error)
|
||||||
}
|
}
|
||||||
|
@ -87,8 +87,6 @@ type saveFolderCommand struct {
|
|||||||
PluginId string `json:"-"`
|
PluginId string `json:"-"`
|
||||||
FolderId int64 `json:"folderId"`
|
FolderId int64 `json:"folderId"`
|
||||||
IsFolder bool `json:"isFolder"`
|
IsFolder bool `json:"isFolder"`
|
||||||
|
|
||||||
Result *dashboard
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDashboardModel turns the command into the saveable model
|
// GetDashboardModel turns the command into the saveable model
|
||||||
|
Reference in New Issue
Block a user