@ankhorage/projects 0.4.0 → 0.5.0
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.
- package/README.md +2 -1
- package/dist/capabilities/index.d.ts +20 -0
- package/dist/capabilities/index.js +16 -0
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixHost.d.ts +30 -0
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixHost.js +156 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +35 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
# @ankhorage/projects
|
|
5
5
|
|
|
6
|
-
         
|
|
7
7
|
|
|
8
8
|
Standalone project identity and repository actions.
|
|
9
9
|
|
|
@@ -16,3 +16,4 @@ Standalone project identity and repository actions.
|
|
|
16
16
|
- [Module relationships](././paradox/diagrams/module-relationships.mmd)
|
|
17
17
|
- [Export graph](././paradox/diagrams/export-graph.mmd)
|
|
18
18
|
- [createProjectRepositoryFileSystemAsync sequence](././paradox/diagrams/sequences/create-project-repository-file-system-async.mmd)
|
|
19
|
+
- [createTrustedZefixHost sequence](././paradox/diagrams/sequences/create-trusted-zefix-host.mmd)
|
|
@@ -89,4 +89,24 @@ export declare const CAPABILITIES: readonly [{
|
|
|
89
89
|
};
|
|
90
90
|
readonly label: "Delete project";
|
|
91
91
|
readonly description: "Delete one persisted project by identity.";
|
|
92
|
+
}, {
|
|
93
|
+
readonly id: "projects.zefix.search";
|
|
94
|
+
readonly owner: "@ankhorage/projects";
|
|
95
|
+
readonly access: readonly ["read"];
|
|
96
|
+
readonly binding: {
|
|
97
|
+
readonly kind: "action";
|
|
98
|
+
readonly bindableAs: readonly ["target"];
|
|
99
|
+
};
|
|
100
|
+
readonly label: "Search Zefix companies";
|
|
101
|
+
readonly description: "Search curated Zefix company data through a trusted project host.";
|
|
102
|
+
}, {
|
|
103
|
+
readonly id: "projects.zefix.get-by-uid";
|
|
104
|
+
readonly owner: "@ankhorage/projects";
|
|
105
|
+
readonly access: readonly ["read"];
|
|
106
|
+
readonly binding: {
|
|
107
|
+
readonly kind: "action";
|
|
108
|
+
readonly bindableAs: readonly ["target"];
|
|
109
|
+
};
|
|
110
|
+
readonly label: "Read a Zefix company by UID";
|
|
111
|
+
readonly description: "Read curated Zefix company data through a trusted project host.";
|
|
92
112
|
}];
|
|
@@ -72,4 +72,20 @@ export const CAPABILITIES = [
|
|
|
72
72
|
label: 'Delete project',
|
|
73
73
|
description: 'Delete one persisted project by identity.',
|
|
74
74
|
},
|
|
75
|
+
{
|
|
76
|
+
id: 'projects.zefix.search',
|
|
77
|
+
owner: '@ankhorage/projects',
|
|
78
|
+
access: ['read'],
|
|
79
|
+
binding: { kind: 'action', bindableAs: ['target'] },
|
|
80
|
+
label: 'Search Zefix companies',
|
|
81
|
+
description: 'Search curated Zefix company data through a trusted project host.',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: 'projects.zefix.get-by-uid',
|
|
85
|
+
owner: '@ankhorage/projects',
|
|
86
|
+
access: ['read'],
|
|
87
|
+
binding: { kind: 'action', bindableAs: ['target'] },
|
|
88
|
+
label: 'Read a Zefix company by UID',
|
|
89
|
+
description: 'Read curated Zefix company data through a trusted project host.',
|
|
90
|
+
},
|
|
75
91
|
];
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type ApiRequest, type ComposeAuthorizedApiRuntimeOptions } from '@ankhorage/api';
|
|
2
|
+
import { type FastifyInstance, type RegisterFastifyApiOptions } from '@ankhorage/api-fastify';
|
|
3
|
+
import type { InternalRestApiDefinition } from '@ankhorage/contracts/data';
|
|
4
|
+
import { type EndpointTestFetch } from '@ankhorage/data-sources';
|
|
5
|
+
import type { SecretStoreAdapter } from '@ankhorage/secrets/port';
|
|
6
|
+
type TrustedZefixOperation = 'company.search' | 'company.get-by-uid';
|
|
7
|
+
export type CreateTrustedZefixHostInput = Readonly<{
|
|
8
|
+
appId: string;
|
|
9
|
+
projectId: string;
|
|
10
|
+
environment: string;
|
|
11
|
+
definition: InternalRestApiDefinition;
|
|
12
|
+
operations: Readonly<Record<string, TrustedZefixOperation>>;
|
|
13
|
+
authenticateCallerAsync: (request: ApiRequest) => Promise<unknown>;
|
|
14
|
+
authorizeCallerAsync: (input: Readonly<{
|
|
15
|
+
caller: unknown;
|
|
16
|
+
appId: string;
|
|
17
|
+
projectId: string;
|
|
18
|
+
environment: string;
|
|
19
|
+
operationId: string;
|
|
20
|
+
}>) => Promise<boolean>;
|
|
21
|
+
secretStore: SecretStoreAdapter;
|
|
22
|
+
fetch?: EndpointTestFetch;
|
|
23
|
+
}>;
|
|
24
|
+
export type TrustedZefixHost = Readonly<{
|
|
25
|
+
composition: ComposeAuthorizedApiRuntimeOptions;
|
|
26
|
+
registerFastifyAsync: (server: FastifyInstance, options?: RegisterFastifyApiOptions) => Promise<unknown>;
|
|
27
|
+
}>;
|
|
28
|
+
/*** Compose declared Zefix operations into an authenticated, project-scoped trusted host. */
|
|
29
|
+
export declare function createTrustedZefixHost(input: CreateTrustedZefixHostInput): TrustedZefixHost;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import {} from '@ankhorage/api';
|
|
2
|
+
import { registerAuthorizedFastifyApiAsync, } from '@ankhorage/api-fastify';
|
|
3
|
+
import { createZefixPublicRestApi, mapZefixCompanyResult, } from '@ankhorage/data-sources';
|
|
4
|
+
import { createRuntimeApiCapabilityExecutor } from '@ankhorage/runtime/api-capabilities';
|
|
5
|
+
import { isRecord } from '@ankhorage/utility/object';
|
|
6
|
+
import { CAPABILITIES } from '../../../capabilities/index.js';
|
|
7
|
+
/*** Compose declared Zefix operations into an authenticated, project-scoped trusted host. */
|
|
8
|
+
export function createTrustedZefixHost(input) {
|
|
9
|
+
assertHostIdentity(input);
|
|
10
|
+
const executor = createRuntimeApiCapabilityExecutor({
|
|
11
|
+
apis: { zefix: createZefixPublicRestApi() },
|
|
12
|
+
credentialResolver: createZefixCredentialResolver(input),
|
|
13
|
+
...(input.fetch === undefined ? {} : { fetch: input.fetch }),
|
|
14
|
+
});
|
|
15
|
+
const composition = {
|
|
16
|
+
definition: input.definition,
|
|
17
|
+
context: { appId: input.appId, projectId: input.projectId },
|
|
18
|
+
authorizeAsync: async ({ binding, request }) => authorizeRequestAsync(input, binding.operationId, request),
|
|
19
|
+
resolveHandler: ({ binding, declaration, context }) => {
|
|
20
|
+
const operation = input.operations[binding.operationId];
|
|
21
|
+
const capability = resolveCapability(operation);
|
|
22
|
+
const { method } = binding.operation;
|
|
23
|
+
if (!operation ||
|
|
24
|
+
declaration.capability !== capability?.id ||
|
|
25
|
+
binding.operation.intent !== 'read' ||
|
|
26
|
+
method === undefined ||
|
|
27
|
+
!isCanonicalOperationMethod(operation, method))
|
|
28
|
+
return undefined;
|
|
29
|
+
return {
|
|
30
|
+
capability,
|
|
31
|
+
approval: {
|
|
32
|
+
operationId: binding.operationId,
|
|
33
|
+
intent: binding.operation.intent,
|
|
34
|
+
method,
|
|
35
|
+
capabilityId: capability.id,
|
|
36
|
+
owner: capability.owner,
|
|
37
|
+
scope: context,
|
|
38
|
+
},
|
|
39
|
+
handler: async (request) => executeZefixOperationAsync(executor, operation, request),
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
composition,
|
|
45
|
+
registerFastifyAsync: (server, options) => registerAuthorizedFastifyApiAsync(server, composition, options),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/*** Bind each exposed host declaration to the one canonical Zefix HTTP method. */
|
|
49
|
+
function isCanonicalOperationMethod(operation, method) {
|
|
50
|
+
return ((operation === 'company.search' && method === 'POST') ||
|
|
51
|
+
(operation === 'company.get-by-uid' && method === 'GET'));
|
|
52
|
+
}
|
|
53
|
+
/*** Deny before the operation handler can resolve a Vault secret or contact Zefix. */
|
|
54
|
+
async function authorizeRequestAsync(input, operationId, request) {
|
|
55
|
+
try {
|
|
56
|
+
const caller = await input.authenticateCallerAsync(request);
|
|
57
|
+
return caller === undefined
|
|
58
|
+
? false
|
|
59
|
+
: input.authorizeCallerAsync({
|
|
60
|
+
caller,
|
|
61
|
+
appId: input.appId,
|
|
62
|
+
projectId: input.projectId,
|
|
63
|
+
environment: input.environment,
|
|
64
|
+
operationId,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/*** Resolve Basic credentials only for the canonical Zefix reference and immutable server scope. */
|
|
72
|
+
function createZefixCredentialResolver(input) {
|
|
73
|
+
return async (credential) => {
|
|
74
|
+
if (credential.id !== 'zefix-credentials' || credential.kind !== 'basic')
|
|
75
|
+
return undefined;
|
|
76
|
+
const result = await input.secretStore.resolve({
|
|
77
|
+
scope: { projectId: input.projectId, environment: input.environment },
|
|
78
|
+
ref: 'zefix-credentials',
|
|
79
|
+
});
|
|
80
|
+
const username = result.ok ? result.data.username : undefined;
|
|
81
|
+
const password = result.ok ? result.data.password : undefined;
|
|
82
|
+
return typeof username === 'string' && typeof password === 'string'
|
|
83
|
+
? { headers: { authorization: `Basic ${btoa(`${username}:${password}`)}` } }
|
|
84
|
+
: undefined;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/*** Execute only an approved Zefix operation and return the curated company projection. */
|
|
88
|
+
async function executeZefixOperationAsync(executor, operation, request) {
|
|
89
|
+
const input = operation === 'company.search' ? request.body : { id: request.params.id };
|
|
90
|
+
const values = isHostBindingRecord(input) ? input : undefined;
|
|
91
|
+
const result = await executor({
|
|
92
|
+
capability: `api.zefix.${operation}`,
|
|
93
|
+
...(values === undefined ? {} : { input: values }),
|
|
94
|
+
});
|
|
95
|
+
if (!result.ok)
|
|
96
|
+
return zefixUnavailableResponse();
|
|
97
|
+
const companies = curateZefixResult(result.data);
|
|
98
|
+
return companies === undefined ? zefixUnavailableResponse() : { body: companies };
|
|
99
|
+
}
|
|
100
|
+
/*** Strip provider wire payloads before any Zefix result crosses the trusted host boundary. */
|
|
101
|
+
function curateZefixResult(value) {
|
|
102
|
+
if (!Array.isArray(value))
|
|
103
|
+
return undefined;
|
|
104
|
+
const mapped = value.map((candidate) => {
|
|
105
|
+
const company = asZefixCompany(candidate);
|
|
106
|
+
return company ? mapZefixCompanyResult(company) : undefined;
|
|
107
|
+
});
|
|
108
|
+
return mapped.every((result) => result !== undefined)
|
|
109
|
+
? mapped.map((result) => result.company)
|
|
110
|
+
: undefined;
|
|
111
|
+
}
|
|
112
|
+
/*** Return the one credential-safe failure representation for Vault or provider failures. */
|
|
113
|
+
function zefixUnavailableResponse() {
|
|
114
|
+
return { status: 502, body: { error: { code: 'zefix_unavailable' } } };
|
|
115
|
+
}
|
|
116
|
+
/*** Narrow unknown endpoint data to the optional-field Zefix short-company DTO. */
|
|
117
|
+
function asZefixCompany(value) {
|
|
118
|
+
return isRecord(value) &&
|
|
119
|
+
optionalString(value.name) &&
|
|
120
|
+
optionalString(value.uid) &&
|
|
121
|
+
optionalString(value.chid) &&
|
|
122
|
+
optionalString(value.legalSeat)
|
|
123
|
+
? value
|
|
124
|
+
: undefined;
|
|
125
|
+
}
|
|
126
|
+
/*** Accept missing or string wire fields while rejecting non-serializable provider data. */
|
|
127
|
+
function optionalString(value) {
|
|
128
|
+
return value === undefined || typeof value === 'string';
|
|
129
|
+
}
|
|
130
|
+
/*** Admit only serializable operation input before passing it to Runtime's binding executor. */
|
|
131
|
+
function isHostBindingRecord(value) {
|
|
132
|
+
return isRecord(value) && Object.values(value).every(isHostBindingValue);
|
|
133
|
+
}
|
|
134
|
+
/*** Recursively reject host objects and functions from untrusted request input. */
|
|
135
|
+
function isHostBindingValue(value) {
|
|
136
|
+
return (value === null ||
|
|
137
|
+
typeof value === 'string' ||
|
|
138
|
+
typeof value === 'number' ||
|
|
139
|
+
typeof value === 'boolean' ||
|
|
140
|
+
(Array.isArray(value) && value.every(isHostBindingValue)) ||
|
|
141
|
+
(isRecord(value) && Object.values(value).every(isHostBindingValue)));
|
|
142
|
+
}
|
|
143
|
+
/*** Resolve the one Projects-owned capability permitted for each declared Zefix operation. */
|
|
144
|
+
function resolveCapability(operation) {
|
|
145
|
+
const id = operation === 'company.search'
|
|
146
|
+
? 'projects.zefix.search'
|
|
147
|
+
: operation === 'company.get-by-uid'
|
|
148
|
+
? 'projects.zefix.get-by-uid'
|
|
149
|
+
: undefined;
|
|
150
|
+
return CAPABILITIES.find((candidate) => candidate.id === id);
|
|
151
|
+
}
|
|
152
|
+
/*** Reject incomplete immutable host configuration before Fastify routes are registered. */
|
|
153
|
+
function assertHostIdentity(input) {
|
|
154
|
+
if (!input.appId.trim() || !input.projectId.trim() || !input.environment.trim())
|
|
155
|
+
throw new Error('Trusted Zefix host requires immutable app, project, and environment identities.');
|
|
156
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export { projects } from './features/projects/application/projects.js';
|
|
|
3
3
|
export { createExpoProjectMaterializer, type CreateExpoProjectMaterializerInput, type ExpoProjectMaterializationResult, type ExpoProjectMaterializer, } from './features/projects/composition/createExpoProjectMaterializer.js';
|
|
4
4
|
export { createProjectsApiHandlerResolver } from './features/projects/composition/createProjectsApiHandlerResolver.js';
|
|
5
5
|
export type { ProjectMaterializer, ProjectRepository, } from './features/projects/ports/ProjectRepository.js';
|
|
6
|
+
export { createTrustedZefixHost, type CreateTrustedZefixHostInput, type TrustedZefixHost, } from './features/trusted-zefix-host/composition/createTrustedZefixHost.js';
|
|
6
7
|
export type { CreateProjectInput, MaterializeProjectRevisionInput, Project, ProjectManifest, ProjectManifestDraft, ProjectManifestRevision, ReloadProjectManifestInput, SaveProjectManifestInput, UpdateProjectInput, } from './types/projects.js';
|
package/dist/index.js
CHANGED
|
@@ -2,3 +2,4 @@ export { createProjectRepositoryFileSystemAsync } from './features/projects/adap
|
|
|
2
2
|
export { projects } from './features/projects/application/projects.js';
|
|
3
3
|
export { createExpoProjectMaterializer, } from './features/projects/composition/createExpoProjectMaterializer.js';
|
|
4
4
|
export { createProjectsApiHandlerResolver } from './features/projects/composition/createProjectsApiHandlerResolver.js';
|
|
5
|
+
export { createTrustedZefixHost, } from './features/trusted-zefix-host/composition/createTrustedZefixHost.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ankhorage/projects",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Standalone project identity and repository actions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ankhorage",
|
|
@@ -62,10 +62,14 @@
|
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
64
|
"@ankhorage/api": "^0.5.1",
|
|
65
|
+
"@ankhorage/api-fastify": "^0.4.0",
|
|
65
66
|
"@ankhorage/contracts": "^26.11.1",
|
|
67
|
+
"@ankhorage/data-sources": "^2.4.0",
|
|
66
68
|
"@ankhorage/expo-runtime": "^7.4.3",
|
|
67
69
|
"@ankhorage/navigator": "^3.5.22",
|
|
68
70
|
"@ankhorage/permissions": "^0.2.64",
|
|
71
|
+
"@ankhorage/runtime": "^5.6.4",
|
|
72
|
+
"@ankhorage/secrets": "^0.3.2",
|
|
69
73
|
"@ankhorage/templates": "^12.6.30",
|
|
70
74
|
"@ankhorage/utility": "^1.10.0"
|
|
71
75
|
},
|
|
@@ -206,6 +210,36 @@
|
|
|
206
210
|
},
|
|
207
211
|
"label": "Delete project",
|
|
208
212
|
"description": "Delete one persisted project by identity."
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
"id": "projects.zefix.search",
|
|
216
|
+
"owner": "@ankhorage/projects",
|
|
217
|
+
"access": [
|
|
218
|
+
"read"
|
|
219
|
+
],
|
|
220
|
+
"binding": {
|
|
221
|
+
"kind": "action",
|
|
222
|
+
"bindableAs": [
|
|
223
|
+
"target"
|
|
224
|
+
]
|
|
225
|
+
},
|
|
226
|
+
"label": "Search Zefix companies",
|
|
227
|
+
"description": "Search curated Zefix company data through a trusted project host."
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
"id": "projects.zefix.get-by-uid",
|
|
231
|
+
"owner": "@ankhorage/projects",
|
|
232
|
+
"access": [
|
|
233
|
+
"read"
|
|
234
|
+
],
|
|
235
|
+
"binding": {
|
|
236
|
+
"kind": "action",
|
|
237
|
+
"bindableAs": [
|
|
238
|
+
"target"
|
|
239
|
+
]
|
|
240
|
+
},
|
|
241
|
+
"label": "Read a Zefix company by UID",
|
|
242
|
+
"description": "Read curated Zefix company data through a trusted project host."
|
|
209
243
|
}
|
|
210
244
|
]
|
|
211
245
|
}
|