Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/nais/api

go 1.26.10
go 1.26.11

tool (
github.com/99designs/gqlgen
Expand Down Expand Up @@ -41,7 +41,7 @@ require (
github.com/nais/api/pkg/apiclient v0.0.0-20250219111538-2b76a0fd6ed9
github.com/nais/bifrost v0.0.0-20260106105449-911627ac2c61
github.com/nais/liberator v0.0.0-20261008133002-00075fb0d007
github.com/nais/pgrator/pkg/api v0.0.0-20261106143424-e72706ac0330
github.com/nais/pgrator/pkg/api v0.0.0-20261113135456-297e807a50e6
github.com/nais/tester v0.2.0
github.com/nais/unleasherator v0.0.0-20251216221129-efebc54203fe
github.com/nais/v13s/pkg/api v0.0.0-20261105092527-4fb2d5f61e67
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,8 @@ github.com/nais/liberator v0.0.0-20261008133002-00075fb0d007 h1:F3ChkijLTpjxqGpQ
github.com/nais/liberator v0.0.0-20261008133002-00075fb0d007/go.mod h1:PI0BYiAkX+ike2N1ZIOns0aUkhgiNKiCmy+YGKDR2R0=
github.com/nais/pgrator/pkg/api v0.0.0-20261106143424-e72706ac0330 h1:bPVghfEV8ILMO9njAlBO/L/Vd6X9UkSedsSveztueSA=
github.com/nais/pgrator/pkg/api v0.0.0-20261106143424-e72706ac0330/go.mod h1:jwBmSICTVLZyeJyD6ck+LLb2V5FsgsUbuCbfbHosew0=
github.com/nais/pgrator/pkg/api v0.0.0-20261113135456-297e807a50e6 h1:7bg8b4tTjkOjpLuaLZ0zd49OUefm3RImWDgSsAxrT3w=
github.com/nais/pgrator/pkg/api v0.0.0-20261113135456-297e807a50e6/go.mod h1:uMfm7kYuZxUNDMdf9sLv7MP2Y7RvQYWcI61NEd2Ni5c=
github.com/nais/tester v0.2.0 h1:lcTkDP52ddw9l5s3KC4snUP+GlUpZMUtJ3XR2vF4G0w=
github.com/nais/tester v0.2.0/go.mod h1:Pp7CtcVk/NZI3z0MW7V4j8CPMG5lhKZeAV4IaJKcslo=
github.com/nais/unleasherator v0.0.0-20251216221129-efebc54203fe h1:CdRVopOihru4tXVwKZjhg6C8SbPLCQYOhJKpjBZYhjg=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apiVersion: nais.io/v1
kind: Postgres
metadata:
name: pitr-source
namespace: pg-crud-team
uid: pitr-source-uid
spec:
majorVersion: "18"
highAvailability: true
extensions:
- name: pg_trgm
resources:
cpu: 250m
memory: 1Gi
diskSize: 20Gi
status:
clusterName: pg-pitr-source-main
ownership:
phase: Managed
clusterUID: pitr-source-cluster-uid
---
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: pg-pitr-source-main
namespace: pg-crud-team
uid: pitr-source-cluster-uid
ownerReferences:
- apiVersion: nais.io/v1
kind: Postgres
name: pitr-source
uid: pitr-source-uid
controller: true
spec:
plugins:
- name: barman-cloud.cloudnative-pg.io
parameters:
barmanObjectName: pitr-source-archive
status: {}
---
apiVersion: barmancloud.cnpg.io/v1
kind: ObjectStore
metadata:
name: pitr-source-archive
namespace: pg-crud-team
spec:
configuration:
destinationPath: gs://pitr-source-archive
status:
serverRecoveryWindow:
pg-pitr-source-main:
firstRecoverabilityPoint: "2026-09-29T00:00:00Z"
100 changes: 100 additions & 0 deletions integration_tests/postgres_pitr.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
local member = User.new("pitr-member", "pitr-member@usersen.com")
local outsider = User.new("pitr-outsider", "pitr-outsider@usersen.com")
local team = Team.new("pg-crud-team", "Postgres PITR", "#pitr")
team:addMember(member)
local otherTeam = Team.new("pg-pitr-other", "PITR source isolation", "#pitr-other")
otherTeam:addMember(member)
Helper.readK8sResources("k8s_resources/postgres_crud")
Helper.readK8sResources("k8s_resources/postgres_pitr")

local function restore(name, source, target, extra)
return string.format([[mutation { createPostgres(input: {
teamSlug: "pg-crud-team", environmentName: "dev", name: "%s", majorVersion: "18",
recovery: { sourcePostgres: "%s", targetTime: "%s" }, %s
}) { postgres { name extensions highAvailability resources { cpu memory diskSize }
recovery { sourcePostgres targetTime completed failureMessage } } } }]], name, source, target, extra or "")
end

Test.gql("PITR source exposes the preserved physical archive lower bound", function(t)
t.addHeader("x-user-email", member:email())
t.query [[{ team(slug: "pg-crud-team") { environment(name: "dev") { postgres(name: "pitr-source") { earliestRestoreTime } } } }]]
t.check { data = { team = { environment = { postgres = { earliestRestoreTime = "2026-09-29T00:00:00Z" } } } } }
end)

Test.gql("PITR uses normal create authorization", function(t)
t.addHeader("x-user-email", outsider:email())
t.query(restore("denied-restore", "pitr-source", "2026-09-30T12:00:00Z"))
t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("You are authenticated") } } }
end)

Test.gql("PITR creates an independent Postgres and inherits source configuration", function(t)
t.addHeader("x-user-email", member:email())
t.query(restore("pitr-restored", "pitr-source", "2026-09-30T14:00:00+02:00"))
t.check { data = { createPostgres = { postgres = {
name = "pitr-restored", extensions = { "pg_trgm" }, highAvailability = true,
resources = { cpu = "250m", memory = "1Gi", diskSize = "20Gi" },
recovery = { sourcePostgres = "pitr-source", targetTime = "2026-09-30T12:00:00Z", completed = false, failureMessage = Null },
} } } }
end)

Test.k8s("PITR pins source UID and physical archive identity", function(t)
t.check("nais.io/v1", "postgres", "dev", team:slug(), "pitr-restored", {
apiVersion = "nais.io/v1",
kind = "Postgres",
metadata = Ignore(),
spec = {
majorVersion = "18",
highAvailability = true,
extensions = { { name = "pg_trgm" } },
resources = { cpu = "250m", memory = "1Gi", diskSize = "20Gi" },
recovery = { sourcePostgres = "pitr-source", sourceUID = "pitr-source-uid", bucketName = "pitr-source-archive", serverName = "pg-pitr-source-main", targetTime = "2026-09-30T12:00:00Z" },
},
})
end)

for _, case in ipairs({
{ name = "before archive", source = "pitr-source", time = "2026-09-28T12:00:00Z", message = "earliest recoverable" },
{ name = "missing source", source = "unknown", time = "2026-09-30T12:00:00Z", message = "not found in this team and environment" },
{ name = "missing physical source", source = "branchless", time = "2026-09-30T12:00:00Z", message = "no recoverable cluster" },
{ name = "smaller disk", source = "pitr-source", time = "2026-09-30T12:00:00Z", extra = 'diskSize: "10Gi"', message = "must not be smaller" },
{ name = "missing source extension", source = "pitr-source", time = "2026-09-30T12:00:00Z", extra = "extensions: []", message = "must include source extension" },
}) do
Test.gql("PITR rejects " .. case.name, function(t)
t.addHeader("x-user-email", member:email())
t.query(restore("invalid-restore", case.source, case.time, case.extra))
t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains(case.message) } } }
end)
end

Test.gql("PITR cannot change PostgreSQL major version", function(t)
t.addHeader("x-user-email", member:email())
t.query [[mutation { createPostgres(input: {
teamSlug: "pg-crud-team", environmentName: "dev", name: "wrong-major", majorVersion: "17",
recovery: { sourcePostgres: "pitr-source", targetTime: "2026-09-30T12:00:00Z" }
}) { postgres { name } } }]]
t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("source PostgreSQL major version 18") } } }
end)

Test.gql("PITR source lookup cannot cross environments", function(t)
t.addHeader("x-user-email", member:email())
t.query [[mutation { createPostgres(input: {
teamSlug: "pg-crud-team", environmentName: "dev-gcp", name: "cross-env", majorVersion: "18",
recovery: { sourcePostgres: "pitr-source", targetTime: "2026-09-30T12:00:00Z" }
}) { postgres { name } } }]]
t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("not found in this team and environment") } } }
end)

Test.gql("PITR creation is audited with source and UTC target", function(t)
t.addHeader("x-user-email", member:email())
t.query [[{ team(slug: "pg-crud-team") { activityLog(filter: { activityTypes: [POSTGRES_CREATED] }) { nodes { message } } } }]]
t.check { data = { team = { activityLog = { nodes = { { message = "Requested Postgres recovery from pitr-source to 2026-09-30T12:00:00Z" } } } } } }
end)

Test.gql("PITR source lookup cannot cross teams even for a member of both", function(t)
t.addHeader("x-user-email", member:email())
t.query [[mutation { createPostgres(input: {
teamSlug: "pg-pitr-other", environmentName: "dev", name: "cross-team", majorVersion: "18",
recovery: { sourcePostgres: "pitr-source", targetTime: "2026-09-30T12:00:00Z" }
}) { postgres { name } } }]]
t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("not found in this team and environment") } } }
end)
Loading
Loading