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,535 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAccountBackendCLIHandoff(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
browser, err := s.CreateBrowserAuthSession(d, p.UUID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
verifier := strings.Repeat("a", 43)
|
||||
id, device, code, err := s.StartCLIAuthForLogin(ctx, d, cliTestOrigin, hashAuthToken(verifier), true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request, err := s.CLIAuthRequest(ctx, d, cliTestOrigin, id)
|
||||
if err != nil || !request.Remember {
|
||||
t.Fatal("trusted choice lost", err)
|
||||
}
|
||||
if _, _, _, err = s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier); !errors.Is(err, ErrCLIPending) {
|
||||
t.Fatal("pending state", err)
|
||||
}
|
||||
if _, _, _, err = s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier); !errors.Is(err, ErrCLISlowDown) {
|
||||
t.Fatal("poll limit", err)
|
||||
}
|
||||
if err = s.BeginCLIAuth(ctx, d, cliTestOrigin, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.BindCLIAuth(ctx, d, cliTestOrigin, id, browser); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.DecideCLIAuth(ctx, d, cliTestOrigin, id, browser, code, "", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.DB.Exec(`UPDATE auth_cli_requests SET next_poll_ms=0`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Failed issuance must not consume approval.
|
||||
if _, err = s.DB.Exec(`ALTER TABLE auth_sessions RENAME COLUMN mfa_level TO missing_mfa`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token, _, _, e := s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier); e == nil || token != "" {
|
||||
t.Fatal("partial session returned", e)
|
||||
}
|
||||
request, err = s.CLIAuthRequest(ctx, d, cliTestOrigin, id)
|
||||
if err != nil || request.State != "approved" {
|
||||
t.Fatal("grant consumed on failed issuance", err)
|
||||
}
|
||||
if _, err = s.DB.Exec(`ALTER TABLE auth_sessions RENAME COLUMN missing_mfa TO mfa_level`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
type result struct {
|
||||
token, name string
|
||||
err error
|
||||
}
|
||||
results := make(chan result, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() {
|
||||
token, name, _, e := s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier)
|
||||
results <- result{token, name, e}
|
||||
}()
|
||||
}
|
||||
success := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
r := <-results
|
||||
if r.err != nil {
|
||||
if !errors.Is(r.err, ErrCLIExpired) {
|
||||
t.Fatal(r.err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
success++
|
||||
if r.name != "owner" || !strings.HasPrefix(r.token, "pw_api_") {
|
||||
t.Fatal("invalid exchange response")
|
||||
}
|
||||
if principal, e := s.PrincipalForSession(d, r.token); e != nil || principal.UUID != p.UUID {
|
||||
t.Fatal("CLI session invalid", e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("approval consumed more than once", success)
|
||||
}
|
||||
// Include concurrent admissions at the boundary, not only sequential caps.
|
||||
for i := 0; i < 62; i++ {
|
||||
if _, _, _, e := s.StartCLIAuth(ctx, d, cliTestOrigin, hashAuthToken(verifier)); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
limits := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() { _, _, _, e := s.StartCLIAuth(ctx, d, cliTestOrigin, hashAuthToken(verifier)); limits <- e }()
|
||||
}
|
||||
success = 0
|
||||
for i := 0; i < 2; i++ {
|
||||
if e := <-limits; e == nil {
|
||||
success++
|
||||
} else if !errors.Is(e, ErrCLILimit) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("concurrent admission bypassed limit", success)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendExternalIdentity(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
s.authKey = make([]byte, 32)
|
||||
config := validLDAPProviderConfig()
|
||||
if err := s.SetAuthProviderConfig(d, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider, err := s.AuthProviderConfig(d, "ldap")
|
||||
if err != nil || !provider.Enabled || provider.LDAPBindPassword != config.LDAPBindPassword {
|
||||
t.Fatal("provider configuration mismatch", err)
|
||||
}
|
||||
config.LDAPBindPassword = ""
|
||||
config.LDAPSearchFilter = "(mail={{username}})"
|
||||
if err = s.SetAuthProviderConfig(d, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kept, err := s.AuthProviderConfig(d, "ldap")
|
||||
if err != nil || kept.UUID != provider.UUID || kept.LDAPBindPassword != provider.LDAPBindPassword {
|
||||
t.Fatal("provider lost secret or identity", err)
|
||||
}
|
||||
p, err := s.ProvisionExternalAccount(d, "directory", "Directory User", "mail@example.test", "ldap", provider.UUID, "", "immutable-id", map[string]string{"display": "Directory User"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.AuthenticatePassword("directory", "anything with twelve characters"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("external user has password", err)
|
||||
}
|
||||
for _, lookup := range []func() (*Principal, error){
|
||||
func() (*Principal, error) { return s.PrincipalByExternalUsername(d, "DIRECTORY") },
|
||||
func() (*Principal, error) { return s.PrincipalByVerifiedEmail(d, "MAIL@example.test") },
|
||||
func() (*Principal, error) { return s.PrincipalByDisplayName(d, "Directory User") },
|
||||
func() (*Principal, error) {
|
||||
return s.PrincipalByExternalIdentity(d, "ldap", provider.UUID, "", "immutable-id")
|
||||
},
|
||||
} {
|
||||
found, err := lookup()
|
||||
if err != nil || found.UUID != p.UUID {
|
||||
t.Fatal("external identity lookup", err)
|
||||
}
|
||||
}
|
||||
if err = s.BindExternalIdentityIfProviderUnbound(d, p.UUID, "ldap", provider.UUID, "", "replacement-id", nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("provider identity replaced", err)
|
||||
}
|
||||
second, err := s.CreatePasswordAccount(d, "second", "synthetic password for backend tests", "Directory User", "mail@example.test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.PrincipalByVerifiedEmail(d, "mail@example.test"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("ambiguous email accepted", err)
|
||||
}
|
||||
if _, err = s.PrincipalByDisplayName(d, "Directory User"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("ambiguous display accepted", err)
|
||||
}
|
||||
if err = s.BindExternalIdentity(second.UUID, "ldap", provider.UUID, "", "immutable-id", nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("identity owner replaced", err)
|
||||
}
|
||||
if _, err = s.ProvisionExternalAccount(d, "rollback-ext", "", "", "ldap", provider.UUID, "", "immutable-id", nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("duplicate identity provisioned", err)
|
||||
}
|
||||
if _, err = s.PrincipalByUsername(d, "rollback-ext"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("partial external account remains", err)
|
||||
}
|
||||
if err = s.BindExternalIdentityIfProviderUnbound(d, second.UUID, "oidc", "oidc-provider", "https://issuer.example.test", "unique", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
challenge, err := s.CreateAuthChallenge(d, p.UUID, "magic-link", map[string]string{"return": "/docs"}, time.Minute)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.ConsumeAuthChallenge("outside", "magic-link", challenge, nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("cross-domain challenge accepted", err)
|
||||
}
|
||||
results := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() {
|
||||
var payload map[string]string
|
||||
id, e := s.ConsumeAuthChallenge(d, "magic-link", challenge, &payload)
|
||||
if e == nil && (id != p.UUID || payload["return"] != "/docs") {
|
||||
e = fmt.Errorf("challenge payload mismatch")
|
||||
}
|
||||
results <- e
|
||||
}()
|
||||
}
|
||||
success := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
if e := <-results; e == nil {
|
||||
success++
|
||||
} else if !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("challenge accepted more than once", success)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendTOTP(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
s.authKey = make([]byte, 32) // Synthetic credential encryption key.
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret, err := s.BeginTOTPEnrollment(p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.HasTOTP(p.UUID) || s.PendingTOTPSecret(p.UUID) != secret {
|
||||
t.Fatal("pending enrollment mismatch")
|
||||
}
|
||||
code, err := totpCode(secret, time.Now().Unix()/30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.ConfirmTOTPEnrollment(p.UUID, code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !s.HasTOTP(p.UUID) || s.PendingTOTPSecret(p.UUID) != "" {
|
||||
t.Fatal("confirmed enrollment mismatch")
|
||||
}
|
||||
if err = s.VerifyTOTP(p.UUID, code); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("enrollment code replay", err)
|
||||
}
|
||||
// Reset only synthetic counters to exercise single-use concurrent login
|
||||
// without wall-clock sleeps or a test-dependent production clock.
|
||||
if _, err = s.DB.Exec(`UPDATE auth_credentials SET parameters_json='{}' WHERE kind='totp'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, _ = totpCode(secret, time.Now().Unix()/30)
|
||||
challenge, err := s.CreateTOTPLoginChallengeForLogin(d, p.UUID, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err = s.CompleteTOTPLoginChallengeForLogin("outside", challenge, code); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("cross-domain TOTP challenge", err)
|
||||
}
|
||||
results := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() {
|
||||
id, remember, e := s.CompleteTOTPLoginChallengeForLogin(d, challenge, code)
|
||||
if e == nil && (id != p.UUID || !remember) {
|
||||
e = fmt.Errorf("challenge result mismatch")
|
||||
}
|
||||
results <- e
|
||||
}()
|
||||
}
|
||||
success := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
if e := <-results; e == nil {
|
||||
success++
|
||||
} else if !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("concurrent code consumers", success)
|
||||
}
|
||||
challenge, err = s.CreateTOTPLoginChallenge(d, p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 6; i++ {
|
||||
if _, e := s.CompleteTOTPLoginChallenge(d, challenge, "invalid"); !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if err = s.PurgeExpiredAuthArtifacts(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var remaining int
|
||||
if err = s.DB.QueryRow(`SELECT count(*) FROM auth_challenges`).Scan(&remaining); err != nil || remaining != 0 {
|
||||
t.Fatal("consumed challenge cleanup", remaining, err)
|
||||
}
|
||||
if err = s.DisableTOTP(p.UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.HasTOTP(p.UUID) {
|
||||
t.Fatal("TOTP still enabled")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendProfileAndSessionManagement(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.UpdatePrincipalProfile(p.UUID, " 日本語の名前 ", " user@example.test "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data := []byte{0, 255, 1, 0, 128}
|
||||
if err = s.SetPrincipalAvatar(p.UUID, "image/png", data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kind, got, err := s.PrincipalAvatar(p.UUID)
|
||||
if err != nil || kind != "image/png" || !bytes.Equal(got, data) {
|
||||
t.Fatal("avatar round trip", kind, got, err)
|
||||
}
|
||||
found, err := s.PrincipalByUsername(d, " OWNER ")
|
||||
if err != nil || found.DisplayName != "日本語の名前" || found.Email != "user@example.test" || !found.HasAvatar {
|
||||
t.Fatal(found, err)
|
||||
}
|
||||
if _, err = s.PrincipalByUsername("other-domain", "owner"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("cross-domain profile lookup", err)
|
||||
}
|
||||
if err = s.SetPrincipalAvatar(p.UUID, "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found, err = s.PrincipalByUsername(d, "owner")
|
||||
if err != nil || found.HasAvatar {
|
||||
t.Fatal("avatar removal", err)
|
||||
}
|
||||
principals, err := s.ListDomainPrincipals(d)
|
||||
if err != nil || len(principals) != 1 || principals[0].UUID != p.UUID {
|
||||
t.Fatal(principals, err)
|
||||
}
|
||||
principals, err = s.ListDomainPrincipals("other-domain")
|
||||
if err != nil || len(principals) != 0 {
|
||||
t.Fatal("cross-domain principal list", err)
|
||||
}
|
||||
token, err := s.CreateBrowserAuthSession(d, p.UUID, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessions, err := s.ListAuthSessions(d)
|
||||
if err != nil || len(sessions) != 1 || sessions[0].MFALevel != 2 {
|
||||
t.Fatal(sessions, err)
|
||||
}
|
||||
id := sessions[0].UUID
|
||||
if err = s.RevokeAuthSessionByID("other-domain", id); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("cross-domain revocation", err)
|
||||
}
|
||||
if _, err = s.PrincipalForSession(d, token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.RevokeAuthSessionByID(d, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.PrincipalForSession(d, token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("revoked session accepted", err)
|
||||
}
|
||||
sessions, err = s.ListAuthSessions(d)
|
||||
if err != nil || len(sessions) != 0 {
|
||||
t.Fatal("revoked session listed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendBootstrapProjectionFailureIsAtomic(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
if _, err := s.DB.Exec(`ALTER TABLE campus_admin_grants RENAME COLUMN source TO missing_source`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "", ""); err == nil {
|
||||
t.Fatal("bootstrap succeeded with broken projection")
|
||||
}
|
||||
for _, table := range []string{"principals", "principal_identities", "auth_credentials", "auth_roles", "auth_capability_grants", "auth_role_bindings", "domain_memberships"} {
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`SELECT count(*) FROM ` + table).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("%s has partial bootstrap data: %d %v", table, count, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func accountBackendStore(t *testing.T, b storeSQLBackend) (*Store, string) {
|
||||
t.Helper()
|
||||
s := testNamespaceStore(t, b)
|
||||
if b == storePostgreSQL {
|
||||
if err := s.ensurePostgreSQLAuthFoundation(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
d, err := s.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, d.UUID
|
||||
}
|
||||
|
||||
func TestAccountBackendLoginAndSession(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
const password = "synthetic password for backend tests"
|
||||
p, err := s.BootstrapOwner(d, " Owner ", password, "所有者", "owner@example.test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !s.IsBuiltinGroupAdmin(d, p.UUID, "domain_admin") || !s.IsBuiltinGroupAdmin(d, p.UUID, "campus_admin") {
|
||||
t.Fatal("bootstrap did not publish admin groups")
|
||||
}
|
||||
if _, err := s.BootstrapOwner(d, "second", password, "", ""); err == nil {
|
||||
t.Fatal("second bootstrap accepted")
|
||||
}
|
||||
if _, err := s.AuthenticatePassword("owner", "incorrect password"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("incorrect password accepted", err)
|
||||
}
|
||||
logged, err := s.AuthenticatePassword("OWNER", password)
|
||||
if err != nil || logged.UUID != p.UUID {
|
||||
t.Fatal("login mismatch", err)
|
||||
}
|
||||
member, err := s.CreatePasswordAccount(d, "member", password, "Member", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.authorizeGrant(AuthorizationRequest{DomainUUID: d, PrincipalUUID: member.UUID, Capability: "read", TargetType: "domain", TargetID: d}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.authorizeGrant(AuthorizationRequest{DomainUUID: d, PrincipalUUID: member.UUID, Capability: "write", TargetType: "domain", TargetID: d}, false); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("member can write", err)
|
||||
}
|
||||
token, err := s.CreateBrowserAuthSession(d, p.UUID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.PrincipalForSession(d, token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, invalid := range []string{strings.TrimPrefix(token, "pw_browser_"), strings.Replace(token, "pw_browser_", "pw_api_", 1)} {
|
||||
if _, err := s.PrincipalForSession(d, invalid); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("token purpose changed", err)
|
||||
}
|
||||
}
|
||||
if _, err := s.PrincipalForSession("outside", token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("cross-domain session accepted", err)
|
||||
}
|
||||
if err := s.SetPrincipalState(d, p.UUID, "suspended"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.CreateAuthSession(d, p.UUID); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("suspended principal obtained session", err)
|
||||
}
|
||||
if err := s.SetPrincipalState(d, p.UUID, "active"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.PrincipalForSession(d, token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("session resurrected", err)
|
||||
}
|
||||
token, err = s.CreateAuthSession(d, p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.RevokeAuthSession(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.PrincipalForSession(d, token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("revocation ignored", err)
|
||||
}
|
||||
if _, err := s.DB.Exec(`ALTER TABLE auth_role_bindings RENAME COLUMN role_uuid TO missing_role`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.CreatePasswordAccount(d, "rollback", password, "", ""); err == nil {
|
||||
t.Fatal("partial account accepted")
|
||||
}
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`SELECT count(*) FROM principal_identities WHERE subject='rollback'`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("partial identity remains", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendConcurrentBootstrap(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
var wg sync.WaitGroup
|
||||
results := make(chan error, 2)
|
||||
for _, name := range []string{"owner-one", "owner-two"} {
|
||||
wg.Add(1)
|
||||
go func(name string) {
|
||||
defer wg.Done()
|
||||
_, err := s.BootstrapOwner(d, name, "synthetic password for bootstrap", "", "")
|
||||
results <- err
|
||||
}(name)
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
success := 0
|
||||
for err := range results {
|
||||
if err == nil {
|
||||
success++
|
||||
} else if !strings.Contains(err.Error(), "owner already exists") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatalf("bootstrap successes=%d", success)
|
||||
}
|
||||
count, err := s.PrincipalCount()
|
||||
if err != nil || count != 1 {
|
||||
t.Fatal("multiple bootstrap principals", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user