@apso/cli 0.48.0 → 0.49.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +28 -1
  2. package/dist/commands/dev.d.ts +2 -0
  3. package/dist/commands/dev.js +20 -3
  4. package/dist/commands/generate.js +49 -13
  5. package/dist/commands/mcp/serve.js +24 -41
  6. package/dist/commands/schema/diff.js +3 -4
  7. package/dist/commands/schema/pull.js +13 -11
  8. package/dist/commands/schema/push.js +6 -5
  9. package/dist/commands/schema/validate.d.ts +10 -0
  10. package/dist/commands/schema/validate.js +54 -0
  11. package/dist/commands/test/access.d.ts +11 -0
  12. package/dist/commands/test/access.js +65 -0
  13. package/dist/lib/access/cases.d.ts +120 -0
  14. package/dist/lib/access/cases.js +1163 -0
  15. package/dist/lib/access/index.d.ts +4 -0
  16. package/dist/lib/access/index.js +7 -0
  17. package/dist/lib/access/report.d.ts +75 -0
  18. package/dist/lib/access/report.js +118 -0
  19. package/dist/lib/access/rules.json +207 -0
  20. package/dist/lib/access/run.d.ts +38 -0
  21. package/dist/lib/access/run.js +111 -0
  22. package/dist/lib/access/tokens.d.ts +46 -0
  23. package/dist/lib/access/tokens.js +139 -0
  24. package/dist/lib/api/services.d.ts +10 -7
  25. package/dist/lib/api/services.js +19 -32
  26. package/dist/lib/apsorc-parser.d.ts +1 -1
  27. package/dist/lib/apsorc-parser.js +2 -0
  28. package/dist/lib/deploy/server-push.js +3 -1
  29. package/dist/lib/generators/go.d.ts +13 -1
  30. package/dist/lib/generators/go.js +144 -33
  31. package/dist/lib/generators/python.d.ts +3 -3
  32. package/dist/lib/generators/python.js +48 -24
  33. package/dist/lib/generators/typescript.d.ts +3 -5
  34. package/dist/lib/generators/typescript.js +78 -64
  35. package/dist/lib/guards.d.ts +111 -13
  36. package/dist/lib/guards.js +287 -23
  37. package/dist/lib/templates/go/handlers/handler.eta +8 -8
  38. package/dist/lib/templates/go/handlers/scope.eta +3 -2
  39. package/dist/lib/templates/go/index-module.eta +8 -5
  40. package/dist/lib/templates/go/mcp.eta +307 -0
  41. package/dist/lib/templates/go/middleware/auth.eta +587 -166
  42. package/dist/lib/templates/go/middleware/permission.eta +174 -0
  43. package/dist/lib/templates/go/services/service.eta +37 -16
  44. package/dist/lib/templates/go/utils/query.eta +37 -2
  45. package/dist/lib/templates/guards/guards.module.eta +37 -29
  46. package/dist/lib/templates/guards/index.eta +4 -0
  47. package/dist/lib/templates/guards/jwt-auth.guard.eta +114 -29
  48. package/dist/lib/templates/guards/key.guard.eta +280 -0
  49. package/dist/lib/templates/guards/permission.guard.eta +168 -0
  50. package/dist/lib/templates/guards/relations.interceptor.eta +124 -0
  51. package/dist/lib/templates/guards/scope.guard.eta +189 -41
  52. package/dist/lib/templates/python/auth.eta +354 -95
  53. package/dist/lib/templates/python/index-module.eta +7 -7
  54. package/dist/lib/templates/python/mcp.eta +284 -0
  55. package/dist/lib/templates/python/models/model.eta +6 -5
  56. package/dist/lib/templates/python/permissions.eta +121 -0
  57. package/dist/lib/templates/python/routers/router.eta +39 -1
  58. package/dist/lib/templates/python/schemas/schema.eta +18 -5
  59. package/dist/lib/templates/python/services/service.eta +38 -15
  60. package/dist/lib/templates/python/utils/query.eta +52 -25
  61. package/dist/lib/templates/rest/controller-rest.eta +43 -23
  62. package/dist/lib/templates/rest/index-module-rest.eta +0 -4
  63. package/dist/lib/templates/rest/service-rest.eta +3 -44
  64. package/dist/lib/types/auth.d.ts +84 -80
  65. package/dist/lib/types/auth.js +77 -60
  66. package/dist/lib/types/entity.d.ts +17 -9
  67. package/dist/lib/types/entity.js +3 -0
  68. package/dist/lib/types/index.d.ts +2 -2
  69. package/dist/lib/types/index.js +6 -5
  70. package/dist/lib/utils/crud-deps.d.ts +77 -6
  71. package/dist/lib/utils/crud-deps.js +197 -14
  72. package/dist/lib/utils/schema-convert.d.ts +20 -43
  73. package/dist/lib/utils/schema-convert.js +28 -78
  74. package/dist/lib/utils/template.d.ts +11 -0
  75. package/dist/lib/utils/template.js +36 -1
  76. package/dist/lib/verify/index.d.ts +6 -1
  77. package/dist/lib/verify/index.js +30 -13
  78. package/npm-shrinkwrap.json +2 -2
  79. package/oclif.manifest.json +36 -1
  80. package/package.json +1 -1
  81. package/dist/lib/templates/guards/api-key-auth.guard.eta +0 -145
  82. package/dist/lib/templates/guards/auth.guard.eta +0 -233
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const core_1 = require("@oclif/core");
5
+ const base_command_1 = tslib_1.__importDefault(require("../../lib/base-command"));
6
+ const access_1 = require("../../lib/access");
7
+ const apsorc_parser_1 = require("../../lib/apsorc-parser");
8
+ class TestAccess extends base_command_1.default {
9
+ async run() {
10
+ const { flags } = await this.parse(TestAccess);
11
+ const { entities, relationshipMap, auth, language } = (0, apsorc_parser_1.parseApsorc)();
12
+ const result = await (0, access_1.accessTest)(process.cwd(), entities, relationshipMap, {
13
+ skipBuild: flags["skip-build"],
14
+ language,
15
+ auth,
16
+ log: flags.verbose && !flags.json ? (line) => this.log(line) : undefined,
17
+ });
18
+ if (flags.json) {
19
+ this.log(JSON.stringify(result));
20
+ }
21
+ else {
22
+ for (const s of result.steps) {
23
+ this.log(`${s.ok ? "ok " : "FAIL"} ${s.name}`);
24
+ if (s.output)
25
+ this.log(s.output.replace(/^/gm, " "));
26
+ }
27
+ for (const note of result.notes)
28
+ this.log(note);
29
+ if (result.lines.length > 0)
30
+ this.log("");
31
+ for (const line of result.lines) {
32
+ const count = line.cases > 0 ? `${line.cases} cases` : line.detail;
33
+ this.log(`${line.id.padEnd(4)} ${line.result.padEnd(8)} ${count}`);
34
+ }
35
+ if (result.failures.length > 0) {
36
+ this.log("\nFailed cases:");
37
+ for (const f of result.failures)
38
+ this.log(`- ${f.label}${f.detail ? `\n ${f.detail}` : ""}`);
39
+ }
40
+ if (result.lines.length > 0)
41
+ this.log(result.ok
42
+ ? "\nAccess rules passed. Rule text: https://github.com/apsoai/cli/issues/208"
43
+ : "\nAccess rules FAILED.");
44
+ }
45
+ if (!result.ok)
46
+ this.exit(1);
47
+ }
48
+ }
49
+ TestAccess.description = "Start the service and prove the access rules on this project's own schema: who may call, tenant scope (scopeBy), permissions, API keys and MCP tools. Test sign-ins are signed by a local issuer with the claim names from the auth block; the service is pointed at it through AUTH_JWT_ISSUER and AUTH_JWKS_URI for this run only. Rows are created for two test tenants and deleted afterwards. TypeScript services run on a local PGlite database; Python and Go services run on the database in DATABASE_URL. Prints pass or fail per rule and exits 1 on any failure";
50
+ TestAccess.examples = [`$ apso test access`, `$ apso test access --json`];
51
+ TestAccess.flags = {
52
+ "skip-build": core_1.Flags.boolean({
53
+ description: "Use the existing dist/ build instead of building first (TypeScript)",
54
+ default: false,
55
+ }),
56
+ verbose: core_1.Flags.boolean({
57
+ description: "Print every case, not only the failures",
58
+ default: false,
59
+ }),
60
+ json: core_1.Flags.boolean({
61
+ description: "Print the result as JSON",
62
+ default: false,
63
+ }),
64
+ };
65
+ exports.default = TestAccess;
@@ -0,0 +1,120 @@
1
+ /**
2
+ * The access rule cases that follow from a schema: given the entities, their
3
+ * scopeBy, permissions and mcp settings, and a running service, they seed two
4
+ * tenants and check the rules in rules.json over HTTP only. The same cases
5
+ * run in CI against a generated service in each language
6
+ * (scripts/auth-smoke.mjs) and on an owner's own project (`apso test access`).
7
+ */
8
+ import { Entity, EntityPermissions } from "../types/entity";
9
+ import { RelationshipMap } from "../types/relationship";
10
+ import { Checker, Language } from "./report";
11
+ import { Caller } from "./tokens";
12
+ type Headers = Record<string, string>;
13
+ type Row = Record<string, any>;
14
+ export interface Res {
15
+ status: number;
16
+ text: string;
17
+ json?: any;
18
+ /** The Content-Range response header, when the service sent one. */
19
+ range?: string;
20
+ }
21
+ export declare function call(method: string, url: string, { headers, body }?: {
22
+ headers?: Headers;
23
+ body?: unknown;
24
+ }): Promise<Res>;
25
+ export interface AccessEntity {
26
+ name: string;
27
+ route: string;
28
+ fields: NonNullable<Entity["fields"]>;
29
+ payload: Row;
30
+ /** Required foreign keys: property -> the entity it points to. */
31
+ parents: {
32
+ property: string;
33
+ entity: string;
34
+ }[];
35
+ /** Relations a `join` can name: property -> the entity it loads, and for a many-to-one its foreign key. */
36
+ relations: {
37
+ property: string;
38
+ entity: string;
39
+ fk?: string;
40
+ }[];
41
+ /**
42
+ * The column a caller's rows are limited by: the scopeBy field, or for a
43
+ * path through a parent (project.organizationId) the foreign key to it.
44
+ */
45
+ scope?: {
46
+ field: string;
47
+ parent?: string;
48
+ bypassRoles: string[];
49
+ };
50
+ permissions?: EntityPermissions;
51
+ /** MCP tool names by operation, for the operations the schema opts in. */
52
+ tools: {
53
+ list?: string;
54
+ get?: string;
55
+ create?: string;
56
+ update?: string;
57
+ delete?: string;
58
+ };
59
+ }
60
+ export interface AccessModel {
61
+ language: Language;
62
+ entities: AccessEntity[];
63
+ /** Entities the cases cannot cover, and why. */
64
+ skipped: {
65
+ name: string;
66
+ reason: string;
67
+ }[];
68
+ /** The entity whose id is the tenant id, when scopeBy fields point at one. */
69
+ tenantRoot?: string;
70
+ /** The entity whose rows grant permissions at runtime, when the schema has one. */
71
+ grants?: string;
72
+ }
73
+ /**
74
+ * Describe a schema for the cases.
75
+ * @param {Entity[]} entities - The .apsorc entities
76
+ * @param {RelationshipMap} relationships - The parsed relationships
77
+ * @param {Language} language - The service's language
78
+ * @param {object} [auth] - The .apsorc auth block, if any
79
+ * @returns {AccessModel} The schema as the cases use it
80
+ */
81
+ export declare function buildModel(entities: Entity[], relationships: RelationshipMap, language: Language, auth?: {
82
+ permissionEntity?: string;
83
+ }): AccessModel;
84
+ export interface McpClient {
85
+ listTools(): Promise<string[]>;
86
+ /** The tool's JSON answer, or undefined with `error` set when the call is refused. */
87
+ callTool(name: string, args: Row): Promise<{
88
+ json?: any;
89
+ error?: string;
90
+ }>;
91
+ }
92
+ export interface AccessContext {
93
+ base: string;
94
+ model: AccessModel;
95
+ t: Checker;
96
+ /** A valid API key. The cases seed both tenants with it and read back what was stored. */
97
+ key: Headers;
98
+ /** Headers for a signed-in user. Leave out when the service has no sign-in. */
99
+ user?: (caller?: Caller) => Headers;
100
+ /** True when a user token can carry roles. */
101
+ roles?: boolean;
102
+ /** Tokens the service must refuse. */
103
+ badTokens?: {
104
+ label: string;
105
+ headers: Headers;
106
+ }[];
107
+ /** Connect to the service's MCP endpoint with these headers. Leave out to skip the MCP cases. */
108
+ mcp?: (headers: Headers) => Promise<McpClient>;
109
+ log?: (line: string) => void;
110
+ /** Told about answers that are not leaks but are still wrong, such as a 500 for a query the service should refuse. */
111
+ warn?: (line: string) => void;
112
+ }
113
+ /**
114
+ * Run every case the schema calls for against the service at ctx.base.
115
+ * The service must be running with the API key in ctx.key accepted.
116
+ * @param {AccessContext} ctx - The service, its schema and how to call it
117
+ * @returns {Promise<void>} Resolves when the cases have run; results are on ctx.t
118
+ */
119
+ export declare function runAccessCases(ctx: AccessContext): Promise<void>;
120
+ export {};