mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-13 04:06:59 +00:00
865d792874
* feat(pricing): collapse to two tiers (Community + Admiral) Collapse Sencho's pricing from three tiers (Community / Skipper / Admiral) to two: a generous free Community tier and a single paid Admiral tier. The Skipper tier is removed. Now free in Community: auto-heal, auto-update, scheduled operations, webhooks, notification routing, Fleet Actions and bulk operations, SSO preset providers (Google / GitHub / Okta), unlimited users with admin and viewer roles, and deploy safety (atomic deploys, auto-rollback, and one-click rollback). Admiral (paid) is focused on running and governing a fleet: blueprints, Fleet Secrets, deploy enforcement, vulnerability report export, audit log, host console, private registries, mesh networking, node cordon, managed cloud backup, LDAP / Active Directory SSO, and the advanced RBAC roles (deployer, node-admin, auditor) with per-resource scoped assignments. Internally the license variant distinction is removed so tier is binary (community / paid). License validation still verifies the Lemon Squeezy store and product before granting paid status. Docs and the contributor guide are updated to the two-tier model. * docs(pricing): correct licensing page to two-tier pricing and tidy stale tier wording The licensing docs page kept the old Admiral pricing plus a Founder Lifetime column and an Enterprise paragraph after the two-tier collapse. Update it to $12/month or $99/year, drop the lifetime and Enterprise content, and link to the pricing page for current pricing. Also fix stale "Skipper" wording in CLA.md, SUPPORT.md, one test title, and three test comments. Historical CHANGELOG entries and the retired-Skipper license-guard test are intentionally left as-is. * docs: align licensing and SSO pages with the two-tier model Correct the SSO overview so the Google, GitHub, and Okta presets read as available on every tier, matching the provider table; only LDAP and Active Directory require Sencho Admiral. Remove the lifetime-plan references from the licensing, settings, and troubleshooting pages so they reflect subscription-only Admiral pricing. * fix(rbac): omit scoped permissions from /me on the Community tier Scoped role assignments only take effect on the paid tier, but GET /api/permissions/me returned them unconditionally, so a downgraded instance with leftover assignments rendered per-resource affordances the API then rejected with 403. The endpoint now mirrors the permission middleware and includes scoped permissions only on the paid tier. Adds a regression test covering the downgrade case. * docs: use custom-pricing wording on the contact page The two-tier model has no Enterprise tier; reword the contact page's enterprise pricing/deals to custom pricing/deals so it does not imply a tier that no longer exists.
292 lines
11 KiB
TypeScript
292 lines
11 KiB
TypeScript
/**
|
|
* Authorization and validation tests for the Federation cordon routes
|
|
* (POST /api/nodes/:id/cordon and /uncordon).
|
|
*
|
|
* These guard the same boundary the NodeCard cordon control renders against, so
|
|
* a UI gate and a route guard cannot silently drift apart. Cordon/uncordon
|
|
* require the paid tier AND the node:manage permission (held by admin and
|
|
* node-admin roles). The guard order is:
|
|
* rejectApiTokenScope (SCOPE_DENIED) -> requirePermission (PERMISSION_DENIED)
|
|
* -> requirePaid (PAID_REQUIRED) -> invalid-id 400
|
|
* -> reason 400 (cordon only) -> 404.
|
|
*/
|
|
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
|
|
import request from 'supertest';
|
|
import type { LicenseTier } from '../services/license-types';
|
|
import type { UserRole } from '../services/DatabaseService';
|
|
import { setupTestDb, cleanupTestDb, loginAsTestAdmin, TEST_USERNAME } from './helpers/setupTestDb';
|
|
import { createTestApiToken } from './helpers/apiTokenTestHelper';
|
|
|
|
let tmpDir: string;
|
|
let app: import('express').Express;
|
|
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
|
|
let LicenseService: typeof import('../services/LicenseService').LicenseService;
|
|
let adminCookie: string;
|
|
let adminUserId: number;
|
|
const roleCookie: Record<string, string> = {};
|
|
let counter = 0;
|
|
|
|
function setLicense(tier: LicenseTier): void {
|
|
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue(tier);
|
|
}
|
|
|
|
function seedNode(): { id: number; name: string } {
|
|
counter += 1;
|
|
const name = `cordon-authz-node-${counter}`;
|
|
const db = DatabaseService.getInstance().getDb();
|
|
const result = db.prepare(
|
|
`INSERT INTO nodes (name, type, mode, compose_dir, is_default, status, created_at)
|
|
VALUES (?, 'local', 'proxy', '/tmp/compose', 0, 'online', ?)`,
|
|
).run(name, Date.now());
|
|
return { id: result.lastInsertRowid as number, name };
|
|
}
|
|
|
|
async function seedAndLogin(role: UserRole): Promise<string> {
|
|
const bcrypt = (await import('bcrypt')).default;
|
|
const supertest = (await import('supertest')).default;
|
|
const username = `cordon-${role}`;
|
|
const password = `cordon-${role}-pass`;
|
|
const passwordHash = await bcrypt.hash(password, 1);
|
|
DatabaseService.getInstance().addUser({ username, password_hash: passwordHash, role });
|
|
const res = await supertest(app).post('/api/auth/login').send({ username, password });
|
|
const cookies = res.headers['set-cookie'] as string | string[];
|
|
return Array.isArray(cookies) ? cookies[0] : cookies;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
tmpDir = await setupTestDb();
|
|
({ DatabaseService } = await import('../services/DatabaseService'));
|
|
({ LicenseService } = await import('../services/LicenseService'));
|
|
|
|
vi.spyOn(LicenseService.getInstance(), 'getTier').mockReturnValue('paid');
|
|
|
|
({ app } = await import('../index'));
|
|
adminCookie = await loginAsTestAdmin(app);
|
|
for (const role of ['node-admin', 'viewer', 'deployer', 'auditor'] as const) {
|
|
roleCookie[role] = await seedAndLogin(role);
|
|
}
|
|
const adminRow = DatabaseService.getInstance().getDb()
|
|
.prepare('SELECT id FROM users WHERE username = ?')
|
|
.get(TEST_USERNAME) as { id: number } | undefined;
|
|
if (!adminRow) throw new Error('seeded admin user not found');
|
|
adminUserId = adminRow.id;
|
|
});
|
|
|
|
afterAll(() => cleanupTestDb(tmpDir));
|
|
|
|
beforeEach(() => {
|
|
vi.restoreAllMocks();
|
|
setLicense('paid');
|
|
DatabaseService.getInstance().getDb().prepare('DELETE FROM nodes WHERE is_default = 0').run();
|
|
});
|
|
|
|
describe('POST /api/nodes/:id/cordon authorization', () => {
|
|
it('lets an admin cordon a node and stores the trimmed reason', async () => {
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({ reason: ' patching kernel ' });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.cordoned).toBe(true);
|
|
expect(res.body.cordoned_reason).toBe('patching kernel');
|
|
});
|
|
|
|
it('lets a node-admin cordon a node', async () => {
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', roleCookie['node-admin'])
|
|
.send({});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.cordoned).toBe(true);
|
|
});
|
|
|
|
it.each(['viewer', 'deployer', 'auditor'])(
|
|
'rejects a %s without node:manage with PERMISSION_DENIED',
|
|
async (role) => {
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', roleCookie[role])
|
|
.send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('PERMISSION_DENIED');
|
|
},
|
|
);
|
|
|
|
it('rejects an admin on a Community license with PAID_REQUIRED', async () => {
|
|
setLicense('community');
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('PAID_REQUIRED');
|
|
});
|
|
|
|
it('rejects a full-admin API token with SCOPE_DENIED (API tokens cannot manage nodes)', async () => {
|
|
const node = seedNode();
|
|
const token = createTestApiToken({ db: DatabaseService, scope: 'full-admin', userId: adminUserId });
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Authorization', `Bearer ${token}`)
|
|
.send({ reason: 'ci' });
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('SCOPE_DENIED');
|
|
});
|
|
|
|
it('rejects a reason longer than 256 characters with 400', async () => {
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({ reason: 'a'.repeat(257) });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a non-string reason with 400', async () => {
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({ reason: 123 });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects an invalid node id with 400', async () => {
|
|
const res = await request(app)
|
|
.post('/api/nodes/0/cordon')
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a numeric-prefix node id with 400', async () => {
|
|
const res = await request(app)
|
|
.post('/api/nodes/1abc/cordon')
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('returns 404 for a node that does not exist', async () => {
|
|
const res = await request(app)
|
|
.post('/api/nodes/999999/cordon')
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('stores null when the reason is whitespace only', async () => {
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({ reason: ' ' });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.cordoned).toBe(true);
|
|
expect(res.body.cordoned_reason).toBeNull();
|
|
});
|
|
|
|
it('checks node:manage before the tier gate (Community viewer gets PERMISSION_DENIED, not PAID_REQUIRED)', async () => {
|
|
setLicense('community');
|
|
const node = seedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/cordon`)
|
|
.set('Cookie', roleCookie['viewer'])
|
|
.send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('PERMISSION_DENIED');
|
|
});
|
|
});
|
|
|
|
describe('POST /api/nodes/:id/uncordon authorization', () => {
|
|
function seedCordonedNode(): { id: number; name: string } {
|
|
const node = seedNode();
|
|
DatabaseService.getInstance().setNodeCordoned(node.id, true, 'pre');
|
|
return node;
|
|
}
|
|
|
|
it('lets an admin uncordon a node', async () => {
|
|
const node = seedCordonedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/uncordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.cordoned).toBe(false);
|
|
expect(res.body.cordoned_reason).toBeNull();
|
|
});
|
|
|
|
it('lets a node-admin uncordon a node', async () => {
|
|
const node = seedCordonedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/uncordon`)
|
|
.set('Cookie', roleCookie['node-admin'])
|
|
.send({});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.cordoned).toBe(false);
|
|
});
|
|
|
|
it.each(['viewer', 'deployer', 'auditor'])(
|
|
'rejects a %s without node:manage with PERMISSION_DENIED',
|
|
async (role) => {
|
|
const node = seedCordonedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/uncordon`)
|
|
.set('Cookie', roleCookie[role])
|
|
.send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('PERMISSION_DENIED');
|
|
},
|
|
);
|
|
|
|
it('rejects an admin on a Community license with PAID_REQUIRED', async () => {
|
|
setLicense('community');
|
|
const node = seedCordonedNode();
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/uncordon`)
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('PAID_REQUIRED');
|
|
});
|
|
|
|
it('rejects a full-admin API token with SCOPE_DENIED', async () => {
|
|
const node = seedCordonedNode();
|
|
const token = createTestApiToken({ db: DatabaseService, scope: 'full-admin', userId: adminUserId });
|
|
const res = await request(app)
|
|
.post(`/api/nodes/${node.id}/uncordon`)
|
|
.set('Authorization', `Bearer ${token}`)
|
|
.send({});
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.code).toBe('SCOPE_DENIED');
|
|
});
|
|
|
|
it('returns 404 for a node that does not exist', async () => {
|
|
const res = await request(app)
|
|
.post('/api/nodes/999999/uncordon')
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('rejects an invalid node id with 400', async () => {
|
|
const res = await request(app)
|
|
.post('/api/nodes/0/uncordon')
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a numeric-prefix node id with 400', async () => {
|
|
const res = await request(app)
|
|
.post('/api/nodes/1abc/uncordon')
|
|
.set('Cookie', adminCookie)
|
|
.send({});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|