Skip to content

Commit

Permalink
satellite/satellitedb: add version column to api_keys table
Browse files Browse the repository at this point in the history
This change adds a column that holds API key versions. This will allow
us to implement version-specific functionality (such as Object Lock
features) for API keys.

storj/edge#399

Change-Id: I5297d145db2b9c9c51a387610bcb539086c95b3e
  • Loading branch information
jewharton committed Mar 22, 2024
1 parent cca4f20 commit 58e3191
Show file tree
Hide file tree
Showing 11 changed files with 1,385 additions and 490 deletions.
1 change: 1 addition & 0 deletions satellite/console/apikeys.go
Expand Up @@ -62,6 +62,7 @@ type APIKeyInfo struct {
Head []byte `json:"-"`
Secret []byte `json:"-"`
CreatedAt time.Time `json:"createdAt"`
Version uint `json:"version"`

// TODO move this closer to metainfo
ProjectRateLimit *int `json:"-"`
Expand Down
1 change: 1 addition & 0 deletions satellite/console/consoleweb/consoleapi/apidocs.gen.md
Expand Up @@ -268,6 +268,7 @@ Gets API keys by project ID
userAgent: string
name: string
createdAt: string // Date timestamp formatted as `2006-01-02T15:00:00Z`
version: number
}

]
Expand Down
9 changes: 6 additions & 3 deletions satellite/satellitedb/apikeys.go
Expand Up @@ -74,7 +74,7 @@ func (keys *apikeys) GetPagedByProjectID(ctx context.Context, projectID uuid.UUI
}

repoundQuery := keys.db.Rebind(`
SELECT ak.id, ak.project_id, ak.name, ak.user_agent, ak.created_at, p.public_id
SELECT ak.id, ak.project_id, ak.name, ak.user_agent, ak.created_at, ak.version, p.public_id
FROM api_keys ak, projects p
WHERE ak.project_id = ?
AND ak.project_id = p.id
Expand All @@ -98,7 +98,7 @@ func (keys *apikeys) GetPagedByProjectID(ctx context.Context, projectID uuid.UUI
for rows.Next() {
ak := console.APIKeyInfo{}

err = rows.Scan(&ak.ID, &ak.ProjectID, &ak.Name, &ak.UserAgent, &ak.CreatedAt, &ak.ProjectPublicID)
err = rows.Scan(&ak.ID, &ak.ProjectID, &ak.Name, &ak.UserAgent, &ak.CreatedAt, &ak.Version, &ak.ProjectPublicID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -207,7 +207,9 @@ func (keys *apikeys) Create(ctx context.Context, head []byte, info console.APIKe
return nil, err
}

optional := dbx.ApiKey_Create_Fields{}
optional := dbx.ApiKey_Create_Fields{
Version: dbx.ApiKey_Version(info.Version),
}
if info.UserAgent != nil {
optional.UserAgent = dbx.ApiKey_UserAgent(info.UserAgent)
}
Expand Down Expand Up @@ -279,6 +281,7 @@ func apiKeyToAPIKeyInfo(ctx context.Context, key *dbx.ApiKey) (_ *console.APIKey
CreatedAt: key.CreatedAt,
Head: key.Head,
Secret: key.Secret,
Version: key.Version,
}

if key.UserAgent != nil {
Expand Down
2 changes: 2 additions & 0 deletions satellite/satellitedb/dbx/project.dbx
Expand Up @@ -234,6 +234,8 @@ model api_key (
field created_at timestamp (autoinsert)
// created_by is an UUID of the user who created this key.
field created_by user.id restrict (nullable)
// version specifies the version number of the api key.
field version uint (default 0)
)

create api_key ( )
Expand Down
97 changes: 81 additions & 16 deletions satellite/satellitedb/dbx/satellitedb.dbx.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions satellite/satellitedb/dbx/satellitedb.dbx.pgx.sql
Expand Up @@ -524,6 +524,7 @@ CREATE TABLE api_keys (
user_agent bytea,
created_at timestamp with time zone NOT NULL,
created_by bytea REFERENCES users( id ),
version integer NOT NULL DEFAULT 0,
PRIMARY KEY ( id ),
UNIQUE ( head ),
UNIQUE ( name, project_id )
Expand Down
1 change: 1 addition & 0 deletions satellite/satellitedb/dbx/satellitedb.dbx.pgxcockroach.sql
Expand Up @@ -524,6 +524,7 @@ CREATE TABLE api_keys (
user_agent bytea,
created_at timestamp with time zone NOT NULL,
created_by bytea REFERENCES users( id ),
version integer NOT NULL DEFAULT 0,
PRIMARY KEY ( id ),
UNIQUE ( head ),
UNIQUE ( name, project_id )
Expand Down
8 changes: 8 additions & 0 deletions satellite/satellitedb/migrate.go
Expand Up @@ -2678,6 +2678,14 @@ func (db *satelliteDB) ProductionMigration() *migrate.Migration {
`ALTER TABLE bucket_metainfos ADD COLUMN created_by bytea REFERENCES users( id ) DEFAULT NULL;`,
},
},
{
DB: &db.migrationDB,
Description: "add version column to api_keys table",
Version: 265,
Action: migrate.SQL{
`ALTER TABLE api_keys ADD COLUMN version integer NOT NULL DEFAULT 0;`,
},
},
// NB: after updating testdata in `testdata`, run
// `go generate` to update `migratez.go`.
},
Expand Down

0 comments on commit 58e3191

Please sign in to comment.