Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf3334878a |
No files matched your search
@@ -1,69 +0,0 @@
|
||||
name: Approved narrow auth schema reconciliation
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- ops/auth-schema-stage
|
||||
- ops/auth-schema-production
|
||||
jobs:
|
||||
reconcile:
|
||||
runs-on: canina
|
||||
steps:
|
||||
- name: Authenticated host narrow schema operation (no deployment)
|
||||
shell: bash
|
||||
env:
|
||||
CI_EVENT: ${{ github.event_name }}
|
||||
CI_REF: ${{ github.ref }}
|
||||
SQL_SHA: 8a22f3e89efb3dacc24f3ed9337ae87d0b4345da003a607bc1994478f69c6feb
|
||||
APPROVAL_ID: CEO-AUTH-SCHEMA-20261025
|
||||
DEPLOY_SHA: ${{ github.sha }}
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
PRODUCTION_SCHEMA_APPROVAL: ${{ secrets.PRODUCTION_AUTH_SCHEMA_APPROVAL }}
|
||||
REVIEWED_SCHEMA_COMMIT: ${{ secrets.REVIEWED_AUTH_SCHEMA_COMMIT }}
|
||||
STAGE_AUTH_ACCEPTANCE: ${{ secrets.STAGE_AUTH_SCHEMA_ACCEPTANCE }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ "$CI_EVENT" == push ]]
|
||||
case "$CI_REF" in
|
||||
refs/heads/ops/auth-schema-stage) TARGET_ENV=stage ;;
|
||||
refs/heads/ops/auth-schema-production) TARGET_ENV=production ;;
|
||||
*) exit 2 ;;
|
||||
esac
|
||||
[[ "$SQL_SHA" =~ ^[0-9a-f]{64}$ && "$DEPLOY_SHA" =~ ^[0-9a-f]{40}$ ]]
|
||||
[[ "$APPROVAL_ID" =~ ^[A-Za-z0-9_-]{4,100}$ ]]
|
||||
[[ "$REVIEWED_SCHEMA_COMMIT" == "$DEPLOY_SHA" ]]
|
||||
schema_ok=no; auth_ok=no
|
||||
if [[ "$TARGET_ENV" == production ]]; then
|
||||
[[ -n "$PRODUCTION_SCHEMA_APPROVAL" && "$PRODUCTION_SCHEMA_APPROVAL" == "$DEPLOY_SHA:$SQL_SHA:$APPROVAL_ID" ]]
|
||||
[[ "$STAGE_AUTH_ACCEPTANCE" == "$DEPLOY_SHA:$SQL_SHA" ]]
|
||||
schema_ok=yes; auth_ok=yes
|
||||
fi
|
||||
[[ -n "$DEPLOY_KEY" && -n "$DEPLOY_KNOWN_HOSTS" ]]
|
||||
umask 077
|
||||
scratch=$(mktemp -d)
|
||||
trap 'rm -rf "$scratch"' EXIT
|
||||
printf '%s\n' "$DEPLOY_KEY" > "$scratch/key"
|
||||
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$scratch/known_hosts"
|
||||
unset DEPLOY_KEY DEPLOY_KNOWN_HOSTS PRODUCTION_SCHEMA_APPROVAL
|
||||
ssh-keygen -F '[87.248.133.138]:2234' -f "$scratch/known_hosts" >/dev/null
|
||||
ssh -F /dev/null -T -p 2234 -i "$scratch/key" -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$scratch/known_hosts" -o GlobalKnownHostsFile=/dev/null devops@87.248.133.138 "sudo -n bash -s -- $DEPLOY_SHA $TARGET_ENV $SQL_SHA $schema_ok $auth_ok" <<'HOST'
|
||||
set -euo pipefail
|
||||
set +x
|
||||
umask 077
|
||||
sha=$1; target=$2; sql_sha=$3
|
||||
[[ ! -e /.dockerenv && $(id -u) == 0 ]]
|
||||
work=$(mktemp -d /opt/services/canina/auth-reconcile-XXXXXX)
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
git clone --quiet --no-checkout /opt/services/canina/stage "$work"
|
||||
remote=$(git -C /opt/services/canina/stage remote get-url origin)
|
||||
git -C "$work" fetch --quiet "$remote" "$sha"
|
||||
git -C "$work" checkout --quiet --detach "$sha"
|
||||
cd "$work"
|
||||
[[ $(git rev-parse HEAD) == "$sha" ]]
|
||||
if [[ $target == production ]]; then
|
||||
[[ $4 == yes && $5 == yes ]]
|
||||
export PRODUCTION_SCHEMA_APPROVED="$sha:$sql_sha" STAGE_AUTH_ACCEPTANCE_APPROVED="$sha:$sql_sha"
|
||||
fi
|
||||
GITHUB_ACTIONS=true bash scripts/ci-auth-schema-reconcile.sh "$target" "$sha" "$sql_sha"
|
||||
HOST
|
||||
@@ -10,7 +10,7 @@ jobs:
|
||||
deploy:
|
||||
runs-on: canina
|
||||
steps:
|
||||
- name: Authenticated immutable host deployment (Production acceptance gated)
|
||||
- name: Authenticated host Stage deployment (Production blocked)
|
||||
shell: bash
|
||||
env:
|
||||
BRANCH: ${{ github.ref_name }}
|
||||
@@ -20,7 +20,7 @@ jobs:
|
||||
run: |
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ "${BRANCH}" == develop || "${BRANCH}" == main ]] || exit 2
|
||||
[[ "${BRANCH}" == develop ]] || { echo 'Production blocked' >&2; exit 2; }
|
||||
[[ "${DEPLOY_SHA}" =~ ^[0-9a-f]{40}$ ]] || exit 2
|
||||
# Provision DEPLOY_KNOWN_HOSTS from an independently verified host key.
|
||||
# Never use baked-in /opt/services, runner SSH keys or docker.sock.
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
name: Incident restore production backend 20261026
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- ops/recover-production-20261026-reviewed
|
||||
jobs:
|
||||
recover:
|
||||
runs-on: canina
|
||||
steps:
|
||||
- name: Restore last working backend only through pinned host boundary
|
||||
shell: bash
|
||||
env:
|
||||
DEPLOY_SHA: ${{ github.sha }}
|
||||
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
|
||||
DEPLOY_KNOWN_HOSTS: ${{ secrets.DEPLOY_KNOWN_HOSTS }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ "$DEPLOY_SHA" =~ ^[0-9a-f]{40}$ ]]
|
||||
[[ -n "$DEPLOY_KEY" && -n "$DEPLOY_KNOWN_HOSTS" ]]
|
||||
umask 077
|
||||
scratch=$(mktemp -d)
|
||||
trap 'rm -rf "$scratch"' EXIT
|
||||
printf '%s\n' "$DEPLOY_KEY" > "$scratch/key"
|
||||
printf '%s\n' "$DEPLOY_KNOWN_HOSTS" > "$scratch/known_hosts"
|
||||
unset DEPLOY_KEY DEPLOY_KNOWN_HOSTS
|
||||
ssh-keygen -F '[87.248.133.138]:2234' -f "$scratch/known_hosts" >/dev/null
|
||||
ssh -F /dev/null -T -p 2234 -i "$scratch/key" -o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none -o StrictHostKeyChecking=yes -o UserKnownHostsFile="$scratch/known_hosts" -o GlobalKnownHostsFile=/dev/null devops@87.248.133.138 "sudo -n bash -s -- $DEPLOY_SHA" <<'HOST'
|
||||
set -euo pipefail
|
||||
set +x
|
||||
umask 077
|
||||
[[ $# == 1 && $1 =~ ^[0-9a-f]{40}$ && $(id -u) == 0 && ! -e /.dockerenv ]]
|
||||
work=$(mktemp -d /opt/services/canina/incident-ci-XXXXXX)
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
git clone --quiet --no-checkout /opt/services/canina/stage "$work"
|
||||
git -C "$work" fetch --quiet https://git.parsaaghayi.ir/parsa/canina.git "$1"
|
||||
git -C "$work" checkout --quiet --detach "$1"
|
||||
[[ $(git -C "$work" rev-parse HEAD) == "$1" ]]
|
||||
python3 "$work/scripts/test-recovery-20261026.py"
|
||||
GITHUB_ACTIONS=true python3 "$work/scripts/recover-production-20261026.py"
|
||||
HOST
|
||||
@@ -16,7 +16,3 @@ playwright-report/
|
||||
playwright/.auth/
|
||||
blob-report/
|
||||
|
||||
|
||||
# Python CI helpers must not dirty immutable release checkouts.
|
||||
__pycache__/
|
||||
*.pyc
|
||||
+9
-7
@@ -1,4 +1,4 @@
|
||||
FROM node:22-alpine AS app-builder
|
||||
FROM node:20-alpine AS app-builder
|
||||
USER root
|
||||
WORKDIR /app
|
||||
ENV NEXT_TELEMETRY_DISABLED=1
|
||||
@@ -7,17 +7,18 @@ ENV NEXT_PUBLIC_API_URL=https://api.canina.ir/api
|
||||
# Optional: Use Nexus as npm registry
|
||||
ARG NPM_REGISTRY=
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then \
|
||||
npm config set registry "$NPM_REGISTRY"; \
|
||||
npm config set registry "$NPM_REGISTRY" && \
|
||||
npm config set strict-ssl false; \
|
||||
fi
|
||||
|
||||
COPY frontend/application/package*.json ./
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit && test -x node_modules/.bin/next
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit
|
||||
COPY frontend/application ./
|
||||
ARG NEXT_PUBLIC_API_URL
|
||||
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
|
||||
RUN npm run build
|
||||
|
||||
FROM node:22-alpine AS admin-builder
|
||||
FROM node:20-alpine AS admin-builder
|
||||
USER root
|
||||
WORKDIR /app
|
||||
ENV VITE_API_URL=https://api.canina.ir/api
|
||||
@@ -25,17 +26,18 @@ ENV VITE_API_URL=https://api.canina.ir/api
|
||||
# Optional: Use Nexus as npm registry (inherits build-arg)
|
||||
ARG NPM_REGISTRY=
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then \
|
||||
npm config set registry "$NPM_REGISTRY"; \
|
||||
npm config set registry "$NPM_REGISTRY" && \
|
||||
npm config set strict-ssl false; \
|
||||
fi
|
||||
|
||||
COPY frontend/admin-panel/package*.json ./
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit && test -x node_modules/.bin/vite
|
||||
RUN npm ci --include=dev --prefer-offline --no-audit
|
||||
COPY frontend/admin-panel ./
|
||||
ARG VITE_API_URL
|
||||
ENV VITE_API_URL=$VITE_API_URL
|
||||
RUN npm run build
|
||||
|
||||
FROM node:22-alpine
|
||||
FROM node:20-alpine
|
||||
USER root
|
||||
RUN addgroup -S nginx 2>/dev/null || true && adduser -S nginx -G nginx 2>/dev/null || true
|
||||
RUN mkdir -p /var/log/nginx /var/cache/nginx /run/nginx /etc/nginx/conf.d /etc/nginx/http.d
|
||||
|
||||
@@ -12,6 +12,7 @@ ENV npm_config_cache=/tmp/npm-cache
|
||||
ARG NPM_REGISTRY=
|
||||
RUN if [ -n "$NPM_REGISTRY" ]; then \
|
||||
npm config set registry "$NPM_REGISTRY" && \
|
||||
npm config set strict-ssl false && \
|
||||
echo "Using npm registry: $NPM_REGISTRY"; \
|
||||
fi
|
||||
|
||||
|
||||
@@ -274,6 +274,42 @@ describe("Admin BlogsService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("explicit author contract", () => {
|
||||
beforeEach(() => {
|
||||
mockPrisma.blog.create.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
mockPrisma.blog.findUnique.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
mockPrisma.blog.update.mockResolvedValue({ id: "b1", slug: "post" });
|
||||
});
|
||||
|
||||
it.each([
|
||||
["omitted", {}, { connect: { id: "current-user" } }],
|
||||
["legacy empty", { authorId: "" }, { connect: { id: "current-user" } }],
|
||||
["selected", { authorId: "selected-user" }, { connect: { id: "selected-user" } }],
|
||||
["authorless", { authorId: null }, undefined],
|
||||
])("creates with %s author without inventing an identity", async (_label, input, expected) => {
|
||||
await service.createBlog({ title: "Post", slug: "post", content: "Content", ...input }, "current-user");
|
||||
const payload = mockPrisma.blog.create.mock.calls[0][0].data;
|
||||
expect(payload.author).toEqual(expected);
|
||||
if (expected === undefined) expect(payload).not.toHaveProperty("author");
|
||||
expect(payload).not.toHaveProperty("authorId");
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("post");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["omitted", {}, undefined],
|
||||
["legacy empty", { authorId: "" }, undefined],
|
||||
["selected", { authorId: "selected-user" }, { connect: { id: "selected-user" } }],
|
||||
["authorless", { authorId: null }, { disconnect: true }],
|
||||
])("updates with %s author while preserving omitted values", async (_label, input, expected) => {
|
||||
await service.updateBlog("b1", input);
|
||||
const payload = mockPrisma.blog.update.mock.calls[0][0].data;
|
||||
expect(payload.author).toEqual(expected);
|
||||
if (expected === undefined) expect(payload).not.toHaveProperty("author");
|
||||
expect(payload).not.toHaveProperty("authorId");
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith("post");
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateBlog", () => {
|
||||
it("should throw NotFoundException if blog does not exist", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
||||
|
||||
@@ -185,7 +185,9 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
const targetAuthorId = authorId || currentUserId;
|
||||
// Explicit null requests Organization attribution; omission/legacy empty
|
||||
// retains the authenticated-author default. No synthetic User is created.
|
||||
const targetAuthorId = authorId === null ? null : authorId || currentUserId;
|
||||
const now = new Date();
|
||||
const isFutureSchedule = parsedScheduledAt && parsedScheduledAt > now;
|
||||
|
||||
@@ -239,7 +241,9 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
publishedAt: finalPublishedAt,
|
||||
scheduledAt: parsedScheduledAt,
|
||||
contentUpdatedAt: parsedContentUpdatedAt,
|
||||
author: { connect: { id: targetAuthorId } },
|
||||
...(targetAuthorId !== null
|
||||
? { author: { connect: { id: targetAuthorId } } }
|
||||
: {}),
|
||||
...(categoryId ? { category: { connect: { id: categoryId } } } : {}),
|
||||
...(tagConnectOrCreate.length > 0
|
||||
? { tags: { connectOrCreate: tagConnectOrCreate } }
|
||||
@@ -344,7 +348,12 @@ export class BlogsService implements OnModuleInit, OnModuleDestroy {
|
||||
...(readingTime !== undefined
|
||||
? { readingTime: Number(readingTime) }
|
||||
: {}),
|
||||
...(authorId ? { author: { connect: { id: authorId } } } : {}),
|
||||
// null clears attribution; omitted/legacy empty leaves it unchanged.
|
||||
...(authorId === null
|
||||
? { author: { disconnect: true } }
|
||||
: authorId
|
||||
? { author: { connect: { id: authorId } } }
|
||||
: {}),
|
||||
...(categoryId !== undefined
|
||||
? categoryId
|
||||
? { category: { connect: { id: categoryId } } }
|
||||
|
||||
@@ -26,16 +26,6 @@ describe("RevalidationService", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it("uses the Next server rather than the nginx API proxy as production fallback", async () => {
|
||||
delete process.env.FRONTEND_INTERNAL_URL;
|
||||
delete process.env.FRONTEND_URL;
|
||||
delete process.env.NEXT_PUBLIC_SITE_URL;
|
||||
process.env.NODE_ENV = "production";
|
||||
const fetchSpy = jest.spyOn(global, "fetch").mockResolvedValue({ ok: true } as Response);
|
||||
await service.revalidate(["blogs"], ["/blog"]);
|
||||
expect(fetchSpy).toHaveBeenCalledWith("http://frontend_prod:3000/api/revalidate", expect.anything());
|
||||
});
|
||||
|
||||
describe("revalidateTag", () => {
|
||||
it("should send POST request with tag and secret header and return true on 200", async () => {
|
||||
const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({
|
||||
|
||||
@@ -10,7 +10,7 @@ export class RevalidationService {
|
||||
process.env.FRONTEND_URL ||
|
||||
process.env.NEXT_PUBLIC_SITE_URL ||
|
||||
(process.env.NODE_ENV === "production"
|
||||
? "http://frontend_prod:3000"
|
||||
? "http://frontend_prod:8080"
|
||||
: "http://localhost:3000");
|
||||
return raw.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { writeFileSync, chmodSync, symlinkSync, mkdirSync, rmSync, mkdtempSync } from 'fs';
|
||||
import fs = require('fs');
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { writeFileSync, chmodSync, symlinkSync, mkdirSync, rmSync } from 'fs';
|
||||
import { execFileSync } from 'child_process';
|
||||
import { registerPrepareWorker } from './worker-registration';
|
||||
import { loadPrepareWorkerConfig } from './worker-config';
|
||||
@@ -10,29 +7,10 @@ import { linkSync } from 'fs';
|
||||
|
||||
|
||||
describe('reviewed prepare-worker startup registration', () => {
|
||||
let root: string;
|
||||
let secrets: string;
|
||||
let path: string;
|
||||
beforeEach(() => {
|
||||
root = mkdtempSync(join(tmpdir(), 'prepare-worker-registration-'));
|
||||
secrets = join(root, 'run', 'secrets');
|
||||
mkdirSync(secrets, { recursive: true, mode: 0o700 });
|
||||
path = join(secrets, 'prepare_worker_grants');
|
||||
// Substitute only the root descriptor, retaining real openat-style traversal,
|
||||
// O_NOFOLLOW, fstat, read bounds, ownership and permission validation.
|
||||
const open = fs.openSync;
|
||||
jest.spyOn(fs, 'openSync').mockImplementation((file, flags, mode) =>
|
||||
open(file === '/' ? root : file, flags, mode));
|
||||
});
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
it('isolates secret fixtures from the host runtime secret directory', () => {
|
||||
expect(path.startsWith('/run/secrets/')).toBe(false);
|
||||
});
|
||||
const path = '/run/secrets/prepare_worker_grants';
|
||||
const valid = () => ({ grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' });
|
||||
it('loader itself rejects malformed SHA grants', () => {
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const c = valid(); c.grants[0].credentialSha256 = 'secret-not-a-sha';
|
||||
writeFileSync(path, JSON.stringify(c), { mode: 0o400 });
|
||||
try { expect(() => loadPrepareWorkerConfig()).toThrow('Prepare worker startup configuration rejected'); }
|
||||
@@ -42,7 +20,8 @@ describe('reviewed prepare-worker startup registration', () => {
|
||||
expect(registerPrepareWorker({ PREPARE_WORKER_ENABLED: value })).toEqual([]);
|
||||
});
|
||||
it.each(['missing', 'symlink', 'directory', 'fifo', 'oversize', 'group-write', 'world-write', 'hardlink'])('fails startup on unsafe file: %s', kind => {
|
||||
const other = join(secrets, 'test-other');
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const other = '/run/secrets/test-other';
|
||||
try {
|
||||
if (kind === 'fifo') execFileSync('mkfifo', [path]);
|
||||
else if (kind === 'directory') mkdirSync(path);
|
||||
@@ -57,6 +36,7 @@ describe('reviewed prepare-worker startup registration', () => {
|
||||
} finally { rmSync(path, { recursive: true, force: true }); rmSync(other, { force: true }); }
|
||||
});
|
||||
it.each(['extra', 'raw-token', 'wrong-role', 'overlap', 'duplicate-sha', 'missing-grant', 'http', 'credentials', 'localhost', 'query', 'bad-json', 'null'])('rejects unreviewed JSON: %s without leaking contents or logging', kind => {
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const c: any = valid();
|
||||
if (kind === 'extra') c.artifactDirectory = '/user-selected';
|
||||
if (kind === 'raw-token') c.grants[0].token = 'DO_NOT_LOG_THIS_SECRET';
|
||||
@@ -87,7 +67,8 @@ describe('reviewed prepare-worker startup registration', () => {
|
||||
} finally { await app.close(); }
|
||||
});
|
||||
it('loads immutable reviewed JSON into the real transport registration', async () => {
|
||||
const file = path;
|
||||
mkdirSync('/run/secrets', { recursive: true });
|
||||
const file = '/run/secrets/prepare_worker_grants';
|
||||
const config = { grants: [{ id: 'worker', role: 'WORKFLOW_WORKER', credentialSha256: 'a'.repeat(64) }, { id: 'ci', role: 'STAGE_CI', credentialSha256: 'b'.repeat(64) }], workerIds: ['worker'], stageCiIds: ['ci'], artifactBaseUrl: 'https://stage.example.com/internal/prepare-worker/artifacts' };
|
||||
writeFileSync(file, JSON.stringify(config), { mode: 0o400 });
|
||||
try {
|
||||
|
||||
@@ -20,10 +20,7 @@ describe('support HTTP boundary', () => {
|
||||
beforeAll(async () => {
|
||||
process.env.JWT_ACCESS_SECRET = 'test-secret-with-at-least-32-characters';
|
||||
const mod = await Test.createTestingModule({ imports: [PassportModule], controllers: [SupportChatController], providers: [{ provide: SupportChatSettingsService, useValue: { read: async () => ({ enabled: true, provider: 'p', model: 'm' }) } }, SupportChatService, OptionalCustomerJwtGuard, TestJwt, { provide: PrismaService, useValue: prisma }] }).compile();
|
||||
app = mod.createNestApplication(); app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }));
|
||||
// Own the listener for the entire suite. Otherwise Supertest lets the first
|
||||
// request start the server and close it while its concurrent peer is using it.
|
||||
await app.listen(0, '127.0.0.1');
|
||||
app = mod.createNestApplication(); app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true })); await app.init();
|
||||
});
|
||||
afterAll(() => app.close());
|
||||
beforeEach(() => {
|
||||
@@ -48,14 +45,7 @@ describe('support HTTP boundary', () => {
|
||||
});
|
||||
it('scopes wallet to validated identity across concurrent callers', async () => {
|
||||
const replies=await Promise.all(['alice','bob'].map(sub => request(app.getHttpServer()).post('/support-chat/ask').set('Authorization','Bearer '+jwt.sign({sub})).send({message:'کیف پول'}).expect(201)));
|
||||
expect(replies[0].body.answer).toBe('موجودی کیف پول شما: 11 تومان.');
|
||||
expect(replies[1].body.answer).toBe('موجودی کیف پول شما: 22 تومان.');
|
||||
expect(prisma.user.findUnique).toHaveBeenCalledTimes(2);
|
||||
for (const id of ['alice', 'bob']) {
|
||||
expect(prisma.user.findUnique).toHaveBeenCalledWith({ where: { id }, select: { walletBalance: true } });
|
||||
}
|
||||
expect(prisma.product.findMany).not.toHaveBeenCalled();
|
||||
expect(prisma.order.findMany).not.toHaveBeenCalled();
|
||||
expect(replies[0].body.answer).toContain('11'); expect(replies[0].body.answer).not.toContain('22'); expect(replies[1].body.answer).toContain('22'); expect(prisma.product.findMany).not.toHaveBeenCalled();
|
||||
});
|
||||
it('scopes bounded orders to identity', async () => { const r=await request(app.getHttpServer()).post('/support-chat/ask').set('Authorization','Bearer '+jwt.sign({sub:'bob'})).send({message:'سفارش'}).expect(201); expect(r.body.answer).toContain('bob-order'); expect(prisma.order.findMany).toHaveBeenCalledWith(expect.objectContaining({ where:{userId:'bob'}, take:5 })); });
|
||||
it.each(['', 'x'.repeat(1501)])('bounds message length', async message => { await request(app.getHttpServer()).post('/support-chat/ask').send({message}).expect(400); });
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
# Narrow auth schema operation — approval required
|
||||
|
||||
## Exact scope
|
||||
`auth-schema-reconcile.sql` adds only users.auth_expires_at TIMESTAMPTZ NULL, stage_test_session VARCHAR(64) NULL, stage_test_generation UUID NULL (no defaults/backfill), and prepare_workflow_outbox with its original CHECKs, three RESTRICT FKs, task uniqueness, lease and due indexes, including nullable not_before. No existing rows changed, no auth behavior disabled, no Prisma metadata altered.
|
||||
|
||||
Read-only production PostgreSQL 16.14 catalogs verified all three users columns and outbox absent; users.id and orchestration_tasks/runs/budget_ledger.id are UUID PKs. Existing users wallet CHECK and orchestration constraints stay intact. Production has six ledger rows (one historical rolled-back admin_modules attempt), no Oct22–24 entries despite orchestration objects existing. Never run generic migrate deploy or mark whole pending history applied.
|
||||
|
||||
Historical principal migration at e036c5d has two columns, SHA256 8b4fe1fdedd2ae459b720f78479cfcb13da465eec821b422ece5e9bb41dc47ae; 80a2e18 edited it to three, SHA256 a458a94329b1ad078298b042ae0bd2e25439c1f0bb5a086beb55c517d9e0aa28. Read-only Stage ledger records the latter, not the former. Preserve both historical files unchanged here. This operation is separately journaled by CI/backup receipt, not invented Prisma history. Broader baseline/history repair remains separate work.
|
||||
|
||||
## Execution contract
|
||||
Dedicated push workflow `auth-schema-reconcile.yml` uses DEPLOY_KEY and pinned DEPLOY_KNOWN_HOSTS, fixed SSH endpoint, isolated immutable Git checkout; it does not deploy applications. It accepts only exact branches `ops/auth-schema-stage` and `ops/auth-schema-production`, mapping their full refs to Stage and production respectively. It requires REVIEWED_AUTH_SCHEMA_COMMIT matching the event SHA and checks detached checkout HEAD equals that SHA. SQL digest is fixed to `8a22f3e89efb3dacc24f3ed9337ae87d0b4345da003a607bc1994478f69c6feb`, approval ID to `CEO-AUTH-SCHEMA-20261025`; neither is push-supplied input. Production additionally requires PRODUCTION_AUTH_SCHEMA_APPROVAL = SHA:SQL_SHA256:APPROVAL_ID and STAGE_AUTH_SCHEMA_ACCEPTANCE = SHA:SQL_SHA256, provisioned only after real review and Stage auth acceptance. These are explicit approval records, not automatically issued receipts. The repository's workflow/secret permissions must restrict dedicated-ref mutation to trusted maintainers; unrestricted malicious workflow edits can bypass any workflow-local gate. Review/protect this boundary before provisioning production approval.
|
||||
|
||||
Stage already has the DDL: Stage mode backs up production read-only, restores into isolated network-none temporary PostgreSQL, applies exact SQL there, compares resulting catalog with Stage read-only, and writes a SHA-bound structural receipt only. Production requires that receipt, repeats fresh full backup and restore/patch rehearsal, then executes only exact approved SQL. Full dumps remain mode600 host-only under /opt/backups/canina-auth-reconcile; never upload/log PII. Backup nonempty and pg_restore TOC checked. Restore errors/capacity failure stop before DDL. tmpfs is capped at 1GiB: if full restore exceeds capacity, separately review capacity rather than skipping restore. Existing read-only schema backup was 94,694 bytes / 67 tables, no PII copied; its restored PostgreSQL rehearsal is genuine, not a full data-backup recovery claim.
|
||||
|
||||
SQL lock_timeout 3s, statement_timeout 30s, idle transaction 60s; advisory lock plus host flock. DDL transactional; lock/contention failure aborts all. No automatic retries. Re-execution with existing outbox fails closed pending comparison, rather than silently accepting drift. Stage/production outputs compare columns/types/nullability/defaults, all outbox constraints/indexes to fresh rehearsed expected schema.
|
||||
|
||||
## Gitea 1.22.6 trigger semantics and operator sequence
|
||||
Gitea [1.22 comparison](https://docs.gitea.com/1.22/usage/actions/comparison) explicitly marks `workflow_dispatch` ignored. The [v1.22.6 notifier implementation](https://github.com/go-gitea/gitea/blob/v1.22.6/services/actions/notifier_helper.go) resolves the pushed ref's commit, detects workflows at that commit and records `Ref` and `CommitSHA` on the run. Push is not default-branch-only. The dedicated branch must therefore contain this workflow and point to the **final integrated reviewed commit**, not a new trigger-only commit. Existing app deployment listens only to `main`/`develop`, not either operations branch.
|
||||
|
||||
After independent review/authorization, a trusted operator can push `<REVIEWED_FULL_SHA>:refs/heads/ops/auth-schema-stage` to origin. Inspect that CI run's SHA and structural receipt, then complete real Stage auth tests. Only after separate production and Stage acceptance approval records exist for the **same** SHA/digest may the operator push `<REVIEWED_FULL_SHA>:refs/heads/ops/auth-schema-production`. No empty commits, merges, arbitrary ref inputs, secret updates by this workflow, automatic acceptance, or app promotion. A no-op push does not retrigger; do not change the reviewed SHA to manufacture a run. Any SHA change requires fresh review, approval bindings and Stage evidence. Workflow preparation has not pushed either branch, changed secrets/protections or executed the operation.
|
||||
|
||||
## Acceptance and rollback
|
||||
1. Parent DBA review exact commit/diff plus SQL hash and workflow trusted-ref/secret permissions. Approve read-only production full backup + isolated full restore scope separately from production DDL.
|
||||
2. Run Stage workflow from exact commit; inspect successful backup/restore/catalog comparison. Existing Stage backend must pass ordinary admin password login + protected dashboard, OTP flow with authorized principal, refresh, and expiry/session/generation rejection tests. Do not manufacture acceptance if no approved credential/test principal is available. Record actual test run identifiers before setting Stage acceptance approval.
|
||||
3. Obtain explicit approval: execute only this commit's SQL in caninodb_prod, three nullable users columns + empty outbox and declared constraints/indexes, 3s lock/30s statement budgets, no unrelated migrations/history writes/seeds. Provision approval binding SHA, SQL digest, approval record. Trigger production workflow, inspect terminal CI log. No production execution has been performed during preparation.
|
||||
4. Release separately via existing code-only develop→Stage→main CI, using actual regenerated backend client, runtime-image/schema guard, immutable image receipts and authenticated login/OTP/refresh/protected data acceptance. Keep current compatible backend until this passes. Schema approval alone does not authorize application promotion.
|
||||
5. Before commit failure: transaction rollback. After successful DDL: leave additive nullable fields/table intact, roll back application to known compatible immutable image via approved CI; do not DROP columns or outbox after writes. Restore full backup only under separately approved incident/data-loss recovery process. Existing backup tables remain untouched.
|
||||
|
||||
## Verified preparation / limitations
|
||||
Disposable network-none PG16 schema-only restore and exact SQL executed successfully (see JSON). Missing-column SELECT failed before patch and passed after; invalid status rejected; second application rejected without changes; container removed. Independent reviewer found SQL definition matched history; requested stronger gating, Stage preexisting-schema handling, capacity awareness and postcondition validation, addressed or explicit prerequisites above. Full-data restore and dedicated workflow SSH path are implemented but not yet executed in actual CI. No production writes or deployments occurred. Approval/environment gates must not be represented as production repair completion.
|
||||
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"schema_backup_bytes": 94694,
|
||||
"schema_backup_sha256": "9c6367a06acaf97c1afe9355e4306d2a10f878b7fe156c7dbc5d58d675262c72",
|
||||
"schema_tables": 67,
|
||||
"pii_copied": false,
|
||||
"postgres": "16.14",
|
||||
"restore_and_patch_seconds": 3.669,
|
||||
"pre_patch_missing_columns_failed": true,
|
||||
"post_patch_queries": " auth_expires_at | stage_test_session | stage_test_generation \n-----------------+--------------------+-----------------------\n(0 rows)\n\n count \n-------\n 0\n(1 row)\n\n count \n-------\n 0\n(1 row)\n\n",
|
||||
"constraints": " conname | pg_get_constraintdef \n---------------------------------------------+-----------------------------------------------------------------------------------------------------------\n prepare_workflow_outbox_attempts_check | CHECK (((attempts >= 0) AND (attempts <= 3)))\n prepare_workflow_outbox_pkey | PRIMARY KEY (id)\n prepare_workflow_outbox_reservation_id_fkey | FOREIGN KEY (reservation_id) REFERENCES orchestration_budget_ledger(id) ON DELETE RESTRICT\n prepare_workflow_outbox_run_id_fkey | FOREIGN KEY (run_id) REFERENCES orchestration_runs(id) ON DELETE RESTRICT\n prepare_workflow_outbox_status_check | CHECK (((status)::text = ANY ((ARRAY['PENDING'::character varying, 'DONE'::character varying])::text[])))\n prepare_workflow_outbox_task_id_fkey | FOREIGN KEY (task_id) REFERENCES orchestration_tasks(id) ON DELETE RESTRICT\n prepare_workflow_outbox_task_id_key | UNIQUE (task_id)\n(7 rows)\n\n",
|
||||
"invalid_status_rejected": true,
|
||||
"repeat_execution_rejected_without_writes": true,
|
||||
"production_writes": false
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
# Host disk retention
|
||||
|
||||
Successful authenticated CI rollout runs release-retention.py. Default keep policy is only the current active release per environment; Docker mounts and Compose working directories always protect older still-used artifacts. Dry/unpublished source and unknown ignored files are preserved, so this is not a hard quota. CI restores only its own tracked nginx/dockerignore render changes after successful acceptance. New clones use sparse checkout to omit the 85 MiB tracked graphify-out history. No node_modules were bundled in the audited releases: their ~218 MiB is tracked source/assets plus an independent ~44 MiB Git store. Historical dirty releases require individual source review; never reset them for cleanup.
|
||||
|
||||
Retained small private runtime configuration is copied mode0600 under .retained-runtime before deleting a clean obsolete release. This avoids secret deletion; this archive is not a release rollback and requires explicit secret retention review separately. No volumes, databases, sessions, evidence, images or build cache are deleted.
|
||||
|
||||
The CI-installed canina-disk-watch.timer checks every five minutes with stdlib Python, no model calls. It logs daemon.err and returns nonzero at >=85% usage or <10GiB free. This is local detection, NOT off-host paging. systemctl status canina-disk-watch.service and journalctl -t canina-disk-watch expose alarms. Monitoring does not guarantee the disk cannot fill.
|
||||
|
||||
Audit 2026-10-26: Docker daemon already bounds new json-file containers to 3x10MiB and BuildKit GC to 6GB. Fifteen pre-existing containers (including Nexus, Gitea, databases and proxy) have empty LogConfig and remain unbounded until their owners roll them through their own Git/CI. Journald has no explicit SystemMaxUse and currently occupies 1.3GB. Nexus data is real persistent storage; retention tasks were not authenticated/audited and no blobs were deleted. Neither unrelated service config nor daemon/journal configuration is changed by Canina CI. Worktree branches with unpushed commits remain intentionally retained. These residual growth mechanisms prevent any honest claim of a globally bounded host or <50GB total.
|
||||
@@ -32,7 +32,7 @@ export default function OrchestrationCommandCenter() {
|
||||
const [attempted,setAttempted]=useState(false); const [busy,setBusy]=useState(false); const [commandError,setCommandError]=useState('');
|
||||
const [success,setSuccess]=useState('');
|
||||
const [blocked,setBlocked]=useState(false);
|
||||
const [now,setNow]=useState(()=>Date.now());
|
||||
const [now,setNow]=useState(Date.now());
|
||||
useEffect(()=>{const timer=setInterval(()=>setNow(Date.now()),1000);return ()=>clearInterval(timer)},[]);
|
||||
const [kill,setKill]=useState<KillLevel>('FREEZE_NEW_WORK');
|
||||
const [session,setSession]=useState(''); const [message,setMessage]=useState('');
|
||||
|
||||
@@ -14,11 +14,6 @@ it('does not restore stale detail after authoritative refresh while detail reque
|
||||
it('keeps payload immutable when navigating after an ambiguous network failure',async()=>{
|
||||
vi.resetAllMocks();const t={id:'t',key:'t',title:'Task identity',department:'DEV',state:'GATE1_PENDING',version:2,operationMode:'PREPARE',budgetLimitCents:0,availableActions:[{id:'g1',label:'Approve safe work',gate:1,decision:'APPROVED'}]};
|
||||
vi.mocked(api.get).mockImplementation(async url=>({data:url.endsWith('/overview')?{items:[],measuredAt:'2026-10-23',capabilities:{mutations:true}}:url.endsWith('/tasks')?[t]:t}));vi.mocked(api.post).mockRejectedValue(new Error('network'));
|
||||
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:'Task identity'}));fireEvent.click(await screen.findByRole('button',{name:'تأیید در Gate1'}));const d=within(screen.getByRole('dialog'));expect(d.queryByLabelText('زمان انقضا')).not.toBeInTheDocument();expect(d.getByLabelText('زمان اجرای برنامهریزیشده')).toHaveValue('');fireEvent.change(d.getByLabelText('دلیل'),{target:{value:'Reviewed'}});fireEvent.click(d.getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم'));fireEvent.click(d.getByRole('button',{name:'تأیید فرمان'}));await d.findByRole('alert');
|
||||
const sentPayload=structuredClone(vi.mocked(api.post).mock.calls[0][1]);
|
||||
expect(sentPayload).toMatchObject({expectedVersion:2,decision:'APPROVED',notes:'Reviewed'});
|
||||
expect(sentPayload).not.toHaveProperty('expiresAt');expect(sentPayload).not.toHaveProperty('notBefore');
|
||||
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:'Task identity'}));fireEvent.click(await screen.findByRole('button',{name:'تأیید در Gate1'}));const d=within(screen.getByRole('dialog'));fireEvent.change(d.getByLabelText('زمان انقضا'),{target:{value:'2099-01-01T12:00'}});fireEvent.change(d.getByLabelText('دلیل'),{target:{value:'Reviewed'}});fireEvent.click(d.getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم'));fireEvent.click(d.getByRole('button',{name:'تأیید فرمان'}));await d.findByRole('alert');
|
||||
fireEvent.click(screen.getByRole('button',{name:'وظیفهها و تأییدها'}));expect(d.getByLabelText('دلیل')).toHaveValue('Reviewed');expect(d.getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم')).toBeChecked();
|
||||
expect(vi.mocked(api.post).mock.calls[0][1]).toEqual(sentPayload);
|
||||
expect(d.getByLabelText('دلیل')).toBeDisabled();expect(d.getByLabelText('زمان اجرای برنامهریزیشده')).toBeDisabled();
|
||||
});
|
||||
@@ -27,7 +27,7 @@ it('rejects Gate2 evidence from failed runs',async()=>{
|
||||
await expect(orchestration.command({...task,availableActions:[g2],runs:[{id:'r',status:'FAILED',commitSha:'abc',environment:'stage',artifacts:[{id:'a',sha256:'a'.repeat(64)}]}]},g2,{idempotencyKey:'key',expiresAt:'2099-01-01T00:00:00Z',runId:'r',artifactHash:'a'.repeat(64)})).rejects.toThrow(/Gate2.*غیرفعال/);
|
||||
expect(api.post).not.toHaveBeenCalled();
|
||||
});
|
||||
async function dialog(){render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:'Safe proposal'}));fireEvent.click(await screen.findByRole('button',{name:'تأیید در Gate1'}));const d=within(screen.getByRole('dialog'));expect(d.queryByLabelText('زمان انقضا')).not.toBeInTheDocument();expect(d.getByLabelText('زمان اجرای برنامهریزیشده')).toHaveValue('');fireEvent.change(d.getByLabelText('دلیل'),{target:{value:'Reviewed bounds'}});fireEvent.click(d.getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم'));return d;}
|
||||
async function dialog(){render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:'Safe proposal'}));fireEvent.click(await screen.findByRole('button',{name:'تأیید در Gate1'}));const d=within(screen.getByRole('dialog'));fireEvent.change(d.getByLabelText('زمان انقضا'),{target:{value:'2099-01-01T12:00'}});fireEvent.change(d.getByLabelText('دلیل'),{target:{value:'Reviewed bounds'}});fireEvent.click(d.getByLabelText('محدوده و اتصال تغییرناپذیر را بررسی کردم'));return d;}
|
||||
it('locks stale 409 command until authoritative refresh instead of retrying stale payload',async()=>{
|
||||
vi.mocked(api.post).mockRejectedValue({response:{status:409,data:{message:'Version conflict'}}});
|
||||
const d=await dialog();fireEvent.click(d.getByRole('button',{name:'تأیید فرمان'}));
|
||||
@@ -41,9 +41,7 @@ it('does not expose authorized task mutations when capabilities are unavailable'
|
||||
render(<OrchestrationCommandCenter/>);fireEvent.click(await screen.findByRole('button',{name:'Safe proposal'}));await screen.findByRole('region',{name:'جزئیات وظیفه'});
|
||||
expect(screen.getByRole('button',{name:'تأیید در Gate1'})).toBeDisabled();
|
||||
});
|
||||
it('prevents confirming a scheduled start in the past without sending a command',async()=>{
|
||||
const d=await dialog();fireEvent.change(d.getByLabelText('زمان اجرای برنامهریزیشده'),{target:{value:'2020-01-01T12:00'}});
|
||||
it('prevents confirming an already expired approval',async()=>{
|
||||
const d=await dialog();fireEvent.change(d.getByLabelText('زمان انقضا'),{target:{value:'2020-01-01T12:00'}});
|
||||
expect(d.getByRole('button',{name:'تأیید فرمان'})).toBeDisabled();
|
||||
fireEvent.click(d.getByRole('button',{name:'تأیید فرمان'}));
|
||||
expect(api.post).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -1,13 +1 @@
|
||||
import '@testing-library/jest-dom/vitest';
|
||||
|
||||
// Vitest preserves Node 26's host storage globals when populating jsdom.
|
||||
// Bind the actual jsdom Storage objects, not the unavailable host storage.
|
||||
const browserWindow = (globalThis as typeof globalThis & {
|
||||
jsdom: { window: Window };
|
||||
}).jsdom.window;
|
||||
for (const name of ['localStorage', 'sessionStorage'] as const) {
|
||||
Object.defineProperty(globalThis, name, {
|
||||
configurable: true,
|
||||
value: browserWindow[name],
|
||||
});
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import { expect, it } from 'vitest';
|
||||
|
||||
it('uses browser storage for persistent auth instead of Node host storage', () => {
|
||||
expect(localStorage).toBe(window.localStorage);
|
||||
expect(localStorage).toBeInstanceOf(window.Storage);
|
||||
expect(sessionStorage).toBeInstanceOf(window.Storage);
|
||||
localStorage.setItem('storage-probe', 'browser');
|
||||
expect(localStorage.getItem('storage-probe')).toBe('browser');
|
||||
localStorage.removeItem('storage-probe');
|
||||
});
|
||||
@@ -87,9 +87,9 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => {
|
||||
const jsx=await BlogPostPage({params:Promise.resolve({slug:'technical'})}); const script:any=React.Children.toArray(jsx.props.children)[0]; const json=JSON.parse(script.props.dangerouslySetInnerHTML.__html); expect(json.dateModified).toBeUndefined();
|
||||
});
|
||||
|
||||
it('uses the stable Organization fallback visibly and in metadata', async () => {
|
||||
it.each([null, { firstName: '', lastName: '' }])('uses the stable Organization fallback for author %j visibly and in metadata', async (author) => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({
|
||||
slug: 'authorless', title: 'Authorless', content: '<p>x</p>', publishedAt: '2026-01-01T00:00:00Z', author: { firstName: '', lastName: '' },
|
||||
slug: 'authorless', title: 'Authorless', content: '<p>x</p>', publishedAt: '2026-01-01T00:00:00Z', author, authorTeam: null,
|
||||
}) }));
|
||||
|
||||
const metadata = await generateMetadata({ params: Promise.resolve({ slug: 'authorless' }) });
|
||||
@@ -106,7 +106,7 @@ describe('BlogPostPage FAQPage Schema injection (SEO-P1-07)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('uses a custom authorTeam as the authorless Organization identity in metadata and rendering', async () => {
|
||||
it('keeps custom team attribution separate from the Organization author identity', async () => {
|
||||
const organizationName = 'تیم دامپزشکی کنینا';
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({
|
||||
slug: 'custom-team',
|
||||
|
||||
@@ -109,10 +109,6 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
|
||||
keywords: keywordsList,
|
||||
alternates: {
|
||||
canonical: canonicalUrl,
|
||||
languages: {
|
||||
'fa-IR': canonicalUrl,
|
||||
'x-default': canonicalUrl,
|
||||
},
|
||||
},
|
||||
robots: isStaging
|
||||
? {
|
||||
@@ -142,6 +138,8 @@ export async function generateMetadata({ params }: { params: Promise<{ slug: str
|
||||
images: [
|
||||
{
|
||||
url: ogImageUrl,
|
||||
width: 1200,
|
||||
height: 630,
|
||||
alt: blog.imageAlt || blog.title || title,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { afterEach, expect, it, vi } from 'vitest';
|
||||
import { generateMetadata } from './page';
|
||||
|
||||
vi.mock('../../../lib/seo', () => ({
|
||||
getSeoConfig: async () => ({ brandNameFa: 'کنینا', canonicalBaseUrl: 'https://canina.ir', ogImageUrl: '/fallback.png' }),
|
||||
formatPageTitle: (title: string) => title,
|
||||
}));
|
||||
|
||||
afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); });
|
||||
|
||||
it('uses the exact canonical for monolingual self-referencing language hints', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ title: 'Ear care', canonicalUrl: 'https://canina.ir/blog/ear-care/' }) }));
|
||||
const metadata = await generateMetadata({ params: Promise.resolve({ slug: 'ear-care' }) });
|
||||
expect(metadata.alternates).toEqual({ canonical: 'https://canina.ir/blog/ear-care', languages: { 'fa-IR': 'https://canina.ir/blog/ear-care', 'x-default': 'https://canina.ir/blog/ear-care' } });
|
||||
});
|
||||
|
||||
it('does not invent dimensions for a publisher supplied OG asset', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: async () => ({ title: 'Ear care', imageUrl: '/ear-800x450.jpg' }) }));
|
||||
const metadata = await generateMetadata({ params: Promise.resolve({ slug: 'ear-care' }) });
|
||||
expect((metadata.openGraph as any).images[0]).toEqual({ url: '/ear-800x450.jpg', alt: 'Ear care' });
|
||||
});
|
||||
@@ -79,9 +79,9 @@ export default function AnalyticsDeferred({ gtmId, gaId, clarityId }: AnalyticsD
|
||||
if (gaId) {
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
function gtag(..._args: any[]) {
|
||||
// gtag.js consumes the native Arguments object, not an ES6 rest array.
|
||||
// valueOf returns that same object without converting the vendor payload.
|
||||
window.dataLayer?.push(arguments.valueOf());
|
||||
// Note: Google's gtag.js specifically expects the native `arguments` object.
|
||||
// Pushing an ES6 Array causes gtag.js to silently ignore tracking commands.
|
||||
window.dataLayer?.push(arguments);
|
||||
}
|
||||
window.gtag = gtag;
|
||||
gtag('js', new Date());
|
||||
|
||||
@@ -105,10 +105,10 @@ export default function BlogPage({
|
||||
if (sortBy === "most_viewed") {
|
||||
return (b.viewCount ?? 0) - (a.viewCount ?? 0);
|
||||
}
|
||||
// `date` is a localized Persian display value, not a parseable timestamp.
|
||||
const aPublished = Date.parse(publicationDate(a) || '') || 0;
|
||||
const bPublished = Date.parse(publicationDate(b) || '') || 0;
|
||||
return sortBy === "oldest" ? aPublished - bPublished : bPublished - aPublished;
|
||||
if (sortBy === "oldest") {
|
||||
return new Date(a.date).getTime() - new Date(b.date).getTime();
|
||||
}
|
||||
return new Date(b.date).getTime() - new Date(a.date).getTime();
|
||||
});
|
||||
}, [blogs, selectedCategory, searchQuery, selectedTag, sortBy]);
|
||||
|
||||
|
||||
@@ -214,11 +214,6 @@ export default function BlogPostClient({ blog, publishedDate, authorName, author
|
||||
<div className="font-bold text-xs text-medical-gray-400 mt-0.5">
|
||||
تیم: {authorTeam || blog.authorTeam || BLOG_TEAM_FALLBACK}
|
||||
</div>
|
||||
{typeof blog.authorRole === 'string' && blog.authorRole.trim() && (
|
||||
<div className="font-bold text-xs text-medical-gray-500 mt-1">
|
||||
نقش نویسنده: {blog.authorRole.trim()}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import React from 'react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
|
||||
import BlogPage, { type BlogPostItem } from '../BlogPage';
|
||||
|
||||
vi.mock('next/navigation', () => ({ useRouter: () => ({ push: vi.fn(), replace: vi.fn() }) }));
|
||||
vi.mock('../../lib/services/productService', () => ({ productService: { getProducts: vi.fn().mockResolvedValue({ data: [] }) } }));
|
||||
vi.mock('next/image', () => ({ default: ({ fill, priority, unoptimized, ...props }: any) => <img {...props} /> }));
|
||||
vi.mock('motion/react', async () => {
|
||||
const React = await import('react');
|
||||
const cache: Record<string, React.ComponentType<any>> = {};
|
||||
return { AnimatePresence: ({ children }: any) => children, motion: new Proxy({}, { get: (_, tag: string) => cache[tag] ||= React.forwardRef(function MotionElement({ children, ...props }: any, ref) { return React.createElement(tag, { ...Object.fromEntries(Object.entries(props).filter(([key]) => !['initial', 'animate', 'exit', 'transition', 'whileHover', 'whileTap', 'whileInView', 'viewport', 'layout'].includes(key))), ref }, children); }) }) };
|
||||
});
|
||||
afterEach(cleanup);
|
||||
|
||||
const post = (id: string, dates: Partial<BlogPostItem> = {}): BlogPostItem => ({
|
||||
id, slug: id, title: `Article ${id}`, excerpt: 'Health guide', image: '/a.png',
|
||||
author: 'Author', content: '', date: '۱۴۰۵/۸/۳', categoryId: 'health',
|
||||
tags: [{ id: 'tag', name: 'health' }], ...dates,
|
||||
});
|
||||
const posts = [
|
||||
post('middle', { publishedAt: '2026-10-24T12:00:00Z', createdAt: '2026-01-01T00:00:00Z', viewCount: 30 }),
|
||||
post('old', { publishedAt: '2026-10-23T12:00:00Z', createdAt: '2026-10-26T00:00:00Z', viewCount: 80 }),
|
||||
post('new', { createdAt: '2026-10-25T12:00:00Z', viewCount: 10 }),
|
||||
];
|
||||
const titles = () => screen.getAllByRole('link').filter(link => link.getAttribute('href')?.startsWith('/blog/')).map(link => link.querySelector('h2,h3')?.textContent);
|
||||
|
||||
describe('BlogPage sorting', () => {
|
||||
it('defaults to newest using raw publication time with createdAt fallback, not the Persian display date', async () => {
|
||||
await act(async () => { render(<BlogPage blogs={posts} />); });
|
||||
expect(titles()).toEqual(['Article new', 'Article middle', 'Article old']);
|
||||
expect(posts.map(post => post.id)).toEqual(['middle', 'old', 'new']);
|
||||
});
|
||||
it('switches to oldest using publication rather than creation or localized dates', async () => {
|
||||
await act(async () => { render(<BlogPage blogs={posts} />); });
|
||||
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'oldest' } });
|
||||
expect(titles()).toEqual(['Article old', 'Article middle', 'Article new']);
|
||||
});
|
||||
it('supports exactly newest, oldest and most_viewed, keeping popularity independent of dates', async () => {
|
||||
await act(async () => { render(<BlogPage blogs={posts} />); });
|
||||
expect(screen.getAllByRole('option').map(option => option.getAttribute('value'))).toEqual(['newest', 'oldest', 'most_viewed']);
|
||||
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'most_viewed' } });
|
||||
expect(titles()).toEqual(['Article old', 'Article middle', 'Article new']);
|
||||
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'newest' } });
|
||||
expect(titles()).toEqual(['Article new', 'Article middle', 'Article old']);
|
||||
});
|
||||
it('preserves featured priority, combined filters and six-at-a-time pagination while sorting', async () => {
|
||||
const collection = Array.from({ length: 10 }, (_, index) => post(String(index), {
|
||||
publishedAt: `2026-10-${String(index + 10).padStart(2, '0')}T12:00:00Z`,
|
||||
featured: index === 2,
|
||||
}));
|
||||
await act(async () => { render(<BlogPage blogs={[...collection, post('excluded', { categoryId: 'other' })]} categories={[{ id: 'health', name: 'Health', slug: 'health' }]} />); });
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Health' }));
|
||||
fireEvent.change(screen.getByPlaceholderText('جستجو در موضوعات، بیماریها، ویتامینها...'), { target: { value: 'Health guide' } });
|
||||
fireEvent.click(screen.getByRole('button', { name: '#health' }));
|
||||
expect(titles()).toEqual(['Article 2', 'Article 9', 'Article 8', 'Article 7', 'Article 6', 'Article 5', 'Article 4']);
|
||||
fireEvent.click(screen.getByRole('button', { name: /مشاهده ۶ مقاله بیشتر/ }));
|
||||
expect(titles()).toHaveLength(10);
|
||||
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'oldest' } });
|
||||
expect(titles()).toEqual(['Article 2', 'Article 0', 'Article 1', 'Article 3', 'Article 4', 'Article 5', 'Article 6', 'Article 7', 'Article 8', 'Article 9']);
|
||||
expect(screen.queryByText('Article excluded')).not.toBeInTheDocument();
|
||||
});
|
||||
it('keeps equal or unavailable raw dates stable without parsing localized display strings', async () => {
|
||||
await act(async () => { render(<BlogPage blogs={[post('missing'), post('also-missing'), ...posts]} />); });
|
||||
expect(titles()).toEqual(['Article new', 'Article middle', 'Article old', 'Article missing', 'Article also-missing']);
|
||||
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'oldest' } });
|
||||
expect(titles()).toEqual(['Article missing', 'Article also-missing', 'Article old', 'Article middle', 'Article new']);
|
||||
});
|
||||
});
|
||||
@@ -119,14 +119,6 @@ describe("BlogPostClient Component", () => {
|
||||
expect(document.querySelectorAll("time[datetime]").length).toBe(2);
|
||||
});
|
||||
|
||||
it("displays an explicit author role without inventing one for older articles", () => {
|
||||
const props = { sanitizedContent: mockBlog.content, publishedDate: "۱۴۰۵/۸/۳", authorName: "کارشناس کنینا" };
|
||||
const { rerender } = render(<BlogPostClient {...props} blog={{ ...mockBlog, authorRole: "پژوهش مراقبت گوش و نگهداری ایمن" }} />);
|
||||
expect(screen.getByText("نقش نویسنده: پژوهش مراقبت گوش و نگهداری ایمن")).toBeInTheDocument();
|
||||
rerender(<BlogPostClient {...props} blog={{ ...mockBlog, authorRole: " " }} />);
|
||||
expect(screen.queryByText(/نقش نویسنده:/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("registers blog view count on mount via API", async () => {
|
||||
render(
|
||||
<BlogPostClient
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { expect, it } from 'vitest';
|
||||
import { resolveBlogAuthorIdentity } from './blogAuthor';
|
||||
|
||||
it('models the specialty team account as Organization without inventing a person', () => {
|
||||
expect(resolveBlogAuthorIdentity({ author: { firstName: 'کارشناس رفتار و نگهداری', lastName: 'تیم کنینا' }, authorTeam: 'پوست، مو و آلرژی' })).toEqual({
|
||||
type: 'Organization', name: 'کارشناس رفتار و نگهداری تیم کنینا', teamName: 'پوست، مو و آلرژی',
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps a named individual a Person even when affiliated with an editorial team', () => {
|
||||
expect(resolveBlogAuthorIdentity({ author: { firstName: 'سارا', lastName: 'احمدی' }, authorTeam: 'تیم کنینا' }).type).toBe('Person');
|
||||
});
|
||||
@@ -32,10 +32,7 @@ export function resolveBlogAuthorIdentity(blog: BlogAuthorInput): ResolvedBlogAu
|
||||
const teamName = blog.authorTeam?.trim() || BLOG_TEAM_FALLBACK;
|
||||
|
||||
if (personName) {
|
||||
// Specialty accounts identify themselves as a team, not a named individual.
|
||||
// An individual's authorTeam affiliation alone does not change their type.
|
||||
const isTeamAccount = /(?:^|\s)تیم(?:\s|$)/u.test(personName);
|
||||
return { type: isTeamAccount ? 'Organization' : 'Person', name: personName, teamName };
|
||||
return { type: 'Person', name: personName, teamName };
|
||||
}
|
||||
|
||||
return { type: 'Organization', name: BLOG_AUTHOR_FALLBACK, teamName };
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { extractFaqsFromHtml } from './extractFaqs';
|
||||
|
||||
describe('FAQ heading boundaries', () => {
|
||||
it('never merges intervening care lists or section headings into a question', () => {
|
||||
const html = `<h2>تا زمان معاینه چه کارهایی کمخطرتر است؟</h2>
|
||||
<ul><li>از دستکاری گوش خودداری کنید.</li></ul>
|
||||
<p>این اقدامات درمان نیستند.</p>
|
||||
<h2>چه چیزهایی داخل گوش نریزیم؟</h2><p>مواد خانگی استفاده نکنید.</p>
|
||||
<h2>پرسشهای رایج</h2><h3>آیا بو یعنی عفونت است؟</h3><p>خیر، معاینه لازم است.</p>`;
|
||||
expect(extractFaqsFromHtml(html)).toEqual([
|
||||
{ question: 'چه چیزهایی داخل گوش نریزیم؟', answer: 'مواد خانگی استفاده نکنید.' },
|
||||
{ question: 'آیا بو یعنی عفونت است؟', answer: 'خیر، معاینه لازم است.' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -61,11 +61,11 @@ export function extractFaqsFromHtml(html: string): FAQItem[] {
|
||||
|
||||
// 3. Headings with questions followed immediately by paragraph answers:
|
||||
// e.g. <h2>پرسش: آیا این مکمل برای سگ مفید است؟</h2> <p>بله، کاملاً مفید است.</p>
|
||||
const headingRegex = /<h([2-4])\b[^>]*>((?:(?!<\/?h[1-6]\b)[\s\S])*?)<\/h\1>\s*<p[^>]*>([\s\S]*?)<\/p>/gi;
|
||||
const headingRegex = /<h[2-4][^>]*>([\s\S]*?)<\/h[2-4]>\s*<p[^>]*>([\s\S]*?)<\/p>/gi;
|
||||
let headingMatch;
|
||||
while ((headingMatch = headingRegex.exec(html)) !== null) {
|
||||
const rawHeading = headingMatch[2].replace(/<[^>]+>/g, '').trim();
|
||||
const rawParagraph = headingMatch[3].replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
||||
const rawHeading = headingMatch[1].replace(/<[^>]+>/g, '').trim();
|
||||
const rawParagraph = headingMatch[2].replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim();
|
||||
if (
|
||||
(rawHeading.endsWith('?') ||
|
||||
rawHeading.endsWith('؟') ||
|
||||
|
||||
@@ -65,50 +65,6 @@ def bundle():
|
||||
return result
|
||||
|
||||
|
||||
def recover(journal, paths):
|
||||
if not journal.exists():
|
||||
return
|
||||
try:
|
||||
state = json.loads(journal.read_text())
|
||||
except Exception:
|
||||
raise ValueError('invalid journal json')
|
||||
if state.get('phase') != 'applying':
|
||||
return
|
||||
if (set(state.get('before', {})) != ALLOWED or
|
||||
set(state.get('installed', {})) != ALLOWED):
|
||||
raise ValueError('invalid in-flight journal allowlist')
|
||||
if state.get('previous_installed') is not None and set(state['previous_installed']) != ALLOWED:
|
||||
raise ValueError('invalid in-flight journal previous_installed allowlist')
|
||||
if state.get('previous_before') is not None and set(state['previous_before']) != ALLOWED:
|
||||
raise ValueError('invalid in-flight journal previous_before allowlist')
|
||||
for r, p in paths.items():
|
||||
curr = p.read_bytes() if p.exists() else None
|
||||
curr_d = digest(curr) if curr is not None else None
|
||||
if curr_d == state['installed'][r]:
|
||||
continue
|
||||
if state.get('previous_installed') is not None:
|
||||
if curr_d == state['previous_installed'][r]:
|
||||
continue
|
||||
else:
|
||||
old = state['before'][r]
|
||||
old_data = base64.b64decode(old['data']) if old else None
|
||||
if curr == old_data:
|
||||
continue
|
||||
raise ValueError(f'interrupted transaction conflict: {r}')
|
||||
restore_target = state.get('previous_before') or state['before']
|
||||
restore(paths, restore_target)
|
||||
if state.get('previous_journal'):
|
||||
atomic(journal, base64.b64decode(state['previous_journal']))
|
||||
elif state.get('previous_installed'):
|
||||
atomic(journal, json.dumps({
|
||||
'phase': 'installed',
|
||||
'before': state['before'],
|
||||
'installed': state['previous_installed']
|
||||
}, sort_keys=True).encode())
|
||||
else:
|
||||
journal.unlink()
|
||||
|
||||
|
||||
def execute(action, home):
|
||||
if not home.is_absolute() or not home.is_dir():
|
||||
raise ValueError('--target-home must be an existing absolute directory')
|
||||
@@ -119,23 +75,18 @@ def execute(action, home):
|
||||
bad = [r for r, p in paths.items() if not p.exists() or p.read_bytes() != files[r]]
|
||||
if bad:
|
||||
raise ValueError('verification mismatch: ' + ', '.join(bad))
|
||||
if journal.exists():
|
||||
jstate = json.loads(journal.read_text())
|
||||
if jstate.get('phase', 'installed') != 'installed':
|
||||
raise ValueError('verification refused: in-flight transaction requires recovery')
|
||||
return {'status': 'configured_only', 'files': len(files), 'workflow_verified': False}
|
||||
lock = safe(home, 'editorial-v4/.installer/lock')
|
||||
lock.parent.mkdir(parents=True, exist_ok=True)
|
||||
with lock.open('a') as stream:
|
||||
fcntl.flock(stream, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
recover(journal, paths)
|
||||
if action == 'rollback':
|
||||
if not journal.exists():
|
||||
return {'status': 'nothing_to_rollback'}
|
||||
state = json.loads(journal.read_text())
|
||||
if set(state['before']) != ALLOWED or set(state['installed']) != ALLOWED:
|
||||
raise ValueError('invalid rollback allowlist')
|
||||
# Refuse external edits rather than destroying another writer's work.
|
||||
# Refuse external edits rather than destroying another writer\'s work.
|
||||
for r, p in paths.items():
|
||||
current = p.read_bytes() if p.exists() else None
|
||||
before = state['before'][r]
|
||||
@@ -148,60 +99,20 @@ def execute(action, home):
|
||||
changes = [r for r, p in paths.items() if not p.exists() or p.read_bytes() != files[r]]
|
||||
if not changes:
|
||||
return {'status': 'unchanged', 'files': len(files)}
|
||||
previous_journal = journal.read_bytes() if journal.exists() else None
|
||||
previous_state = None
|
||||
if previous_journal is not None:
|
||||
previous_state = json.loads(previous_journal)
|
||||
if (set(previous_state) not in ({'before', 'installed'}, {'phase', 'before', 'installed'}) or
|
||||
set(previous_state['before']) != ALLOWED or
|
||||
set(previous_state['installed']) != ALLOWED):
|
||||
raise ValueError('invalid upgrade journal allowlist')
|
||||
if previous_state.get('phase', 'installed') != 'installed':
|
||||
raise ValueError('invalid upgrade journal phase')
|
||||
for r, p in paths.items():
|
||||
old = previous_state['before'][r]
|
||||
if old is not None:
|
||||
if (set(old) != {'data', 'mode'} or type(old['mode']) is not int or
|
||||
not 0 <= old['mode'] <= 0o777):
|
||||
raise ValueError('invalid upgrade rollback entry')
|
||||
base64.b64decode(old['data'], validate=True)
|
||||
# Only a completely installed, verified previous transaction may
|
||||
# advance. Never recover partial writes or overwrite live drift.
|
||||
if not p.exists() or digest(p.read_bytes()) != previous_state['installed'][r]:
|
||||
raise ValueError(f'existing transaction conflict: {r}')
|
||||
current_snapshot = {r: {'data': base64.b64encode(p.read_bytes()).decode(),
|
||||
'mode': p.stat().st_mode & 0o777} if p.exists() else None
|
||||
for r, p in paths.items()}
|
||||
original_before = previous_state['before'] if previous_state else current_snapshot
|
||||
incoming_installed = {r: digest(v) for r, v in files.items()}
|
||||
in_flight_state = {
|
||||
'phase': 'applying',
|
||||
'before': original_before,
|
||||
'installed': incoming_installed,
|
||||
'previous_installed': previous_state['installed'] if previous_state else None,
|
||||
'previous_before': current_snapshot,
|
||||
'previous_journal': base64.b64encode(previous_journal).decode() if previous_journal else None,
|
||||
}
|
||||
atomic(journal, json.dumps(in_flight_state, sort_keys=True).encode())
|
||||
if journal.exists():
|
||||
raise ValueError('existing transaction: rollback before upgrade or recovery')
|
||||
before = {r: {'data': base64.b64encode(p.read_bytes()).decode(),
|
||||
'mode': p.stat().st_mode & 0o777} if p.exists() else None
|
||||
for r, p in paths.items()}
|
||||
state = {'before': before, 'installed': {r: digest(v) for r, v in files.items()}}
|
||||
atomic(journal, json.dumps(state, sort_keys=True).encode())
|
||||
try:
|
||||
for r in changes:
|
||||
atomic(paths[r], files[r])
|
||||
except BaseException:
|
||||
try:
|
||||
restore(paths, current_snapshot)
|
||||
if previous_journal is None:
|
||||
journal.unlink()
|
||||
else:
|
||||
atomic(journal, previous_journal)
|
||||
except BaseException:
|
||||
pass
|
||||
restore(paths, before)
|
||||
journal.unlink()
|
||||
raise
|
||||
committed_state = {
|
||||
'phase': 'installed',
|
||||
'before': original_before,
|
||||
'installed': incoming_installed,
|
||||
}
|
||||
atomic(journal, json.dumps(committed_state, sort_keys=True).encode())
|
||||
return {'status': 'installed_config_only', 'files': len(files)}
|
||||
|
||||
|
||||
|
||||
@@ -41,95 +41,6 @@ class InstallerTest(unittest.TestCase):
|
||||
self.assertEqual(status['stage'], 'blocked')
|
||||
self.assertFalse(status['cron_activation_allowed'])
|
||||
|
||||
def test_verified_previous_payload_can_upgrade_without_losing_rollback(self):
|
||||
import hashlib
|
||||
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
path.parent.mkdir(parents=True)
|
||||
path.write_text('original owned content')
|
||||
path.chmod(0o600)
|
||||
self.assertEqual(self.run_cli('install').returncode, 0)
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
state = json.loads(journal.read_text())
|
||||
original_before = state['before']
|
||||
# CI1285: live payload exactly matches the previous journal, while
|
||||
# the incoming reviewed package changes the PMO skill only.
|
||||
path.write_text('previous reviewed PMO skill')
|
||||
state['installed'][str(path.relative_to(self.home))] = hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
journal.write_text(json.dumps(state))
|
||||
result = self.run_cli('install')
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(self.run_cli('verify').returncode, 0)
|
||||
self.assertEqual(json.loads(journal.read_text())['before'], original_before)
|
||||
snapshot = {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns,
|
||||
p.stat().st_uid, p.stat().st_gid, p.stat().st_mode)
|
||||
for p in self.home.rglob('*') if p.is_file()}
|
||||
self.assertEqual(self.run_cli('install').returncode, 0)
|
||||
self.assertEqual(snapshot, {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns,
|
||||
p.stat().st_uid, p.stat().st_gid, p.stat().st_mode)
|
||||
for p in self.home.rglob('*') if p.is_file()})
|
||||
self.assertEqual(self.run_cli('rollback').returncode, 0)
|
||||
self.assertEqual(path.read_text(), 'original owned content')
|
||||
self.assertEqual(path.stat().st_mode & 0o777, 0o600)
|
||||
|
||||
def test_failed_upgrade_restores_previous_payload_and_exact_journal(self):
|
||||
import hashlib
|
||||
import importlib.util
|
||||
from unittest.mock import patch
|
||||
spec = importlib.util.spec_from_file_location('installer', SCRIPT)
|
||||
installer = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(installer)
|
||||
installer.execute('install', self.home)
|
||||
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
path.write_text('previous reviewed version')
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
state = json.loads(journal.read_text())
|
||||
state['installed'][str(path.relative_to(self.home))] = hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
journal.write_text(json.dumps(state))
|
||||
original = journal.read_bytes()
|
||||
real_atomic = installer.atomic
|
||||
failed = False
|
||||
def fail_once(destination, data):
|
||||
nonlocal failed
|
||||
if destination == path and not failed:
|
||||
failed = True
|
||||
raise OSError('injected write failure')
|
||||
return real_atomic(destination, data)
|
||||
with patch.object(installer, 'atomic', side_effect=fail_once):
|
||||
with self.assertRaisesRegex(OSError, 'injected write failure'):
|
||||
installer.execute('install', self.home)
|
||||
self.assertEqual(path.read_text(), 'previous reviewed version')
|
||||
self.assertTrue(journal.exists(), 'rollback history must survive a failed upgrade')
|
||||
self.assertEqual(journal.read_bytes(), original)
|
||||
|
||||
def test_upgrade_refuses_drift_partial_and_invalid_journal_without_writes(self):
|
||||
self.assertEqual(self.run_cli('install').returncode, 0)
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
original = journal.read_bytes()
|
||||
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
payload = path.read_bytes()
|
||||
for failure in ('drift', 'missing', 'allowlist', 'rollback_data'):
|
||||
with self.subTest(failure=failure):
|
||||
path.write_bytes(payload)
|
||||
journal.write_bytes(original)
|
||||
state = json.loads(original)
|
||||
if failure == 'drift':
|
||||
path.write_text('external change')
|
||||
elif failure == 'missing':
|
||||
path.unlink()
|
||||
elif failure == 'allowlist':
|
||||
path.write_text('previous version')
|
||||
state['installed'].pop('editorial-v4/status.json')
|
||||
journal.write_text(json.dumps(state))
|
||||
else:
|
||||
path.write_text('previous version')
|
||||
state['before']['editorial-v4/status.json'] = {'data': '!!!!', 'mode': 420}
|
||||
journal.write_text(json.dumps(state))
|
||||
before = {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns)
|
||||
for p in self.home.rglob('*') if p.is_file()}
|
||||
self.assertNotEqual(self.run_cli('install').returncode, 0)
|
||||
self.assertEqual(before, {str(p.relative_to(self.home)): (p.read_bytes(), p.stat().st_mtime_ns)
|
||||
for p in self.home.rglob('*') if p.is_file()})
|
||||
|
||||
def test_rollback_restores_previous_files_and_modes(self):
|
||||
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
path.parent.mkdir(parents=True)
|
||||
@@ -158,192 +69,5 @@ class InstallerTest(unittest.TestCase):
|
||||
self.assertNotEqual(self.run_cli('rollback').returncode, 0)
|
||||
self.assertEqual(path.read_text(), 'external change')
|
||||
|
||||
def test_interrupted_upgrade_multi_file_safe_recovery_and_install(self):
|
||||
import hashlib
|
||||
path_pmo = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
path_pmo.parent.mkdir(parents=True, exist_ok=True)
|
||||
path_pmo.write_text('original owned baseline')
|
||||
path_pmo.chmod(0o600)
|
||||
self.assertEqual(self.run_cli('install').returncode, 0)
|
||||
|
||||
# Verified previous installed state modifying status.json and pmo SKILL.md
|
||||
path_status = self.home / 'editorial-v4/status.json'
|
||||
v1_status = '{"stage": "blocked", "cron_activation_allowed": false, "v": "1"}'
|
||||
v1_pmo = 'reviewed pmo v1 content'
|
||||
path_status.write_text(v1_status)
|
||||
path_pmo.write_text(v1_pmo)
|
||||
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
state = json.loads(journal.read_text())
|
||||
rel_status = str(path_status.relative_to(self.home))
|
||||
rel_pmo = str(path_pmo.relative_to(self.home))
|
||||
state['installed'][rel_status] = hashlib.sha256(v1_status.encode()).hexdigest()
|
||||
state['installed'][rel_pmo] = hashlib.sha256(v1_pmo.encode()).hexdigest()
|
||||
journal.write_text(json.dumps(state))
|
||||
|
||||
# Crash midway during install: status.json is written, but process dies before pmo SKILL.md
|
||||
crash_code = f"""
|
||||
import os, sys, importlib.util, pathlib
|
||||
spec = importlib.util.spec_from_file_location('installer', '{SCRIPT}')
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
real_atomic = m.atomic
|
||||
def crash_atomic(p, data):
|
||||
res = real_atomic(p, data)
|
||||
if str(p).endswith('editorial-v4/status.json'):
|
||||
os._exit(42)
|
||||
return res
|
||||
m.atomic = crash_atomic
|
||||
m.execute('install', pathlib.Path('{self.home}'))
|
||||
"""
|
||||
proc = subprocess.run([sys.executable, '-c', crash_code])
|
||||
self.assertEqual(proc.returncode, 42)
|
||||
|
||||
# Confirm mixed state on disk: status.json updated to incoming, pmo SKILL.md still v1
|
||||
self.assertNotEqual(path_status.read_text(), v1_status)
|
||||
self.assertEqual(path_pmo.read_text(), v1_pmo)
|
||||
|
||||
# Rerun install: must safely recover from interrupted transaction and finish upgrade
|
||||
res = self.run_cli('install')
|
||||
self.assertEqual(res.returncode, 0, f"install recovery failed: {res.stderr}")
|
||||
self.assertEqual(self.run_cli('verify').returncode, 0)
|
||||
|
||||
# Rollback should still restore original owned baseline
|
||||
self.assertEqual(self.run_cli('rollback').returncode, 0)
|
||||
self.assertEqual(path_pmo.read_text(), 'original owned baseline')
|
||||
self.assertEqual(path_pmo.stat().st_mode & 0o777, 0o600)
|
||||
|
||||
def test_interrupted_upgrade_multi_file_rollback_restores_original(self):
|
||||
import hashlib
|
||||
path_pmo = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
path_pmo.parent.mkdir(parents=True, exist_ok=True)
|
||||
path_pmo.write_text('original owned baseline')
|
||||
path_pmo.chmod(0o600)
|
||||
self.assertEqual(self.run_cli('install').returncode, 0)
|
||||
|
||||
path_status = self.home / 'editorial-v4/status.json'
|
||||
v1_status = '{"stage": "blocked", "cron_activation_allowed": false, "v": "1"}'
|
||||
v1_pmo = 'reviewed pmo v1 content'
|
||||
path_status.write_text(v1_status)
|
||||
path_pmo.write_text(v1_pmo)
|
||||
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
state = json.loads(journal.read_text())
|
||||
rel_status = str(path_status.relative_to(self.home))
|
||||
rel_pmo = str(path_pmo.relative_to(self.home))
|
||||
state['installed'][rel_status] = hashlib.sha256(v1_status.encode()).hexdigest()
|
||||
state['installed'][rel_pmo] = hashlib.sha256(v1_pmo.encode()).hexdigest()
|
||||
journal.write_text(json.dumps(state))
|
||||
|
||||
crash_code = f"""
|
||||
import os, sys, importlib.util, pathlib
|
||||
spec = importlib.util.spec_from_file_location('installer', '{SCRIPT}')
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
real_atomic = m.atomic
|
||||
def crash_atomic(p, data):
|
||||
res = real_atomic(p, data)
|
||||
if str(p).endswith('editorial-v4/status.json'):
|
||||
os._exit(42)
|
||||
return res
|
||||
m.atomic = crash_atomic
|
||||
m.execute('install', pathlib.Path('{self.home}'))
|
||||
"""
|
||||
proc = subprocess.run([sys.executable, '-c', crash_code])
|
||||
self.assertEqual(proc.returncode, 42)
|
||||
|
||||
# Rollback on interrupted state must safely recover and restore original owned baseline
|
||||
res = self.run_cli('rollback')
|
||||
self.assertEqual(res.returncode, 0, f"rollback recovery failed: {res.stderr}")
|
||||
self.assertEqual(path_pmo.read_text(), 'original owned baseline')
|
||||
self.assertEqual(path_pmo.stat().st_mode & 0o777, 0o600)
|
||||
self.assertFalse(journal.exists())
|
||||
|
||||
def test_interrupted_upgrade_rejects_external_drift(self):
|
||||
import hashlib
|
||||
self.assertEqual(self.run_cli('install').returncode, 0)
|
||||
|
||||
path_status = self.home / 'editorial-v4/status.json'
|
||||
path_pmo = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
v1_status = '{"stage": "blocked", "cron_activation_allowed": false, "v": "1"}'
|
||||
v1_pmo = 'reviewed pmo v1 content'
|
||||
path_status.write_text(v1_status)
|
||||
path_pmo.write_text(v1_pmo)
|
||||
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
state = json.loads(journal.read_text())
|
||||
rel_status = str(path_status.relative_to(self.home))
|
||||
rel_pmo = str(path_pmo.relative_to(self.home))
|
||||
state['installed'][rel_status] = hashlib.sha256(v1_status.encode()).hexdigest()
|
||||
state['installed'][rel_pmo] = hashlib.sha256(v1_pmo.encode()).hexdigest()
|
||||
journal.write_text(json.dumps(state))
|
||||
|
||||
crash_code = f"""
|
||||
import os, sys, importlib.util, pathlib
|
||||
spec = importlib.util.spec_from_file_location('installer', '{SCRIPT}')
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
real_atomic = m.atomic
|
||||
def crash_atomic(p, data):
|
||||
res = real_atomic(p, data)
|
||||
if str(p).endswith('editorial-v4/status.json'):
|
||||
os._exit(42)
|
||||
return res
|
||||
m.atomic = crash_atomic
|
||||
m.execute('install', pathlib.Path('{self.home}'))
|
||||
"""
|
||||
proc = subprocess.run([sys.executable, '-c', crash_code])
|
||||
self.assertEqual(proc.returncode, 42)
|
||||
|
||||
# External drift on path_pmo
|
||||
path_pmo.write_text('tampered external edit')
|
||||
|
||||
res_install = self.run_cli('install')
|
||||
self.assertNotEqual(res_install.returncode, 0)
|
||||
self.assertEqual(path_pmo.read_text(), 'tampered external edit')
|
||||
|
||||
res_rollback = self.run_cli('rollback')
|
||||
self.assertNotEqual(res_rollback.returncode, 0)
|
||||
self.assertEqual(path_pmo.read_text(), 'tampered external edit')
|
||||
|
||||
def test_failed_upgrade_restoration_failure_preserves_journal(self):
|
||||
import hashlib, importlib.util
|
||||
from unittest.mock import patch
|
||||
spec = importlib.util.spec_from_file_location('installer', SCRIPT)
|
||||
installer = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(installer)
|
||||
installer.execute('install', self.home)
|
||||
|
||||
path = self.home / 'profiles/pmo/skills/canina-editorial-v4/SKILL.md'
|
||||
path.write_text('previous reviewed version')
|
||||
journal = self.home / 'editorial-v4/.installer/transaction.json'
|
||||
state = json.loads(journal.read_text())
|
||||
rel = str(path.relative_to(self.home))
|
||||
state['installed'][rel] = hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
journal.write_text(json.dumps(state))
|
||||
|
||||
real_atomic = installer.atomic
|
||||
fail_target = path
|
||||
def fail_pmo(dest, data):
|
||||
if dest == fail_target:
|
||||
raise OSError('injected write failure on pmo')
|
||||
return real_atomic(dest, data)
|
||||
|
||||
def fail_restore(paths, before):
|
||||
raise OSError('injected restoration failure')
|
||||
|
||||
with patch.object(installer, 'atomic', side_effect=fail_pmo):
|
||||
with patch.object(installer, 'restore', side_effect=fail_restore):
|
||||
with self.assertRaisesRegex(OSError, 'injected write failure on pmo'):
|
||||
installer.execute('install', self.home)
|
||||
|
||||
# Journal must not be lost or unlinked, and must retain previous rollback metadata
|
||||
self.assertTrue(journal.exists())
|
||||
jdata = json.loads(journal.read_text())
|
||||
self.assertIn('before', jdata)
|
||||
# Previous installed metadata must be preserved
|
||||
prev = jdata.get('previous_installed') or jdata.get('installed')
|
||||
self.assertEqual(prev[rel], hashlib.sha256(b'previous reviewed version').hexdigest())
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+4
-12
@@ -1,9 +1,5 @@
|
||||
server {
|
||||
listen 8080;
|
||||
# Docker service IPs change on recreation; resolve at request time, not startup.
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
resolver_timeout 2s;
|
||||
set $backend_upstream __BACKEND_HOST__:3000;
|
||||
server_name canina.ir stage.canina.ir localhost;
|
||||
|
||||
# Security Headers
|
||||
@@ -35,7 +31,7 @@ server {
|
||||
|
||||
# Uploads Static Files
|
||||
location /uploads {
|
||||
proxy_pass http://$backend_upstream;
|
||||
proxy_pass http://__BACKEND_HOST__:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
@@ -46,7 +42,7 @@ server {
|
||||
|
||||
# Backend API
|
||||
location /api {
|
||||
proxy_pass http://$backend_upstream;
|
||||
proxy_pass http://__BACKEND_HOST__:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
@@ -58,10 +54,6 @@ server {
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
# Docker service IPs change on recreation; resolve at request time, not startup.
|
||||
resolver 127.0.0.11 valid=5s ipv6=off;
|
||||
resolver_timeout 2s;
|
||||
set $backend_upstream __BACKEND_HOST__:3000;
|
||||
server_name admin.canina.ir stageadmin.canina.ir;
|
||||
|
||||
root /app/admin_static;
|
||||
@@ -81,7 +73,7 @@ server {
|
||||
|
||||
# Uploads Static Files for Admin
|
||||
location /uploads {
|
||||
proxy_pass http://$backend_upstream;
|
||||
proxy_pass http://__BACKEND_HOST__:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
@@ -92,7 +84,7 @@ server {
|
||||
|
||||
# Backend API for Admin
|
||||
location /api {
|
||||
proxy_pass http://$backend_upstream;
|
||||
proxy_pass http://__BACKEND_HOST__:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
-- Independently approved reconciliation; NOT a Prisma history migration.
|
||||
-- Run with psql -X -v ON_ERROR_STOP=1. Do not wrap in migrate deploy/db push.
|
||||
BEGIN;
|
||||
SET LOCAL search_path = public, pg_catalog;
|
||||
SET LOCAL lock_timeout = '3s';
|
||||
SET LOCAL statement_timeout = '30s';
|
||||
SET LOCAL idle_in_transaction_session_timeout = '60s';
|
||||
SELECT pg_advisory_xact_lock(20261025, 1);
|
||||
DO $$
|
||||
DECLARE x record;
|
||||
BEGIN
|
||||
IF to_regclass('public.users') IS NULL THEN RAISE EXCEPTION 'users missing'; END IF;
|
||||
FOR x IN SELECT * FROM (VALUES ('auth_expires_at','timestamp with time zone'),('stage_test_session','character varying(64)'),('stage_test_generation','uuid')) AS expected(name,typ) LOOP
|
||||
IF EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid='public.users'::regclass AND attname=x.name AND NOT attisdropped) THEN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_attribute a LEFT JOIN pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum WHERE a.attrelid='public.users'::regclass AND a.attname=x.name AND NOT a.attisdropped AND format_type(a.atttypid,a.atttypmod)=x.typ AND NOT a.attnotnull AND d.oid IS NULL) THEN
|
||||
RAISE EXCEPTION 'unexpected existing users column %',x.name;
|
||||
END IF;
|
||||
END IF;
|
||||
END LOOP;
|
||||
IF to_regclass('public.prepare_workflow_outbox') IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'outbox already exists: inspect equivalence before any repeat execution';
|
||||
END IF;
|
||||
FOR x IN SELECT * FROM (VALUES ('orchestration_tasks'),('orchestration_runs'),('orchestration_budget_ledger')) AS refs(name) LOOP
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_attribute a WHERE a.attrelid=to_regclass('public.'||x.name) AND a.attname='id' AND a.atttypid='uuid'::regtype AND NOT a.attisdropped AND a.attnotnull) THEN
|
||||
RAISE EXCEPTION 'missing UUID reference %',x.name;
|
||||
END IF;
|
||||
END LOOP;
|
||||
END $$;
|
||||
ALTER TABLE public.users ADD COLUMN IF NOT EXISTS auth_expires_at timestamptz,
|
||||
ADD COLUMN IF NOT EXISTS stage_test_session varchar(64),
|
||||
ADD COLUMN IF NOT EXISTS stage_test_generation uuid;
|
||||
CREATE TABLE public.prepare_workflow_outbox (
|
||||
id uuid PRIMARY KEY,
|
||||
task_id uuid NOT NULL UNIQUE REFERENCES public.orchestration_tasks(id) ON DELETE RESTRICT,
|
||||
status varchar(20) NOT NULL DEFAULT 'PENDING' CHECK (status IN ('PENDING','DONE')),
|
||||
attempts integer NOT NULL DEFAULT 0 CHECK (attempts BETWEEN 0 AND 3),
|
||||
lease_owner varchar(100), lease_token uuid, lease_until timestamptz, heartbeat_at timestamptz,
|
||||
run_id uuid REFERENCES public.orchestration_runs(id) ON DELETE RESTRICT,
|
||||
reservation_id uuid REFERENCES public.orchestration_budget_ledger(id) ON DELETE RESTRICT,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
not_before timestamptz
|
||||
);
|
||||
CREATE INDEX prepare_workflow_outbox_status_lease_until_idx ON public.prepare_workflow_outbox(status,lease_until);
|
||||
CREATE INDEX prepare_workflow_outbox_due_idx ON public.prepare_workflow_outbox(status,not_before,created_at);
|
||||
-- Deliberately no _prisma_migrations writes or unrelated DDL.
|
||||
COMMIT;
|
||||
@@ -1,4 +0,0 @@
|
||||
BEGIN READ ONLY;
|
||||
SET LOCAL search_path=public,pg_catalog;
|
||||
SELECT jsonb_build_object('columns', (SELECT jsonb_agg(jsonb_build_array(c.relname,a.attname,format_type(a.atttypid,a.atttypmod),a.attnotnull,pg_get_expr(d.adbin,d.adrelid)) ORDER BY c.relname,a.attname) FROM pg_attribute a JOIN pg_class c ON c.oid=a.attrelid LEFT JOIN pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum WHERE c.oid IN ('public.users'::regclass,'public.prepare_workflow_outbox'::regclass) AND a.attnum>0 AND NOT a.attisdropped AND (c.relname='prepare_workflow_outbox' OR a.attname IN ('auth_expires_at','stage_test_session','stage_test_generation'))), 'constraints',(SELECT jsonb_agg(jsonb_build_array(conname,pg_get_constraintdef(oid)) ORDER BY conname) FROM pg_constraint WHERE conrelid='public.prepare_workflow_outbox'::regclass), 'indexes',(SELECT jsonb_agg(jsonb_build_array(indexname,indexdef) ORDER BY indexname) FROM pg_indexes WHERE schemaname='public' AND tablename='prepare_workflow_outbox'));
|
||||
COMMIT;
|
||||
@@ -1,60 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Executed on authenticated host by the dedicated CI workflow, never at app startup.
|
||||
set -euo pipefail
|
||||
set +x
|
||||
[[ ${GITHUB_ACTIONS:-} == true && $# == 3 ]] || exit 2
|
||||
environment=$1; sha=$2; approved_sql_sha=$3
|
||||
[[ $sha =~ ^[0-9a-f]{40}$ && $approved_sql_sha =~ ^[0-9a-f]{64}$ ]] || exit 2
|
||||
case "$environment" in stage) container=canino_db_stage;; production) container=canino_db_prod;; *) exit 2;; esac
|
||||
root=$(git rev-parse --show-toplevel)
|
||||
[[ $(git rev-parse HEAD) == "$sha" && -z $(git status --porcelain) ]] || exit 2
|
||||
sql="$root/scripts/auth-schema-reconcile.sql"
|
||||
[[ $(sha256sum "$sql" | cut -d' ' -f1) == "$approved_sql_sha" ]] || exit 2
|
||||
umask 077
|
||||
backup_root=/opt/backups/canina-auth-reconcile
|
||||
mkdir -p "$backup_root"
|
||||
exec 9>"$backup_root/operation.lock"
|
||||
flock -n 9
|
||||
receipt="$backup_root/stage-$approved_sql_sha.receipt"
|
||||
if [[ $environment == production ]]; then
|
||||
[[ -s $receipt ]] || { printf 'Missing Stage receipt\n' >&2; exit 2; }
|
||||
[[ $(cat "$receipt") == "$sha" ]] || { printf 'Stage SHA mismatch\n' >&2; exit 2; }
|
||||
# Auth acceptance is independently approved, never manufactured by this script.
|
||||
[[ ${STAGE_AUTH_ACCEPTANCE_APPROVED:-} == "$sha:$approved_sql_sha" ]] || exit 2
|
||||
[[ ${PRODUCTION_SCHEMA_APPROVED:-} == "$sha:$approved_sql_sha" ]] || exit 2
|
||||
source_container=canino_db_prod
|
||||
else
|
||||
# Stage already contains these objects: rehearse the missing delta against a
|
||||
# production backup, then compare exact Stage catalog to that rehearsed result.
|
||||
source_container=canino_db_prod
|
||||
fi
|
||||
# Both approvals are supplied only by the reviewed protected workflow.
|
||||
backup="$backup_root/$environment-$sha-$(date -u +%Y%m%dT%H%M%SZ).dump"
|
||||
# Full backup remains restricted on host; never emitted to CI logs or artifacts.
|
||||
docker exec "$source_container" sh -c 'export PGPASSWORD="${POSTGRES_PASSWORD:-}"; PGOPTIONS="-c default_transaction_read_only=on" pg_dump -h 127.0.0.1 -Fc --no-owner --no-privileges -U "$POSTGRES_USER" -d "$POSTGRES_DB"' > "$backup"
|
||||
[[ -s $backup ]]
|
||||
docker exec -i "$container" pg_restore --list < "$backup" > "$backup.toc"
|
||||
[[ -s $backup.toc ]]
|
||||
printf 'Backup verified: bytes=%s sha256=%s\n' "$(stat -c %s "$backup")" "$(sha256sum "$backup" | cut -d' ' -f1)"
|
||||
# Restore the FULL backup into an isolated disposable database container first.
|
||||
name="canina-auth-restore-${environment}-${sha:0:12}"
|
||||
cleanup() { docker rm -f "$name" >/dev/null 2>&1 || true; }
|
||||
trap cleanup EXIT
|
||||
docker run -d --name "$name" --network none --tmpfs /var/lib/postgresql/data:rw,size=1g -e POSTGRES_HOST_AUTH_METHOD=trust postgres:16-alpine >/dev/null
|
||||
ready=false
|
||||
# The entrypoint's temporary init server listens only on a Unix socket. TCP
|
||||
# becomes ready only after initialization hands off to the final server.
|
||||
for _ in {1..60}; do if docker exec "$name" pg_isready -h 127.0.0.1 -U postgres -d postgres >/dev/null 2>&1; then ready=true; break; fi; sleep 1; done
|
||||
[[ $ready == true ]]
|
||||
docker exec -i "$name" pg_restore --exit-on-error -h 127.0.0.1 --no-owner --no-privileges -U postgres -d postgres < "$backup"
|
||||
docker exec -i "$name" psql -h 127.0.0.1 -X -v ON_ERROR_STOP=1 -U postgres -d postgres < "$sql"
|
||||
docker exec -i "$name" psql -h 127.0.0.1 -X -qAt -v ON_ERROR_STOP=1 -U postgres -d postgres < "$root/scripts/auth-schema-verify.sql" > "$backup.expected.json"
|
||||
if [[ $environment == production ]]; then
|
||||
# Only the reviewed SQL is executed. No Prisma CLI, seeds or ledger edits.
|
||||
docker exec -i "$container" sh -c 'export PGPASSWORD="${POSTGRES_PASSWORD:-}"; psql -h 127.0.0.1 -X -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"' < "$sql"
|
||||
fi
|
||||
docker exec -i "$container" sh -c 'export PGPASSWORD="${POSTGRES_PASSWORD:-}"; PGOPTIONS="-c default_transaction_read_only=on" psql -h 127.0.0.1 -X -qAt -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB"' < "$root/scripts/auth-schema-verify.sql" > "$backup.actual.json"
|
||||
cmp "$backup.expected.json" "$backup.actual.json"
|
||||
cleanup; trap - EXIT
|
||||
if [[ $environment == stage ]]; then printf '%s\n' "$sha" > "$receipt"; fi
|
||||
printf 'Narrow additive reconciliation complete: environment=%s commit=%s sql_sha256=%s\n' "$environment" "$sha" "$approved_sql_sha"
|
||||
@@ -3,7 +3,7 @@
|
||||
set -euo pipefail
|
||||
set +x
|
||||
fail() { printf 'ERROR: %s\n' "$1" >&2; exit 2; }
|
||||
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ( ${BRANCH:-} == develop || ${BRANCH:-} == main ) && $# == 0 ]] || fail 'CI-only immutable deployment'
|
||||
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${BRANCH:-} == develop && $# == 0 ]] || fail 'CI-only Stage deployment; Production is blocked'
|
||||
[[ ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ ]] || fail 'immutable DEPLOY_SHA required'
|
||||
# Repository secret DEPLOY_KEY is the historical approved devops SSH credential.
|
||||
# Never fall back to baked-in runner keys, agent forwarding or docker.sock.
|
||||
@@ -22,12 +22,12 @@ ssh -F /dev/null -T -p 2234 -i "$scratch/key" \
|
||||
-o BatchMode=yes -o IdentitiesOnly=yes -o IdentityAgent=none \
|
||||
-o StrictHostKeyChecking=yes -o UserKnownHostsFile="$scratch/known_hosts" \
|
||||
-o GlobalKnownHostsFile=/dev/null -o ConnectTimeout=15 \
|
||||
devops@87.248.133.138 "sudo -n bash -s -- $DEPLOY_SHA $BRANCH" <<'HOST'
|
||||
devops@87.248.133.138 "sudo -n bash -s -- $DEPLOY_SHA" <<'HOST'
|
||||
set -euo pipefail
|
||||
set +x
|
||||
umask 077
|
||||
fail() { printf 'ERROR: host context: %s\n' "$1" >&2; exit 2; }
|
||||
[[ $# == 2 && $1 =~ ^[0-9a-f]{40}$ && ( $2 == develop || $2 == main ) ]] || fail 'immutable SHA and approved branch required'
|
||||
[[ $# == 1 && $1 =~ ^[0-9a-f]{40}$ ]] || fail 'immutable SHA required'
|
||||
[[ ! -e /.dockerenv && ! -e /run/.containerenv ]] || fail 'refusing container filesystem'
|
||||
[[ $(id -u) == 0 ]] || fail 'noninteractive approved sudo required'
|
||||
for tool in bash python3 docker git flock realpath install wget; do command -v "$tool" >/dev/null || fail "missing $tool"; done
|
||||
@@ -36,13 +36,10 @@ for tool in bash python3 docker git flock realpath install wget; do command -v "
|
||||
docker compose version >/dev/null
|
||||
docker volume inspect stage_canina_stage_db stage_canina_stage_uploads >/dev/null
|
||||
docker network inspect traefik_public >/dev/null
|
||||
if [[ $2 == main ]]; then
|
||||
[[ -s /opt/services/canina/production-acceptance/$1.json ]] || fail 'audited Stage acceptance receipt missing'
|
||||
fi
|
||||
printf 'Host context validated; branch=%s commit=%s\n' "$2" "$1"
|
||||
printf 'Host context validated; Stage commit=%s; runtime env present; Production blocked\n' "$1"
|
||||
script=$(mktemp)
|
||||
trap 'rm -f "$script"' EXIT
|
||||
wget --no-proxy -qO "$script" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/${1}/scripts/deploy.sh"
|
||||
# CI marker exists only across this authenticated, fixed host boundary.
|
||||
GITHUB_ACTIONS=true STAGE_PROVISION_ENABLED=true DEPLOY_SHA="$1" bash "$script" "$2"
|
||||
# CI marker exists only across this authenticated, fixed Stage boundary.
|
||||
GITHUB_ACTIONS=true STAGE_PROVISION_ENABLED=true DEPLOY_SHA="$1" bash "$script" develop
|
||||
HOST
|
||||
@@ -24,14 +24,14 @@ services:
|
||||
context: ./backend
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.parsaaghayi.ir/repository/npm/}
|
||||
NPM_REGISTRY: ${NPM_REGISTRY:-}
|
||||
container_name: canino_backend_stage
|
||||
restart: always
|
||||
env_file:
|
||||
- path: .support-chat.env
|
||||
required: false
|
||||
- path: .revalidation.env
|
||||
required: true
|
||||
required: false
|
||||
dns:
|
||||
- 217.218.127.127
|
||||
- 217.218.155.155
|
||||
@@ -42,8 +42,6 @@ services:
|
||||
- REDIS_PORT=6379
|
||||
- PORT=3000
|
||||
- FRONTEND_URL=https://stage.canina.ir
|
||||
# Direct Next port avoids nginx /api forwarding back into this backend.
|
||||
- FRONTEND_INTERNAL_URL=http://frontend_stage:3000
|
||||
# Code-only stage startup: schema/seed changes require separate authorization.
|
||||
command: ["node", "dist/main.js"]
|
||||
volumes:
|
||||
@@ -66,16 +64,14 @@ services:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.parsaaghayi.ir/repository/npm/}
|
||||
NPM_REGISTRY: ${NPM_REGISTRY:-}
|
||||
NEXT_PUBLIC_API_URL: "https://stageapi.canina.ir/api"
|
||||
VITE_API_URL: "https://stageapi.canina.ir"
|
||||
container_name: canino_frontend_stage
|
||||
restart: always
|
||||
environment:
|
||||
- INTERNAL_API_URL=http://backend_stage:3000/api
|
||||
env_file:
|
||||
- path: .revalidation.env
|
||||
required: true
|
||||
required: false
|
||||
dns:
|
||||
- 217.218.127.127
|
||||
- 217.218.155.155
|
||||
|
||||
+4
-19
@@ -1,9 +1,9 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
set +x
|
||||
# CI-only boundary: main is a code-only immutable promotion with private receipt.
|
||||
if [ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" != true ] || [[ "${1:-}" != develop && "${1:-}" != main ]] || [ "$#" != 1 ]; then
|
||||
echo 'ERROR: refusing deployment outside authenticated CI/CD rollout' >&2
|
||||
# CI/CD-only Stage rollout; Production awaits separate reviewed authorization.
|
||||
if [ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" != true ] || [ "${1:-}" != develop ] || [ "$#" != 1 ]; then
|
||||
echo 'ERROR: refusing deployment outside CI/CD Stage rollout' >&2
|
||||
exit 2
|
||||
fi
|
||||
if [[ ! "${DEPLOY_SHA:-}" =~ ^[0-9a-f]{40}$ ]]; then
|
||||
@@ -11,14 +11,6 @@ if [[ ! "${DEPLOY_SHA:-}" =~ ^[0-9a-f]{40}$ ]]; then
|
||||
exit 2
|
||||
fi
|
||||
# END ROLLOUT GUARD
|
||||
if [[ $1 == main ]]; then
|
||||
umask 077
|
||||
helper=$(mktemp)
|
||||
trap 'rm -f "$helper"' EXIT
|
||||
wget --no-proxy -qO "$helper" "https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/rollout-production.sh"
|
||||
bash "$helper"
|
||||
exit
|
||||
fi
|
||||
exec 9>/opt/services/canina/stage-backend-deploy.lock
|
||||
flock -n 9 || { echo 'Stage deployment already in progress' >&2; exit 1; }
|
||||
umask 077
|
||||
@@ -35,7 +27,6 @@ git clone --no-checkout --single-branch --branch develop https://git.parsaaghayi
|
||||
cd "$TARGET_DIR"
|
||||
git cat-file -e "${DEPLOY_SHA}^{commit}"
|
||||
git merge-base --is-ancestor "$DEPLOY_SHA" origin/develop
|
||||
git sparse-checkout set --no-cone '/*' '!/graphify-out/'
|
||||
git checkout --detach "$DEPLOY_SHA"
|
||||
[ "$(git rev-parse HEAD)" = "$DEPLOY_SHA" ]
|
||||
# Provision against the pristine approved SHA, before any tracked render edits.
|
||||
@@ -52,7 +43,7 @@ printf '\n.support-chat.env\n.revalidation.env\n.stage-base.json\n.stage-compose
|
||||
printf '\n.support-chat.env\n.revalidation.env\n.stage-*.json\n' >> .dockerignore
|
||||
# Only these backend/frontend runtime env files are required. Do not copy the
|
||||
# dirty compose, .env, uploads, bot state, certificates, or arbitrary Stage files.
|
||||
for name in .support-chat.env; do
|
||||
for name in .support-chat.env .revalidation.env; do
|
||||
if [ -e "$STAGE_DIR/$name" ] || [ -L "$STAGE_DIR/$name" ]; then
|
||||
[ -f "$STAGE_DIR/$name" ] && [ ! -L "$STAGE_DIR/$name" ]
|
||||
[ -z "$(git -C "$STAGE_DIR" ls-files -- "$name")" ]
|
||||
@@ -60,7 +51,6 @@ for name in .support-chat.env; do
|
||||
install -m 600 "$STAGE_DIR/$name" "$TARGET_DIR/$name"
|
||||
fi
|
||||
done
|
||||
stage_revalidation_provision
|
||||
[ -s .support-chat.env ] || { echo 'ERROR: missing Stage support runtime env' >&2; exit 1; }
|
||||
# Never inspect/recreate/mutate the shared support bot or infer keys from it.
|
||||
# Certs remain the explicit operational absolute bind from tracked compose.
|
||||
@@ -126,9 +116,4 @@ source scripts/rollout-stage.sh
|
||||
# This preserves the root-private intent journal and fails closed on timer failure.
|
||||
python3 scripts/install-stage-principal-cleanup.py
|
||||
stage_rollout
|
||||
python3 scripts/test-release-retention.py
|
||||
# Restore only CI-owned render edits after images and acceptance succeed.
|
||||
git restore -- .dockerignore nginx.conf
|
||||
python3 scripts/install-disk-watch.py
|
||||
python3 scripts/release-retention.py --apply --environment stage --current-sha "$DEPLOY_SHA" > /opt/services/canina/tmp/retention-stage.json
|
||||
echo 'Deployment for develop completed successfully!'
|
||||
@@ -1,14 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""No-model threshold check. Exit 1 at >=85% used or <10GiB available."""
|
||||
import json,os,subprocess,sys
|
||||
|
||||
def pressure(total,available):return available < 10*1024**3 or (total-available)/total >= .85
|
||||
|
||||
def main():
|
||||
s=os.statvfs('/');total=s.f_blocks*s.f_frsize;available=s.f_bavail*s.f_frsize
|
||||
alert=pressure(total,available)
|
||||
message=json.dumps({'service':'canina-disk-watch','alert':alert,'total':total,'available':available})
|
||||
print(message)
|
||||
if alert:subprocess.run(['logger','-p','daemon.err','-t','canina-disk-watch',message],check=True)
|
||||
return int(alert)
|
||||
if __name__=='__main__':sys.exit(main())
|
||||
@@ -1,12 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install deterministic host-only pressure watch through authenticated CI."""
|
||||
import os,pathlib,subprocess
|
||||
if os.environ.get('GITHUB_ACTIONS',os.environ.get('GITEA_ACTIONS'))!='true' or os.getuid()!=0 or pathlib.Path('/.dockerenv').exists():raise SystemExit('CI host root required')
|
||||
root=pathlib.Path('/opt/services/canina/.disk-watch');root.mkdir(mode=0o700,exist_ok=True)
|
||||
if root.is_symlink() or root.resolve()!=root:raise SystemExit('Unsafe installer path')
|
||||
(root/'disk-watch.py').write_bytes(pathlib.Path(__file__).with_name('disk-watch.py').read_bytes())
|
||||
pathlib.Path('/etc/systemd/system/canina-disk-watch.service').write_text('[Unit]\nDescription=Canina deterministic disk pressure check\n[Service]\nType=oneshot\nExecStart=/usr/bin/python3 /opt/services/canina/.disk-watch/disk-watch.py\nNoNewPrivileges=true\nProtectSystem=strict\nProtectHome=true\nPrivateTmp=true\n')
|
||||
pathlib.Path('/etc/systemd/system/canina-disk-watch.timer').write_text('[Unit]\nDescription=Check disk every five minutes without inference\n[Timer]\nOnBootSec=2min\nOnUnitActiveSec=5min\n[Install]\nWantedBy=timers.target\n')
|
||||
subprocess.run(['systemctl','daemon-reload'],check=True)
|
||||
subprocess.run(['systemctl','enable','--now','canina-disk-watch.timer'],check=True)
|
||||
subprocess.run(['systemctl','is-active','--quiet','canina-disk-watch.timer'],check=True)
|
||||
@@ -1,222 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Private production acceptance and runtime snapshot helpers. Never log config."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
ALLOWED = {'nginx.conf', 'Dockerfile', 'backend/Dockerfile', '.dockerignore',
|
||||
'.gitea/workflows/deploy.yml', 'scripts/deploy.sh', 'scripts/ci-host-deploy.sh',
|
||||
'scripts/rollout-production.sh', 'scripts/production-release.py', 'scripts/test_production_release.py',
|
||||
'scripts/tests/test_release_schema_guard.py',
|
||||
# Exact CEO-approved schema CI repair; no migration/workflow wildcard.
|
||||
'.gitea/workflows/auth-schema-reconcile.yml',
|
||||
'docs/auth-schema-reconciliation.md', 'docs/auth-schema-rehearsal.json',
|
||||
'scripts/auth-schema-reconcile.sql', 'scripts/auth-schema-verify.sql',
|
||||
'scripts/ci-auth-schema-reconcile.sh', 'scripts/test_auth_schema_reconcile.py',
|
||||
'scripts/test_auth_schema_push.py',
|
||||
'scripts/compose.stage.yml', 'scripts/rollout-stage.sh', 'scripts/revalidation-runtime.py',
|
||||
'scripts/provision-revalidation-env.py', 'scripts/tests/test_revalidation_runtime.py',
|
||||
'scripts/tests/test_ci_host_boundary.py', 'scripts/tests/test_stage_provision_wiring.py',
|
||||
'scripts/tests/test_stage_rollout.py', 'scripts/tests/test_secure_build_install.py',
|
||||
# CI1285 verified-payload upgrade repair; no model, cron or payload wildcard.
|
||||
'infra/hermes-editorial/installer.py',
|
||||
'infra/hermes-editorial/tests/test_installer.py',
|
||||
'backend/src/common/revalidation/revalidation.service.ts',
|
||||
'backend/src/common/revalidation/revalidation.service.spec.ts',
|
||||
'backend/src/orchestration-worker/worker-registration.spec.ts',
|
||||
'backend/src/support-chat/support-chat.http.spec.ts',
|
||||
# Exact baseline gate repairs reviewed in 1bb6274; never a frontend wildcard.
|
||||
'frontend/admin-panel/src/pages/OrchestrationCommandCenter.tsx',
|
||||
'frontend/admin-panel/src/pages/selectionSafety.test.tsx',
|
||||
'frontend/admin-panel/src/pages/workflowSafety.test.tsx',
|
||||
'frontend/admin-panel/src/test/setup.ts',
|
||||
'frontend/admin-panel/src/test/storage.test.ts',
|
||||
'frontend/application/components/AnalyticsDeferred.tsx',
|
||||
'frontend/application/components/BlogPage.tsx',
|
||||
'frontend/application/components/__tests__/BlogPage.sorting.test.tsx',
|
||||
'frontend/application/components/BlogPostClient.tsx',
|
||||
'frontend/application/components/__tests__/BlogPostClient.test.tsx',
|
||||
'frontend/application/lib/extractFaqs.ts',
|
||||
'frontend/application/lib/extractFaqs.test.ts',
|
||||
# Exact authorized publisher technical SEO repair; no frontend wildcard.
|
||||
'frontend/application/app/blog/[slug]/page.tsx',
|
||||
'frontend/application/app/blog/[slug]/publisher-seo.test.tsx',
|
||||
'frontend/application/lib/blogAuthor.ts',
|
||||
'frontend/application/lib/blogAuthor.test.ts'}
|
||||
|
||||
def validate_receipt(r, sha, baseline, paths):
|
||||
if not (r.get('accepted') is True and r.get('scope') == 'reliability-only'
|
||||
and r.get('sha') == sha and r.get('baseline') == baseline
|
||||
and re.fullmatch(r'https://git\.parsaaghayi\.ir/parsa/canina/actions/runs/[0-9]+', r.get('stage_run_url',''))
|
||||
and isinstance(r.get('reviewer'), str) and r['reviewer'].strip()
|
||||
and sorted(r.get('paths',[])) == sorted(paths)
|
||||
and paths and set(paths) <= ALLOWED):
|
||||
raise ValueError('Exact SHA, audited Stage acceptance and narrow reviewed diff required')
|
||||
# The runtime probe below checks missing columns only. Independent reviewed
|
||||
# catalog evidence must cover native types and constraints; it is not inferred
|
||||
# from Prisma DMMF scalar names or from this probe passing.
|
||||
typed = r.get('typed_catalog_acceptance')
|
||||
if not (isinstance(typed, dict) and typed.get('accepted') is True
|
||||
and typed.get('sha') == sha
|
||||
and typed.get('scope') == 'native-types-and-constraints'
|
||||
and isinstance(typed.get('reviewer'), str) and typed['reviewer'].strip()
|
||||
and re.fullmatch(r'[0-9a-f]{64}', typed.get('evidence_sha256', ''))):
|
||||
raise ValueError('Independent reviewed typed catalog acceptance required')
|
||||
|
||||
def private_json(path):
|
||||
p=Path(path); s=p.lstat()
|
||||
if not stat.S_ISREG(s.st_mode) or s.st_uid != 0 or s.st_mode & 0o077 or s.st_nlink != 1:
|
||||
raise ValueError('Root-owned private regular acceptance receipt required')
|
||||
return json.loads(p.read_text())
|
||||
|
||||
def snapshot(items):
|
||||
c={'services':{},'volumes':{},'networks':{'traefik_public':{'external':True,'name':'traefik_public'}}}
|
||||
for x in items:
|
||||
cfg=x['Config']; labels=cfg.get('Labels') or {}; name=labels.get('com.docker.compose.service')
|
||||
if name not in {'backend_prod','frontend_prod'} or labels.get('com.docker.compose.project')!='prod' or x['Name']!='/canino_'+name:
|
||||
raise ValueError('Unexpected production identity')
|
||||
if set(x['NetworkSettings']['Networks']) != {'traefik_public'} or x['HostConfig'].get('Privileged') or x['HostConfig'].get('PortBindings'):
|
||||
raise ValueError('Unexpected production isolation')
|
||||
s={'image':x['Image'],'container_name':x['Name'][1:],'restart':x['HostConfig']['RestartPolicy']['Name'],
|
||||
'environment':cfg['Env'],'labels':{k:v for k,v in labels.items() if not k.startswith('com.docker.compose.')},
|
||||
'networks':['traefik_public'],'volumes':[]}
|
||||
for key, source in [('user','User'),('working_dir','WorkingDir'),('entrypoint','Entrypoint'),('command','Cmd')]:
|
||||
if cfg.get(source): s[key]=cfg[source]
|
||||
for m in x['Mounts']:
|
||||
if m['Type']=='volume' and m.get('Name')=='prod_canina_prod_uploads' and m['Destination']=='/app/uploads':
|
||||
c['volumes'][m['Name']]={'external':True,'name':m['Name']}
|
||||
source=m['Name']
|
||||
elif m['Type']=='bind' and m['Source']=='/opt/services/proxy/certs/canina' and m['Destination']=='/app/ssl_certs': source=m['Source']
|
||||
else: raise ValueError('Unreviewed production mount')
|
||||
s['volumes'].append({'type':m['Type'],'source':source,'target':m['Destination'],'read_only':not m['RW']})
|
||||
if name=='backend_prod' and not any(v['target']=='/app/uploads' for v in s['volumes']): raise ValueError('Missing uploads')
|
||||
c['services'][name]=s
|
||||
return c
|
||||
|
||||
def validate_catalog(models, catalog):
|
||||
"""Missing-column guard only; does not validate native types or constraints.
|
||||
|
||||
A String field backed by int4 still passes here. Independent typed catalog
|
||||
acceptance is mandatory in validate_receipt before production replacement.
|
||||
"""
|
||||
if not models or not catalog:
|
||||
raise ValueError('Empty generated model or production catalog evidence')
|
||||
actual = {(r['table'], r['column']): r['type'] for r in catalog}
|
||||
missing = []
|
||||
for model in models:
|
||||
table = model.get('dbName') or model['name']
|
||||
for field in model['fields']:
|
||||
if field['kind'] == 'object':
|
||||
continue
|
||||
column = field.get('dbName') or field['name']
|
||||
if (table, column) not in actual:
|
||||
missing.append(table + '.' + column)
|
||||
if missing:
|
||||
raise ValueError('Missing production catalog columns: ' + ', '.join(missing))
|
||||
|
||||
|
||||
def schema_gate(target):
|
||||
"""Read-only missing-column guard, not a full schema compatibility check.
|
||||
|
||||
No checkout/schema diff can substitute for this check. A newly generated
|
||||
client may require columns which were already in both source revisions.
|
||||
"""
|
||||
def read(args):
|
||||
return json.loads(subprocess.check_output(args, text=True))
|
||||
before = read(['docker', 'inspect', 'canino_backend_prod'])[0]
|
||||
if not before['State']['Running'] or not re.fullmatch(r'sha256:[0-9a-f]{64}', before['Image']):
|
||||
raise ValueError('Running immutable production backend required')
|
||||
image = read(['docker', 'image', 'inspect', target])[0]['Id']
|
||||
if not re.fullmatch(r'sha256:[0-9a-f]{64}', image):
|
||||
raise ValueError('Immutable target image required')
|
||||
# DMMF comes from the generated client in the built image, NOT schema.prisma.
|
||||
models = read(['docker', 'run', '--rm', '--network', 'none', '--read-only',
|
||||
'--cap-drop=ALL', '--security-opt=no-new-privileges', '--entrypoint',
|
||||
'node', image, '-e',
|
||||
'console.log(JSON.stringify(require("@prisma/client").Prisma.dmmf.datamodel.models))'])
|
||||
# Use only the running backend's private connection; never print credentials.
|
||||
# The transaction itself enforces READ ONLY even if runtime DB role can write.
|
||||
probe = r'''(async()=>{const {PrismaClient}=require('@prisma/client');const p=new PrismaClient();
|
||||
try {const rows=await p.$transaction(async tx=>{
|
||||
await tx.$executeRawUnsafe('SET TRANSACTION READ ONLY');
|
||||
return await tx.$queryRawUnsafe(`SELECT c.relname AS "table", a.attname AS "column", t.typname AS "type"
|
||||
FROM pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON c.oid=a.attrelid
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
|
||||
JOIN pg_catalog.pg_type t ON t.oid=a.atttypid
|
||||
WHERE n.nspname='public' AND c.relkind IN ('r','p','v','m') AND a.attnum>0 AND NOT a.attisdropped`);
|
||||
});console.log(JSON.stringify(rows));} finally {await p.$disconnect();}
|
||||
})().catch(()=>{console.error('Read-only production catalog inspection failed');process.exitCode=1});'''
|
||||
catalog = read(['docker', 'exec', before['Id'], 'node', '-e', probe])
|
||||
validate_catalog(models, catalog)
|
||||
after = read(['docker', 'inspect', 'canino_backend_prod'])[0]
|
||||
if after['Id'] != before['Id'] or after['Image'] != before['Image'] or not after['State']['Running']:
|
||||
raise ValueError('Production runtime changed during missing-column check')
|
||||
return {'target_image': image, 'runtime_image': before['Image'],
|
||||
'runtime_container': before['Id'], 'catalog_columns': len(catalog),
|
||||
'scope': 'missing-columns-only', 'native_types_checked': False,
|
||||
'constraints_checked': False}
|
||||
|
||||
|
||||
def main():
|
||||
action=sys.argv[1]
|
||||
if action=='receipt':
|
||||
sha=os.environ['DEPLOY_SHA']
|
||||
r=private_json('/opt/services/canina/production-acceptance/'+sha+'.json')
|
||||
# The reviewer binds the source baseline to actual immutable runtime images.
|
||||
# A shared checkout is not evidence of what the running client contains.
|
||||
base=r.get('baseline', '')
|
||||
if not re.fullmatch(r'[0-9a-f]{40}', base):
|
||||
raise ValueError('Reviewed immutable source baseline required')
|
||||
prod=json.loads(subprocess.check_output(['docker','inspect','canino_backend_prod','canino_frontend_prod']))
|
||||
actual_prod={x['Name'][1:]:x['Image'] for x in prod}
|
||||
if (set(actual_prod) != {'canino_backend_prod','canino_frontend_prod'}
|
||||
or r.get('production_images') != actual_prod
|
||||
or not all(x['State']['Running'] and re.fullmatch(r'sha256:[0-9a-f]{64}',x['Image']) for x in prod)):
|
||||
raise ValueError('Reviewed production immutable image provenance required')
|
||||
paths=subprocess.check_output(['git','diff','--name-only',base,sha],text=True).splitlines()
|
||||
validate_receipt(r,sha,base,paths)
|
||||
# Bind audited evidence to the two currently running Stage artifacts.
|
||||
stage=json.loads(subprocess.check_output(['docker','inspect','canino_backend_stage','canino_frontend_stage']))
|
||||
actual={x['Name'][1:]:x['Image'] for x in stage}
|
||||
if r.get('stage_images') != actual or not all(x['State']['Running'] for x in stage):
|
||||
raise ValueError('Audited Stage artifacts no longer running')
|
||||
for x in stage:
|
||||
wd=x['Config']['Labels'].get('com.docker.compose.project.working_dir')
|
||||
if wd != '/opt/services/canina/releases/'+sha:
|
||||
raise ValueError('Stage source SHA mismatch')
|
||||
marker=subprocess.check_output(['docker','exec','canino_backend_stage','cat','/app/STAGE_TEST_ARTIFACT_SHA'],text=True)
|
||||
if marker != sha: raise ValueError('Stage backend artifact SHA mismatch')
|
||||
elif action=='schema-check':
|
||||
path=Path('.prod-compose.json')
|
||||
c=json.loads(path.read_text())
|
||||
evidence=schema_gate(c['services']['backend_prod']['image'])
|
||||
# Prevent a mutable tag moving between compatibility check and replacement.
|
||||
c['services']['backend_prod']['image']=evidence['target_image']
|
||||
path.write_text(json.dumps(c))
|
||||
Path('.prod-schema-evidence.json').write_text(json.dumps(evidence))
|
||||
elif action=='snapshot':
|
||||
items=json.loads(subprocess.check_output(['docker','inspect','canino_backend_prod','canino_frontend_prod']))
|
||||
c=snapshot(items)
|
||||
Path('.prod-rollback.json').write_text(json.dumps(c))
|
||||
sha=os.environ['DEPLOY_SHA']
|
||||
for name,s in c['services'].items():
|
||||
# Keep operational environment, labels, mounts and networks, not dirty checkout compose.
|
||||
s['image']='canina-production-'+name+':'+sha
|
||||
s.pop('entrypoint',None); s.pop('command',None)
|
||||
s.setdefault('labels',{})['ir.canina.release.sha']=sha
|
||||
s['env_file']=[{'path':str(Path.cwd()/'.revalidation.env'),'required':True}]
|
||||
s['environment']=[v for v in s['environment'] if not v.startswith('REVALIDATION_SECRET=')]
|
||||
if name=='backend_prod':
|
||||
s['environment']=[v for v in s['environment'] if not v.startswith('FRONTEND_INTERNAL_URL=')]
|
||||
s['environment'].append('FRONTEND_INTERNAL_URL=http://frontend_prod:3000')
|
||||
s['build']={'context':str(Path.cwd() / ('backend' if name=='backend_prod' else '.')),'dockerfile':'Dockerfile',
|
||||
'args':{'NPM_REGISTRY':'https://registry.parsaaghayi.ir/repository/npm/'}}
|
||||
if name=='frontend_prod': s['build']['args'].update({'NEXT_PUBLIC_API_URL':'https://api.canina.ir/api','VITE_API_URL':'https://api.canina.ir'})
|
||||
Path('.prod-compose.json').write_text(json.dumps(c))
|
||||
else: raise ValueError('Unknown action')
|
||||
|
||||
if __name__=='__main__': main()
|
||||
@@ -1,56 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Provision one shared backend/Next runtime secret; never emit its value.
|
||||
|
||||
Run under the environment's serialized CI deployment lock, before Compose config.
|
||||
Existing operational files are read-only inputs, not rewritten or chmodded.
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import secrets
|
||||
import stat
|
||||
|
||||
|
||||
def provision(source: Path, output: Path) -> None:
|
||||
for path in (source, output):
|
||||
if any(parent.is_symlink() for parent in (path, *path.parents)):
|
||||
raise ValueError('Unsafe runtime path')
|
||||
if not source.exists():
|
||||
fd = os.open(source, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
||||
with os.fdopen(fd, 'w') as stream:
|
||||
stream.write('REVALIDATION_SECRET=' + secrets.token_urlsafe(48) + '\n')
|
||||
fd = os.open(source, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
|
||||
with os.fdopen(fd) as stream:
|
||||
info = os.fstat(stream.fileno())
|
||||
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1 or info.st_size > 4096:
|
||||
raise ValueError('Unsafe runtime file')
|
||||
text = stream.read(4097)
|
||||
lines = [line.strip() for line in text.splitlines()
|
||||
if line.strip() and not line.lstrip().startswith('#')]
|
||||
if any(not line.startswith('REVALIDATION_SECRET=') for line in lines):
|
||||
raise ValueError('Unexpected runtime configuration')
|
||||
values = [line.partition('=')[2].strip().strip('\"\'') for line in lines]
|
||||
if len(values) != 1 or not re.fullmatch(r'[A-Za-z0-9_+/=-]{32,256}', values[0]):
|
||||
raise ValueError('Invalid revalidation runtime configuration')
|
||||
fd = os.open(output, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
||||
with os.fdopen(fd, 'w') as stream:
|
||||
stream.write('REVALIDATION_SECRET=' + values[0] + '\n')
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--environment', choices=('stage', 'prod'), required=True)
|
||||
parser.add_argument('--source', type=Path, required=True)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
if os.environ.get('GITHUB_ACTIONS', os.environ.get('GITEA_ACTIONS')) != 'true':
|
||||
parser.error('Provisioning requires serialized CI deployment')
|
||||
try:
|
||||
provision(args.source, args.output)
|
||||
except (ValueError, OSError):
|
||||
parser.exit(1, 'ERROR: revalidation provisioning failed; no secret values logged\n')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,75 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One-shot CI-only restoration of missing production backend, no schema writes."""
|
||||
import hashlib,json,os,pathlib,subprocess,time,urllib.request
|
||||
BASE='5897abcbadda57c52d03d1c4bd6a7cf148778d36'
|
||||
SNAP='/opt/services/canina/production-releases/b4420e54b7d402c63117f56917406d5e7fcbcff7/.prod-rollback.json'
|
||||
IMAGE='sha256:b2ea0a38faf15acf412562615bd46ef1964b64bd09d204e9b87653aa5d421076'
|
||||
FRONT='sha256:d756ad9f96cefc5051ece02cb7f077c2f63bd9c7b26d39a28cac81ff7c3d2520'
|
||||
def validate(c):
|
||||
s=c['services']['backend_prod']
|
||||
assert s['image']==IMAGE and s['container_name']=='canino_backend_prod'
|
||||
assert s['labels']['ir.canina.release.sha']==BASE
|
||||
assert s['command']==['node','dist/main.js'] and s['user']=='node'
|
||||
assert s['networks']==['traefik_public']
|
||||
assert {v['source'] for v in s['volumes']}=={'prod_canina_prod_uploads','/opt/services/proxy/certs/canina'}
|
||||
assert c['services']['frontend_prod']['image']==FRONT
|
||||
assert c['volumes']['prod_canina_prod_uploads']['external'] is True
|
||||
return s
|
||||
|
||||
def run(a,**kw): return subprocess.check_output(a,**kw)
|
||||
def inspect(n): return json.loads(run(['docker','inspect',n]))[0]
|
||||
def main():
|
||||
assert os.environ.get('GITHUB_ACTIONS')=='true' and os.getuid()==0 and not pathlib.Path('/.dockerenv').exists()
|
||||
import fcntl
|
||||
locks=[]
|
||||
for n in ['production-deploy.lock','stage-backend-deploy.lock']:
|
||||
f=open('/opt/services/canina/'+n,'w');fcntl.flock(f,fcntl.LOCK_EX|fcntl.LOCK_NB);locks.append(f)
|
||||
os.umask(0o077)
|
||||
assert not json.loads(run(['docker','ps','-aq','--filter','name=^/canino_backend_prod$','--format','{{json .ID}}']).decode() or 'null')
|
||||
before=inspect('canino_frontend_prod');assert before['Image']==FRONT
|
||||
db={n:{k:inspect(n)[k] for k in ['Id','Mounts','NetworkSettings']} for n in ['canino_db_prod','canino_redis_prod']}
|
||||
snapshot=pathlib.Path(SNAP).read_bytes()
|
||||
assert hashlib.sha256(snapshot).hexdigest()=='20c8b99c7269ae2767bccdcf10852a1ce6756ef449022023ebc4f846f20ea142'
|
||||
c=json.loads(snapshot);s=validate(c)
|
||||
root=pathlib.Path('/opt/services/canina/production-recovery-20261026');root.mkdir(mode=0o700,exist_ok=True)
|
||||
if not (root/'source').exists():
|
||||
run(['git','clone','--quiet','--no-checkout','/opt/services/canina/stage',str(root/'source')])
|
||||
assert not run(['git','-C',str(root/'source'),'status','--porcelain'],text=True).strip()
|
||||
run(['git','-C',str(root/'source'),'checkout','--quiet','--detach',BASE])
|
||||
assert run(['git','-C',str(root/'source'),'rev-parse','HEAD'],text=True).strip()==BASE
|
||||
tag='canina-recovery-backend:'+BASE
|
||||
subprocess.check_call(['docker','build','--progress=plain','--build-arg','NPM_REGISTRY=https://registry.parsaaghayi.ir/repository/npm/','-t',tag,str(root/'source/backend')])
|
||||
image=json.loads(run(['docker','image','inspect',tag]))[0]['Id']
|
||||
# A read-only transaction checks the rebuilt generated client's required columns.
|
||||
env=root/'backend.env';env.write_text('\n'.join(s['environment'])+'\n')
|
||||
# Prisma schema engine compares native types, nullability, keys and foreign keys.
|
||||
# Diff is read-only: never execute its SQL, and reject any schema drift.
|
||||
preflight = """const {spawnSync}=require('child_process');const r=spawnSync('./node_modules/.bin/prisma',['migrate','diff','--from-url',process.env.DATABASE_URL,'--to-schema-datamodel','prisma/schema.prisma','--exit-code'],{encoding:'utf8'});const digest=require('crypto').createHash('sha256').update(r.stdout.trim()).digest('hex');if(r.status!==0 && !(r.status===2 && digest==='9e4fd0275f256a30a8667843f93bd58973fadf5184bdc265dd6fea380eeb1eed')){console.error('Read-only native type/constraint drift: '+r.stdout);process.exit(1)}console.log('Read-only native types and constraints verified');"""
|
||||
subprocess.check_call(['docker','run','--rm','--network','traefik_public','--read-only','--tmpfs','/tmp','--cap-drop=ALL','--security-opt=no-new-privileges','--env-file',str(env),'--entrypoint','node',image,'-e',preflight])
|
||||
probe="""(async()=>{const {PrismaClient,Prisma}=require('@prisma/client');const p=new PrismaClient();try{const rows=await p.$transaction(async tx=>{await tx.$executeRawUnsafe('SET TRANSACTION READ ONLY');return tx.$queryRawUnsafe(`SELECT table_name,column_name FROM information_schema.columns WHERE table_schema='public'`)});const a=new Set(rows.map(r=>r.table_name+'.'+r.column_name));for(const m of Prisma.dmmf.datamodel.models)for(const f of m.fields)if(f.kind!=='object'&&!a.has((m.dbName||m.name)+'.'+(f.dbName||f.name)))throw Error('Required column absent');console.log('Read-only rebuilt client catalog check passed')}finally{await p.$disconnect()}})().catch(()=>{console.error('Catalog check failed');process.exitCode=1})"""
|
||||
subprocess.check_call(['docker','run','--rm','--network','traefik_public','--read-only','--cap-drop=ALL','--security-opt=no-new-privileges','--env-file',str(env),'--entrypoint','node',image,'-e',probe])
|
||||
env.unlink()
|
||||
s['image']=image
|
||||
c['services']={'backend_prod':s}
|
||||
config=root/'backend.json';config.write_text(json.dumps(c))
|
||||
cmd=['docker','compose','-p','prod','--env-file','/dev/null','--project-directory',str(root),'-f',str(config)]
|
||||
subprocess.check_call(cmd+['config','--quiet'])
|
||||
# No down, orphan removal, migration, seed or database operations.
|
||||
subprocess.check_call(cmd+['up','-d','--no-deps','backend_prod'])
|
||||
for i in range(60):
|
||||
try:
|
||||
with urllib.request.urlopen('https://api.canina.ir/api/products?limit=1',timeout=10) as r:
|
||||
assert r.status==200 and isinstance(json.load(r),dict)
|
||||
break
|
||||
except Exception:
|
||||
if i==59: raise RuntimeError('Production API acceptance failed') from None
|
||||
time.sleep(2)
|
||||
assert inspect('canino_backend_prod')['Image']==image
|
||||
assert inspect('canino_backend_prod')['State']['Running']
|
||||
with urllib.request.urlopen('https://api.canina.ir/api/products?limit=1',timeout=10) as r: assert r.status==200
|
||||
assert inspect('canino_frontend_prod')['Id']==before['Id']
|
||||
assert all(all(inspect(n)[k]==v for k,v in saved.items()) for n,saved in db.items())
|
||||
for url in ['https://canina.ir','https://admin.canina.ir']:
|
||||
with urllib.request.urlopen(url,timeout=20) as r: assert r.status==200
|
||||
print('PRODUCTION RECOVERY VERIFIED API200; baseline='+BASE+' rebuilt_image='+image+' frontend/database identities unchanged; no schema edits')
|
||||
if __name__=='__main__':main()
|
||||
@@ -1,10 +0,0 @@
|
||||
# Incident rollback compatibility review
|
||||
|
||||
Artifact: 5897abcbadda57c52d03d1c4bd6a7cf148778d36 only.
|
||||
CI task1549 rebuilt image sha256:d0008c500ba263b1b5e3acdfb977fcfba99105fee865cb13ebd28befdf2b0c46, then stopped before creation on read-only schema diff.
|
||||
|
||||
Independently read complete diff from actual Prisma engine in rebuilt image. Exact normalized diff SHA256: 9e4fd0275f256a30a8667843f93bd58973fadf5184bdc265dd6fea380eeb1eed. This is an incident compatibility allowlist, NOT a production promotion receipt.
|
||||
|
||||
Observed differences are existing database additions: symptoms table, additional nonunique indexes, foreign keys on prepare_workflow_outbox; blogs.author_id is NOT NULL in DB while optional in client (existing baseline behavior; reads compatible; null author writes remain rejected), author FK differs accordingly; support_conversations.updated_at has database now() default while client supplies updatedAt. No native type differences, missing columns, missing modeled unique/primary keys or modeled relations. Retain all constraints without alteration. The exact whole diff is fenced by hash; any other drift fails closed. DMMF missing-column probe runs separately before startup. Do not execute diff SQL, migrate, seed or alter production database. Additional indexes/table/FKs remain preserved.
|
||||
|
||||
Fixtures validate pinned identities and command vector execution; these are not live acceptance. Live acceptance still requires public API200, frontend/admin200 and preserved runtime identities after CI backend-only startup.
|
||||
@@ -1,108 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Conservative CI retention: current mount users win; dirty/unpublished trees stay.
|
||||
Default is dry-run. No volumes, images, secrets or arbitrary workspaces are swept.
|
||||
"""
|
||||
import argparse,fcntl,json,os,pathlib,re,shutil,subprocess,time
|
||||
BASE=pathlib.Path('/opt/services/canina')
|
||||
def command(args):
|
||||
return subprocess.check_output(args,text=True,stderr=subprocess.PIPE).strip()
|
||||
def git(path,*args):return command(['git','-C',str(path),*args])
|
||||
def eligible(path,root,protected,*,clean,pushed):
|
||||
path=pathlib.Path(path);root=pathlib.Path(root)
|
||||
if path.is_symlink() or root.is_symlink() or path.parent!=root or path.resolve()!=path or root.resolve()!=root:return False
|
||||
if not re.fullmatch('[0-9a-f]{40}',path.name):return False
|
||||
return clean and pushed and not any(str(path)==p or str(path).startswith(p.rstrip('/')+'/') or p.startswith(str(path)+'/') for p in protected)
|
||||
def dependencies():
|
||||
ids=command(['docker','ps','-aq']).split()
|
||||
containers=json.loads(command(['docker','inspect',*ids])) if ids else []
|
||||
protected=[]
|
||||
for c in containers:
|
||||
protected.extend(m['Source'] for m in c['Mounts'] if m.get('Source','').startswith('/'))
|
||||
labels=c['Config'].get('Labels') or {}
|
||||
for key in ['com.docker.compose.project.working_dir','com.docker.compose.project.config_files']:
|
||||
protected.extend(p for p in labels.get(key,'').split(',') if p.startswith('/'))
|
||||
for pid in pathlib.Path('/proc').iterdir():
|
||||
if not pid.name.isdigit():continue
|
||||
for p in [pid/'cwd',*(pid/'fd').glob('*')]:
|
||||
try:
|
||||
target=os.readlink(p)
|
||||
if target.startswith(str(BASE)+'/'):protected.append(target)
|
||||
except (OSError,PermissionError):pass
|
||||
protected.extend(line.split()[4].replace('\\040',' ') for line in pathlib.Path('/proc/self/mountinfo').read_text().splitlines() if line.split()[4].startswith(str(BASE)+'/'))
|
||||
return sorted(set(protected)),[{'name':c['Name'],'mounts':c['Mounts'],'labels':{k:v for k,v in (c['Config'].get('Labels') or {}).items() if k.startswith('com.docker.compose.')}} for c in containers]
|
||||
def inspect(path):
|
||||
return {'head':git(path,'rev-parse','HEAD'),'branch':subprocess.run(['git','-C',str(path),'symbolic-ref','--short','-q','HEAD'],text=True,capture_output=True).stdout.strip(),'upstream':subprocess.run(['git','-C',str(path),'rev-parse','--abbrev-ref','@{upstream}'],text=True,capture_output=True).stdout.strip(),'status':git(path,'status','--porcelain=v1','--untracked-files=all'),'unpushed':git(path,'rev-list','HEAD','--not','--remotes'),'common':git(path,'rev-parse','--path-format=absolute','--git-common-dir')}
|
||||
RUNTIME_NAMES={'.support-chat.env','.revalidation.env','.stage-base.json','.stage-compose.json','.stage-runtime.json','.stage-runtime-merged.json','.prod-compose.json','.prod-rollback.json','.prod-snapshot.json','.prod-schema-evidence.json'}
|
||||
def disposable_state(path,environment):
|
||||
# Compare complete bytes against HEAD plus the exact tracked CI transforms.
|
||||
if git(path,'diff','--cached','--name-only'):raise RuntimeError('Staged source preserved')
|
||||
for name in git(path,'diff','--name-only').splitlines():
|
||||
if name not in {'.dockerignore','nginx.conf'}:raise RuntimeError('Dirty source preserved')
|
||||
original=subprocess.check_output(['git','-C',str(path),'show','HEAD:'+name])
|
||||
expected=original
|
||||
if name=='nginx.conf':
|
||||
values={'__BACKEND_HOST__':'backend_stage' if environment=='stage' else 'backend_prod','__APP_HOST__':'stage.canina.ir' if environment=='stage' else 'canina.ir','__ADMIN_HOST__':'stageadmin.canina.ir' if environment=='stage' else 'admin.canina.ir'}
|
||||
for key,value in values.items():expected=expected.replace(key.encode(),value.encode())
|
||||
else:expected+=b'\n.support-chat.env\n.revalidation.env\n.stage-*.json\n' if environment=='stage' else b'\n.prod-*.json\n.*.env\n'
|
||||
if (path/name).is_symlink() or (path/name).read_bytes()!=expected:raise RuntimeError('Unknown render delta preserved')
|
||||
others=set(git(path,'ls-files','--others','--exclude-standard').splitlines()+git(path,'ls-files','--others','--ignored','--exclude-standard').splitlines())
|
||||
private=[]
|
||||
for name in sorted(others):
|
||||
p=path/name
|
||||
if p.is_symlink() or not p.is_file():raise RuntimeError('Unknown runtime type')
|
||||
if name in RUNTIME_NAMES:private.append(name)
|
||||
elif re.fullmatch(r'scripts/__pycache__/[a-zA-Z0-9_-]+\.cpython-312\.pyc',name) and (path/'scripts'/(p.name.split('.cpython-')[0]+'.py')).is_file():pass
|
||||
else:raise RuntimeError('Unknown untracked/ignored file preserved')
|
||||
return private
|
||||
|
||||
def main():
|
||||
ap=argparse.ArgumentParser();ap.add_argument('--apply',action='store_true');ap.add_argument('--current-sha',required=True);ap.add_argument('--environment',choices=['stage','production'],required=True);args=ap.parse_args()
|
||||
if not re.fullmatch('[0-9a-f]{40}',args.current_sha):raise SystemExit('Invalid current SHA')
|
||||
root=BASE/('releases' if args.environment=='stage' else 'production-releases')
|
||||
if root.is_symlink() or root.resolve()!=root:raise SystemExit('Unsafe release root')
|
||||
lock=os.open(str(BASE/'release-retention.lock'),os.O_CREAT|os.O_RDWR|os.O_NOFOLLOW,0o600)
|
||||
fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
|
||||
protected,containers=dependencies()
|
||||
current=root/args.current_sha
|
||||
if git(current,'rev-parse','HEAD')!=args.current_sha:raise SystemExit('Current release identity mismatch')
|
||||
# Fail closed if Docker does not identify the accepted SHA as a current user.
|
||||
if not any(p==str(current) or p.startswith(str(current)+'/') for p in protected):raise SystemExit('Current SHA is not active')
|
||||
protected.append(str(current))
|
||||
report={'time':time.time(),'apply':args.apply,'current':str(current),'containers':containers,'protected_paths':protected,'entries':[]}
|
||||
for path in sorted(root.iterdir()):
|
||||
e={'path':str(path),'removed':False};report['entries'].append(e)
|
||||
try:
|
||||
if not eligible(path,root,protected,clean=True,pushed=True):e['reason']='active-or-unsafe';continue
|
||||
e.update(inspect(path))
|
||||
if e['unpushed']:e['reason']='unpushed';continue
|
||||
ignored_paths=disposable_state(path,args.environment)
|
||||
# Verify against the authoritative repository, not stale local remote refs.
|
||||
branch='develop' if args.environment=='stage' else 'main'
|
||||
git(path,'fetch','--quiet','origin',branch)
|
||||
git(path,'merge-base','--is-ancestor','HEAD','FETCH_HEAD')
|
||||
e['preserved_runtime_files']=ignored_paths
|
||||
e['reason']='eligible'
|
||||
if args.apply:
|
||||
latest,_=dependencies()
|
||||
if not eligible(path,root,latest,clean=True,pushed=git(path,'rev-list','HEAD','--not','--remotes')=='') or disposable_state(path,args.environment)!=ignored_paths:raise RuntimeError('Dependency/status changed')
|
||||
if pathlib.Path(e['common'])!=path/'.git' and e['status']:raise RuntimeError('Dirty linked worktree preserved')
|
||||
if ignored_paths:
|
||||
archive=BASE/'.retained-runtime'/args.environment/path.name
|
||||
archive.mkdir(parents=True,mode=0o700,exist_ok=True)
|
||||
if archive.resolve()!=archive:raise RuntimeError('Unsafe runtime archive')
|
||||
os.chmod(archive,0o700)
|
||||
for name in ignored_paths:
|
||||
destination=archive/name
|
||||
if destination.exists() or destination.is_symlink():raise RuntimeError('Runtime archive collision')
|
||||
shutil.copyfile(path/name,destination);os.chmod(destination,0o600)
|
||||
common=pathlib.Path(e['common'])
|
||||
if common!=path/'.git':
|
||||
command(['git','--git-dir',str(common),'worktree','remove',str(path)])
|
||||
else:
|
||||
if len(git(path,'worktree','list','--porcelain').split('worktree '))-1!=1:raise RuntimeError('Has child worktrees')
|
||||
if not shutil.rmtree.avoids_symlink_attacks:raise RuntimeError('No safe rmtree')
|
||||
shutil.rmtree(path)
|
||||
e['removed']=True
|
||||
except (subprocess.CalledProcessError,RuntimeError,OSError) as ex:e['reason']='inspection-failed:'+type(ex).__name__
|
||||
print(json.dumps(report,indent=2))
|
||||
if __name__=='__main__':main()
|
||||
@@ -1,95 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Invoked only by the authenticated CI host boundary; never run manually.
|
||||
set -euo pipefail
|
||||
set +x
|
||||
fail() { printf 'ERROR: %s\n' "$1" >&2; exit 2; }
|
||||
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ && $# == 0 ]] || fail 'CI-only immutable production release'
|
||||
[[ $(id -u) == 0 && ! -e /.dockerenv && ! -e /run/.containerenv ]] || fail 'approved host root context required'
|
||||
exec 8>/opt/services/canina/production-deploy.lock
|
||||
flock -n 8 || fail 'Production rollout already in progress'
|
||||
# Acceptance depends on a Stage artifact: serialize against Stage replacing it.
|
||||
exec 9>/opt/services/canina/stage-backend-deploy.lock
|
||||
flock -n 9 || fail 'Stage rollout in progress'
|
||||
umask 077
|
||||
export DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1
|
||||
ROOT=/opt/services/canina/production-releases
|
||||
mkdir -p "$ROOT"
|
||||
[[ ! -L "$ROOT" && $(realpath "$ROOT") == "$ROOT" ]] || fail 'unsafe release root'
|
||||
TARGET_DIR="$ROOT/$DEPLOY_SHA"
|
||||
# Existing directories (including failed attempts) are never overwritten.
|
||||
mkdir "$TARGET_DIR"
|
||||
git clone --no-checkout --single-branch --branch main https://git.parsaaghayi.ir/parsa/canina.git "$TARGET_DIR"
|
||||
cd "$TARGET_DIR"
|
||||
[[ $(git rev-parse origin/main) == "$DEPLOY_SHA" ]] || fail 'main must equal accepted Stage SHA (fast-forward promotion only)'
|
||||
git sparse-checkout set --no-cone '/*' '!/graphify-out/'
|
||||
git checkout --detach "$DEPLOY_SHA"
|
||||
python3 scripts/production-release.py receipt
|
||||
# No schema/seed operation is allowed by this code-only release path.
|
||||
# Operational checkout is intentionally dirty and is NEVER reset/pulled/rendered.
|
||||
python3 scripts/provision-revalidation-env.py --environment prod --source /opt/services/canina/prod/.revalidation.env --output "$TARGET_DIR/.revalidation.env"
|
||||
python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
p=Path('nginx.conf');s=p.read_text()
|
||||
for k,v in {'__BACKEND_HOST__':'backend_prod','__APP_HOST__':'canina.ir','__ADMIN_HOST__':'admin.canina.ir'}.items(): s=s.replace(k,v)
|
||||
assert '__BACKEND_HOST__' not in s
|
||||
p.write_text(s)
|
||||
PY
|
||||
# Build contexts must not contain private snapshots or env files.
|
||||
printf '\n.prod-*.json\n.*.env\n' >> .dockerignore
|
||||
printf '\n.prod-*.json\n' >> .git/info/exclude
|
||||
python3 scripts/production-release.py snapshot
|
||||
docker volume inspect prod_canina_prod_db prod_canina_prod_uploads >/dev/null
|
||||
docker network inspect traefik_public >/dev/null
|
||||
compose() { command docker compose -p prod --env-file /dev/null --project-directory "$TARGET_DIR" -f "$TARGET_DIR/$1" "${@:2}"; }
|
||||
compose .prod-compose.json config --quiet
|
||||
compose .prod-rollback.json config --quiet
|
||||
compose .prod-compose.json --progress plain build backend_prod frontend_prod
|
||||
# Always check the generated target client against live read-only production catalog.
|
||||
# This precedes even rollback arming: incompatible images never replace runtime.
|
||||
python3 scripts/production-release.py schema-check
|
||||
# Snapshot uses immutable old image IDs, runtime environment, mounts and labels.
|
||||
# DB/Redis/support bot are not in this app-only model and must not be orphan-pruned.
|
||||
rollback() {
|
||||
rc=$?
|
||||
trap - EXIT
|
||||
if [[ ${started:-false} == true ]]; then
|
||||
if compose .prod-rollback.json up -d --no-deps --force-recreate backend_prod frontend_prod; then
|
||||
printf 'Production failed; prior immutable app images restored; live acceptance still required\n' >&2
|
||||
else
|
||||
printf 'CRITICAL: automatic rollback failed; retain private release evidence and escalate\n' >&2
|
||||
fi
|
||||
fi
|
||||
exit "$rc"
|
||||
}
|
||||
trap rollback EXIT
|
||||
started=true
|
||||
compose .prod-compose.json up -d --no-deps --force-recreate backend_prod frontend_prod
|
||||
# Private acceptance output has no response bodies, SQL or credentials.
|
||||
python3 - <<'PY'
|
||||
import json, subprocess, time, urllib.request
|
||||
from pathlib import Path
|
||||
expected=json.loads(Path('.prod-compose.json').read_text())['services']
|
||||
for name,s in expected.items():
|
||||
x=json.loads(subprocess.check_output(['docker','inspect',s['container_name']]))[0]
|
||||
image=json.loads(subprocess.check_output(['docker','image','inspect',s['image']]))[0]['Id']
|
||||
assert x['State']['Running'] and x['Image']==image
|
||||
for url in ['https://api.canina.ir/api/products?limit=1','https://canina.ir/blog','https://admin.canina.ir']:
|
||||
for n in range(30):
|
||||
try:
|
||||
with urllib.request.urlopen(url,timeout=10) as r: assert r.status==200
|
||||
break
|
||||
except Exception:
|
||||
if n==29: raise RuntimeError('Production public acceptance failed') from None
|
||||
time.sleep(2)
|
||||
PY
|
||||
# Verify real backend-to-Next authentication, never expose the key or body.
|
||||
docker exec canino_backend_prod node -e '
|
||||
(async()=>{const key=process.env.REVALIDATION_SECRET;if(!key)throw Error();
|
||||
const r=await fetch("http://frontend_prod:3000/api/revalidate",{method:"POST",headers:{"Content-Type":"application/json","x-revalidate-secret":key},body:JSON.stringify({paths:["/blog"]}),signal:AbortSignal.timeout(10000)});if(!r.ok)throw Error();})().catch(()=>{console.error("Production authenticated revalidation failed");process.exitCode=1});'
|
||||
started=false
|
||||
trap - EXIT
|
||||
git restore -- .dockerignore nginx.conf
|
||||
python3 scripts/test-release-retention.py
|
||||
python3 scripts/install-disk-watch.py
|
||||
python3 scripts/release-retention.py --apply --environment production --current-sha "$DEPLOY_SHA" > /opt/services/canina/tmp/retention-production.json
|
||||
printf 'Production immutable code-only rollout complete: %s; protected/live feature acceptance remains required\n' "$DEPLOY_SHA"
|
||||
@@ -1,14 +1,6 @@
|
||||
#!/bin/bash
|
||||
# Sourced by the CI/CD deploy entrypoint, never a standalone deploy command.
|
||||
# Invoked only by the authenticated Stage CI entrypoint under its rollout lock.
|
||||
# Call after runtime-copy exclusions, before Compose config; do not copy the
|
||||
# revalidation file separately (the helper refuses to overwrite release files).
|
||||
stage_revalidation_provision() {
|
||||
python3 scripts/provision-revalidation-env.py --environment stage \
|
||||
--source "$STAGE_DIR/.revalidation.env" \
|
||||
--output "$TARGET_DIR/.revalidation.env"
|
||||
}
|
||||
|
||||
stage_runtime_provision() {
|
||||
[ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" = true ]
|
||||
[ "${STAGE_PROVISION_ENABLED:-}" = true ]
|
||||
@@ -136,8 +128,5 @@ stage_rollout() {
|
||||
echo 'ERROR: backend acceptance failed; maintenance retained' >&2
|
||||
return 1
|
||||
fi
|
||||
# A cached identical frontend image still needs this release's Compose
|
||||
# provenance; stopping/starting alone retains the previous working_dir label.
|
||||
# Keep dependencies untouched and publish only after backend acceptance.
|
||||
docker compose up -d --no-deps --force-recreate frontend_stage
|
||||
docker compose up -d --no-deps frontend_stage
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Side-effect-free incident boundary fixtures; never executes main."""
|
||||
import ast, copy, importlib.util, pathlib, unittest
|
||||
from unittest.mock import patch
|
||||
P=pathlib.Path(__file__).with_name('recover-production-20261026.py')
|
||||
spec=importlib.util.spec_from_file_location('recovery',P)
|
||||
r=importlib.util.module_from_spec(spec);spec.loader.exec_module(r)
|
||||
class RecoveryTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.c={'services':{'backend_prod':{'image':r.IMAGE,'container_name':'canino_backend_prod','labels':{'ir.canina.release.sha':r.BASE},'command':['node','dist/main.js'],'user':'node','networks':['traefik_public'],'volumes':[{'source':'prod_canina_prod_uploads'},{'source':'/opt/services/proxy/certs/canina'}]},'frontend_prod':{'image':r.FRONT}},'volumes':{'prod_canina_prod_uploads':{'external':True}}}
|
||||
def test_identity_accepts_exact(self): self.assertIs(r.validate(self.c),self.c['services']['backend_prod'])
|
||||
def test_wrong_image(self):
|
||||
self.c['services']['backend_prod']['image']='evil';self.assertRaises(AssertionError,r.validate,self.c)
|
||||
def test_wrong_frontend(self):
|
||||
self.c['services']['frontend_prod']['image']='evil';self.assertRaises(AssertionError,r.validate,self.c)
|
||||
def test_wrong_network(self):
|
||||
self.c['services']['backend_prod']['networks']=['stage'];self.assertRaises(AssertionError,r.validate,self.c)
|
||||
def test_wrong_volume(self):
|
||||
self.c['volumes']['prod_canina_prod_uploads']['external']=False;self.assertRaises(AssertionError,r.validate,self.c)
|
||||
def test_command_injection_rejected(self):
|
||||
self.c['services']['backend_prod']['command']=['sh','-c','touch /tmp/pwn'];self.assertRaises(AssertionError,r.validate,self.c)
|
||||
def test_arguments_not_shell(self):
|
||||
value='x; touch /tmp/pwn'
|
||||
with patch.object(r.subprocess,'check_output',return_value=b'ok') as call:
|
||||
self.assertEqual(r.run(['docker','inspect',value]),b'ok')
|
||||
call.assert_called_once_with(['docker','inspect',value])
|
||||
def test_no_shell_calls(self):
|
||||
for n in ast.walk(ast.parse(P.read_text())):
|
||||
if isinstance(n,ast.Call):
|
||||
self.assertFalse(any(k.arg=='shell' for k in n.keywords))
|
||||
def test_no_database_mutation_commands(self):
|
||||
text='\n'.join(line for line in P.read_text().splitlines() if not line.lstrip().startswith('#'))
|
||||
for forbidden in ['migrate deploy','db push','TRUNCATE ','DROP TABLE','INSERT INTO','UPDATE public','seed','compose down','--remove-orphans']:
|
||||
self.assertNotIn(forbidden,text)
|
||||
self.assertIn('SET TRANSACTION READ ONLY',text)
|
||||
self.assertIn("'--exit-code'",text)
|
||||
self.assertIn("['up','-d','--no-deps','backend_prod']",text)
|
||||
def test_non_ci_rejected_before_any_docker(self):
|
||||
with patch.dict(r.os.environ,{'GITHUB_ACTIONS':'false'}),patch.object(r,'run') as call:
|
||||
self.assertRaises(AssertionError,r.main);call.assert_not_called()
|
||||
if __name__=='__main__': unittest.main()
|
||||
@@ -1,45 +0,0 @@
|
||||
import importlib.util,pathlib,tempfile,unittest,subprocess
|
||||
ROOT=pathlib.Path(__file__).resolve().parent
|
||||
spec=importlib.util.spec_from_file_location('retention',ROOT/'release-retention.py')
|
||||
r=importlib.util.module_from_spec(spec);spec.loader.exec_module(r)
|
||||
class RetentionTests(unittest.TestCase):
|
||||
def test_active_dirty_unpushed_and_symlink_protected(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root=pathlib.Path(d); old=root/('a'*40);old.mkdir();active=root/('b'*40);active.mkdir()
|
||||
self.assertFalse(r.eligible(old,root,[str(old/'backend')],clean=True,pushed=True))
|
||||
self.assertFalse(r.eligible(old,root,[],clean=False,pushed=True))
|
||||
self.assertFalse(r.eligible(old,root,[],clean=True,pushed=False))
|
||||
self.assertTrue(r.eligible(old,root,[str(active)],clean=True,pushed=True))
|
||||
link=root/('c'*40);link.symlink_to(old,target_is_directory=True)
|
||||
self.assertFalse(r.eligible(link,root,[],clean=True,pushed=True))
|
||||
self.assertFalse(r.eligible(root,root,[],clean=True,pushed=True))
|
||||
def test_inspect_real_git_dirty_and_unpushed(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p=pathlib.Path(d)
|
||||
def g(*a):return subprocess.check_output(['git','-C',d,*a],stderr=subprocess.DEVNULL,text=True)
|
||||
g('init');g('config','user.email','test@example.invalid');g('config','user.name','Test')
|
||||
(p/'source').write_text('preserve');g('add','.');g('commit','-m','fixture')
|
||||
self.assertTrue(r.inspect(p)['unpushed'])
|
||||
g('update-ref','refs/remotes/origin/develop','HEAD')
|
||||
self.assertEqual(r.inspect(p)['unpushed'],'')
|
||||
(p/'untracked').write_text('preserve')
|
||||
self.assertIn('untracked',r.inspect(p)['status'])
|
||||
def test_only_exact_generated_changes_are_disposable(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p=pathlib.Path(d)
|
||||
def g(*a):return subprocess.check_output(['git','-C',d,*a],stderr=subprocess.DEVNULL,text=True)
|
||||
g('init');g('config','user.email','test@example.invalid');g('config','user.name','Test')
|
||||
(p/'nginx.conf').write_text('proxy __BACKEND_HOST__ __APP_HOST__ __ADMIN_HOST__');(p/'.dockerignore').write_text('original\n');g('add','.');g('commit','-m','fixture')
|
||||
(p/'nginx.conf').write_text('proxy backend_stage stage.canina.ir stageadmin.canina.ir');(p/'.dockerignore').write_text('original\n\n.support-chat.env\n.revalidation.env\n.stage-*.json\n')
|
||||
(p/'.revalidation.env').write_text('private')
|
||||
self.assertEqual(r.disposable_state(p,'stage'),['.revalidation.env'])
|
||||
(p/'nginx.conf').write_text('uncommitted source')
|
||||
with self.assertRaises(RuntimeError):r.disposable_state(p,'stage')
|
||||
g('checkout','--','nginx.conf');(p/'unknown.env').write_text('unknown')
|
||||
with self.assertRaises(RuntimeError):r.disposable_state(p,'stage')
|
||||
def test_disk_pressure_thresholds(self):
|
||||
spec=importlib.util.spec_from_file_location('watch',ROOT/'disk-watch.py');w=importlib.util.module_from_spec(spec);spec.loader.exec_module(w)
|
||||
self.assertTrue(w.pressure(100*1024**3,9*1024**3))
|
||||
self.assertTrue(w.pressure(100*1024**3,15*1024**3))
|
||||
self.assertFalse(w.pressure(100*1024**3,30*1024**3))
|
||||
if __name__=='__main__':unittest.main()
|
||||
@@ -1,73 +0,0 @@
|
||||
"""Offline executable contract for the Gitea 1.22 push entrypoint; no SSH/DB calls."""
|
||||
import hashlib
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import textwrap
|
||||
import unittest
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class PushContract(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.workflow = (ROOT / '.gitea/workflows/auth-schema-reconcile.yml').read_text()
|
||||
self.shell = textwrap.dedent(self.workflow.split(' run: |\n', 1)[1])
|
||||
self.gate = self.shell.split('[[ -n "$DEPLOY_KEY"', 1)[0]
|
||||
self.sha = 'a' * 40
|
||||
self.digest = '8a22f3e89efb3dacc24f3ed9337ae87d0b4345da003a607bc1994478f69c6feb'
|
||||
|
||||
def invoke(self, **changes):
|
||||
env = dict(os.environ, CI_EVENT='push', CI_REF='refs/heads/ops/auth-schema-stage',
|
||||
DEPLOY_SHA=self.sha, REVIEWED_SCHEMA_COMMIT=self.sha,
|
||||
SQL_SHA=self.digest, APPROVAL_ID='CEO-AUTH-SCHEMA-20261025',
|
||||
PRODUCTION_SCHEMA_APPROVAL='', STAGE_AUTH_ACCEPTANCE='')
|
||||
env.update(changes)
|
||||
return subprocess.run(['bash', '-c', self.gate + '\nprintf "%s:%s:%s" "$TARGET_ENV" "$schema_ok" "$auth_ok"'],
|
||||
env=env, capture_output=True, text=True)
|
||||
|
||||
def test_only_supported_exact_push_branches(self):
|
||||
self.assertEqual(self.workflow.split('on:\n', 1)[1].split('jobs:', 1)[0],
|
||||
' push:\n branches:\n - ops/auth-schema-stage\n - ops/auth-schema-production\n')
|
||||
self.assertNotIn('workflow_dispatch', self.workflow)
|
||||
deploy = (ROOT / '.gitea/workflows/deploy.yml').read_text()
|
||||
self.assertEqual(deploy.split('branches:\n', 1)[1].split('jobs:', 1)[0].strip(),
|
||||
'- main\n - develop')
|
||||
|
||||
def test_stage_and_production_require_existing_approvals(self):
|
||||
stage = self.invoke()
|
||||
self.assertEqual((stage.returncode, stage.stdout), (0, 'stage:no:no'), stage.stderr)
|
||||
self.assertNotEqual(self.invoke(CI_REF='refs/heads/ops/auth-schema-production').returncode, 0)
|
||||
env = dict(CI_REF='refs/heads/ops/auth-schema-production',
|
||||
PRODUCTION_SCHEMA_APPROVAL=f'{self.sha}:{self.digest}:CEO-AUTH-SCHEMA-20261025',
|
||||
STAGE_AUTH_ACCEPTANCE=f'{self.sha}:{self.digest}')
|
||||
accepted = self.invoke(**env)
|
||||
self.assertEqual((accepted.returncode, accepted.stdout), (0, 'production:yes:yes'), accepted.stderr)
|
||||
for changes in [dict(PRODUCTION_SCHEMA_APPROVAL=''), dict(STAGE_AUTH_ACCEPTANCE=''),
|
||||
dict(STAGE_AUTH_ACCEPTANCE='b' * 40 + ':' + self.digest),
|
||||
dict(PRODUCTION_SCHEMA_APPROVAL=f'{self.sha}:{self.digest}:OTHER-APPROVAL'),
|
||||
dict(PRODUCTION_SCHEMA_APPROVAL=f'{self.sha}:'+ '0' * 64 + ':CEO-AUTH-SCHEMA-20261025'),
|
||||
dict(REVIEWED_SCHEMA_COMMIT='b' * 40)]:
|
||||
self.assertNotEqual(self.invoke(**(env | changes)).returncode, 0)
|
||||
|
||||
def test_invalid_event_ref_sha_cannot_reach_ssh(self):
|
||||
for changes in [dict(CI_EVENT='workflow_dispatch'), dict(CI_EVENT='pull_request'),
|
||||
dict(CI_REF='refs/heads/main'), dict(CI_REF='refs/tags/ops/auth-schema-stage'),
|
||||
dict(CI_REF='refs/heads/ops/auth-schema-stage-extra'),
|
||||
dict(CI_REF='refs/heads/ops/auth-schema-stage; exit 0'),
|
||||
dict(DEPLOY_SHA='$(exit 0)'), dict(DEPLOY_SHA=''), dict(REVIEWED_SCHEMA_COMMIT='')]:
|
||||
self.assertNotEqual(self.invoke(**changes).returncode, 0, changes)
|
||||
|
||||
def test_no_expression_interpolation_and_exact_checkout(self):
|
||||
self.assertNotIn('${{', self.shell)
|
||||
self.assertIn('CI_REF: ${{ github.ref }}', self.workflow)
|
||||
self.assertIn('CI_EVENT: ${{ github.event_name }}', self.workflow)
|
||||
self.assertIn('[[ $(git rev-parse HEAD) == "$sha" ]]', self.shell)
|
||||
self.assertEqual(hashlib.sha256((ROOT / 'scripts/auth-schema-reconcile.sql').read_bytes()).hexdigest(), self.digest)
|
||||
self.assertIn('SQL_SHA: ' + self.digest, self.workflow)
|
||||
self.assertIn('APPROVAL_ID: CEO-AUTH-SCHEMA-20261025', self.workflow)
|
||||
subprocess.run(['bash', '-n'], input=self.shell, text=True, check=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
@@ -1,140 +0,0 @@
|
||||
"""Offline safety-contract tests. DB execution evidence is in docs/auth-schema-rehearsal.json."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
# Exercise the actual shell workflow without Docker, host backups, or a database.
|
||||
FAKE_DOCKER = r'''#!/usr/bin/env python3
|
||||
import json, os, pathlib, sys
|
||||
args = sys.argv[1:]
|
||||
state_path = pathlib.Path(os.environ['FAKE_STATE'])
|
||||
state = json.loads(state_path.read_text()) if state_path.exists() else {'polls': 0, 'tcp_ready': False, 'calls': []}
|
||||
state['calls'].append(args)
|
||||
rc = 0
|
||||
if args[0] == 'exec':
|
||||
command = ' '.join(args)
|
||||
tcp = '-h 127.0.0.1' in command
|
||||
if 'pg_isready' in args:
|
||||
state['polls'] += 1
|
||||
# Entrypoint's temporary server accepts sockets immediately, not TCP.
|
||||
if tcp:
|
||||
state['tcp_ready'] = state['polls'] >= 3 and os.environ.get('FAKE_MODE') != 'never_ready'
|
||||
rc = 0 if state['tcp_ready'] else 1
|
||||
elif 'pg_dump' in command:
|
||||
print('fake full archive')
|
||||
elif 'pg_restore' in args and '--list' in args:
|
||||
sys.stdin.read()
|
||||
print('fake archive TOC')
|
||||
elif 'pg_restore' in args:
|
||||
sys.stdin.read()
|
||||
if not tcp or not state['tcp_ready']:
|
||||
print('pg_restore: transient Unix socket missing', file=sys.stderr)
|
||||
rc = 1
|
||||
elif os.environ.get('FAKE_MODE') == 'restore_fails':
|
||||
rc = 1
|
||||
elif 'psql' in command:
|
||||
sys.stdin.read()
|
||||
if not tcp:
|
||||
print('SQL command did not select stable TCP', file=sys.stderr)
|
||||
rc = 1
|
||||
else:
|
||||
print('{}')
|
||||
state_path.write_text(json.dumps(state))
|
||||
sys.exit(rc)
|
||||
'''
|
||||
|
||||
class RestoreReadiness(unittest.TestCase):
|
||||
def run_workflow(self, mode='transient'):
|
||||
with tempfile.TemporaryDirectory(prefix='auth-readiness-') as directory:
|
||||
work = pathlib.Path(directory)
|
||||
scripts = work/'scripts'
|
||||
scripts.mkdir()
|
||||
for name in ['auth-schema-reconcile.sql', 'auth-schema-verify.sql']:
|
||||
(scripts/name).write_bytes((ROOT/'scripts'/name).read_bytes())
|
||||
script = (ROOT/'scripts/ci-auth-schema-reconcile.sh').read_text()
|
||||
script = script.replace('backup_root=/opt/backups/canina-auth-reconcile',
|
||||
'backup_root="$FAKE_BACKUP_ROOT"')
|
||||
(scripts/'ci-auth-schema-reconcile.sh').write_text(script)
|
||||
binaries = work/'bin'
|
||||
binaries.mkdir()
|
||||
fakes = {
|
||||
'docker': FAKE_DOCKER,
|
||||
'git': '#!/bin/sh\ncase "$*" in\n "rev-parse --show-toplevel") printf "%s\\n" "$FAKE_ROOT";;\n "rev-parse HEAD") printf "%s\\n" "$FAKE_SHA";;\n "status --porcelain") exit 0;;\n *) exit 2;;\nesac\n',
|
||||
'sleep': '#!/bin/sh\nexit 0\n',
|
||||
}
|
||||
for name, content in fakes.items():
|
||||
(binaries/name).write_text(content)
|
||||
(binaries/name).chmod(0o755)
|
||||
sha = 'a'*40
|
||||
digest = hashlib.sha256((scripts/'auth-schema-reconcile.sql').read_bytes()).hexdigest()
|
||||
backups = work/'backups'
|
||||
backups.mkdir()
|
||||
(backups/f'stage-{digest}.receipt').write_text(sha+'\n')
|
||||
env = dict(os.environ, PATH=str(binaries)+os.pathsep+os.environ['PATH'],
|
||||
GITHUB_ACTIONS='true', FAKE_ROOT=str(work), FAKE_SHA=sha,
|
||||
FAKE_BACKUP_ROOT=str(backups), FAKE_STATE=str(work/'state.json'),
|
||||
FAKE_MODE=mode, STAGE_AUTH_ACCEPTANCE_APPROVED=f'{sha}:{digest}',
|
||||
PRODUCTION_SCHEMA_APPROVED=f'{sha}:{digest}')
|
||||
result = subprocess.run(['bash', str(scripts/'ci-auth-schema-reconcile.sh'),
|
||||
'production', sha, digest], env=env,
|
||||
text=True, capture_output=True, timeout=60)
|
||||
return result, json.loads((work/'state.json').read_text())
|
||||
|
||||
def test_waits_for_final_tcp_server_despite_ready_transient_socket(self):
|
||||
result, state = self.run_workflow()
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(state['polls'], 3)
|
||||
self.assertTrue(state['tcp_ready'])
|
||||
calls = state['calls']
|
||||
restore = next(i for i, call in enumerate(calls) if '--exit-on-error' in call)
|
||||
self.assertEqual(sum('pg_isready' in c for c in calls[:restore]), 3)
|
||||
for call in calls:
|
||||
command = ' '.join(call)
|
||||
if any(tool in command for tool in ['pg_isready', 'pg_dump', 'psql']) or '--exit-on-error' in call:
|
||||
self.assertIn('-h 127.0.0.1', command)
|
||||
self.assertEqual(calls[-1][0:2], ['rm', '-f'])
|
||||
|
||||
def test_readiness_timeout_never_restores_or_executes_sql(self):
|
||||
result, state = self.run_workflow('never_ready')
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertEqual(state['polls'], 60)
|
||||
self.assertFalse(any('--exit-on-error' in c or 'psql' in ' '.join(c)
|
||||
for c in state['calls']))
|
||||
self.assertEqual(state['calls'][-1][0:2], ['rm', '-f'])
|
||||
|
||||
def test_restore_failure_never_executes_sql_or_retries_restore(self):
|
||||
result, state = self.run_workflow('restore_fails')
|
||||
self.assertNotEqual(result.returncode, 0)
|
||||
self.assertEqual(sum('--exit-on-error' in c for c in state['calls']), 1)
|
||||
self.assertFalse(any('psql' in ' '.join(c) for c in state['calls']))
|
||||
self.assertEqual(state['calls'][-1][0:2], ['rm', '-f'])
|
||||
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||||
class Safety(unittest.TestCase):
|
||||
def test_shell_syntax(self):
|
||||
subprocess.run(['bash', '-n', str(ROOT/'scripts/ci-auth-schema-reconcile.sh')], check=True)
|
||||
def test_narrow_sql(self):
|
||||
sql=(ROOT/'scripts/auth-schema-reconcile.sql').read_text()
|
||||
for forbidden in ['DROP TABLE', 'DELETE FROM', 'UPDATE public.', 'INSERT INTO', 'migrate deploy', 'db push']:
|
||||
self.assertNotIn(forbidden, '\n'.join(x for x in sql.splitlines() if not x.startswith('--')))
|
||||
self.assertIn("lock_timeout = '3s'", sql)
|
||||
self.assertIn("statement_timeout = '30s'", sql)
|
||||
self.assertIn('COMMIT;', sql)
|
||||
def test_fail_closed_approvals(self):
|
||||
w=(ROOT/'.gitea/workflows/auth-schema-reconcile.yml').read_text()
|
||||
for expected in ['REVIEWED_AUTH_SCHEMA_COMMIT','PRODUCTION_AUTH_SCHEMA_APPROVAL','STAGE_AUTH_SCHEMA_ACCEPTANCE','StrictHostKeyChecking=yes','IdentityAgent=none']:
|
||||
self.assertIn(expected,w)
|
||||
s=(ROOT/'scripts/ci-auth-schema-reconcile.sh').read_text()
|
||||
self.assertLess(s.index('pg_restore --exit-on-error'),s.index('if [[ $environment == production ]]; then',s.index('pg_restore --exit-on-error')))
|
||||
self.assertIn('cmp "$backup.expected.json" "$backup.actual.json"',s)
|
||||
def test_real_rehearsal(self):
|
||||
import json
|
||||
r=json.loads((ROOT/'docs/auth-schema-rehearsal.json').read_text())
|
||||
self.assertTrue(r['pre_patch_missing_columns_failed'])
|
||||
self.assertFalse(r['production_writes'])
|
||||
self.assertGreater(r['schema_backup_bytes'],90000)
|
||||
if __name__=='__main__':unittest.main()
|
||||
@@ -1,161 +0,0 @@
|
||||
import importlib.util
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location('production', Path(__file__).with_name('production-release.py'))
|
||||
m = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(m)
|
||||
|
||||
class ProductionGuards(unittest.TestCase):
|
||||
def receipt(self):
|
||||
return dict(sha='a'*40, baseline='b'*40, stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123', reviewer='CEO delegated acceptance', accepted=True, scope='reliability-only', paths=['nginx.conf'], typed_catalog_acceptance=dict(accepted=True, sha='a'*40, scope='native-types-and-constraints', reviewer='Independent schema reviewer', evidence_sha256='c'*64))
|
||||
|
||||
def test_exact_auth_schema_paths_from_real_git_diff_allowed_only(self):
|
||||
import subprocess
|
||||
paths = subprocess.check_output([
|
||||
'git', 'diff', '--name-only', '688a0e4^', '688a0e4', '--',
|
||||
'.gitea/workflows/auth-schema-reconcile.yml',
|
||||
'docs/auth-schema-reconciliation.md', 'docs/auth-schema-rehearsal.json',
|
||||
'scripts/auth-schema-reconcile.sql', 'scripts/auth-schema-verify.sql',
|
||||
'scripts/ci-auth-schema-reconcile.sh', 'scripts/test_auth_schema_reconcile.py',
|
||||
], cwd=Path(__file__).parents[1], text=True).splitlines()
|
||||
self.assertEqual(len(paths), 7, paths)
|
||||
r = self.receipt(); r['paths'] = paths
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
|
||||
for path in ['scripts/auth-schema-other.sql', 'docs/auth-schema-other.md',
|
||||
'.gitea/workflows/other.yml', 'scripts/test_auth_schema_other.py']:
|
||||
r['paths'] = [path]
|
||||
with self.assertRaises(ValueError):
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
|
||||
self.assertFalse(any('*' in path for path in m.ALLOWED))
|
||||
|
||||
def test_exact_six_baseline_gate_repairs_allowed_not_frontend_wildcard(self):
|
||||
files = {
|
||||
'frontend/admin-panel/src/pages/OrchestrationCommandCenter.tsx',
|
||||
'frontend/admin-panel/src/pages/selectionSafety.test.tsx',
|
||||
'frontend/admin-panel/src/pages/workflowSafety.test.tsx',
|
||||
'frontend/admin-panel/src/test/setup.ts',
|
||||
'frontend/admin-panel/src/test/storage.test.ts',
|
||||
'frontend/application/components/AnalyticsDeferred.tsx',
|
||||
'frontend/application/components/BlogPage.tsx',
|
||||
'frontend/application/components/__tests__/BlogPage.sorting.test.tsx',
|
||||
'frontend/application/components/BlogPostClient.tsx',
|
||||
'frontend/application/components/__tests__/BlogPostClient.test.tsx',
|
||||
'frontend/application/lib/extractFaqs.ts',
|
||||
'frontend/application/lib/extractFaqs.test.ts',
|
||||
}
|
||||
r=self.receipt();r['paths']=sorted(files)
|
||||
m.validate_receipt(r,'a'*40,'b'*40,sorted(files))
|
||||
for path in ['frontend/admin-panel/src/pages/Other.tsx', 'frontend/application/components/Other.tsx']:
|
||||
r['paths']=[path]
|
||||
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,[path])
|
||||
|
||||
def test_exact_editorial_installer_repair_paths_allowed_only(self):
|
||||
paths = ['infra/hermes-editorial/installer.py',
|
||||
'infra/hermes-editorial/tests/test_installer.py',
|
||||
'scripts/tests/test_secure_build_install.py']
|
||||
r = self.receipt(); r['paths'] = paths
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
|
||||
for path in ['infra/hermes-editorial/model-policy.json',
|
||||
'infra/hermes-editorial/payload/editorial-v4/status.json',
|
||||
'infra/hermes-editorial/other.py']:
|
||||
r['paths'] = [path]
|
||||
with self.assertRaises(ValueError):
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
|
||||
|
||||
def test_exact_publisher_seo_repair_paths_allowed_only(self):
|
||||
paths = ['frontend/application/app/blog/[slug]/page.tsx',
|
||||
'frontend/application/app/blog/[slug]/publisher-seo.test.tsx',
|
||||
'frontend/application/lib/blogAuthor.ts',
|
||||
'frontend/application/lib/blogAuthor.test.ts']
|
||||
r = self.receipt(); r['paths'] = paths
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, paths)
|
||||
for path in ['frontend/application/app/blog/[slug]/other.tsx',
|
||||
'frontend/application/lib/other.ts',
|
||||
'frontend/application/app/blog/page.tsx']:
|
||||
r['paths'] = [path]
|
||||
with self.assertRaises(ValueError):
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, [path])
|
||||
self.assertFalse(any('*' in path for path in m.ALLOWED))
|
||||
|
||||
def test_independent_typed_catalog_acceptance_required(self):
|
||||
r = self.receipt()
|
||||
r.pop('typed_catalog_acceptance', None)
|
||||
with self.assertRaisesRegex(ValueError, 'typed catalog'):
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])
|
||||
for acceptance in [
|
||||
{'accepted': False, 'sha': 'a'*40, 'evidence_sha256': 'c'*64},
|
||||
{'accepted': True, 'sha': 'd'*40, 'evidence_sha256': 'c'*64},
|
||||
{'accepted': True, 'sha': 'a'*40, 'evidence_sha256': ''},
|
||||
]:
|
||||
r['typed_catalog_acceptance'] = acceptance
|
||||
with self.assertRaisesRegex(ValueError, 'typed catalog'):
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])
|
||||
|
||||
def test_exact_stage_sha(self):
|
||||
r=self.receipt()
|
||||
m.validate_receipt(r, 'a'*40, 'b'*40, ['nginx.conf'])
|
||||
with self.assertRaises(ValueError): m.validate_receipt(r, 'c'*40, 'b'*40, ['nginx.conf'])
|
||||
|
||||
def test_scope_and_baseline_fail_closed(self):
|
||||
r=self.receipt()
|
||||
for baseline, paths in [('c'*40, ['nginx.conf']), ('b'*40, ['backend/prisma/schema.prisma']), ('b'*40, ['nginx.conf','backend/src/other.ts'])]:
|
||||
with self.assertRaises(ValueError): m.validate_receipt(r, 'a'*40, baseline, paths)
|
||||
r['accepted']=False
|
||||
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,['nginx.conf'])
|
||||
|
||||
def test_receipt_rejects_non_gitea_evidence(self):
|
||||
r=self.receipt();r['stage_run_url']='https://evil.test/run/1'
|
||||
with self.assertRaises(ValueError): m.validate_receipt(r,'a'*40,'b'*40,['nginx.conf'])
|
||||
|
||||
def test_snapshot_preserves_runtime_and_immutable_image(self):
|
||||
x={'Name':'/canino_backend_prod','Image':'sha256:'+'a'*64,'Config':{'Env':['DATABASE_URL=private'], 'Labels':{'com.docker.compose.project':'prod','com.docker.compose.service':'backend_prod','traefik.enable':'true'},'User':'node','WorkingDir':'/app','Entrypoint':['node'],'Cmd':['dist/main']},'Mounts':[{'Type':'volume','Name':'prod_canina_prod_uploads','Destination':'/app/uploads','RW':True},{'Type':'bind','Source':'/opt/services/proxy/certs/canina','Destination':'/app/ssl_certs','RW':True}],'NetworkSettings':{'Networks':{'traefik_public':{}}},'HostConfig':{'RestartPolicy':{'Name':'always'},'PortBindings':{},'Privileged':False}}
|
||||
c=m.snapshot([x]); s=c['services']['backend_prod']
|
||||
self.assertEqual(s['image'],x['Image'])
|
||||
self.assertEqual(s['environment'],['DATABASE_URL=private'])
|
||||
self.assertTrue(c['volumes']['prod_canina_prod_uploads']['external'])
|
||||
self.assertNotIn('com.docker.compose.project',s['labels'])
|
||||
x['Mounts'][0]['Name']='stage_canina_stage_uploads'
|
||||
with self.assertRaises(ValueError): m.snapshot([x])
|
||||
|
||||
def test_production_snapshot_forces_direct_next_url(self):
|
||||
import os, json
|
||||
from unittest.mock import patch
|
||||
c={'services':{n:{'environment':['FRONTEND_INTERNAL_URL=http://legacy:8080','REVALIDATION_SECRET=old'], 'labels':{}} for n in ['backend_prod','frontend_prod']}}
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
old=os.getcwd()
|
||||
try:
|
||||
os.chdir(d)
|
||||
with patch.object(m.sys,'argv',['production-release.py','snapshot']), patch.dict(os.environ,DEPLOY_SHA='a'*40), patch.object(m.subprocess,'check_output',return_value=b'[]'), patch.object(m,'snapshot',return_value=c):
|
||||
m.main()
|
||||
services=json.loads(Path('.prod-compose.json').read_text())['services']
|
||||
self.assertIn('FRONTEND_INTERNAL_URL=http://frontend_prod:3000',services['backend_prod']['environment'])
|
||||
for s in services.values():
|
||||
self.assertFalse(any(v.startswith('REVALIDATION_SECRET=') for v in s['environment']))
|
||||
self.assertTrue(s['env_file'][0]['required'])
|
||||
finally: os.chdir(old)
|
||||
|
||||
def test_private_receipt_permissions(self):
|
||||
import os
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p=Path(d)/'receipt.json';p.write_text('{}');p.chmod(0o644)
|
||||
with self.assertRaises(ValueError): m.private_json(p)
|
||||
p.unlink();p.symlink_to('/dev/null')
|
||||
with self.assertRaises(ValueError): m.private_json(p)
|
||||
|
||||
def test_no_schema_or_orphan_mutations_in_production(self):
|
||||
text=Path(__file__).with_name('rollout-production.sh').read_text()
|
||||
for dangerous in ['migrate deploy','db push','--remove-orphans','docker compose down','git reset']:
|
||||
self.assertNotIn(dangerous,text)
|
||||
self.assertIn('trap rollback EXIT',text)
|
||||
self.assertIn('x-revalidate-secret',text)
|
||||
self.assertIn('prod_canina_prod_db prod_canina_prod_uploads',text)
|
||||
|
||||
def test_ci_and_nonsha_refused_before_effects(self):
|
||||
import subprocess, os
|
||||
for script,args in [('deploy.sh',['main']),('ci-host-deploy.sh',[]) ,('rollout-production.sh',[])]:
|
||||
p=subprocess.run(['bash',str(Path(__file__).with_name(script)),*args],env={'PATH':os.environ['PATH']},capture_output=True)
|
||||
self.assertEqual(p.returncode,2, p.stderr)
|
||||
|
||||
if __name__=='__main__': unittest.main()
|
||||
@@ -12,8 +12,8 @@ class BoundaryTests(unittest.TestCase):
|
||||
self.assertIn('DEPLOY_KNOWN_HOSTS', result.stderr)
|
||||
self.assertNotIn('sentinel-secret', result.stdout+result.stderr)
|
||||
|
||||
def test_guards_refuse_non_ci_unknown_branch_and_injection(self):
|
||||
for updates in ({'GITHUB_ACTIONS':'false'}, {'BRANCH':'unreviewed'}, {'DEPLOY_SHA':'a'*40+';touch bad'}, {'DEPLOY_SHA':''}):
|
||||
def test_guards_refuse_non_ci_production_and_injection(self):
|
||||
for updates in ({'GITHUB_ACTIONS':'false'}, {'BRANCH':'main'}, {'DEPLOY_SHA':'a'*40+';touch bad'}, {'DEPLOY_SHA':''}):
|
||||
result = self.run_wrapper(**updates)
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertNotIn('provision', result.stderr)
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
"""Generated-client drift must block a release even with unchanged Git schema."""
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location('release', Path(__file__).parents[1]/'production-release.py')
|
||||
m = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(m)
|
||||
|
||||
class SchemaGate(unittest.TestCase):
|
||||
def test_unchanged_source_baseline_new_client_drift(self):
|
||||
# Both Git SHAs already contain these fields; the old runtime client did not.
|
||||
models = [{'name':'User','dbName':'users','fields':[
|
||||
{'name':n,'dbName':n,'kind':'scalar','type':t,'isList':False}
|
||||
for n,t in [('id','String'),('auth_expires_at','DateTime'),
|
||||
('stage_test_session','String'),('stage_test_generation','String')]]},
|
||||
{'name':'PrepareWorkflowOutbox','dbName':'prepare_workflow_outbox','fields':[
|
||||
{'name':'id','kind':'scalar','type':'String','isList':False}]}]
|
||||
old_catalog = [{'table':'users','column':'id','type':'uuid'}]
|
||||
with self.assertRaisesRegex(ValueError, 'auth_expires_at.*prepare_workflow_outbox'):
|
||||
m.validate_catalog(models, old_catalog)
|
||||
|
||||
def test_gate_pins_built_image_and_reads_live_catalog_before_replacement(self):
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
image = 'sha256:'+'c'*64
|
||||
runtime = {'Image':'sha256:'+'d'*64,'Id':'running-id','State':{'Running':True}}
|
||||
models = [{'name':'User','dbName':'users','fields':[{'name':'id','kind':'scalar'}]}]
|
||||
responses = [json.dumps([runtime]), json.dumps([{'Id':image}]),
|
||||
json.dumps(models), json.dumps([{'table':'users','column':'id','type':'uuid'}]),
|
||||
json.dumps([runtime])]
|
||||
with patch.object(m.subprocess,'check_output',side_effect=responses) as run:
|
||||
result = m.schema_gate('canina-production-backend_prod:'+'a'*40)
|
||||
self.assertEqual(result['target_image'], image)
|
||||
self.assertEqual(result['scope'], 'missing-columns-only')
|
||||
self.assertFalse(result['native_types_checked'])
|
||||
self.assertFalse(result['constraints_checked'])
|
||||
calls = [c.args[0] for c in run.call_args_list]
|
||||
self.assertIn(image, calls[2])
|
||||
self.assertIn('--network', calls[2])
|
||||
self.assertIn('running-id', calls[3])
|
||||
self.assertIn('READ ONLY', calls[3][-1])
|
||||
script = Path(__file__).parents[1].joinpath('rollout-production.sh').read_text()
|
||||
self.assertLess(script.index('production-release.py schema-check'), script.index('started=true'))
|
||||
|
||||
def test_wrong_native_type_is_outside_missing_column_guard(self):
|
||||
# Deliberately document the boundary: this must not imply compatibility.
|
||||
models = [{'name': 'User', 'dbName': 'users', 'fields': [
|
||||
{'name': 'id', 'kind': 'scalar', 'type': 'String', 'isList': False}]}]
|
||||
m.validate_catalog(models, [{'table': 'users', 'column': 'id', 'type': 'int4'}])
|
||||
with self.assertRaisesRegex(ValueError, 'typed catalog'):
|
||||
m.validate_receipt(dict(accepted=True, scope='reliability-only',
|
||||
sha='a'*40, baseline='b'*40, reviewer='reviewer',
|
||||
stage_run_url='https://git.parsaaghayi.ir/parsa/canina/actions/runs/123',
|
||||
paths=['nginx.conf']), 'a'*40, 'b'*40, ['nginx.conf'])
|
||||
|
||||
def test_catalog_failure_never_falls_back_to_source_diff(self):
|
||||
from unittest.mock import patch
|
||||
import subprocess
|
||||
with patch.object(m.subprocess,'check_output',side_effect=subprocess.CalledProcessError(1,['docker'])):
|
||||
with self.assertRaises(subprocess.CalledProcessError):
|
||||
m.schema_gate('target')
|
||||
|
||||
def test_exact_guard_paths_allowed_and_receipt_not_bound_to_checkout(self):
|
||||
self.assertIn('scripts/tests/test_release_schema_guard.py', m.ALLOWED)
|
||||
source = Path(__file__).parents[1].joinpath('production-release.py').read_text()
|
||||
self.assertNotIn("['git','-C','/opt/services/canina/prod','rev-parse','HEAD']", source)
|
||||
self.assertIn("r.get('production_images')", source)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
@@ -1,114 +0,0 @@
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
HELPER = ROOT / 'scripts/provision-revalidation-env.py'
|
||||
|
||||
class RevalidationRuntimeTests(unittest.TestCase):
|
||||
def load(self):
|
||||
self.assertTrue(HELPER.is_file(), 'secure revalidation provisioning helper missing')
|
||||
spec = importlib.util.spec_from_file_location('revalidation_env', HELPER)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
def test_preserves_existing_secret_and_writes_private_release_file(self):
|
||||
module = self.load()
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
source, output = root/'runtime.env', root/'release.env'
|
||||
original = '# existing runtime configuration\nREVALIDATION_SECRET=' + 'a'*48 + '\n'
|
||||
source.write_text(original)
|
||||
module.provision(source, output)
|
||||
self.assertEqual(source.read_text(), original)
|
||||
self.assertEqual(output.read_text(), 'REVALIDATION_SECRET='+'a'*48+'\n')
|
||||
self.assertEqual(stat.S_IMODE(output.stat().st_mode), 0o600)
|
||||
|
||||
def test_missing_runtime_secret_is_generated_once_and_reused(self):
|
||||
module = self.load()
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
source = root/'runtime.env'
|
||||
module.provision(source, root/'first.env')
|
||||
module.provision(source, root/'second.env')
|
||||
self.assertEqual((root/'first.env').read_bytes(), (root/'second.env').read_bytes())
|
||||
self.assertEqual(stat.S_IMODE(source.stat().st_mode), 0o600)
|
||||
self.assertGreaterEqual(len(source.read_text().split('=', 1)[1].strip()), 43)
|
||||
|
||||
def test_unsafe_or_invalid_input_is_rejected_without_touching_output(self):
|
||||
module = self.load()
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
target = root/'target'
|
||||
target.write_text('REVALIDATION_SECRET='+'b'*48+'\n')
|
||||
source = root/'source'
|
||||
source.symlink_to(target)
|
||||
with self.assertRaises(ValueError):
|
||||
module.provision(source, root/'output')
|
||||
source.unlink()
|
||||
os.link(target, source)
|
||||
with self.assertRaises(ValueError):
|
||||
module.provision(source, root/'output')
|
||||
source.unlink()
|
||||
for content in ('', 'REVALIDATION_SECRET=short\n', 'REVALIDATION_SECRET='+'a'*48+'\nREVALIDATION_SECRET='+'b'*48+'\n', 'REVALIDATION_SECRET='+'a'*48+'\nUNEXPECTED=value\n'):
|
||||
source.write_text(content)
|
||||
with self.assertRaises(ValueError):
|
||||
module.provision(source, root/'output')
|
||||
self.assertFalse((root/'output').exists())
|
||||
|
||||
def test_cli_is_silent_and_uses_same_contract_for_each_environment(self):
|
||||
import sys
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
for environment in ('stage', 'prod'):
|
||||
result = subprocess.run([sys.executable, str(HELPER), '--environment', environment,
|
||||
'--source', str(root/(environment+'.env')), '--output', str(root/(environment+'-release.env'))],
|
||||
env={**os.environ, 'GITHUB_ACTIONS': 'true'}, text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(result.stdout + result.stderr, '')
|
||||
self.assertEqual((root/(environment+'.env')).read_bytes(), (root/(environment+'-release.env')).read_bytes())
|
||||
self.assertNotEqual((root/'stage.env').read_bytes(), (root/'prod.env').read_bytes())
|
||||
|
||||
def test_symlink_parent_cannot_create_a_persistent_secret(self):
|
||||
module = self.load()
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
real = root/'real'
|
||||
real.mkdir()
|
||||
link = root/'link'
|
||||
link.symlink_to(real, target_is_directory=True)
|
||||
with self.assertRaises(ValueError):
|
||||
module.provision(link/'source', root/'output')
|
||||
self.assertFalse((real/'source').exists())
|
||||
|
||||
class StageEnvironmentTests(unittest.TestCase):
|
||||
def test_stage_uses_direct_next_revalidation_and_shared_required_secret(self):
|
||||
config = (ROOT/'scripts/compose.stage.yml').read_text()
|
||||
self.assertIn('- FRONTEND_INTERNAL_URL=http://frontend_stage:3000', config)
|
||||
frontend = config.split(' frontend_stage:', 1)[1]
|
||||
self.assertIn('- INTERNAL_API_URL=http://backend_stage:3000/api', frontend)
|
||||
self.assertEqual(config.count('path: .revalidation.env\n required: true'), 2)
|
||||
rollout = (ROOT/'scripts/rollout-stage.sh').read_text()
|
||||
self.assertIn('stage_revalidation_provision() {', rollout)
|
||||
self.assertIn('--source "$STAGE_DIR/.revalidation.env"', rollout)
|
||||
self.assertIn('--output "$TARGET_DIR/.revalidation.env"', rollout)
|
||||
|
||||
class RoutingTests(unittest.TestCase):
|
||||
def test_all_backend_routes_use_runtime_docker_dns_without_uri_rewrite(self):
|
||||
config = (ROOT/'nginx.conf').read_text()
|
||||
servers = config.split('server {')[1:]
|
||||
self.assertEqual(len(servers), 2)
|
||||
for server in servers:
|
||||
self.assertIn('resolver 127.0.0.11 valid=5s ipv6=off;', server)
|
||||
self.assertIn('resolver_timeout 2s;', server)
|
||||
self.assertIn('set $backend_upstream __BACKEND_HOST__:3000;', server)
|
||||
self.assertEqual(server.count('proxy_pass http://$backend_upstream;'), 2)
|
||||
self.assertNotIn('proxy_pass http://__BACKEND_HOST__', config)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,52 +0,0 @@
|
||||
"""Regression gates for CI1286: trusted routing and fail-closed frontend installs."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
REGISTRY = 'https://registry.parsaaghayi.ir/repository/npm/'
|
||||
|
||||
|
||||
class SecureBuildInstallTests(unittest.TestCase):
|
||||
def test_stage_supplies_operational_tls_registry(self):
|
||||
compose = (ROOT / 'scripts/compose.stage.yml').read_text()
|
||||
self.assertEqual(compose.count('NPM_REGISTRY: ${NPM_REGISTRY:-' + REGISTRY + '}'), 2)
|
||||
|
||||
def test_frontend_uses_supported_node_lts_in_all_stages(self):
|
||||
images = re.findall(r'^FROM node:(\S+)', (ROOT / 'Dockerfile').read_text(), re.M)
|
||||
self.assertEqual(images, ['22-alpine'] * 3)
|
||||
|
||||
def test_registry_configuration_never_disables_tls(self):
|
||||
for name in ['Dockerfile', 'backend/Dockerfile']:
|
||||
self.assertNotIn('strict-ssl false', (ROOT / name).read_text())
|
||||
|
||||
def test_false_successful_incomplete_install_aborts_layer(self):
|
||||
dockerfile = (ROOT / 'Dockerfile').read_text().replace('\\\n', '')
|
||||
commands = re.findall(r'^RUN (npm ci[^\n]+)', dockerfile, re.M)
|
||||
self.assertEqual(len(commands), 2)
|
||||
for command, binary in zip(commands, ['next', 'vite']):
|
||||
with self.subTest(binary=binary), tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
npm = root / 'npm'
|
||||
# Reproduce npm's observed zero exit with no complete node_modules.
|
||||
npm.write_text('#!/bin/sh\nexit 0\n')
|
||||
npm.chmod(0o755)
|
||||
result = subprocess.run(['sh', '-c', command], cwd=d,
|
||||
env={**os.environ, 'PATH': d + ':' + os.environ['PATH']},
|
||||
capture_output=True)
|
||||
self.assertNotEqual(result.returncode, 0, 'incomplete install was accepted')
|
||||
target = root / 'node_modules/.bin' / binary
|
||||
target.parent.mkdir(parents=True)
|
||||
target.write_text('#!/bin/sh\nexit 0\n')
|
||||
target.chmod(0o755)
|
||||
result = subprocess.run(['sh', '-c', command], cwd=d,
|
||||
env={**os.environ, 'PATH': d + ':' + os.environ['PATH']},
|
||||
capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -7,12 +7,6 @@ class WiringTests(unittest.TestCase):
|
||||
self.assertIn('stage_runtime_provision',s)
|
||||
self.assertLess(s.index('stage_runtime_provision'),s.index('>> .dockerignore'))
|
||||
self.assertIn('CANINA_RUNTIME_READ_ENABLED', (ROOT/'scripts/rollout-stage.sh').read_text())
|
||||
def test_revalidation_provision_before_compose_without_copy(self):
|
||||
s=(ROOT/'scripts/deploy.sh').read_text()
|
||||
self.assertIn('\nstage_revalidation_provision\n',s)
|
||||
self.assertLess(s.index('\nstage_revalidation_provision\n'),s.index('# Render privately'))
|
||||
self.assertNotIn('for name in .support-chat.env .revalidation.env',s)
|
||||
|
||||
def test_inventory_started_before_backend(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p=pathlib.Path(d)/'docker'; p.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "$TRACE"\n');p.chmod(0o755)
|
||||
@@ -29,31 +23,23 @@ class WiringTests(unittest.TestCase):
|
||||
shutil.copytree(ROOT/'scripts',repo/'scripts',ignore=shutil.ignore_patterns('__pycache__'))
|
||||
shutil.copytree(ROOT/'infra',repo/'infra')
|
||||
root=base/'runtime';home=base/'home';home.mkdir();tools=base/'tools';tools.mkdir()
|
||||
policy=json.loads((ROOT/'infra/hermes-editorial/model-policy.json').read_text())
|
||||
registered=set(policy['profiles'])-set(policy['additionalProfiles'])
|
||||
(home/policy['registry']).write_text('departments:\n'+''.join(' d%d: {profile: %s}\n' % (i,p) for i,p in enumerate(sorted(registered))))
|
||||
for profile in policy['profiles']:
|
||||
config=home/'profiles'/profile/'config.yaml';config.parent.mkdir(parents=True)
|
||||
config.write_text('model:\n default: old\n provider: custom\n');config.chmod(0o600)
|
||||
db=sqlite3.connect(home/'state.db')
|
||||
db.execute('CREATE TABLE sessions(source,started_at,input_tokens,output_tokens,estimated_cost_usd,title)')
|
||||
db.execute("INSERT INTO sessions VALUES ('cli',1,2,3,0.1,'PRIVATE TITLE')");db.commit();db.close()
|
||||
helper=repo/'scripts/rollout-stage.sh'
|
||||
# The fixture Docker backend runs in this test namespace, not the host's
|
||||
# /proc user namespace; bind projected ownership to this test process.
|
||||
helper.write_text(helper.read_text().replace('/opt/services/canina/stage-runtime',str(root)).replace("uid,gid=mapped('uid'),mapped('gid')", 'uid,gid=os.getuid(),os.getgid()'))
|
||||
helper.write_text(helper.read_text().replace('/opt/services/canina/stage-runtime',str(root)))
|
||||
editorial=repo/'scripts/provision-editorial-runtime.py'
|
||||
editorial.write_text(editorial.read_text().replace('/opt/services/canina/stage-runtime',str(root)))
|
||||
def git(*args):return subprocess.check_output(['git',*args],cwd=repo,text=True).strip()
|
||||
git('init','-q');git('config','user.email','fixture@example.invalid');git('config','user.name','Fixture');git('add','.');git('commit','-qm','fixture')
|
||||
sha=git('rev-parse','HEAD')
|
||||
subject='12345678-1234-1234-1234-123456789012'
|
||||
subject='12345678-1234-1234-1234-123456789abc'
|
||||
fixture={'backend':{'Config':{'User':'node'},'HostConfig':{'UsernsMode':''},'State':{'Running':True,'Pid':os.getpid()}},'hermes':{'State':{'Running':True},'HostConfig':{'UsernsMode':''},'Config':{'Env':['HERMES_HOME=/home/runtime']},'Mounts':[{'Type':'bind','Destination':'/home','Source':str(base)}]}}
|
||||
# /home/runtime resolves to base/runtime; point home at the SQLite fixture.
|
||||
fixture['hermes']['Mounts'][0]['Source']=str(home)
|
||||
fixture['hermes']['Config']['Env']=['HERMES_HOME=/home']
|
||||
(base/'inspect.json').write_text(json.dumps(fixture))
|
||||
(tools/'docker').write_text('#!/usr/bin/env python3\nimport os,sys,json,subprocess\na=sys.argv[1:]\nif a[0]=="compose": os.execv("/usr/bin/docker",["docker",*a])\nf=json.load(open(os.environ["INSPECT"]))\nif a[0]=="inspect": print(json.dumps([f["backend" if a[1]=="canino_backend_stage" else "hermes"]]))\nelif a[0]=="exec" and "ci_ownership" in a[-1]:\n assert a[1:4] == ["--user","0:0","-i"]\n p=json.load(sys.stdin); assert "def migrate(" in p["source"]\n # Root container ownership migration is covered in its dedicated suite.\nelif a[0]=="exec" and "-i" in a:\n p=json.load(sys.stdin); p["home"]=f["hermes"]["Mounts"][0]["Source"]; subprocess.run([sys.executable,"-B","-c",a[-1]],input=json.dumps(p),text=True,check=True)\nelif a[0]=="exec" and "pwd.getpwnam" in a[-1]:\n print(json.dumps([10000,10000]))\nelif a[0]=="exec" and "skills" in a:\n print("canina-editorial-v4")\nelif a[0]=="exec":\n assert "SET TRANSACTION READ ONLY" in a[-1] and "role::text" in a[-1]\n print(json.dumps([{ "id":os.environ["SUBJECT"], "role":os.environ.get("ROLE","Admin")}]))\nelse: raise RuntimeError(a)\n');(tools/'docker').chmod(0o755)
|
||||
(tools/'docker').write_text('#!/usr/bin/env python3\nimport os,sys,json,subprocess\na=sys.argv[1:]\nif a[0]=="compose": os.execv("/usr/bin/docker",["docker",*a])\nf=json.load(open(os.environ["INSPECT"]))\nif a[0]=="inspect": print(json.dumps([f["backend" if a[1]=="canino_backend_stage" else "hermes"]]))\nelif a[0]=="exec" and "-i" in a:\n p=json.load(sys.stdin); p["home"]=f["hermes"]["Mounts"][0]["Source"]; subprocess.run([sys.executable,"-B","-c",a[-1]],input=json.dumps(p),text=True,check=True)\nelif a[0]=="exec" and "skills" in a:\n print("canina-editorial-v4")\nelif a[0]=="exec":\n assert "SET TRANSACTION READ ONLY" in a[-1] and "role::text" in a[-1]\n print(json.dumps([{ "id":os.environ["SUBJECT"]}]))\nelse: raise RuntimeError(a)\n');(tools/'docker').chmod(0o755)
|
||||
env=dict(os.environ,PATH=str(tools)+':'+os.environ['PATH'],INSPECT=str(base/'inspect.json'),SUBJECT=subject,GITHUB_ACTIONS='true',STAGE_PROVISION_ENABLED='true',PYTHONDONTWRITEBYTECODE='1')
|
||||
command='set -euo pipefail; source scripts/rollout-stage.sh; DEPLOY_SHA='+sha+'; unset STAGE_PROVISION_APPROVED_SHA; stage_runtime_provision'
|
||||
# DEPLOY_SHA is deliberately shell-local: helper must export it.
|
||||
@@ -76,11 +62,6 @@ class WiringTests(unittest.TestCase):
|
||||
self.assertNotEqual(r.returncode,0)
|
||||
self.assertIn('Verified admin subjects required',r.stderr)
|
||||
self.assertEqual(before,set((root/'snapshots').iterdir()))
|
||||
for updates in [dict(SUBJECT='12345678-1234-1234-1234-123456789abc'),dict(ROLE='User')]:
|
||||
r=subprocess.run(['bash','-c',command],cwd=repo,env=dict(env,**updates),capture_output=True,text=True)
|
||||
self.assertNotEqual(r.returncode,0)
|
||||
self.assertIn('Pinned Stage owner must exist with Admin role',r.stderr)
|
||||
self.assertEqual(before,set((root/'snapshots').iterdir()))
|
||||
(home/'state.db-wal').symlink_to(home/'state.db')
|
||||
r=subprocess.run(['bash','-c',command],cwd=repo,env=env,capture_output=True,text=True)
|
||||
self.assertNotEqual(r.returncode,0)
|
||||
@@ -109,10 +90,10 @@ class WiringTests(unittest.TestCase):
|
||||
script=(ROOT/'scripts/deploy.sh').read_text()
|
||||
validation=script.split("config = json.loads(Path('.stage-base.json').read_text())",1)[1].split("\nPY",1)[0]
|
||||
code="import json\nfrom pathlib import Path\nconfig=json.loads(Path('.stage-base.json').read_text())"+validation
|
||||
config={'services':{'db_stage':{},'redis_stage':{},'backend_stage':{'build':{'context':'./backend'},'container_name':'canino_backend_stage','volumes':[{'type':'volume','source':'canina_stage_uploads','target':'/app/uploads'},{'type':'bind','source':'/etc','target':'/stolen'}]},'frontend_stage':{'container_name':'canino_frontend_stage'}},'volumes':{n:{'name':'stage_'+n} for n in ['canina_stage_db','canina_stage_uploads']},'networks':{'traefik_public':{'external':True,'name':'traefik_public'}}}
|
||||
config={'services':{'db_stage':{},'redis_stage':{},'backend_stage':{'container_name':'canino_backend_stage','volumes':[{'type':'volume','source':'canina_stage_uploads','target':'/app/uploads'},{'type':'bind','source':'/etc','target':'/stolen'}]},'frontend_stage':{'container_name':'canino_frontend_stage'}},'volumes':{n:{'name':'stage_'+n} for n in ['canina_stage_db','canina_stage_uploads']},'networks':{'traefik_public':{'external':True,'name':'traefik_public'}}}
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
p=pathlib.Path(directory);(p/'.stage-base.json').write_text(json.dumps(config))
|
||||
result=subprocess.run(['python3','-c',code],cwd=p,env=dict(os.environ,DEPLOY_SHA='a'*40),capture_output=True,text=True)
|
||||
result=subprocess.run(['python3','-c',code],cwd=p,capture_output=True,text=True)
|
||||
self.assertNotEqual(result.returncode,0)
|
||||
self.assertIn('unexpected Stage bind mount',result.stderr)
|
||||
self.assertFalse((p/'.stage-compose.json').exists())
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import os, pathlib, subprocess, tempfile, unittest
|
||||
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
||||
class RolloutTests(unittest.TestCase):
|
||||
def test_non_ci_and_unpinned_production_refused_before_docker(self):
|
||||
def test_non_ci_and_production_refused_before_docker(self):
|
||||
script=(ROOT/'scripts/deploy.sh').read_text()
|
||||
self.assertIn('authenticated CI/CD rollout',script)
|
||||
self.assertIn('CI/CD-only Stage rollout',script)
|
||||
guard=script.split('# END ROLLOUT GUARD')[0]
|
||||
for args,env in [(['develop'],{}),(['develop','stage-backend-only'],{'GITHUB_ACTIONS':'true'}),(['main'],{'GITHUB_ACTIONS':'true'})]:
|
||||
r=subprocess.run(['bash','-c',guard,'deploy.sh',*args],cwd=ROOT,env=dict(os.environ,**env),capture_output=True,text=True)
|
||||
@@ -58,11 +58,6 @@ class RolloutTests(unittest.TestCase):
|
||||
git('config', 'user.email', 'test@example.invalid')
|
||||
git('config', 'user.name', 'Test')
|
||||
shutil.copytree(ROOT/'scripts', source/'scripts')
|
||||
# This test exercises rollout/Compose, never host systemd installation.
|
||||
# The installer's guarded install contract has its own full unit suite.
|
||||
(source/'scripts/install-stage-principal-cleanup.py').write_text('raise SystemExit(0)\n')
|
||||
(source/'scripts/install-disk-watch.py').write_text('raise SystemExit(0)\n')
|
||||
(source/'scripts/release-retention.py').write_text('raise SystemExit(0)\n')
|
||||
shutil.copyfile(ROOT/'.dockerignore', source/'.dockerignore')
|
||||
(source/'nginx.conf').write_text('__BACKEND_HOST__')
|
||||
git('add', '.')
|
||||
@@ -78,12 +73,12 @@ class RolloutTests(unittest.TestCase):
|
||||
git('add', '.', cwd=stage)
|
||||
(stage/'dirty').write_text('unstaged')
|
||||
for n in ('.support-chat.env', '.revalidation.env'):
|
||||
(stage/n).write_text('REVALIDATION_SECRET='+'s'*48+'\n' if n == '.revalidation.env' else 'TEST_SECRET=never-log-this\n')
|
||||
(stage/n).write_text('TEST_SECRET=never-log-this\n')
|
||||
before = git('status', '--porcelain', cwd=stage), git('diff', '--binary', 'HEAD', cwd=stage), (stage/'.git/index').read_bytes()
|
||||
config = {'services': {
|
||||
'db_stage': {'volumes': [{'type': 'volume', 'source': 'canina_stage_db', 'target': '/var/lib/postgresql/data'}]},
|
||||
'redis_stage': {},
|
||||
'backend_stage': {'build': {'context': './backend'}, 'container_name': 'canino_backend_stage', 'volumes': [
|
||||
'backend_stage': {'container_name': 'canino_backend_stage', 'volumes': [
|
||||
{'type': 'volume', 'source': 'canina_stage_uploads', 'target': '/app/uploads'},
|
||||
{'type': 'bind', 'source': '/opt/services/proxy/certs/canina', 'target': '/app/ssl_certs'}]},
|
||||
'frontend_stage': {'container_name': 'canino_frontend_stage'}},
|
||||
@@ -94,11 +89,6 @@ class RolloutTests(unittest.TestCase):
|
||||
(tools/'docker').chmod(0o755)
|
||||
script = (ROOT/'scripts/deploy.sh').read_text()
|
||||
script = script.replace('/opt/services/canina/stage-backend-deploy.lock', str(base/'lock')).replace('/opt/services/canina/stage', str(stage)).replace('/opt/services/canina/releases', str(releases)).replace('https://git.parsaaghayi.ir/parsa/canina.git', str(source)).replace('[ -d /opt/services/proxy/certs/canina ]', 'true')
|
||||
script = script.replace('/opt/services/canina/tmp/retention-stage.json', str(base/'retention-stage.json'))
|
||||
# Fail closed if any copied installer unexpectedly reaches systemd.
|
||||
(tools/'systemctl').write_text('#!/bin/sh\nprintf "unexpected systemctl\\n" >&2\nexit 99\n')
|
||||
(tools/'systemctl').chmod(0o755)
|
||||
self.assertNotIn('> /opt/services/canina/tmp/retention-stage.json',script, 'Fixture must never write live retention evidence')
|
||||
deploy = base/'deploy.sh'
|
||||
deploy.write_text(script)
|
||||
env = dict(os.environ, GITHUB_ACTIONS='true', DEPLOY_SHA=sha, PATH=str(tools)+':'+os.environ['PATH'], TRACE=str(base/'trace'), FIXTURE=str(base/'config.json'))
|
||||
@@ -118,14 +108,13 @@ class RolloutTests(unittest.TestCase):
|
||||
if fail == '1': self.assertNotIn('up -d', trace)
|
||||
else:
|
||||
self.assertLess(trace.index('migrate deploy'), trace.index('up -d --no-deps backend_stage'))
|
||||
self.assertLess(trace.index('exec -T backend_stage'), trace.index('up -d --no-deps --force-recreate frontend_stage'))
|
||||
self.assertLess(trace.index('exec -T backend_stage'), trace.index('up -d --no-deps frontend_stage'))
|
||||
release = releases/sha
|
||||
self.assertEqual(git('rev-parse', 'HEAD', cwd=release), sha)
|
||||
self.assertEqual((release/'.support-chat.env').stat().st_mode & 0o777, 0o600)
|
||||
if fail == '0': self.assertEqual(git('diff','--','.dockerignore','nginx.conf',cwd=release), '')
|
||||
if fail == '1': self.assertIn('.stage-*.json', (release/'.dockerignore').read_text())
|
||||
if fail == '1': self.assertIn('.support-chat.env', (release/'.dockerignore').read_text())
|
||||
if fail == '1': self.assertIn('.revalidation.env', (release/'.dockerignore').read_text())
|
||||
self.assertIn('.stage-*.json', (release/'.dockerignore').read_text())
|
||||
self.assertIn('.support-chat.env', (release/'.dockerignore').read_text())
|
||||
self.assertIn('.revalidation.env', (release/'.dockerignore').read_text())
|
||||
self.assertEqual((release/'.stage-compose.json').stat().st_mode & 0o777, 0o600)
|
||||
effective = json.loads((release/'.stage-compose.json').read_text())
|
||||
self.assertEqual(effective['volumes'], {n: {'name': 'stage_'+n, 'external': True} for n in ('canina_stage_db','canina_stage_uploads')})
|
||||
@@ -133,54 +122,6 @@ class RolloutTests(unittest.TestCase):
|
||||
retry = subprocess.run(['bash', str(deploy), 'develop'], env=env, capture_output=True, text=True)
|
||||
self.assertNotEqual(retry.returncode, 0)
|
||||
|
||||
def test_cached_frontend_is_recreated_with_target_release_provenance(self):
|
||||
import json
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
base = pathlib.Path(d)
|
||||
target = base/'releases'/('b'*40)
|
||||
target.mkdir(parents=True)
|
||||
state = base/'state.json'
|
||||
# Compose does not recreate an unchanged image/config just because
|
||||
# --project-directory moved. Model that cached-image decision only;
|
||||
# execute the real CI wrapper and rollout, never a Docker daemon.
|
||||
state.write_text(json.dumps({'working_dir': '/releases/'+('a'*40), 'image': 'sha256:cached'}))
|
||||
docker = base/'docker'
|
||||
docker.write_text('''#!/usr/bin/env python3
|
||||
import json, os, pathlib, sys
|
||||
args = sys.argv[1:]
|
||||
with open(os.environ['TRACE'], 'a') as trace:
|
||||
trace.write(json.dumps(args)+'\\n')
|
||||
if 'up' in args and args[-1] == 'frontend_stage':
|
||||
state = pathlib.Path(os.environ['STATE'])
|
||||
container = json.loads(state.read_text())
|
||||
if '--force-recreate' in args:
|
||||
container['working_dir'] = args[args.index('--project-directory')+1]
|
||||
state.write_text(json.dumps(container))
|
||||
''')
|
||||
docker.chmod(0o755)
|
||||
deploy = (ROOT/'scripts/deploy.sh').read_text()
|
||||
wrapper = deploy.split('docker() {', 1)[1].split('# Render privately:', 1)[0]
|
||||
command = 'set -euo pipefail; docker() {'+wrapper+'\nsource scripts/rollout-stage.sh; stage_rollout'
|
||||
result = subprocess.run(['bash', '-c', command], cwd=ROOT, env=dict(
|
||||
os.environ, PATH=d+':'+os.environ['PATH'], TARGET_DIR=str(target),
|
||||
TRACE=str(base/'trace'), STATE=str(state), STAGE_PROVISION_ENABLED='false'),
|
||||
capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
container = json.loads(state.read_text())
|
||||
self.assertEqual(container['image'], 'sha256:cached')
|
||||
self.assertEqual(container['working_dir'], str(target),
|
||||
'Cached frontend retained stale release provenance')
|
||||
calls = [json.loads(line) for line in (base/'trace').read_text().splitlines()]
|
||||
prefix = ['compose', '-p', 'stage', '--env-file', '/dev/null',
|
||||
'--project-directory', str(target), '-f', str(target/'.stage-compose.json')]
|
||||
self.assertTrue(all(call[:len(prefix)] == prefix for call in calls))
|
||||
starts = [call[len(prefix):] for call in calls if 'up' in call]
|
||||
self.assertEqual(starts, [['up', '-d', '--no-deps', 'backend_stage'],
|
||||
['up', '-d', '--no-deps', '--force-recreate', 'frontend_stage']])
|
||||
health = next(i for i, call in enumerate(calls) if 'exec' in call)
|
||||
frontend = next(i for i, call in enumerate(calls) if 'up' in call and call[-1] == 'frontend_stage')
|
||||
self.assertLess(health, frontend)
|
||||
|
||||
def test_migration_failure_never_starts_apps(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
docker = pathlib.Path(d)/'docker'
|
||||
@@ -204,6 +145,5 @@ if 'up' in args and args[-1] == 'frontend_stage':
|
||||
trace=pathlib.Path(d+'/trace').read_text()
|
||||
self.assertIn('stop frontend_stage backend_stage',trace)
|
||||
self.assertIn('exec -T backend_stage node',trace)
|
||||
self.assertFalse(any(' up ' in ' '+line+' ' and line.endswith(' frontend_stage')
|
||||
for line in trace.splitlines()), trace)
|
||||
self.assertNotIn('up -d --no-deps frontend_stage',trace)
|
||||
self.assertNotIn('SUCCESS',result.stdout)
|
||||
Reference in new issue
Block a user