satellite/console: Added error for adding api key with existing name attempt. (#3185)

This commit is contained in:
Bogdan Artemenko 2019-10-10 16:28:35 +03:00 committed by GitHub
parent ca0f749428
commit 5f775b9e46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 0 deletions

View File

@ -20,6 +20,8 @@ type APIKeys interface {
Get(ctx context.Context, id uuid.UUID) (*APIKeyInfo, error)
// GetByHead retrieves APIKeyInfo for given key head
GetByHead(ctx context.Context, head []byte) (*APIKeyInfo, error)
// GetByNameAndProjectID retrieves APIKeyInfo for given key name and projectID
GetByNameAndProjectID(ctx context.Context, name string, projectID uuid.UUID) (*APIKeyInfo, error)
// Create creates and stores new APIKeyInfo
Create(ctx context.Context, head []byte, info APIKeyInfo) (*APIKeyInfo, error)
// Update updates APIKeyInfo in store

View File

@ -44,6 +44,7 @@ const (
oldPassIncorrectErrMsg = "Old password is incorrect, please try again"
passwordIncorrectErrMsg = "Your password needs at least %d characters long"
projectOwnerDeletionForbiddenErrMsg = "%s is a project owner and can not be deleted"
apiKeyWithNameExistsErrMsg = "An API Key with this name already exists in this project, please use a different name"
teamMemberDoesNotExistErrMsg = `There is no account on this Satellite for the user(s) you have entered.
Please add team members with active accounts`
@ -779,6 +780,11 @@ func (s *Service) CreateAPIKey(ctx context.Context, projectID uuid.UUID, name st
return nil, nil, ErrUnauthorized.Wrap(err)
}
_, err = s.store.APIKeys().GetByNameAndProjectID(ctx, name, projectID)
if err == nil {
return nil, nil, errs.New(apiKeyWithNameExistsErrMsg)
}
secret, err := macaroon.NewSecret()
if err != nil {
return nil, nil, ErrConsoleInternal.Wrap(err)

View File

@ -151,6 +151,19 @@ func (keys *apikeys) GetByHead(ctx context.Context, head []byte) (_ *console.API
return fromDBXAPIKey(ctx, dbKey)
}
// GetByNameAndProjectID implements satellite.APIKeys
func (keys *apikeys) GetByNameAndProjectID(ctx context.Context, name string, projectID uuid.UUID) (_ *console.APIKeyInfo, err error) {
defer mon.Task()(&ctx)(&err)
dbKey, err := keys.methods.Get_ApiKey_By_Name_And_ProjectId(ctx,
dbx.ApiKey_Name(name),
dbx.ApiKey_ProjectId(projectID[:]))
if err != nil {
return nil, err
}
return fromDBXAPIKey(ctx, dbKey)
}
// Create implements satellite.APIKeys
func (keys *apikeys) Create(ctx context.Context, head []byte, info console.APIKeyInfo) (_ *console.APIKeyInfo, err error) {
defer mon.Task()(&ctx)(&err)

View File

@ -423,6 +423,11 @@ read one (
select api_key
where api_key.head = ?
)
read one (
select api_key
where api_key.name = ?
where api_key.project_id = ?
)
read all (
select api_key
where api_key.project_id = ?

View File

@ -7819,6 +7819,28 @@ func (obj *postgresImpl) Get_ApiKey_By_Head(ctx context.Context,
}
func (obj *postgresImpl) Get_ApiKey_By_Name_And_ProjectId(ctx context.Context,
api_key_name ApiKey_Name_Field,
api_key_project_id ApiKey_ProjectId_Field) (
api_key *ApiKey, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT api_keys.id, api_keys.project_id, api_keys.head, api_keys.name, api_keys.secret, api_keys.partner_id, api_keys.created_at FROM api_keys WHERE api_keys.name = ? AND api_keys.project_id = ?")
var __values []interface{}
__values = append(__values, api_key_name.value(), api_key_project_id.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
api_key = &ApiKey{}
err = obj.driver.QueryRow(__stmt, __values...).Scan(&api_key.Id, &api_key.ProjectId, &api_key.Head, &api_key.Name, &api_key.Secret, &api_key.PartnerId, &api_key.CreatedAt)
if err != nil {
return nil, obj.makeErr(err)
}
return api_key, nil
}
func (obj *postgresImpl) All_ApiKey_By_ProjectId_OrderBy_Asc_Name(ctx context.Context,
api_key_project_id ApiKey_ProjectId_Field) (
rows []*ApiKey, err error) {
@ -12256,6 +12278,28 @@ func (obj *sqlite3Impl) Get_ApiKey_By_Head(ctx context.Context,
}
func (obj *sqlite3Impl) Get_ApiKey_By_Name_And_ProjectId(ctx context.Context,
api_key_name ApiKey_Name_Field,
api_key_project_id ApiKey_ProjectId_Field) (
api_key *ApiKey, err error) {
var __embed_stmt = __sqlbundle_Literal("SELECT api_keys.id, api_keys.project_id, api_keys.head, api_keys.name, api_keys.secret, api_keys.partner_id, api_keys.created_at FROM api_keys WHERE api_keys.name = ? AND api_keys.project_id = ?")
var __values []interface{}
__values = append(__values, api_key_name.value(), api_key_project_id.value())
var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt)
obj.logStmt(__stmt, __values...)
api_key = &ApiKey{}
err = obj.driver.QueryRow(__stmt, __values...).Scan(&api_key.Id, &api_key.ProjectId, &api_key.Head, &api_key.Name, &api_key.Secret, &api_key.PartnerId, &api_key.CreatedAt)
if err != nil {
return nil, obj.makeErr(err)
}
return api_key, nil
}
func (obj *sqlite3Impl) All_ApiKey_By_ProjectId_OrderBy_Asc_Name(ctx context.Context,
api_key_project_id ApiKey_ProjectId_Field) (
rows []*ApiKey, err error) {
@ -16454,6 +16498,17 @@ func (rx *Rx) Get_ApiKey_By_Id(ctx context.Context,
return tx.Get_ApiKey_By_Id(ctx, api_key_id)
}
func (rx *Rx) Get_ApiKey_By_Name_And_ProjectId(ctx context.Context,
api_key_name ApiKey_Name_Field,
api_key_project_id ApiKey_ProjectId_Field) (
api_key *ApiKey, err error) {
var tx *Tx
if tx, err = rx.getTx(ctx); err != nil {
return
}
return tx.Get_ApiKey_By_Name_And_ProjectId(ctx, api_key_name, api_key_project_id)
}
func (rx *Rx) Get_BucketMetainfo_By_ProjectId_And_Name(ctx context.Context,
bucket_metainfo_project_id BucketMetainfo_ProjectId_Field,
bucket_metainfo_name BucketMetainfo_Name_Field) (
@ -17387,6 +17442,11 @@ type Methods interface {
api_key_id ApiKey_Id_Field) (
api_key *ApiKey, err error)
Get_ApiKey_By_Name_And_ProjectId(ctx context.Context,
api_key_name ApiKey_Name_Field,
api_key_project_id ApiKey_ProjectId_Field) (
api_key *ApiKey, err error)
Get_BucketMetainfo_By_ProjectId_And_Name(ctx context.Context,
bucket_metainfo_project_id BucketMetainfo_ProjectId_Field,
bucket_metainfo_name BucketMetainfo_Name_Field) (

View File

@ -185,6 +185,13 @@ func (m *lockedAPIKeys) GetByHead(ctx context.Context, head []byte) (*console.AP
return m.db.GetByHead(ctx, head)
}
// GetByNameAndProjectID retrieves APIKeyInfo for given key name and projectID
func (m *lockedAPIKeys) GetByNameAndProjectID(ctx context.Context, name string, projectID uuid.UUID) (*console.APIKeyInfo, error) {
m.Lock()
defer m.Unlock()
return m.db.GetByNameAndProjectID(ctx, name, projectID)
}
// GetPagedByProjectID is a method for querying API keys from the database by projectID and cursor
func (m *lockedAPIKeys) GetPagedByProjectID(ctx context.Context, projectID uuid.UUID, cursor console.APIKeyCursor) (akp *console.APIKeyPage, err error) {
m.Lock()