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
+138
-335
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
@@ -24,6 +25,12 @@ type Domain struct {
|
||||
}
|
||||
|
||||
func (s *Store) EnsureDomainsSchema() error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
if err := s.ensurePostgreSQLNamespaces(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.EnsureAtLeastOneDomain()
|
||||
}
|
||||
|
||||
var legacyTable int
|
||||
var domainsTable int
|
||||
@@ -115,43 +122,19 @@ func (s *Store) EnsureDomainsSchema() error {
|
||||
}
|
||||
|
||||
func (s *Store) EnsureAtLeastOneDomain() error {
|
||||
|
||||
var count int
|
||||
|
||||
err := s.DB.QueryRow(`
|
||||
SELECT count(*)
|
||||
FROM domains
|
||||
`).Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
return s.namespaceWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO domains(uuid,name,title,domain,enabled,created_at)
|
||||
SELECT `+s.sqlParam(1)+`,'default','Default Domain','',1,`+s.sqlParam(2)+` WHERE NOT EXISTS(SELECT 1 FROM domains)`, uuid.NewString(), time.Now().Unix())
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = s.DB.Exec(`
|
||||
INSERT INTO domains (
|
||||
uuid,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabled,
|
||||
created_at
|
||||
)
|
||||
VALUES (?, 'default', 'Default Domain', '', 1, ?)
|
||||
`,
|
||||
uuid.NewString(),
|
||||
time.Now().Unix(),
|
||||
)
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) GetDefaultDomain() (*Domain, error) {
|
||||
return s.GetDefaultDomainContext(context.Background())
|
||||
}
|
||||
|
||||
row := s.DB.QueryRow(`
|
||||
func (s *Store) GetDefaultDomainContext(ctx context.Context) (*Domain, error) {
|
||||
row := s.DB.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
@@ -172,19 +155,24 @@ func (s *Store) GetDefaultDomain() (*Domain, error) {
|
||||
LIMIT 1
|
||||
`)
|
||||
|
||||
return scanDomain(row)
|
||||
d, err := scanDomain(row)
|
||||
return d, s.sqlError("read default domain", err)
|
||||
}
|
||||
|
||||
func (s *Store) GetDomainByHost(
|
||||
host string,
|
||||
) (*Domain, error) {
|
||||
return s.GetDomainByHostContext(context.Background(), host)
|
||||
}
|
||||
|
||||
func (s *Store) GetDomainByHostContext(ctx context.Context, host string) (*Domain, error) {
|
||||
|
||||
host =
|
||||
normalizeDomainHost(
|
||||
host,
|
||||
)
|
||||
|
||||
row := s.DB.QueryRow(`
|
||||
row := s.DB.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
d.id,
|
||||
d.uuid,
|
||||
@@ -204,19 +192,24 @@ func (s *Store) GetDomainByHost(
|
||||
AND lower(
|
||||
CASE
|
||||
WHEN d.domain = '' THEN c.fqdn
|
||||
WHEN instr(d.domain, '.') > 0 THEN d.domain
|
||||
WHEN `+s.sqlHasDot("d.domain")+` THEN d.domain
|
||||
ELSE d.domain || '.' || c.fqdn
|
||||
END
|
||||
) = ?
|
||||
) = `+s.sqlParam(1)+`
|
||||
ORDER BY d.id
|
||||
LIMIT 1
|
||||
`, host)
|
||||
|
||||
return scanDomain(row)
|
||||
d, err := scanDomain(row)
|
||||
return d, s.sqlError("resolve domain host", err)
|
||||
}
|
||||
|
||||
func (s *Store) GetDomainByUUID(domainUUID string) (*Domain, error) {
|
||||
row := s.DB.QueryRow(`
|
||||
return s.GetDomainByUUIDContext(context.Background(), domainUUID)
|
||||
}
|
||||
|
||||
func (s *Store) GetDomainByUUIDContext(ctx context.Context, domainUUID string) (*Domain, error) {
|
||||
row := s.DB.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
id, uuid, name, title, domain, enabled, created_at,
|
||||
COALESCE(campus_uuid, ''),
|
||||
@@ -224,32 +217,40 @@ func (s *Store) GetDomainByUUID(domainUUID string) (*Domain, error) {
|
||||
COALESCE(instance_fqdn, ''),
|
||||
COALESCE(instance_fingerprint, '')
|
||||
FROM domains
|
||||
WHERE uuid = ?
|
||||
WHERE uuid = `+s.sqlParam(1)+`
|
||||
LIMIT 1
|
||||
`, domainUUID)
|
||||
return scanDomain(row)
|
||||
d, err := scanDomain(row)
|
||||
return d, s.sqlError("read domain", err)
|
||||
}
|
||||
|
||||
func (s *Store) GetDomainEffectiveHost(domainUUID string) (string, error) {
|
||||
return s.GetDomainEffectiveHostContext(context.Background(), domainUUID)
|
||||
}
|
||||
|
||||
func (s *Store) GetDomainEffectiveHostContext(ctx context.Context, domainUUID string) (string, error) {
|
||||
var host string
|
||||
err := s.DB.QueryRow(`
|
||||
err := s.DB.QueryRowContext(ctx, `
|
||||
SELECT lower(
|
||||
CASE
|
||||
WHEN d.domain = '' THEN c.fqdn
|
||||
WHEN instr(d.domain, '.') > 0 THEN d.domain
|
||||
WHEN `+s.sqlHasDot("d.domain")+` THEN d.domain
|
||||
ELSE d.domain || '.' || c.fqdn
|
||||
END
|
||||
)
|
||||
FROM domains d
|
||||
JOIN campuses c ON c.uuid = d.campus_uuid
|
||||
WHERE d.uuid = ?
|
||||
WHERE d.uuid = `+s.sqlParam(1)+`
|
||||
`, domainUUID).Scan(&host)
|
||||
return host, err
|
||||
return host, s.sqlError("read domain host", err)
|
||||
}
|
||||
|
||||
func (s *Store) ListDomains() ([]Domain, error) {
|
||||
return s.ListDomainsContext(context.Background())
|
||||
}
|
||||
|
||||
rows, err := s.DB.Query(`
|
||||
func (s *Store) ListDomainsContext(ctx context.Context) ([]Domain, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
@@ -270,7 +271,7 @@ func (s *Store) ListDomains() ([]Domain, error) {
|
||||
`)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, s.sqlError("list domains", err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
@@ -284,7 +285,7 @@ func (s *Store) ListDomains() ([]Domain, error) {
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, s.sqlError("decode domain list", err)
|
||||
}
|
||||
|
||||
out = append(
|
||||
@@ -293,7 +294,7 @@ func (s *Store) ListDomains() ([]Domain, error) {
|
||||
)
|
||||
}
|
||||
|
||||
return out, rows.Err()
|
||||
return out, s.sqlError("list domains", rows.Err())
|
||||
}
|
||||
|
||||
func (s *Store) CreateDomain(
|
||||
@@ -306,118 +307,7 @@ func (s *Store) CreateDomain(
|
||||
instanceFingerprint string,
|
||||
enabled bool,
|
||||
) error {
|
||||
|
||||
name =
|
||||
normalizeDomainName(
|
||||
name,
|
||||
)
|
||||
|
||||
title =
|
||||
strings.TrimSpace(
|
||||
title,
|
||||
)
|
||||
|
||||
domain =
|
||||
normalizeDomainHost(
|
||||
domain,
|
||||
)
|
||||
|
||||
if name == "" {
|
||||
return errors.New("domain name is required")
|
||||
}
|
||||
|
||||
if title == "" {
|
||||
title = name
|
||||
}
|
||||
|
||||
if err := s.ensureCampusExists(campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
placement, err := normalizeDomainPlacement(placement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
instanceFQDN, instanceFingerprint, err = s.normalizeDomainInstance(
|
||||
campusUUID,
|
||||
instanceFQDN,
|
||||
instanceFingerprint,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.ensureDomainUnique(0, campusUUID, name, domain); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enabledValue := 0
|
||||
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
|
||||
domainUUID := uuid.NewString()
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO domains (
|
||||
uuid,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabled,
|
||||
created_at,
|
||||
campus_uuid,
|
||||
placement,
|
||||
instance_fqdn,
|
||||
instance_fingerprint
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
domainUUID,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabledValue,
|
||||
time.Now().Unix(),
|
||||
campusUUID,
|
||||
placement,
|
||||
instanceFQDN,
|
||||
instanceFingerprint,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var hasConfigTable int
|
||||
if err := tx.QueryRow(`
|
||||
SELECT count(*) FROM sqlite_master
|
||||
WHERE type = 'table' AND name = 'config'
|
||||
`).Scan(&hasConfigTable); err != nil {
|
||||
return err
|
||||
}
|
||||
if hasConfigTable != 0 {
|
||||
if _, err := tx.Exec(`
|
||||
INSERT INTO config (domain_uuid, key, value, updated_at)
|
||||
SELECT ?, 'locale',
|
||||
CASE
|
||||
WHEN lower(default_lang) LIKE 'ja%' THEN 'ja'
|
||||
ELSE 'en'
|
||||
END,
|
||||
strftime('%s','now')
|
||||
FROM campuses
|
||||
WHERE uuid = ?
|
||||
ON CONFLICT(domain_uuid, key) DO NOTHING
|
||||
`, domainUUID, campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.EnsureBuiltinAdminGroups()
|
||||
return s.writeDomain(context.Background(), 0, false, campusUUID, name, title, domain, placement, instanceFQDN, instanceFingerprint, enabled)
|
||||
}
|
||||
|
||||
func (s *Store) UpdateDomain(
|
||||
@@ -431,184 +321,85 @@ func (s *Store) UpdateDomain(
|
||||
instanceFingerprint string,
|
||||
enabled bool,
|
||||
) error {
|
||||
|
||||
name =
|
||||
normalizeDomainName(
|
||||
name,
|
||||
)
|
||||
|
||||
title =
|
||||
strings.TrimSpace(
|
||||
title,
|
||||
)
|
||||
|
||||
domain =
|
||||
normalizeDomainHost(
|
||||
domain,
|
||||
)
|
||||
|
||||
if id <= 0 {
|
||||
return errors.New("domain id is required")
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
return errors.New("domain name is required")
|
||||
}
|
||||
|
||||
if title == "" {
|
||||
title = name
|
||||
}
|
||||
|
||||
if err := s.ensureCampusExists(campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
placement, err := normalizeDomainPlacement(placement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
instanceFQDN, instanceFingerprint, err = s.normalizeDomainInstance(
|
||||
campusUUID,
|
||||
instanceFQDN,
|
||||
instanceFingerprint,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if placement == "remote" {
|
||||
var pathCount int
|
||||
if err := s.DB.QueryRow(`
|
||||
SELECT count(*)
|
||||
FROM paths
|
||||
WHERE domain_uuid = (
|
||||
SELECT uuid FROM domains WHERE id = ?
|
||||
)
|
||||
`, id).Scan(&pathCount); err != nil {
|
||||
return err
|
||||
}
|
||||
if pathCount > 0 {
|
||||
return errors.New("domain with local paths cannot become remote")
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.ensureDomainUnique(id, campusUUID, name, domain); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enabledValue := 0
|
||||
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
_, err = tx.Exec(`
|
||||
UPDATE domains
|
||||
SET
|
||||
name = ?,
|
||||
title = ?,
|
||||
domain = ?,
|
||||
enabled = ?,
|
||||
campus_uuid = ?,
|
||||
placement = ?,
|
||||
instance_fqdn = ?,
|
||||
instance_fingerprint = ?
|
||||
WHERE id = ?
|
||||
`,
|
||||
name,
|
||||
title,
|
||||
domain,
|
||||
enabledValue,
|
||||
campusUUID,
|
||||
placement,
|
||||
instanceFQDN,
|
||||
instanceFingerprint,
|
||||
id,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = projectCampusAdminMembershipsTx(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
return s.writeDomain(context.Background(), id, true, campusUUID, name, title, domain, placement, instanceFQDN, instanceFingerprint, enabled)
|
||||
}
|
||||
|
||||
func (s *Store) CountDomains() (int, error) {
|
||||
return s.CountDomainsContext(context.Background())
|
||||
}
|
||||
|
||||
func (s *Store) CountDomainsContext(ctx context.Context) (int, error) {
|
||||
var count int
|
||||
|
||||
err := s.DB.QueryRow(`
|
||||
err := s.DB.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM domains
|
||||
`).Scan(&count)
|
||||
|
||||
return count, err
|
||||
return count, s.sqlError("count domains", err)
|
||||
}
|
||||
|
||||
func (s *Store) DeleteDomain(
|
||||
id int64,
|
||||
) error {
|
||||
var authorityMode string
|
||||
if err := s.DB.QueryRow(`
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
return s.namespaceWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
var err error
|
||||
|
||||
var authorityMode string
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT c.authority_mode
|
||||
FROM domains d
|
||||
JOIN campuses c ON c.uuid = d.campus_uuid
|
||||
WHERE d.id = ?
|
||||
WHERE d.id = `+s.sqlParam(1)+`
|
||||
`, id).Scan(&authorityMode); err != nil {
|
||||
return err
|
||||
}
|
||||
if authorityMode == "managed" {
|
||||
return errors.New("managed campus topology is read-only")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if authorityMode == "managed" {
|
||||
return namespaceValidationError("managed campus topology is read-only")
|
||||
}
|
||||
|
||||
count, err :=
|
||||
s.CountDomains()
|
||||
var count int
|
||||
err = tx.QueryRowContext(ctx, `SELECT count(*) FROM domains`).Scan(&count)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count <= 1 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
if count <= 1 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
|
||||
var pathCount int
|
||||
if err := s.DB.QueryRow(`
|
||||
var pathCount int
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM paths
|
||||
WHERE domain_uuid = (
|
||||
SELECT uuid FROM domains WHERE id = ?
|
||||
SELECT uuid FROM domains WHERE id = `+s.sqlParam(1)+`
|
||||
)
|
||||
`, id).Scan(&pathCount); err != nil {
|
||||
return err
|
||||
}
|
||||
if pathCount > 0 {
|
||||
return errors.New("domain is referenced by paths")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if pathCount > 0 {
|
||||
return namespaceValidationError("domain is referenced by paths")
|
||||
}
|
||||
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec(`
|
||||
if _, err = tx.ExecContext(ctx, `
|
||||
DELETE FROM config
|
||||
WHERE domain_uuid = (SELECT uuid FROM domains WHERE id = ?)
|
||||
WHERE domain_uuid = (SELECT uuid FROM domains WHERE id = `+s.sqlParam(1)+`)
|
||||
`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(`DELETE FROM domains WHERE id = ?`, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = projectCampusAdminMembershipsTx(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM domains WHERE id = `+s.sqlParam(1)+``, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = s.projectCampusAdminMembershipsContext(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func scanDomain(row interface {
|
||||
@@ -650,28 +441,31 @@ func normalizeDomainPlacement(placement string) (string, error) {
|
||||
case "local":
|
||||
case "remote":
|
||||
default:
|
||||
return "", errors.New("invalid domain placement")
|
||||
return "", namespaceValidationError("invalid domain placement")
|
||||
}
|
||||
return placement, nil
|
||||
}
|
||||
|
||||
func (s *Store) normalizeDomainInstance(
|
||||
ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
},
|
||||
campusUUID string,
|
||||
instanceFQDN string,
|
||||
instanceFingerprint string,
|
||||
) (string, string, error) {
|
||||
var authorityMode, campusFQDN string
|
||||
if err := s.DB.QueryRow(`
|
||||
if err := q.QueryRowContext(ctx, `
|
||||
SELECT authority_mode, fqdn
|
||||
FROM campuses
|
||||
WHERE uuid = ?
|
||||
WHERE uuid = `+s.sqlParam(1)+`
|
||||
`, campusUUID).Scan(&authorityMode, &campusFQDN); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
instanceFQDN = normalizeDomainHost(instanceFQDN)
|
||||
if instanceFQDN == "" {
|
||||
if authorityMode == "controller" {
|
||||
return "", "", errors.New(
|
||||
return "", "", namespaceValidationError(
|
||||
"controller campus domain requires an instance FQDN",
|
||||
)
|
||||
}
|
||||
@@ -679,22 +473,22 @@ func (s *Store) normalizeDomainInstance(
|
||||
}
|
||||
if !strings.Contains(instanceFQDN, ".") &&
|
||||
instanceFQDN != "localhost" {
|
||||
return "", "", errors.New("invalid instance FQDN")
|
||||
return "", "", namespaceValidationError("invalid instance FQDN")
|
||||
}
|
||||
instanceFingerprint = strings.ToLower(strings.TrimSpace(instanceFingerprint))
|
||||
if authorityMode == "controller" {
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`
|
||||
if err := q.QueryRowContext(ctx, `
|
||||
SELECT count(*) FROM federation_instances
|
||||
WHERE campus_uuid = ?
|
||||
AND fqdn = ?
|
||||
AND fingerprint = ?
|
||||
WHERE campus_uuid = `+s.sqlParam(1)+`
|
||||
AND fqdn = `+s.sqlParam(2)+`
|
||||
AND fingerprint = `+s.sqlParam(3)+`
|
||||
AND status = 'approved'
|
||||
`, campusUUID, instanceFQDN, instanceFingerprint).Scan(&count); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if count == 0 {
|
||||
return "", "", errors.New(
|
||||
return "", "", namespaceValidationError(
|
||||
"controller domain requires an approved instance fingerprint",
|
||||
)
|
||||
}
|
||||
@@ -702,42 +496,48 @@ func (s *Store) normalizeDomainInstance(
|
||||
return instanceFQDN, instanceFingerprint, nil
|
||||
}
|
||||
|
||||
func (s *Store) ensureCampusExists(campusUUID string) error {
|
||||
func (s *Store) ensureCampusExists(ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}, campusUUID string) error {
|
||||
if strings.TrimSpace(campusUUID) == "" {
|
||||
return errors.New("campus UUID is required")
|
||||
return namespaceValidationError("campus UUID is required")
|
||||
}
|
||||
|
||||
var authorityMode string
|
||||
err := s.DB.QueryRow(`
|
||||
err := q.QueryRowContext(ctx, `
|
||||
SELECT COALESCE(authority_mode, 'standalone')
|
||||
FROM campuses
|
||||
WHERE uuid = ?
|
||||
WHERE uuid = `+s.sqlParam(1)+`
|
||||
`, campusUUID).Scan(&authorityMode)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return errors.New("campus does not exist")
|
||||
return namespaceValidationError("campus does not exist")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if authorityMode == "managed" {
|
||||
return errors.New("managed campus topology is read-only")
|
||||
return namespaceValidationError("managed campus topology is read-only")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) ensureLocalDomain(domainUUID string) error {
|
||||
return s.ensureLocalDomainContext(context.Background(), domainUUID)
|
||||
}
|
||||
|
||||
func (s *Store) ensureLocalDomainContext(ctx context.Context, domainUUID string) error {
|
||||
var placement string
|
||||
err := s.DB.QueryRow(`
|
||||
err := s.DB.QueryRowContext(ctx, `
|
||||
SELECT COALESCE(placement, 'local')
|
||||
FROM domains
|
||||
WHERE uuid = ? AND enabled = 1
|
||||
WHERE uuid = `+s.sqlParam(1)+` AND enabled = 1
|
||||
`, domainUUID).Scan(&placement)
|
||||
if err != nil {
|
||||
return err
|
||||
return s.sqlError("read domain placement", err)
|
||||
}
|
||||
if placement != "local" {
|
||||
return errors.New("remote domain has no local path namespace")
|
||||
return namespaceValidationError("remote domain has no local path namespace")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -789,14 +589,17 @@ func normalizeDomainHost(
|
||||
}
|
||||
|
||||
func (s *Store) ensureDomainUnique(
|
||||
ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
},
|
||||
excludeID int64,
|
||||
campusUUID string,
|
||||
name string,
|
||||
domain string,
|
||||
) error {
|
||||
var campusFQDN string
|
||||
if err := s.DB.QueryRow(`
|
||||
SELECT fqdn FROM campuses WHERE uuid = ?
|
||||
if err := q.QueryRowContext(ctx, `
|
||||
SELECT fqdn FROM campuses WHERE uuid = `+s.sqlParam(1)+`
|
||||
`, campusUUID).Scan(&campusFQDN); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -809,20 +612,20 @@ func (s *Store) ensureDomainUnique(
|
||||
|
||||
var count int
|
||||
|
||||
err := s.DB.QueryRow(`
|
||||
err := q.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM domains d
|
||||
JOIN campuses c ON c.uuid = d.campus_uuid
|
||||
WHERE d.id != ?
|
||||
WHERE d.id != `+s.sqlParam(1)+`
|
||||
AND (
|
||||
lower(d.name) = ?
|
||||
lower(d.name) = `+s.sqlParam(2)+`
|
||||
OR lower(
|
||||
CASE
|
||||
WHEN d.domain = '' THEN c.fqdn
|
||||
WHEN instr(d.domain, '.') > 0 THEN d.domain
|
||||
WHEN `+s.sqlHasDot("d.domain")+` THEN d.domain
|
||||
ELSE d.domain || '.' || c.fqdn
|
||||
END
|
||||
) = ?
|
||||
) = `+s.sqlParam(3)+`
|
||||
)
|
||||
`,
|
||||
excludeID,
|
||||
@@ -835,7 +638,7 @@ func (s *Store) ensureDomainUnique(
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return errors.New("domain name or host already exists")
|
||||
return namespaceValidationError("domain name or host already exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user