Files
upstream-seaweedfs/weed/credential/postgres/postgres_identity.go
upstream-mirror 89f5be005d
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 03:23:42 +05:00

755 lines
25 KiB
Go

package postgres
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/credential"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
)
func (store *PostgresStore) LoadConfiguration(ctx context.Context) (*iam_pb.S3ApiConfiguration, error) {
if !store.configured {
return nil, fmt.Errorf("store not configured")
}
config := &iam_pb.S3ApiConfiguration{}
rows, err := store.db.QueryContext(ctx, "SELECT username, email, account_data, actions, policy_names FROM users")
if err != nil {
glog.Errorf("credential postgres: LoadConfiguration query failed: %v", err)
return nil, fmt.Errorf("failed to query users: %w", err)
}
defer rows.Close()
for rows.Next() {
var username, email string
var accountDataJSON, actionsJSON, policyNamesJSON []byte
if err := rows.Scan(&username, &email, &accountDataJSON, &actionsJSON, &policyNamesJSON); err != nil {
glog.Errorf("credential postgres: LoadConfiguration scan failed: %v", err)
return nil, fmt.Errorf("failed to scan user row: %w", err)
}
identity := &iam_pb.Identity{
Name: username,
}
if len(accountDataJSON) > 0 {
if err := json.Unmarshal(accountDataJSON, &identity.Account); err != nil {
return nil, fmt.Errorf("failed to unmarshal account data for user %s: %v", username, err)
}
}
if len(actionsJSON) > 0 {
if err := json.Unmarshal(actionsJSON, &identity.Actions); err != nil {
return nil, fmt.Errorf("failed to unmarshal actions for user %s: %v", username, err)
}
}
if len(policyNamesJSON) > 0 {
if err := json.Unmarshal(policyNamesJSON, &identity.PolicyNames); err != nil {
return nil, fmt.Errorf("failed to unmarshal policy names for user %s: %v", username, err)
}
}
credRows, err := store.db.QueryContext(ctx, "SELECT access_key, secret_key FROM credentials WHERE username = $1", username)
if err != nil {
return nil, fmt.Errorf("failed to query credentials for user %s: %v", username, err)
}
for credRows.Next() {
var accessKey, secretKey string
if err := credRows.Scan(&accessKey, &secretKey); err != nil {
credRows.Close()
return nil, fmt.Errorf("failed to scan credential row for user %s: %v", username, err)
}
identity.Credentials = append(identity.Credentials, &iam_pb.Credential{
AccessKey: accessKey,
SecretKey: secretKey,
})
}
credErr := credRows.Err()
credRows.Close()
if credErr != nil {
return nil, fmt.Errorf("failed iterating credential rows for user %s: %w", username, credErr)
}
config.Identities = append(config.Identities, identity)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed iterating user rows: %w", err)
}
// groups are part of the snapshot: full-state reconciliation treats their
// absence as deletion
groupRows, err := store.db.QueryContext(ctx, "SELECT name, members, policy_names, disabled FROM groups")
if err != nil {
return nil, fmt.Errorf("failed to query groups: %w", err)
}
defer groupRows.Close()
for groupRows.Next() {
var name string
var membersJSON, policyNamesJSON []byte
var disabled bool
if err := groupRows.Scan(&name, &membersJSON, &policyNamesJSON, &disabled); err != nil {
return nil, fmt.Errorf("failed to scan group row: %w", err)
}
group := &iam_pb.Group{Name: name, Disabled: disabled}
if err := json.Unmarshal(membersJSON, &group.Members); err != nil {
return nil, fmt.Errorf("failed to unmarshal members for group %s: %w", name, err)
}
if err := json.Unmarshal(policyNamesJSON, &group.PolicyNames); err != nil {
return nil, fmt.Errorf("failed to unmarshal policy_names for group %s: %w", name, err)
}
config.Groups = append(config.Groups, group)
}
if err := groupRows.Err(); err != nil {
return nil, fmt.Errorf("failed iterating group rows: %w", err)
}
glog.V(0).Infof("credential postgres: LoadConfiguration loaded %d identities, %d groups", len(config.Identities), len(config.Groups))
return config, nil
}
func (store *PostgresStore) SaveConfiguration(ctx context.Context, config *iam_pb.S3ApiConfiguration) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
// Track which usernames are in the incoming config for pruning
configUsernames := make(map[string]bool, len(config.Identities))
for _, identity := range config.Identities {
configUsernames[identity.Name] = true
}
// Upsert each user's row.
for _, identity := range config.Identities {
var accountDataJSON []byte
if identity.Account != nil {
accountDataJSON, err = json.Marshal(identity.Account)
if err != nil {
return fmt.Errorf("failed to marshal account data for user %s: %v", identity.Name, err)
}
}
var actionsJSON []byte
if identity.Actions != nil {
actionsJSON, err = json.Marshal(identity.Actions)
if err != nil {
return fmt.Errorf("failed to marshal actions for user %s: %v", identity.Name, err)
}
}
var policyNamesJSON []byte
if identity.PolicyNames != nil {
policyNamesJSON, err = json.Marshal(identity.PolicyNames)
if err != nil {
return fmt.Errorf("failed to marshal policy names for user %s: %v", identity.Name, err)
}
}
// Upsert user — preserves the row (and its CASCADE dependents) if it already exists
_, err = tx.ExecContext(ctx,
`INSERT INTO users (username, email, account_data, actions, policy_names)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (username) DO UPDATE SET
email = EXCLUDED.email,
account_data = EXCLUDED.account_data,
actions = EXCLUDED.actions,
policy_names = EXCLUDED.policy_names,
updated_at = CURRENT_TIMESTAMP`,
identity.Name, "", jsonbParam(accountDataJSON), jsonbParam(actionsJSON), jsonbParam(policyNamesJSON))
if err != nil {
return fmt.Errorf("failed to upsert user %s: %v", identity.Name, err)
}
}
// Prune users no longer in config BEFORE replacing credentials. The
// IAM rename path (s3api UpdateUser) renames an identity in place and
// keeps its access keys: the renamed user shows up in the incoming
// config, the old name shows up in the pruned set. If we inserted the
// new credentials first, the renamed user's access keys would still be
// owned by the old row in this transaction and the INSERT would
// violate the global UNIQUE constraint on credentials.access_key.
// CASCADE on the old row releases those keys before the insert.
rows, err := tx.QueryContext(ctx, "SELECT username FROM users")
if err != nil {
return fmt.Errorf("failed to list existing users for pruning: %w", err)
}
var toDelete []string
for rows.Next() {
var username string
if err := rows.Scan(&username); err != nil {
rows.Close()
return fmt.Errorf("failed to scan username for pruning: %w", err)
}
if !configUsernames[username] {
toDelete = append(toDelete, username)
}
}
scanErr := rows.Err()
rows.Close()
if scanErr != nil {
return fmt.Errorf("failed iterating user rows for pruning: %w", scanErr)
}
if len(toDelete) > 0 {
var inlineCount int
if err := tx.QueryRowContext(ctx,
"SELECT COUNT(*) FROM user_inline_policies WHERE username = ANY($1)",
toDelete).Scan(&inlineCount); err != nil {
glog.Warningf("credential postgres: SaveConfiguration failed to count inline policies for prune candidates: %v", err)
} else if inlineCount > 0 {
glog.Warningf("credential postgres: SaveConfiguration pruning %d users will CASCADE-remove %d inline policies; if this was a rename, re-create them under the new name", len(toDelete), inlineCount)
}
if _, err := tx.ExecContext(ctx, "DELETE FROM users WHERE username = ANY($1)", toDelete); err != nil {
return fmt.Errorf("failed to prune users: %w", err)
}
}
// Two-pass credential replace: first clear every user we are about to
// rewrite in a single round-trip, then insert. Doing the per-user delete
// + insert in a single pass would violate the global UNIQUE constraint
// on credentials.access_key when an access key gets reassigned from one
// user to another within the same SaveConfiguration call.
usernames := make([]string, 0, len(configUsernames))
for name := range configUsernames {
usernames = append(usernames, name)
}
if _, err := tx.ExecContext(ctx, "DELETE FROM credentials WHERE username = ANY($1)", usernames); err != nil {
return fmt.Errorf("failed to clear credentials for incoming users: %w", err)
}
for _, identity := range config.Identities {
for _, cred := range identity.Credentials {
if _, err := tx.ExecContext(ctx,
"INSERT INTO credentials (username, access_key, secret_key) VALUES ($1, $2, $3)",
identity.Name, cred.AccessKey, cred.SecretKey); err != nil {
return fmt.Errorf("failed to insert credential for user %s: %v", identity.Name, err)
}
}
}
if err := tx.Commit(); err != nil {
glog.Errorf("credential postgres: SaveConfiguration commit failed: %v", err)
return fmt.Errorf("failed to commit SaveConfiguration: %w", err)
}
if len(toDelete) > 0 {
glog.Warningf("credential postgres: SaveConfiguration saved %d identities, pruned %d", len(config.Identities), len(toDelete))
} else {
glog.V(0).Infof("credential postgres: SaveConfiguration saved %d identities", len(config.Identities))
}
return nil
}
func (store *PostgresStore) CreateUser(ctx context.Context, identity *iam_pb.Identity) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
var count int
err := store.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users WHERE username = $1", identity.Name).Scan(&count)
if err != nil {
glog.Errorf("credential postgres: CreateUser check failed user=%s: %v", identity.Name, err)
return fmt.Errorf("failed to check user existence: %w", err)
}
if count > 0 {
glog.V(1).Infof("credential postgres: CreateUser user=%s already exists", identity.Name)
return credential.ErrUserAlreadyExists
}
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
var accountDataJSON []byte
if identity.Account != nil {
accountDataJSON, err = json.Marshal(identity.Account)
if err != nil {
return fmt.Errorf("failed to marshal account data: %w", err)
}
}
var actionsJSON []byte
if identity.Actions != nil {
actionsJSON, err = json.Marshal(identity.Actions)
if err != nil {
return fmt.Errorf("failed to marshal actions: %w", err)
}
}
var policyNamesJSON []byte
if identity.PolicyNames != nil {
policyNamesJSON, err = json.Marshal(identity.PolicyNames)
if err != nil {
return fmt.Errorf("failed to marshal policy names: %w", err)
}
}
_, err = tx.ExecContext(ctx,
"INSERT INTO users (username, email, account_data, actions, policy_names) VALUES ($1, $2, $3, $4, $5)",
identity.Name, "", jsonbParam(accountDataJSON), jsonbParam(actionsJSON), jsonbParam(policyNamesJSON))
if err != nil {
glog.Errorf("credential postgres: CreateUser insert failed user=%s: %v", identity.Name, err)
return fmt.Errorf("failed to insert user: %w", err)
}
for _, cred := range identity.Credentials {
_, err = tx.ExecContext(ctx,
"INSERT INTO credentials (username, access_key, secret_key) VALUES ($1, $2, $3)",
identity.Name, cred.AccessKey, cred.SecretKey)
if err != nil {
glog.Errorf("credential postgres: CreateUser insert credential failed user=%s accessKey=%s: %v", identity.Name, cred.AccessKey, err)
return fmt.Errorf("failed to insert credential: %w", err)
}
}
if err := tx.Commit(); err != nil {
glog.Errorf("credential postgres: CreateUser commit failed user=%s: %v", identity.Name, err)
return fmt.Errorf("failed to commit create user %s: %w", identity.Name, err)
}
glog.V(0).Infof("credential postgres: CreateUser user=%s credentials=%d actions=%d", identity.Name, len(identity.Credentials), len(identity.Actions))
return nil
}
func (store *PostgresStore) GetUser(ctx context.Context, username string) (*iam_pb.Identity, error) {
if !store.configured {
return nil, fmt.Errorf("store not configured")
}
var email string
var accountDataJSON, actionsJSON, policyNamesJSON []byte
err := store.db.QueryRowContext(ctx,
"SELECT email, account_data, actions, policy_names FROM users WHERE username = $1",
username).Scan(&email, &accountDataJSON, &actionsJSON, &policyNamesJSON)
if err != nil {
if err == sql.ErrNoRows {
glog.V(2).Infof("credential postgres: GetUser user=%s not found", username)
return nil, credential.ErrUserNotFound
}
glog.Errorf("credential postgres: GetUser query failed user=%s: %v", username, err)
return nil, fmt.Errorf("failed to query user: %w", err)
}
identity := &iam_pb.Identity{
Name: username,
}
if len(accountDataJSON) > 0 {
if err := json.Unmarshal(accountDataJSON, &identity.Account); err != nil {
return nil, fmt.Errorf("failed to unmarshal account data: %w", err)
}
}
if len(actionsJSON) > 0 {
if err := json.Unmarshal(actionsJSON, &identity.Actions); err != nil {
return nil, fmt.Errorf("failed to unmarshal actions: %w", err)
}
}
if len(policyNamesJSON) > 0 {
if err := json.Unmarshal(policyNamesJSON, &identity.PolicyNames); err != nil {
return nil, fmt.Errorf("failed to unmarshal policy names: %w", err)
}
}
rows, err := store.db.QueryContext(ctx, "SELECT access_key, secret_key FROM credentials WHERE username = $1", username)
if err != nil {
return nil, fmt.Errorf("failed to query credentials: %w", err)
}
defer rows.Close()
for rows.Next() {
var accessKey, secretKey string
if err := rows.Scan(&accessKey, &secretKey); err != nil {
return nil, fmt.Errorf("failed to scan credential: %w", err)
}
identity.Credentials = append(identity.Credentials, &iam_pb.Credential{
AccessKey: accessKey,
SecretKey: secretKey,
})
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed iterating credential rows: %w", err)
}
glog.V(2).Infof("credential postgres: GetUser user=%s credentials=%d actions=%d", username, len(identity.Credentials), len(identity.Actions))
return identity, nil
}
func (store *PostgresStore) UpdateUser(ctx context.Context, username string, identity *iam_pb.Identity) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
var count int
err = tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM users WHERE username = $1", username).Scan(&count)
if err != nil {
return fmt.Errorf("failed to check user existence: %w", err)
}
if count == 0 {
return credential.ErrUserNotFound
}
var accountDataJSON []byte
if identity.Account != nil {
accountDataJSON, err = json.Marshal(identity.Account)
if err != nil {
return fmt.Errorf("failed to marshal account data: %w", err)
}
}
var actionsJSON []byte
if identity.Actions != nil {
actionsJSON, err = json.Marshal(identity.Actions)
if err != nil {
return fmt.Errorf("failed to marshal actions: %w", err)
}
}
var policyNamesJSON []byte
if identity.PolicyNames != nil {
policyNamesJSON, err = json.Marshal(identity.PolicyNames)
if err != nil {
return fmt.Errorf("failed to marshal policy names: %w", err)
}
}
_, err = tx.ExecContext(ctx,
"UPDATE users SET email = $2, account_data = $3, actions = $4, policy_names = $5, updated_at = CURRENT_TIMESTAMP WHERE username = $1",
username, "", jsonbParam(accountDataJSON), jsonbParam(actionsJSON), jsonbParam(policyNamesJSON))
if err != nil {
glog.Errorf("credential postgres: UpdateUser failed user=%s: %v", username, err)
return fmt.Errorf("failed to update user: %w", err)
}
_, err = tx.ExecContext(ctx, "DELETE FROM credentials WHERE username = $1", username)
if err != nil {
return fmt.Errorf("failed to delete existing credentials: %w", err)
}
for _, cred := range identity.Credentials {
_, err = tx.ExecContext(ctx,
"INSERT INTO credentials (username, access_key, secret_key) VALUES ($1, $2, $3)",
username, cred.AccessKey, cred.SecretKey)
if err != nil {
return fmt.Errorf("failed to insert credential: %w", err)
}
}
if err := tx.Commit(); err != nil {
glog.Errorf("credential postgres: UpdateUser commit failed user=%s: %v", username, err)
return fmt.Errorf("failed to commit update user %s: %w", username, err)
}
glog.V(0).Infof("credential postgres: UpdateUser user=%s credentials=%d", username, len(identity.Credentials))
return nil
}
func (store *PostgresStore) DeleteUser(ctx context.Context, username string) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
result, err := store.db.ExecContext(ctx, "DELETE FROM users WHERE username = $1", username)
if err != nil {
glog.Errorf("credential postgres: DeleteUser failed user=%s: %v", username, err)
return fmt.Errorf("failed to delete user: %w", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
}
if rowsAffected == 0 {
glog.V(1).Infof("credential postgres: DeleteUser user=%s not found", username)
return credential.ErrUserNotFound
}
glog.V(0).Infof("credential postgres: DeleteUser user=%s", username)
return nil
}
// RenameUser atomically renames oldName to newName, re-pointing every
// FK-backed dependent row in a single transaction. The schema's foreign
// keys (credentials.username, user_inline_policies.username) reference
// users.username with ON DELETE CASCADE and the default ON UPDATE
// NO ACTION, which means a plain UPDATE users SET username = newName
// would fail because the children still reference the old name. Instead:
// 1. Insert the new users row by copying every non-key column from old.
// 2. Re-point the dependent tables (credentials, user_inline_policies)
// to the new username — both rows now exist so the FK is satisfied.
// 3. Delete the old users row, which has no remaining dependents.
//
// Implements credential.UserRenamer.
func (store *PostgresStore) RenameUser(ctx context.Context, oldName, newName string) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
if oldName == newName {
return nil
}
tx, err := store.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
// Refuse if oldName is missing or newName already exists, so the
// caller surfaces a recognisable error instead of a constraint
// violation midway through.
var oldExists, newExists bool
if err := tx.QueryRowContext(ctx, "SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)", oldName).Scan(&oldExists); err != nil {
return fmt.Errorf("failed to check old user %s: %w", oldName, err)
}
if !oldExists {
return credential.ErrUserNotFound
}
if err := tx.QueryRowContext(ctx, "SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)", newName).Scan(&newExists); err != nil {
return fmt.Errorf("failed to check new user %s: %w", newName, err)
}
if newExists {
return credential.ErrUserAlreadyExists
}
if _, err := tx.ExecContext(ctx,
`INSERT INTO users (username, email, account_data, actions, policy_names, created_at, updated_at)
SELECT $1, email, account_data, actions, policy_names, created_at, CURRENT_TIMESTAMP
FROM users WHERE username = $2`,
newName, oldName); err != nil {
return fmt.Errorf("failed to insert renamed user %s: %w", newName, err)
}
if _, err := tx.ExecContext(ctx,
"UPDATE credentials SET username = $1 WHERE username = $2", newName, oldName); err != nil {
return fmt.Errorf("failed to re-point credentials to %s: %w", newName, err)
}
if _, err := tx.ExecContext(ctx,
"UPDATE user_inline_policies SET username = $1 WHERE username = $2", newName, oldName); err != nil {
return fmt.Errorf("failed to re-point inline policies to %s: %w", newName, err)
}
if _, err := tx.ExecContext(ctx,
"DELETE FROM users WHERE username = $1", oldName); err != nil {
return fmt.Errorf("failed to drop old user %s: %w", oldName, err)
}
if err := tx.Commit(); err != nil {
glog.Errorf("credential postgres: RenameUser commit failed %s -> %s: %v", oldName, newName, err)
return fmt.Errorf("failed to commit rename %s -> %s: %w", oldName, newName, err)
}
glog.V(0).Infof("credential postgres: RenameUser %s -> %s", oldName, newName)
return nil
}
func (store *PostgresStore) ListUsers(ctx context.Context) ([]string, error) {
if !store.configured {
return nil, fmt.Errorf("store not configured")
}
rows, err := store.db.QueryContext(ctx, "SELECT username FROM users ORDER BY username")
if err != nil {
glog.Errorf("credential postgres: ListUsers query failed: %v", err)
return nil, fmt.Errorf("failed to query users: %w", err)
}
defer rows.Close()
var usernames []string
for rows.Next() {
var username string
if err := rows.Scan(&username); err != nil {
return nil, fmt.Errorf("failed to scan username: %w", err)
}
usernames = append(usernames, username)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed iterating user rows: %w", err)
}
glog.V(1).Infof("credential postgres: ListUsers count=%d", len(usernames))
return usernames, nil
}
func (store *PostgresStore) GetUserByAccessKey(ctx context.Context, accessKey string) (*iam_pb.Identity, error) {
if !store.configured {
return nil, fmt.Errorf("store not configured")
}
var username string
err := store.db.QueryRowContext(ctx, "SELECT username FROM credentials WHERE access_key = $1", accessKey).Scan(&username)
if err != nil {
if err == sql.ErrNoRows {
glog.V(2).Infof("credential postgres: GetUserByAccessKey accessKey=%s not found", accessKey)
return nil, credential.ErrAccessKeyNotFound
}
glog.Errorf("credential postgres: GetUserByAccessKey query failed accessKey=%s: %v", accessKey, err)
return nil, fmt.Errorf("failed to query access key: %w", err)
}
glog.V(2).Infof("credential postgres: GetUserByAccessKey accessKey=%s resolved to user=%s", accessKey, username)
return store.GetUser(ctx, username)
}
func (store *PostgresStore) CreateAccessKey(ctx context.Context, username string, cred *iam_pb.Credential) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
var count int
err := store.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users WHERE username = $1", username).Scan(&count)
if err != nil {
return fmt.Errorf("failed to check user existence: %w", err)
}
if count == 0 {
return credential.ErrUserNotFound
}
_, err = store.db.ExecContext(ctx,
"INSERT INTO credentials (username, access_key, secret_key) VALUES ($1, $2, $3)",
username, cred.AccessKey, cred.SecretKey)
if err != nil {
glog.Errorf("credential postgres: CreateAccessKey failed user=%s accessKey=%s: %v", username, cred.AccessKey, err)
return fmt.Errorf("failed to insert credential: %w", err)
}
glog.V(0).Infof("credential postgres: CreateAccessKey user=%s accessKey=%s", username, cred.AccessKey)
return nil
}
func (store *PostgresStore) DeleteAccessKey(ctx context.Context, username string, accessKey string) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
result, err := store.db.ExecContext(ctx,
"DELETE FROM credentials WHERE username = $1 AND access_key = $2",
username, accessKey)
if err != nil {
glog.Errorf("credential postgres: DeleteAccessKey failed user=%s accessKey=%s: %v", username, accessKey, err)
return fmt.Errorf("failed to delete access key: %w", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
}
if rowsAffected == 0 {
var count int
err = store.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users WHERE username = $1", username).Scan(&count)
if err != nil {
return fmt.Errorf("failed to check user existence: %w", err)
}
if count == 0 {
return credential.ErrUserNotFound
}
return credential.ErrAccessKeyNotFound
}
glog.V(0).Infof("credential postgres: DeleteAccessKey user=%s accessKey=%s", username, accessKey)
return nil
}
func (store *PostgresStore) AttachUserPolicy(ctx context.Context, username string, policyName string) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
identity, err := store.GetUser(ctx, username)
if err != nil {
return err
}
policy, err := store.GetPolicy(ctx, policyName)
if err != nil {
return err
}
if policy == nil {
return credential.ErrPolicyNotFound
}
for _, p := range identity.PolicyNames {
if p == policyName {
return credential.ErrPolicyAlreadyAttached
}
}
identity.PolicyNames = append(identity.PolicyNames, policyName)
if err := store.UpdateUser(ctx, username, identity); err != nil {
return err
}
glog.V(1).Infof("credential postgres: AttachUserPolicy user=%s policy=%s", username, policyName)
return nil
}
func (store *PostgresStore) DetachUserPolicy(ctx context.Context, username string, policyName string) error {
if !store.configured {
return fmt.Errorf("store not configured")
}
identity, err := store.GetUser(ctx, username)
if err != nil {
return err
}
found := false
var newPolicyNames []string
for _, p := range identity.PolicyNames {
if p == policyName {
found = true
} else {
newPolicyNames = append(newPolicyNames, p)
}
}
if !found {
return credential.ErrPolicyNotAttached
}
identity.PolicyNames = newPolicyNames
if err := store.UpdateUser(ctx, username, identity); err != nil {
return err
}
glog.V(1).Infof("credential postgres: DetachUserPolicy user=%s policy=%s", username, policyName)
return nil
}
func (store *PostgresStore) ListAttachedUserPolicies(ctx context.Context, username string) ([]string, error) {
if !store.configured {
return nil, fmt.Errorf("store not configured")
}
identity, err := store.GetUser(ctx, username)
if err != nil {
return nil, err
}
return identity.PolicyNames, nil
}