feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Account identity, credential, membership and role bindings are committed
|
||||
// together. Password hashing intentionally happens before acquiring DB locks.
|
||||
func (s *Store) createPasswordAccountContext(ctx context.Context, domain, username, password, displayName, email string, bootstrap bool) (*Principal, error) {
|
||||
username = strings.ToLower(strings.TrimSpace(username))
|
||||
if !authUsernamePattern.MatchString(username) {
|
||||
return nil, errors.New("username must be 2-64 lowercase letters, digits, underscores, or hyphens")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encoded, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p := &Principal{UUID: uuid.NewString(), DisplayName: strings.TrimSpace(displayName), Email: strings.TrimSpace(email), State: "active"}
|
||||
if p.DisplayName == "" {
|
||||
p.DisplayName = username
|
||||
}
|
||||
err = s.groupWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
if bootstrap {
|
||||
var count int
|
||||
if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM principals`).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 0 {
|
||||
return namespaceValidationError("owner already exists")
|
||||
}
|
||||
}
|
||||
// PostgreSQL cannot bootstrap into a nonexistent or disabled namespace.
|
||||
// SQLite retains its historical authentication-only fixture support.
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
var present bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains WHERE uuid=$1 AND enabled=1 AND placement='local')`, domain).Scan(&present); err != nil {
|
||||
return err
|
||||
}
|
||||
if !present {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
membership, role := uuid.NewString(), ""
|
||||
roleName, description := "member", "Domain member"
|
||||
if bootstrap {
|
||||
roleName, description = "owner", "Domain owner"
|
||||
}
|
||||
err := tx.QueryRowContext(ctx, `SELECT uuid FROM auth_roles WHERE domain_uuid=`+s.sqlParam(1)+` AND name=`+s.sqlParam(2), domain, roleName).Scan(&role)
|
||||
if bootstrap && err == nil {
|
||||
return namespaceValidationError("owner role already exists")
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
role = uuid.NewString()
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO auth_roles(uuid,domain_uuid,name,description,created_at_ms) VALUES(`+s.sqlValues(5)+`)`, role, domain, roleName, description, now); err != nil {
|
||||
return err
|
||||
}
|
||||
capabilities := []string{"read"}
|
||||
if bootstrap {
|
||||
capabilities = []string{"read", "write", "rename", "delete", "link", "manage", "delegate"}
|
||||
}
|
||||
for _, capability := range capabilities {
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(`+s.sqlValues(6)+`)`, uuid.NewString(), role, capability, "domain", domain, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
queries := []struct {
|
||||
q string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO principals(uuid,display_name,email,state,created_at_ms,updated_at_ms) VALUES(` + s.sqlValues(6) + `)`, []any{p.UUID, p.DisplayName, p.Email, p.State, now, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,subject,created_at_ms) VALUES(` + s.sqlValues(5) + `)`, []any{uuid.NewString(), p.UUID, "password", username, now}},
|
||||
{`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,created_at_ms,updated_at_ms) VALUES(` + s.sqlValues(7) + `)`, []any{uuid.NewString(), p.UUID, "password", username, encoded, now, now}},
|
||||
{`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(` + s.sqlValues(5) + `)`, []any{membership, domain, p.UUID, "active", now}},
|
||||
{`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(` + s.sqlValues(3) + `)`, []any{membership, role, now}},
|
||||
}
|
||||
for _, q := range queries {
|
||||
if _, err := tx.ExecContext(ctx, q.q, q.args...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if bootstrap {
|
||||
if s.sqlBackend == storeSQLite {
|
||||
var present bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name='domains' AND type='table')`).Scan(&present); err != nil {
|
||||
return err
|
||||
}
|
||||
if !present {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return s.ensureBuiltinAdminGroupsContext(ctx, tx)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (s *Store) BootstrapOwnerContext(ctx context.Context, domain, username, password, displayName, email string) (*Principal, error) {
|
||||
return s.createPasswordAccountContext(ctx, domain, username, password, displayName, email, true)
|
||||
}
|
||||
func (s *Store) CreatePasswordAccountContext(ctx context.Context, domain, username, password, displayName, email string) (*Principal, error) {
|
||||
return s.createPasswordAccountContext(ctx, domain, username, password, displayName, email, false)
|
||||
}
|
||||
Reference in new issue
Block a user