2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-26 14:00:53 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-16 20:23:28 +00:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
2018-12-26 14:00:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// apikeys is an implementation of satellite.APIKeys
|
|
|
|
type apikeys struct {
|
|
|
|
db dbx.Methods
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByProjectID implements satellite.APIKeys ordered by name
|
2019-01-15 13:03:24 +00:00
|
|
|
func (keys *apikeys) GetByProjectID(ctx context.Context, projectID uuid.UUID) ([]console.APIKeyInfo, error) {
|
2018-12-26 14:00:53 +00:00
|
|
|
dbKeys, err := keys.db.All_ApiKey_By_ProjectId_OrderBy_Asc_Name(ctx, dbx.ApiKey_ProjectId(projectID[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
var apiKeys []console.APIKeyInfo
|
2018-12-26 14:00:53 +00:00
|
|
|
var parseErr errs.Group
|
|
|
|
|
|
|
|
for _, key := range dbKeys {
|
2018-12-27 15:30:15 +00:00
|
|
|
info, err := fromDBXAPIKey(key)
|
2018-12-26 14:00:53 +00:00
|
|
|
if err != nil {
|
|
|
|
parseErr.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
apiKeys = append(apiKeys, *info)
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := parseErr.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKeys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements satellite.APIKeys
|
2019-01-15 13:03:24 +00:00
|
|
|
func (keys *apikeys) Get(ctx context.Context, id uuid.UUID) (*console.APIKeyInfo, error) {
|
2018-12-26 14:00:53 +00:00
|
|
|
dbKey, err := keys.db.Get_ApiKey_By_Id(ctx, dbx.ApiKey_Id(id[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
return fromDBXAPIKey(dbKey)
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 20:23:28 +00:00
|
|
|
// GetByKey implements satellite.APIKeys
|
|
|
|
func (keys *apikeys) GetByKey(ctx context.Context, key console.APIKey) (*console.APIKeyInfo, error) {
|
|
|
|
dbKey, err := keys.db.Get_ApiKey_By_Key(ctx, dbx.ApiKey_Key(key[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromDBXAPIKey(dbKey)
|
|
|
|
}
|
|
|
|
|
2018-12-26 14:00:53 +00:00
|
|
|
// Create implements satellite.APIKeys
|
2019-01-15 13:03:24 +00:00
|
|
|
func (keys *apikeys) Create(ctx context.Context, key console.APIKey, info console.APIKeyInfo) (*console.APIKeyInfo, error) {
|
2018-12-26 14:00:53 +00:00
|
|
|
id, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbKey, err := keys.db.Create_ApiKey(
|
|
|
|
ctx,
|
|
|
|
dbx.ApiKey_Id(id[:]),
|
2018-12-27 15:30:15 +00:00
|
|
|
dbx.ApiKey_ProjectId(info.ProjectID[:]),
|
|
|
|
dbx.ApiKey_Key(key[:]),
|
|
|
|
dbx.ApiKey_Name(info.Name),
|
2018-12-26 14:00:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
return fromDBXAPIKey(dbKey)
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements satellite.APIKeys
|
2019-01-15 13:03:24 +00:00
|
|
|
func (keys *apikeys) Update(ctx context.Context, key console.APIKeyInfo) error {
|
2018-12-26 14:00:53 +00:00
|
|
|
_, err := keys.db.Update_ApiKey_By_Id(
|
|
|
|
ctx,
|
|
|
|
dbx.ApiKey_Id(key.ID[:]),
|
|
|
|
dbx.ApiKey_Update_Fields{
|
|
|
|
Name: dbx.ApiKey_Name(key.Name),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements satellite.APIKeys
|
|
|
|
func (keys *apikeys) Delete(ctx context.Context, id uuid.UUID) error {
|
|
|
|
_, err := keys.db.Delete_ApiKey_By_Id(ctx, dbx.ApiKey_Id(id[:]))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-27 15:30:15 +00:00
|
|
|
// fromDBXAPIKey converts dbx.ApiKey to satellite.APIKeyInfo
|
2019-01-15 13:03:24 +00:00
|
|
|
func fromDBXAPIKey(key *dbx.ApiKey) (*console.APIKeyInfo, error) {
|
2018-12-26 14:00:53 +00:00
|
|
|
id, err := bytesToUUID(key.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := bytesToUUID(key.ProjectId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
return &console.APIKeyInfo{
|
2018-12-26 14:00:53 +00:00
|
|
|
ID: id,
|
|
|
|
ProjectID: projectID,
|
|
|
|
Name: key.Name,
|
|
|
|
CreatedAt: key.CreatedAt,
|
|
|
|
}, nil
|
|
|
|
}
|