feat(api): give status page servers sealed key-value storage
Certificates and ACME keys are stored encrypted in the database, namespaced per owner so an organization's servers cannot read platform or other organizations' keys. Locks live in Redis and expire. Refs #22 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
f52ae33cf0
commit
46f1f82481
11 files changed
+611
-1
No files matched your search
@@ -0,0 +1,104 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: agent_storage.sql
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const storageDelete = `-- name: StorageDelete :execrows
|
||||
DELETE FROM agent_storage
|
||||
WHERE owner = $1 AND (key = $2 OR starts_with(key, $2::text || '/'))
|
||||
`
|
||||
|
||||
type StorageDeleteParams struct {
|
||||
Owner string
|
||||
Key string
|
||||
}
|
||||
|
||||
// Deletes key and, as certmagic expects, everything below it.
|
||||
func (q *Queries) StorageDelete(ctx context.Context, arg StorageDeleteParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, storageDelete, arg.Owner, arg.Key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const storageGet = `-- name: StorageGet :one
|
||||
SELECT value, modified FROM agent_storage WHERE owner = $1 AND key = $2
|
||||
`
|
||||
|
||||
type StorageGetParams struct {
|
||||
Owner string
|
||||
Key string
|
||||
}
|
||||
|
||||
type StorageGetRow struct {
|
||||
Value []byte
|
||||
Modified time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) StorageGet(ctx context.Context, arg StorageGetParams) (StorageGetRow, error) {
|
||||
row := q.db.QueryRow(ctx, storageGet, arg.Owner, arg.Key)
|
||||
var i StorageGetRow
|
||||
err := row.Scan(&i.Value, &i.Modified)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const storageList = `-- name: StorageList :many
|
||||
SELECT key, length(value)::bigint AS size, modified FROM agent_storage
|
||||
WHERE owner = $1 AND ($2::text = '' OR starts_with(key, $2::text || '/'))
|
||||
ORDER BY key
|
||||
`
|
||||
|
||||
type StorageListParams struct {
|
||||
Owner string
|
||||
Prefix string
|
||||
}
|
||||
|
||||
type StorageListRow struct {
|
||||
Key string
|
||||
Size int64
|
||||
Modified time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) StorageList(ctx context.Context, arg StorageListParams) ([]StorageListRow, error) {
|
||||
rows, err := q.db.Query(ctx, storageList, arg.Owner, arg.Prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []StorageListRow{}
|
||||
for rows.Next() {
|
||||
var i StorageListRow
|
||||
if err := rows.Scan(&i.Key, &i.Size, &i.Modified); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const storagePut = `-- name: StoragePut :exec
|
||||
INSERT INTO agent_storage (owner, key, value) VALUES ($1, $2, $3)
|
||||
ON CONFLICT (owner, key) DO UPDATE SET value = EXCLUDED.value, modified = now()
|
||||
`
|
||||
|
||||
type StoragePutParams struct {
|
||||
Owner string
|
||||
Key string
|
||||
Value []byte
|
||||
}
|
||||
|
||||
func (q *Queries) StoragePut(ctx context.Context, arg StoragePutParams) error {
|
||||
_, err := q.db.Exec(ctx, storagePut, arg.Owner, arg.Key, arg.Value)
|
||||
return err
|
||||
}
|
||||
@@ -23,6 +23,13 @@ type Agent struct {
|
||||
RegionID *uuid.UUID
|
||||
}
|
||||
|
||||
type AgentStorage struct {
|
||||
Owner string
|
||||
Key string
|
||||
Value []byte
|
||||
Modified time.Time
|
||||
}
|
||||
|
||||
type ApiToken struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
|
||||
@@ -147,6 +147,11 @@ type Querier interface {
|
||||
SetUserAdmin(ctx context.Context, arg SetUserAdminParams) (int64, error)
|
||||
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
||||
StatusPageMonitors(ctx context.Context, pageID uuid.UUID) ([]StatusPageMonitorsRow, error)
|
||||
// Deletes key and, as certmagic expects, everything below it.
|
||||
StorageDelete(ctx context.Context, arg StorageDeleteParams) (int64, error)
|
||||
StorageGet(ctx context.Context, arg StorageGetParams) (StorageGetRow, error)
|
||||
StorageList(ctx context.Context, arg StorageListParams) ([]StorageListRow, error)
|
||||
StoragePut(ctx context.Context, arg StoragePutParams) error
|
||||
TouchAPIToken(ctx context.Context, id uuid.UUID) error
|
||||
TouchAgent(ctx context.Context, arg TouchAgentParams) error
|
||||
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
-- name: StorageGet :one
|
||||
SELECT value, modified FROM agent_storage WHERE owner = $1 AND key = $2;
|
||||
|
||||
-- name: StoragePut :exec
|
||||
INSERT INTO agent_storage (owner, key, value) VALUES ($1, $2, $3)
|
||||
ON CONFLICT (owner, key) DO UPDATE SET value = EXCLUDED.value, modified = now();
|
||||
|
||||
-- name: StorageDelete :execrows
|
||||
-- Deletes key and, as certmagic expects, everything below it.
|
||||
DELETE FROM agent_storage
|
||||
WHERE owner = sqlc.arg(owner) AND (key = sqlc.arg(key) OR starts_with(key, sqlc.arg(key)::text || '/'));
|
||||
|
||||
-- name: StorageList :many
|
||||
SELECT key, length(value)::bigint AS size, modified FROM agent_storage
|
||||
WHERE owner = sqlc.arg(owner) AND (sqlc.arg(prefix)::text = '' OR starts_with(key, sqlc.arg(prefix)::text || '/'))
|
||||
ORDER BY key;
|
||||
Reference in new issue
Block a user