mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Rename Id to ID for annotation models (#62886)
* Rename Id to ID for annotation models * Add xorm tags * Rename Id to ID for API key models * Add xorm tags
This commit is contained in:
@@ -423,7 +423,7 @@ func (s *ServiceAccountsStoreImpl) MigrateApiKeysToServiceAccounts(ctx context.C
|
||||
s.log.Error("migating to service accounts failed with error", err)
|
||||
return err
|
||||
}
|
||||
s.log.Debug("API key converted to service account token", "keyId", key.Id)
|
||||
s.log.Debug("API key converted to service account token", "keyId", key.ID)
|
||||
}
|
||||
}
|
||||
if err := s.kvStore.Set(ctx, orgId, "serviceaccounts", "migrationStatus", "1"); err != nil {
|
||||
@@ -441,7 +441,7 @@ func (s *ServiceAccountsStoreImpl) MigrateApiKey(ctx context.Context, orgId int6
|
||||
return fmt.Errorf("no API keys to convert found")
|
||||
}
|
||||
for _, key := range basicKeys {
|
||||
if keyId == key.Id {
|
||||
if keyId == key.ID {
|
||||
err := s.CreateServiceAccountFromApikey(ctx, key)
|
||||
if err != nil {
|
||||
s.log.Error("converting to service account failed with error", "keyId", keyId, "error", err)
|
||||
@@ -455,9 +455,9 @@ func (s *ServiceAccountsStoreImpl) MigrateApiKey(ctx context.Context, orgId int6
|
||||
func (s *ServiceAccountsStoreImpl) CreateServiceAccountFromApikey(ctx context.Context, key *apikey.APIKey) error {
|
||||
prefix := "sa-autogen"
|
||||
cmd := user.CreateUserCommand{
|
||||
Login: fmt.Sprintf("%v-%v-%v", prefix, key.OrgId, key.Name),
|
||||
Login: fmt.Sprintf("%v-%v-%v", prefix, key.OrgID, key.Name),
|
||||
Name: fmt.Sprintf("%v-%v", prefix, key.Name),
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
DefaultOrgRole: string(key.Role),
|
||||
IsServiceAccount: true,
|
||||
}
|
||||
@@ -468,7 +468,7 @@ func (s *ServiceAccountsStoreImpl) CreateServiceAccountFromApikey(ctx context.Co
|
||||
return fmt.Errorf("failed to create service account: %w", errCreateSA)
|
||||
}
|
||||
|
||||
if err := s.assignApiKeyToServiceAccount(sess, key.Id, newSA.ID); err != nil {
|
||||
if err := s.assignApiKeyToServiceAccount(sess, key.ID, newSA.ID); err != nil {
|
||||
return fmt.Errorf("failed to migrate API key to service account token: %w", err)
|
||||
}
|
||||
|
||||
@@ -478,7 +478,7 @@ func (s *ServiceAccountsStoreImpl) CreateServiceAccountFromApikey(ctx context.Co
|
||||
|
||||
// RevertApiKey converts service account token to old API key
|
||||
func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64, keyId int64) error {
|
||||
query := apikey.GetByIDQuery{ApiKeyId: keyId}
|
||||
query := apikey.GetByIDQuery{ApiKeyID: keyId}
|
||||
if err := s.apiKeyService.GetApiKeyById(ctx, &query); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -493,7 +493,7 @@ func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64,
|
||||
}
|
||||
|
||||
tokens, err := s.ListTokens(ctx, &serviceaccounts.GetSATokensQuery{
|
||||
OrgID: &key.OrgId,
|
||||
OrgID: &key.OrgID,
|
||||
ServiceAccountID: key.ServiceAccountId,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -506,7 +506,7 @@ func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64,
|
||||
err = s.sqlStore.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||
user := user.User{}
|
||||
has, err := sess.Where(`org_id = ? and id = ? and is_service_account = ?`,
|
||||
key.OrgId, *key.ServiceAccountId, s.sqlStore.GetDialect().BooleanStr(true)).Get(&user)
|
||||
key.OrgID, *key.ServiceAccountId, s.sqlStore.GetDialect().BooleanStr(true)).Get(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -514,11 +514,11 @@ func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64,
|
||||
return serviceaccounts.ErrServiceAccountNotFound
|
||||
}
|
||||
// Detach API key from service account
|
||||
if err := s.detachApiKeyFromServiceAccount(sess, key.Id); err != nil {
|
||||
if err := s.detachApiKeyFromServiceAccount(sess, key.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
// Delete service account
|
||||
if err := s.deleteServiceAccount(sess, key.OrgId, *key.ServiceAccountId); err != nil {
|
||||
if err := s.deleteServiceAccount(sess, key.OrgID, *key.ServiceAccountId); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -180,14 +180,14 @@ func TestStore_MigrateApiKeys(t *testing.T) {
|
||||
_, err := store.orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "main"})
|
||||
require.NoError(t, err)
|
||||
key := tests.SetupApiKey(t, db, c.key)
|
||||
err = store.MigrateApiKey(context.Background(), key.OrgId, key.Id)
|
||||
err = store.MigrateApiKey(context.Background(), key.OrgID, key.ID)
|
||||
if c.expectedErr != nil {
|
||||
require.ErrorIs(t, err, c.expectedErr)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
q := serviceaccounts.SearchOrgServiceAccountsQuery{
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
Query: "",
|
||||
Page: 1,
|
||||
Limit: 50,
|
||||
@@ -195,7 +195,7 @@ func TestStore_MigrateApiKeys(t *testing.T) {
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
key.OrgId: {
|
||||
key.OrgID: {
|
||||
"serviceaccounts:read": {"serviceaccounts:id:*"},
|
||||
},
|
||||
},
|
||||
@@ -208,7 +208,7 @@ func TestStore_MigrateApiKeys(t *testing.T) {
|
||||
require.Equal(t, string(key.Role), saMigrated.Role)
|
||||
|
||||
tokens, err := store.ListTokens(context.Background(), &serviceaccounts.GetSATokensQuery{
|
||||
OrgID: &key.OrgId,
|
||||
OrgID: &key.OrgID,
|
||||
ServiceAccountID: &saMigrated.Id,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@@ -350,7 +350,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
saId = rand.Int63()
|
||||
} else {
|
||||
q := serviceaccounts.SearchOrgServiceAccountsQuery{
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
Query: "",
|
||||
Page: 1,
|
||||
Limit: 50,
|
||||
@@ -358,7 +358,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
key.OrgId: {
|
||||
key.OrgID: {
|
||||
"serviceaccounts:read": {"serviceaccounts:id:*"},
|
||||
},
|
||||
},
|
||||
@@ -369,14 +369,14 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
saId = serviceAccounts.ServiceAccounts[0].Id
|
||||
}
|
||||
|
||||
err = store.RevertApiKey(context.Background(), saId, key.Id)
|
||||
err = store.RevertApiKey(context.Background(), saId, key.ID)
|
||||
|
||||
if c.expectedErr != nil {
|
||||
require.ErrorIs(t, err, c.expectedErr)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
q := serviceaccounts.SearchOrgServiceAccountsQuery{
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
Query: "",
|
||||
Page: 1,
|
||||
Limit: 50,
|
||||
@@ -384,7 +384,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
key.OrgId: {
|
||||
key.OrgID: {
|
||||
"serviceaccounts:read": {"serviceaccounts:id:*"},
|
||||
},
|
||||
},
|
||||
@@ -400,7 +400,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
require.Len(t, apiKeys, 1)
|
||||
apiKey := apiKeys[0]
|
||||
require.Equal(t, c.key.Name, apiKey.Name)
|
||||
require.Equal(t, c.key.OrgId, apiKey.OrgId)
|
||||
require.Equal(t, c.key.OrgId, apiKey.OrgID)
|
||||
require.Equal(t, c.key.Role, apiKey.Role)
|
||||
require.Equal(t, key.Key, apiKey.Key)
|
||||
// Api key should not be linked to service account
|
||||
|
||||
@@ -49,7 +49,7 @@ func (s *ServiceAccountsStoreImpl) AddServiceAccountToken(ctx context.Context, s
|
||||
addKeyCmd := &apikey.AddCommand{
|
||||
Name: cmd.Name,
|
||||
Role: org.RoleViewer,
|
||||
OrgId: cmd.OrgId,
|
||||
OrgID: cmd.OrgId,
|
||||
Key: cmd.Key,
|
||||
SecondsToLive: cmd.SecondsToLive,
|
||||
ServiceAccountID: &serviceAccountId,
|
||||
@@ -107,7 +107,7 @@ func (s *ServiceAccountsStoreImpl) RevokeServiceAccountToken(ctx context.Context
|
||||
|
||||
// assignApiKeyToServiceAccount sets the API key service account ID
|
||||
func (s *ServiceAccountsStoreImpl) assignApiKeyToServiceAccount(sess *db.Session, apiKeyId int64, serviceAccountId int64) error {
|
||||
key := apikey.APIKey{Id: apiKeyId}
|
||||
key := apikey.APIKey{ID: apiKeyId}
|
||||
exists, err := sess.Get(&key)
|
||||
if err != nil {
|
||||
s.log.Warn("API key not loaded", "err", err)
|
||||
@@ -119,7 +119,7 @@ func (s *ServiceAccountsStoreImpl) assignApiKeyToServiceAccount(sess *db.Session
|
||||
}
|
||||
key.ServiceAccountId = &serviceAccountId
|
||||
|
||||
if _, err := sess.ID(key.Id).Update(&key); err != nil {
|
||||
if _, err := sess.ID(key.ID).Update(&key); err != nil {
|
||||
s.log.Warn("Could not update api key", "err", err)
|
||||
return err
|
||||
}
|
||||
@@ -129,7 +129,7 @@ func (s *ServiceAccountsStoreImpl) assignApiKeyToServiceAccount(sess *db.Session
|
||||
|
||||
// detachApiKeyFromServiceAccount converts service account token to old API key
|
||||
func (s *ServiceAccountsStoreImpl) detachApiKeyFromServiceAccount(sess *db.Session, apiKeyId int64) error {
|
||||
key := apikey.APIKey{Id: apiKeyId}
|
||||
key := apikey.APIKey{ID: apiKeyId}
|
||||
exists, err := sess.Get(&key)
|
||||
if err != nil {
|
||||
s.log.Warn("Cannot get API key", "err", err)
|
||||
@@ -141,7 +141,7 @@ func (s *ServiceAccountsStoreImpl) detachApiKeyFromServiceAccount(sess *db.Sessi
|
||||
}
|
||||
key.ServiceAccountId = nil
|
||||
|
||||
if _, err := sess.ID(key.Id).AllCols().Update(&key); err != nil {
|
||||
if _, err := sess.ID(key.ID).AllCols().Update(&key); err != nil {
|
||||
s.log.Error("Could not update api key", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ func TestStore_RevokeServiceAccountToken(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Revoke SAT
|
||||
err = store.RevokeServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.Id)
|
||||
err = store.RevokeServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify against DB
|
||||
@@ -153,14 +153,14 @@ func TestStore_DeleteServiceAccountToken(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// 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)
|
||||
require.Error(t, err)
|
||||
|
||||
// Delete key from wrong org
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID+2, sa.ID, newKey.Id)
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID+2, sa.ID, newKey.ID)
|
||||
require.Error(t, err)
|
||||
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.Id)
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify against DB
|
||||
|
||||
Reference in New Issue
Block a user