feat(staterpc): one run credential per task, kept in the State Store, authorizes its store calls and its push

This commit is contained in:
sam committed 2026-10-04 18:32:45 +11:00
1 parent ad13393e6b
commit 319e8f4945
18 files changed
+1809 -1470

No files matched your search

@@ -0,0 +1,15 @@
-- A run credential authorizes one task's container: its State Store calls
-- and its branch push. Only the SHA-256 of the token is kept, so the table
-- cannot be replayed, and a row outlives a restart of either service.
-- +goose Up
CREATE TABLE run_credentials (
digest bytea PRIMARY KEY,
task_id bigint NOT NULL,
expires_at timestamptz NOT NULL
);
-- +goose Down
DROP TABLE run_credentials;
@@ -316,6 +316,12 @@ type ResourceHistory struct {
OrgID string
}
type RunCredential struct {
Digest []byte
TaskID int64
ExpiresAt time.Time
}
type Session struct {
SessionID string
Platform string
@@ -0,0 +1,60 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: run_credentials.sql
package postgresdb
import (
"context"
"time"
)
const deleteExpiredRunCredentials = `-- name: DeleteExpiredRunCredentials :exec
DELETE FROM run_credentials WHERE expires_at <= $1
`
func (q *Queries) DeleteExpiredRunCredentials(ctx context.Context, expiresAt time.Time) error {
_, err := q.db.Exec(ctx, deleteExpiredRunCredentials, expiresAt)
return err
}
const deleteRunCredential = `-- name: DeleteRunCredential :exec
DELETE FROM run_credentials WHERE digest = $1
`
func (q *Queries) DeleteRunCredential(ctx context.Context, digest []byte) error {
_, err := q.db.Exec(ctx, deleteRunCredential, digest)
return err
}
const insertRunCredential = `-- name: InsertRunCredential :exec
INSERT INTO run_credentials (digest, task_id, expires_at) VALUES ($1, $2, $3)
`
type InsertRunCredentialParams struct {
Digest []byte
TaskID int64
ExpiresAt time.Time
}
func (q *Queries) InsertRunCredential(ctx context.Context, arg InsertRunCredentialParams) error {
_, err := q.db.Exec(ctx, insertRunCredential, arg.Digest, arg.TaskID, arg.ExpiresAt)
return err
}
const runCredentialTask = `-- name: RunCredentialTask :one
SELECT task_id FROM run_credentials WHERE digest = $1 AND expires_at > $2
`
type RunCredentialTaskParams struct {
Digest []byte
ExpiresAt time.Time
}
func (q *Queries) RunCredentialTask(ctx context.Context, arg RunCredentialTaskParams) (int64, error) {
row := q.db.QueryRow(ctx, runCredentialTask, arg.Digest, arg.ExpiresAt)
var task_id int64
err := row.Scan(&task_id)
return task_id, err
}
@@ -0,0 +1,11 @@
-- name: InsertRunCredential :exec
INSERT INTO run_credentials (digest, task_id, expires_at) VALUES ($1, $2, $3);
-- name: DeleteRunCredential :exec
DELETE FROM run_credentials WHERE digest = $1;
-- name: RunCredentialTask :one
SELECT task_id FROM run_credentials WHERE digest = $1 AND expires_at > $2;
-- name: DeleteExpiredRunCredentials :exec
DELETE FROM run_credentials WHERE expires_at <= $1;
@@ -0,0 +1,34 @@
package postgres
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
var _ storecontract.RunCredentialStore = (*Store)(nil)
func (s *Store) PutRunCredential(ctx context.Context, digest [32]byte, taskID int64, expires time.Time) error {
return s.queries().InsertRunCredential(ctx, postgresdb.InsertRunCredentialParams{Digest: digest[:], TaskID: taskID, ExpiresAt: expires})
}
func (s *Store) DeleteRunCredential(ctx context.Context, digest [32]byte) error {
return s.queries().DeleteRunCredential(ctx, digest[:])
}
func (s *Store) RunCredentialTask(ctx context.Context, digest [32]byte, now time.Time) (int64, error) {
id, err := s.queries().RunCredentialTask(ctx, postgresdb.RunCredentialTaskParams{Digest: digest[:], ExpiresAt: now})
if errors.Is(err, pgx.ErrNoRows) {
return 0, storecontract.ErrRunCredentialUnknown
}
return id, err
}
func (s *Store) DeleteExpiredRunCredentials(ctx context.Context, now time.Time) error {
return s.queries().DeleteExpiredRunCredentials(ctx, now)
}