mirror of
https://github.com/grafana/grafana.git
synced 2026-08-19 01:34:54 -05:00
Previews: create crawler auth setup service (#47349)
* #46968: add `RetrieveServiceAccountIdByName` to serviceaccounts service * #46968: improve error logging in rendering service * #46968: add oss crawler account setup * #46968: fix tests * #46968: switch back to ROLE_ADMIN * #46968: rename to crawlerAuth * comment crawler_auth.go
This commit is contained in:
@@ -231,6 +231,47 @@ func (s *ServiceAccountsStoreImpl) RetrieveServiceAccount(ctx context.Context, o
|
||||
return serviceAccount, nil
|
||||
}
|
||||
|
||||
func (s *ServiceAccountsStoreImpl) RetrieveServiceAccountIdByName(ctx context.Context, orgID int64, name string) (int64, error) {
|
||||
serviceAccount := &struct {
|
||||
Id int64
|
||||
}{}
|
||||
|
||||
err := s.sqlStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
||||
sess := dbSession.Table("user")
|
||||
|
||||
whereConditions := []string{
|
||||
fmt.Sprintf("%s.name = ?",
|
||||
s.sqlStore.Dialect.Quote("user")),
|
||||
fmt.Sprintf("%s.org_id = ?",
|
||||
s.sqlStore.Dialect.Quote("user")),
|
||||
fmt.Sprintf("%s.is_service_account = %s",
|
||||
s.sqlStore.Dialect.Quote("user"),
|
||||
s.sqlStore.Dialect.BooleanStr(true)),
|
||||
}
|
||||
whereParams := []interface{}{name, orgID}
|
||||
|
||||
sess.Where(strings.Join(whereConditions, " AND "), whereParams...)
|
||||
|
||||
sess.Cols(
|
||||
"user.id",
|
||||
)
|
||||
|
||||
if ok, err := sess.Get(serviceAccount); err != nil {
|
||||
return err
|
||||
} else if !ok {
|
||||
return serviceaccounts.ErrServiceAccountNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return serviceAccount.Id, nil
|
||||
}
|
||||
|
||||
func (s *ServiceAccountsStoreImpl) UpdateServiceAccount(ctx context.Context,
|
||||
orgID, serviceAccountID int64,
|
||||
saForm *serviceaccounts.UpdateServiceAccountForm) (*serviceaccounts.ServiceAccountProfileDTO, error) {
|
||||
|
||||
@@ -15,17 +15,24 @@ import (
|
||||
func TestStore_CreateServiceAccount(t *testing.T) {
|
||||
_, store := setupTestDatabase(t)
|
||||
t.Run("create service account", func(t *testing.T) {
|
||||
saDTO, err := store.CreateServiceAccount(context.Background(), 1, "new Service Account")
|
||||
serviceAccountName := "new Service Account"
|
||||
serviceAccountOrgId := int64(1)
|
||||
|
||||
saDTO, err := store.CreateServiceAccount(context.Background(), serviceAccountOrgId, serviceAccountName)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sa-new-service-account", saDTO.Login)
|
||||
assert.Equal(t, "new Service Account", saDTO.Name)
|
||||
assert.Equal(t, serviceAccountName, saDTO.Name)
|
||||
assert.Equal(t, 0, int(saDTO.Tokens))
|
||||
|
||||
retrieved, err := store.RetrieveServiceAccount(context.Background(), 1, saDTO.Id)
|
||||
retrieved, err := store.RetrieveServiceAccount(context.Background(), serviceAccountOrgId, saDTO.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sa-new-service-account", retrieved.Login)
|
||||
assert.Equal(t, "new Service Account", retrieved.Name)
|
||||
assert.Equal(t, 1, int(retrieved.OrgId))
|
||||
assert.Equal(t, serviceAccountName, retrieved.Name)
|
||||
assert.Equal(t, serviceAccountOrgId, retrieved.OrgId)
|
||||
|
||||
retrievedId, err := store.RetrieveServiceAccountIdByName(context.Background(), serviceAccountOrgId, serviceAccountName)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, saDTO.Id, retrievedId)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user