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
+170
-168
@@ -1,8 +1,8 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -26,6 +26,9 @@ type Campus struct {
|
||||
}
|
||||
|
||||
func (s *Store) EnsureCampusesSchema() error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return s.ensurePostgreSQLCampusDefaults(context.Background())
|
||||
}
|
||||
_, err := s.DB.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS campuses (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -135,7 +138,11 @@ func (s *Store) EnsureCampusesSchema() error {
|
||||
}
|
||||
|
||||
func (s *Store) GetDefaultCampus() (*Campus, error) {
|
||||
row := s.DB.QueryRow(`
|
||||
return s.GetDefaultCampusContext(context.Background())
|
||||
}
|
||||
|
||||
func (s *Store) GetDefaultCampusContext(ctx context.Context) (*Campus, error) {
|
||||
row := s.DB.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
@@ -157,13 +164,18 @@ func (s *Store) GetDefaultCampus() (*Campus, error) {
|
||||
LIMIT 1
|
||||
`)
|
||||
|
||||
return scanCampus(row)
|
||||
c, err := scanCampus(row)
|
||||
return c, s.sqlError("read default campus", err)
|
||||
}
|
||||
|
||||
func (s *Store) GetCampusByUUID(
|
||||
campusUUID string,
|
||||
) (*Campus, error) {
|
||||
row := s.DB.QueryRow(`
|
||||
return s.GetCampusByUUIDContext(context.Background(), campusUUID)
|
||||
}
|
||||
|
||||
func (s *Store) GetCampusByUUIDContext(ctx context.Context, campusUUID string) (*Campus, error) {
|
||||
row := s.DB.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
@@ -178,16 +190,21 @@ func (s *Store) GetCampusByUUID(
|
||||
enabled,
|
||||
created_at
|
||||
FROM campuses
|
||||
WHERE uuid = ?
|
||||
WHERE uuid = `+s.sqlParam(1)+`
|
||||
AND enabled = 1
|
||||
LIMIT 1
|
||||
`, campusUUID)
|
||||
|
||||
return scanCampus(row)
|
||||
c, err := scanCampus(row)
|
||||
return c, s.sqlError("read campus", err)
|
||||
}
|
||||
|
||||
func (s *Store) ListCampuses() ([]Campus, error) {
|
||||
rows, err := s.DB.Query(`
|
||||
return s.ListCampusesContext(context.Background())
|
||||
}
|
||||
|
||||
func (s *Store) ListCampusesContext(ctx context.Context) ([]Campus, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
uuid,
|
||||
@@ -208,7 +225,7 @@ func (s *Store) ListCampuses() ([]Campus, error) {
|
||||
id
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, s.sqlError("list campuses", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -216,12 +233,12 @@ func (s *Store) ListCampuses() ([]Campus, error) {
|
||||
for rows.Next() {
|
||||
campus, err := scanCampus(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, s.sqlError("decode campus list", err)
|
||||
}
|
||||
campuses = append(campuses, *campus)
|
||||
}
|
||||
|
||||
return campuses, rows.Err()
|
||||
return campuses, s.sqlError("list campuses", rows.Err())
|
||||
}
|
||||
|
||||
func (s *Store) CreateCampus(
|
||||
@@ -235,7 +252,7 @@ func (s *Store) CreateCampus(
|
||||
name = normalizeCampusName(name)
|
||||
title = strings.TrimSpace(title)
|
||||
if name == "" {
|
||||
return errors.New("campus name is required")
|
||||
return namespaceValidationError("campus name is required")
|
||||
}
|
||||
if title == "" {
|
||||
title = name
|
||||
@@ -256,43 +273,22 @@ func (s *Store) CreateCampus(
|
||||
return err
|
||||
}
|
||||
if authorityMode == "managed" {
|
||||
return errors.New(
|
||||
return namespaceValidationError(
|
||||
"managed campus must be added through controller enrollment",
|
||||
)
|
||||
}
|
||||
if err := s.ensureCampusFQDNAvailable(0, fqdn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enabledValue := 0
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
|
||||
_, err = s.DB.Exec(`
|
||||
INSERT INTO campuses (
|
||||
uuid,
|
||||
name,
|
||||
title,
|
||||
default_lang,
|
||||
authority_mode,
|
||||
fqdn,
|
||||
enabled,
|
||||
created_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
uuid.NewString(),
|
||||
name,
|
||||
title,
|
||||
defaultLang,
|
||||
authorityMode,
|
||||
fqdn,
|
||||
enabledValue,
|
||||
time.Now().Unix(),
|
||||
)
|
||||
|
||||
return err
|
||||
return s.namespaceWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
if err := s.ensureCampusFQDNAvailableFrom(ctx, tx, 0, fqdn); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO campuses(uuid,name,title,default_lang,authority_mode,fqdn,enabled,created_at) VALUES (`+s.sqlValues(8)+`)`, uuid.NewString(), name, title, defaultLang, authorityMode, fqdn, enabledValue, time.Now().Unix())
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) UpdateCampus(
|
||||
@@ -305,22 +301,24 @@ func (s *Store) UpdateCampus(
|
||||
enabled bool,
|
||||
) error {
|
||||
if id <= 0 {
|
||||
return errors.New("campus id is required")
|
||||
return namespaceValidationError("campus id is required")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
var existingMode string
|
||||
if err := s.DB.QueryRow(`
|
||||
SELECT authority_mode FROM campuses WHERE id = ?
|
||||
if err := s.DB.QueryRowContext(ctx, `
|
||||
SELECT authority_mode FROM campuses WHERE id = `+s.sqlParam(1)+`
|
||||
`, id).Scan(&existingMode); err != nil {
|
||||
return err
|
||||
return s.sqlError("read campus authority", err)
|
||||
}
|
||||
if existingMode == "managed" {
|
||||
return errors.New("managed campus settings are inherited")
|
||||
return namespaceValidationError("managed campus settings are inherited")
|
||||
}
|
||||
|
||||
name = normalizeCampusName(name)
|
||||
title = strings.TrimSpace(title)
|
||||
if name == "" {
|
||||
return errors.New("campus name is required")
|
||||
return namespaceValidationError("campus name is required")
|
||||
}
|
||||
if title == "" {
|
||||
title = name
|
||||
@@ -340,163 +338,165 @@ func (s *Store) UpdateCampus(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.ensureCampusFQDNAvailable(id, fqdn); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !enabled {
|
||||
var domainCount int
|
||||
if err := s.DB.QueryRow(`
|
||||
return s.namespaceWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
// Re-read inherited status under the same lock as the mutation.
|
||||
if err := tx.QueryRowContext(ctx, `SELECT authority_mode FROM campuses WHERE id=`+s.sqlParam(1), id).Scan(&existingMode); err != nil {
|
||||
return err
|
||||
}
|
||||
if existingMode == "managed" {
|
||||
return namespaceValidationError("managed campus settings are inherited")
|
||||
}
|
||||
if err := s.ensureCampusFQDNAvailableFrom(ctx, tx, id, fqdn); err != nil {
|
||||
return err
|
||||
}
|
||||
if !enabled {
|
||||
var domainCount int
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM domains
|
||||
WHERE campus_uuid = (
|
||||
SELECT uuid
|
||||
FROM campuses
|
||||
WHERE id = ?
|
||||
WHERE id = `+s.sqlParam(1)+`
|
||||
)
|
||||
`, id).Scan(&domainCount); err != nil {
|
||||
return err
|
||||
}
|
||||
if domainCount > 0 {
|
||||
return errors.New("campus is referenced by domains")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if domainCount > 0 {
|
||||
return namespaceValidationError("campus is referenced by domains")
|
||||
}
|
||||
|
||||
var enabledCount int
|
||||
if err := s.DB.QueryRow(`
|
||||
var enabledCount int
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM campuses
|
||||
WHERE enabled = 1
|
||||
AND id != ?
|
||||
AND id != `+s.sqlParam(1)+`
|
||||
`, id).Scan(&enabledCount); err != nil {
|
||||
return err
|
||||
return err
|
||||
}
|
||||
if enabledCount == 0 {
|
||||
return namespaceValidationError("at least one enabled campus is required")
|
||||
}
|
||||
}
|
||||
if enabledCount == 0 {
|
||||
return errors.New("at least one enabled campus is required")
|
||||
|
||||
enabledValue := 0
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
}
|
||||
|
||||
enabledValue := 0
|
||||
if enabled {
|
||||
enabledValue = 1
|
||||
}
|
||||
|
||||
_, err = s.DB.Exec(`
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
UPDATE campuses
|
||||
SET
|
||||
name = ?,
|
||||
title = ?,
|
||||
default_lang = ?,
|
||||
authority_mode = ?,
|
||||
fqdn = ?,
|
||||
enabled = ?
|
||||
WHERE id = ?
|
||||
name = `+s.sqlParam(1)+`,
|
||||
title = `+s.sqlParam(2)+`,
|
||||
default_lang = `+s.sqlParam(3)+`,
|
||||
authority_mode = `+s.sqlParam(4)+`,
|
||||
fqdn = `+s.sqlParam(5)+`,
|
||||
enabled = `+s.sqlParam(6)+`
|
||||
WHERE id = `+s.sqlParam(7)+`
|
||||
`,
|
||||
name,
|
||||
title,
|
||||
defaultLang,
|
||||
authorityMode,
|
||||
fqdn,
|
||||
enabledValue,
|
||||
id,
|
||||
)
|
||||
name,
|
||||
title,
|
||||
defaultLang,
|
||||
authorityMode,
|
||||
fqdn,
|
||||
enabledValue,
|
||||
id,
|
||||
)
|
||||
|
||||
return err
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) DeleteCampus(
|
||||
id int64,
|
||||
) error {
|
||||
var targetEnabled int
|
||||
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 targetEnabled int
|
||||
var authorityMode string
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT enabled, authority_mode
|
||||
FROM campuses
|
||||
WHERE id = ?
|
||||
WHERE id = `+s.sqlParam(1)+`
|
||||
`, id).Scan(&targetEnabled, &authorityMode); err != nil {
|
||||
return err
|
||||
}
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`
|
||||
return err
|
||||
}
|
||||
var count int
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM campuses
|
||||
`).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count <= 1 {
|
||||
return errors.New("at least one campus is required")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if count <= 1 {
|
||||
return namespaceValidationError("at least one campus is required")
|
||||
}
|
||||
|
||||
if targetEnabled != 0 {
|
||||
var enabledCount int
|
||||
if err := s.DB.QueryRow(`
|
||||
if targetEnabled != 0 {
|
||||
var enabledCount int
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM campuses
|
||||
WHERE enabled = 1
|
||||
AND id != ?
|
||||
AND id != `+s.sqlParam(1)+`
|
||||
`, id).Scan(&enabledCount); err != nil {
|
||||
return err
|
||||
return err
|
||||
}
|
||||
if enabledCount == 0 {
|
||||
return namespaceValidationError("at least one enabled campus is required")
|
||||
}
|
||||
}
|
||||
if enabledCount == 0 {
|
||||
return errors.New("at least one enabled campus is required")
|
||||
if authorityMode == "managed" {
|
||||
return s.deleteManagedCampusTx(ctx, tx, id)
|
||||
}
|
||||
}
|
||||
if authorityMode == "managed" {
|
||||
return s.deleteManagedCampus(id)
|
||||
}
|
||||
|
||||
var domainCount int
|
||||
if err := s.DB.QueryRow(`
|
||||
var domainCount int
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT count(*)
|
||||
FROM domains
|
||||
WHERE campus_uuid = (
|
||||
SELECT uuid
|
||||
FROM campuses
|
||||
WHERE id = ?
|
||||
WHERE id = `+s.sqlParam(1)+`
|
||||
)
|
||||
`, id).Scan(&domainCount); err != nil {
|
||||
return err
|
||||
}
|
||||
if domainCount > 0 {
|
||||
return errors.New("campus is referenced by domains")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if domainCount > 0 {
|
||||
return namespaceValidationError("campus is referenced by domains")
|
||||
}
|
||||
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var campusUUID string
|
||||
if err := tx.QueryRow(`
|
||||
SELECT uuid FROM campuses WHERE id = ?
|
||||
var campusUUID string
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT uuid FROM campuses WHERE id = `+s.sqlParam(1)+`
|
||||
`, id).Scan(&campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`
|
||||
DELETE FROM federation_instances WHERE campus_uuid = ?
|
||||
`, campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`
|
||||
DELETE FROM campus_tokens WHERE campus_uuid = ?
|
||||
`, campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.Exec(`
|
||||
DELETE FROM campuses WHERE id = ?
|
||||
return err
|
||||
}
|
||||
if err := s.deleteFederationCampusTx(ctx, tx, campusUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM campuses WHERE id = `+s.sqlParam(1)+`
|
||||
`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return tx.Commit()
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func normalizeCampusName(name string) string {
|
||||
@@ -511,7 +511,7 @@ func normalizeCampusLanguage(raw string) (string, error) {
|
||||
}
|
||||
tag, err := language.Parse(raw)
|
||||
if err != nil {
|
||||
return "", errors.New("invalid campus default language")
|
||||
return "", namespaceValidationError("invalid campus default language")
|
||||
}
|
||||
return tag.String(), nil
|
||||
}
|
||||
@@ -526,38 +526,40 @@ func normalizeCampusAuthority(mode, fqdn string) (string, string, error) {
|
||||
case "standalone", "controller":
|
||||
case "managed":
|
||||
default:
|
||||
return "", "", errors.New("invalid campus authority mode")
|
||||
return "", "", namespaceValidationError("invalid campus authority mode")
|
||||
}
|
||||
if fqdn == "" {
|
||||
return "", "", errors.New("campus FQDN is required")
|
||||
return "", "", namespaceValidationError("campus FQDN is required")
|
||||
}
|
||||
if !strings.Contains(fqdn, ".") && fqdn != "localhost" {
|
||||
return "", "", errors.New("invalid campus FQDN")
|
||||
return "", "", namespaceValidationError("invalid campus FQDN")
|
||||
}
|
||||
return mode, fqdn, nil
|
||||
}
|
||||
|
||||
func (s *Store) ensureCampusFQDNAvailable(id int64, fqdn string) error {
|
||||
func (s *Store) ensureCampusFQDNAvailableFrom(ctx context.Context, q interface {
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}, id int64, fqdn string) error {
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`
|
||||
if err := q.QueryRowContext(ctx, `
|
||||
SELECT count(*) FROM campuses
|
||||
WHERE id != ? AND lower(fqdn) = lower(?)
|
||||
WHERE id != `+s.sqlParam(1)+` AND lower(fqdn) = lower(`+s.sqlParam(2)+`)
|
||||
`, id, fqdn).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("campus FQDN already exists")
|
||||
return namespaceValidationError("campus FQDN already exists")
|
||||
}
|
||||
|
||||
if err := s.DB.QueryRow(`
|
||||
if err := q.QueryRowContext(ctx, `
|
||||
WITH effective_hosts AS (
|
||||
SELECT lower(
|
||||
CASE
|
||||
WHEN d.domain = '' THEN
|
||||
CASE WHEN c.id = ? THEN ? ELSE c.fqdn END
|
||||
WHEN instr(d.domain, '.') > 0 THEN d.domain
|
||||
CASE WHEN c.id = `+s.sqlParam(1)+` THEN `+s.sqlParam(2)+` ELSE c.fqdn END
|
||||
WHEN `+s.sqlHasDot("d.domain")+` THEN d.domain
|
||||
ELSE d.domain || '.' ||
|
||||
CASE WHEN c.id = ? THEN ? ELSE c.fqdn END
|
||||
CASE WHEN c.id = `+s.sqlParam(3)+` THEN `+s.sqlParam(4)+` ELSE c.fqdn END
|
||||
END
|
||||
) AS host
|
||||
FROM domains d
|
||||
@@ -566,12 +568,12 @@ func (s *Store) ensureCampusFQDNAvailable(id int64, fqdn string) error {
|
||||
SELECT count(*) FROM (
|
||||
SELECT host FROM effective_hosts
|
||||
GROUP BY host HAVING count(*) > 1
|
||||
)
|
||||
) AS duplicate_hosts
|
||||
`, id, fqdn, id, fqdn).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("campus FQDN conflicts with a domain host")
|
||||
return namespaceValidationError("campus FQDN conflicts with a domain host")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user