Skip to content

Commit

Permalink
satellite/{console, db, metainfo}: populate created_by column on API …
Browse files Browse the repository at this point in the history
…key and bucket creation

Added logic to populate api_keys.created_by column on API key creation (on console service level).
Added logic to populate bucket_metainfos.created_by column on bucket creation (on metainfo request level).

Issue:
#6855

Change-Id: I660c9a6f4b621862752a67b178d45bc010e070e7
  • Loading branch information
VitaliiShpital committed Mar 21, 2024
1 parent 90437a6 commit db9bf03
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions satellite/admin/bucket_testplanet_test.go
Expand Up @@ -102,6 +102,7 @@ func TestAdminBucketGeofenceAPI(t *testing.T) {
Name: b.Name,
ProjectID: testCase.project,
Created: b.Created,
CreatedBy: b.CreatedBy,
Placement: storj.EU,
})
require.NoError(t, err, "failed to json encode expected bucket")
Expand Down
1 change: 1 addition & 0 deletions satellite/buckets/db.go
Expand Up @@ -35,6 +35,7 @@ type Bucket struct {
ID uuid.UUID
Name string
ProjectID uuid.UUID
CreatedBy uuid.UUID
UserAgent []byte
Created time.Time
PathCipher storj.CipherSuite
Expand Down
1 change: 1 addition & 0 deletions satellite/console/apikeys.go
Expand Up @@ -56,6 +56,7 @@ type APIKeyInfo struct {
ID uuid.UUID `json:"id"`
ProjectID uuid.UUID `json:"projectId"`
ProjectPublicID uuid.UUID `json:"projectPublicId"`
CreatedBy uuid.UUID `json:"createdBy"`
UserAgent []byte `json:"userAgent"`
Name string `json:"name"`
Head []byte `json:"-"`
Expand Down
1 change: 1 addition & 0 deletions satellite/console/consoleweb/consoleapi/apidocs.gen.md
Expand Up @@ -264,6 +264,7 @@ Gets API keys by project ID
id: string // UUID formatted as `00000000-0000-0000-0000-000000000000`
projectId: string // UUID formatted as `00000000-0000-0000-0000-000000000000`
projectPublicId: string // UUID formatted as `00000000-0000-0000-0000-000000000000`
createdBy: string // UUID formatted as `00000000-0000-0000-0000-000000000000`
userAgent: string
name: string
createdAt: string // Date timestamp formatted as `2006-01-02T15:00:00Z`
Expand Down
1 change: 1 addition & 0 deletions satellite/console/service.go
Expand Up @@ -2512,6 +2512,7 @@ func (s *Service) CreateAPIKey(ctx context.Context, projectID uuid.UUID, name st
apikey := APIKeyInfo{
Name: name,
ProjectID: isMember.project.ID,
CreatedBy: user.ID,
Secret: secret,
UserAgent: user.UserAgent,
}
Expand Down
7 changes: 7 additions & 0 deletions satellite/console/service_test.go
Expand Up @@ -582,6 +582,13 @@ func TestService(t *testing.T) {
require.Equal(t, "console service: project usage: some buckets still exist", err.Error())
})

t.Run("CreateAPIKey", func(t *testing.T) {
createdAPIKey, _, err := service.CreateAPIKey(userCtx2, up2Proj.ID, "test key")
require.NoError(t, err)
require.NotNil(t, createdAPIKey)
require.Equal(t, up2Proj.OwnerID, createdAPIKey.CreatedBy)
})

t.Run("GetProjectUsageLimits", func(t *testing.T) {
bandwidthLimit := sat.Config.Console.UsageLimits.Bandwidth.Free
storageLimit := sat.Config.Console.UsageLimits.Storage.Free
Expand Down
7 changes: 4 additions & 3 deletions satellite/metainfo/endpoint_bucket.go
Expand Up @@ -203,7 +203,7 @@ func (endpoint *Endpoint) CreateBucket(ctx context.Context, req *pb.BucketCreate
return nil, rpcstatus.Error(rpcstatus.ResourceExhausted, fmt.Sprintf("number of allocated buckets (%d) exceeded", endpoint.config.ProjectLimits.MaxBuckets))
}

bucketReq, err := convertProtoToBucket(req, keyInfo.ProjectID)
bucketReq, err := convertProtoToBucket(req, keyInfo)
if err != nil {
return nil, rpcstatus.Error(rpcstatus.InvalidArgument, err.Error())
}
Expand Down Expand Up @@ -486,7 +486,7 @@ func getAllowedBuckets(ctx context.Context, header *pb.RequestHeader, action mac
return allowedBuckets, err
}

func convertProtoToBucket(req *pb.BucketCreateRequest, projectID uuid.UUID) (bucket buckets.Bucket, err error) {
func convertProtoToBucket(req *pb.BucketCreateRequest, keyInfo *console.APIKeyInfo) (bucket buckets.Bucket, err error) {
bucketID, err := uuid.New()
if err != nil {
return buckets.Bucket{}, err
Expand All @@ -495,7 +495,8 @@ func convertProtoToBucket(req *pb.BucketCreateRequest, projectID uuid.UUID) (buc
return buckets.Bucket{
ID: bucketID,
Name: string(req.GetName()),
ProjectID: projectID,
ProjectID: keyInfo.ProjectID,
CreatedBy: keyInfo.CreatedBy,
}, nil
}

Expand Down
32 changes: 32 additions & 0 deletions satellite/metainfo/endpoint_bucket_test.go
Expand Up @@ -302,6 +302,38 @@ func TestBucketCreationWithDefaultPlacement(t *testing.T) {
})
}

func TestCraeteBucketWithCreatedBy(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
sat := planet.Satellites[0]
service := sat.API.Console.Service
project := planet.Uplinks[0].Projects[0]

userCtx, err := sat.UserContext(ctx, project.Owner.ID)
require.NoError(t, err)

apiKeyInfo, apiKey, err := service.CreateAPIKey(userCtx, project.ID, "test key")
require.NoError(t, err)
require.NotNil(t, apiKey)
require.False(t, apiKeyInfo.CreatedBy.IsZero())

bucketName := []byte("bucket")
_, err = sat.Metainfo.Endpoint.CreateBucket(ctx, &pb.BucketCreateRequest{
Header: &pb.RequestHeader{
ApiKey: apiKey.SerializeRaw(),
},
Name: bucketName,
})
require.NoError(t, err)

bucket, err := sat.API.DB.Buckets().GetBucket(ctx, bucketName, project.ID)
require.NoError(t, err)
require.False(t, bucket.CreatedBy.IsZero())
require.Equal(t, apiKeyInfo.CreatedBy, bucket.CreatedBy)
})
}

func TestGetBucketLocation(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
Expand Down
12 changes: 12 additions & 0 deletions satellite/satellitedb/apikeys.go
Expand Up @@ -211,6 +211,9 @@ func (keys *apikeys) Create(ctx context.Context, head []byte, info console.APIKe
if info.UserAgent != nil {
optional.UserAgent = dbx.ApiKey_UserAgent(info.UserAgent)
}
if !info.CreatedBy.IsZero() {
optional.CreatedBy = dbx.ApiKey_CreatedBy(info.CreatedBy[:])
}

_, err = keys.methods.Create_ApiKey(
ctx,
Expand Down Expand Up @@ -260,9 +263,18 @@ func apiKeyToAPIKeyInfo(ctx context.Context, key *dbx.ApiKey) (_ *console.APIKey
return nil, err
}

var createdBy uuid.UUID
if key.CreatedBy != nil {
createdBy, err = uuid.FromBytes(key.CreatedBy)
if err != nil {
return nil, err
}
}

result := &console.APIKeyInfo{
ID: id,
ProjectID: projectID,
CreatedBy: createdBy,
Name: key.Name,
CreatedAt: key.CreatedAt,
Head: key.Head,
Expand Down
20 changes: 14 additions & 6 deletions satellite/satellitedb/bucketsdb.go
Expand Up @@ -26,14 +26,13 @@ func (db *bucketsDB) CreateBucket(ctx context.Context, bucket buckets.Bucket) (_

optionalFields := dbx.BucketMetainfo_Create_Fields{}
if bucket.UserAgent != nil {
optionalFields = dbx.BucketMetainfo_Create_Fields{
UserAgent: dbx.BucketMetainfo_UserAgent(bucket.UserAgent),
}
optionalFields.UserAgent = dbx.BucketMetainfo_UserAgent(bucket.UserAgent)
}
if bucket.Versioning != buckets.VersioningUnsupported {
optionalFields = dbx.BucketMetainfo_Create_Fields{
Versioning: dbx.BucketMetainfo_Versioning(int(bucket.Versioning)),
}
optionalFields.Versioning = dbx.BucketMetainfo_Versioning(int(bucket.Versioning))
}
if !bucket.CreatedBy.IsZero() {
optionalFields.CreatedBy = dbx.BucketMetainfo_CreatedBy(bucket.CreatedBy[:])
}
optionalFields.Placement = dbx.BucketMetainfo_Placement(int(bucket.Placement))

Expand Down Expand Up @@ -342,11 +341,20 @@ func convertDBXtoBucket(dbxBucket *dbx.BucketMetainfo) (bucket buckets.Bucket, e
return bucket, buckets.ErrBucket.Wrap(err)
}

var createdBy uuid.UUID
if dbxBucket.CreatedBy != nil {
createdBy, err = uuid.FromBytes(dbxBucket.CreatedBy)
if err != nil {
return bucket, buckets.ErrBucket.Wrap(err)
}
}

bucket = buckets.Bucket{
ID: id,
Name: string(dbxBucket.Name),
ProjectID: project,
Created: dbxBucket.CreatedAt,
CreatedBy: createdBy,
PathCipher: storj.CipherSuite(dbxBucket.PathCipher),
DefaultSegmentsSize: int64(dbxBucket.DefaultSegmentSize),
DefaultRedundancyScheme: storj.RedundancyScheme{
Expand Down
1 change: 1 addition & 0 deletions web/satellite/src/api/v1.gen.ts
Expand Up @@ -8,6 +8,7 @@ export class APIKeyInfo {
id: UUID;
projectId: UUID;
projectPublicId: UUID;
createdBy: UUID;
userAgent: string | null;
name: string;
createdAt: Time;
Expand Down

0 comments on commit db9bf03

Please sign in to comment.