@cap-js/ord 1.4.2 → 1.4.3
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 +46 -16
- package/lib/auth/cf-mtls.js +36 -11
- package/lib/build.js +43 -47
- package/lib/constants.js +3 -0
- package/lib/metaData.js +23 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -114,34 +114,64 @@ This will output something like `admin:$2y$05$...` - use only the hash part (sta
|
|
|
114
114
|
|
|
115
115
|
#### CF mTLS Authentication
|
|
116
116
|
|
|
117
|
-
Configure Cloud Foundry mutual TLS authentication
|
|
117
|
+
Configure Cloud Foundry mutual TLS authentication for SAP BTP Cloud Foundry environments.
|
|
118
|
+
|
|
119
|
+
**Production Configuration with UCL (Recommended)**
|
|
120
|
+
|
|
121
|
+
For SAP UCL (Unified Customer Landscape) integration, enable mTLS in `.cdsrc.json` and configure UCL endpoints via environment variable:
|
|
118
122
|
|
|
119
123
|
```json
|
|
120
124
|
{
|
|
121
|
-
"
|
|
122
|
-
"
|
|
123
|
-
"
|
|
124
|
-
"cfMtls": {
|
|
125
|
-
"certs": [
|
|
126
|
-
{
|
|
127
|
-
"issuer": "CN=SAP PKI Certificate Service Client CA,OU=SAP BTP Clients,O=SAP SE,C=DE",
|
|
128
|
-
"subject": "CN=my-service,OU=SAP Cloud Platform Clients,O=SAP SE,C=DE"
|
|
129
|
-
}
|
|
130
|
-
],
|
|
131
|
-
"rootCaDn": ["CN=SAP Cloud Root CA,O=SAP SE,C=DE"]
|
|
132
|
-
}
|
|
133
|
-
}
|
|
125
|
+
"ord": {
|
|
126
|
+
"authentication": {
|
|
127
|
+
"cfMtls": true
|
|
134
128
|
}
|
|
135
129
|
}
|
|
136
130
|
}
|
|
137
131
|
```
|
|
138
132
|
|
|
139
|
-
|
|
133
|
+
```bash
|
|
134
|
+
export CF_MTLS_TRUSTED_CERTS='{
|
|
135
|
+
"configEndpoints": ["https://your-ucl-endpoint/v1/info"],
|
|
136
|
+
"rootCaDn": ["CN=SAP Cloud Root CA,O=SAP SE,L=Walldorf,C=DE"]
|
|
137
|
+
}'
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Production Configuration with Custom Certificates**
|
|
141
|
+
|
|
142
|
+
For custom certificates without UCL:
|
|
140
143
|
|
|
141
144
|
```bash
|
|
142
|
-
CF_MTLS_TRUSTED_CERTS='{
|
|
145
|
+
export CF_MTLS_TRUSTED_CERTS='{
|
|
146
|
+
"certs": [{"issuer": "CN=My CA,O=MyOrg", "subject": "CN=my-service,O=MyOrg"}],
|
|
147
|
+
"rootCaDn": ["CN=My Root CA,O=MyOrg"]
|
|
148
|
+
}'
|
|
143
149
|
```
|
|
144
150
|
|
|
151
|
+
**Development Configuration**
|
|
152
|
+
|
|
153
|
+
For local development, configure the full mTLS settings directly in `.cdsrc.json`:
|
|
154
|
+
|
|
155
|
+
```json
|
|
156
|
+
{
|
|
157
|
+
"ord": {
|
|
158
|
+
"authentication": {
|
|
159
|
+
"cfMtls": {
|
|
160
|
+
"certs": [
|
|
161
|
+
{
|
|
162
|
+
"issuer": "CN=Test CA,O=MyOrg,C=DE",
|
|
163
|
+
"subject": "CN=test-client,O=MyOrg,C=DE"
|
|
164
|
+
}
|
|
165
|
+
],
|
|
166
|
+
"rootCaDn": ["CN=Test Root CA,O=MyOrg,C=DE"]
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
> **Note:** For detailed CF mTLS configuration options, see the [documentation](./docs/ord.md#cf-mtls-authentication).
|
|
174
|
+
|
|
145
175
|
#### Multiple Authentication Strategies
|
|
146
176
|
|
|
147
177
|
You can configure multiple authentication methods simultaneously to support different client types. Authentication types are detected automatically based on configuration presence:
|
package/lib/auth/cf-mtls.js
CHANGED
|
@@ -179,26 +179,51 @@ async function createCfMtlsConfig(cds, Logger) {
|
|
|
179
179
|
Logger.error("CF mTLS requires CF_INSTANCE_GUID environment variable");
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
//
|
|
182
|
+
// Configuration priority:
|
|
183
|
+
// 1. cfMtls: true - Production mode, requires CF_MTLS_TRUSTED_CERTS env var
|
|
184
|
+
// 2. cfMtls: { ... } - Development mode, uses inline config from .cdsrc.json
|
|
185
|
+
// Note: Environment variable alone is NOT sufficient, explicit .cdsrc.json declaration required
|
|
186
|
+
|
|
183
187
|
let config;
|
|
188
|
+
const cdsConfig = cds.env.ord?.authentication?.cfMtls;
|
|
189
|
+
|
|
190
|
+
// cfMtls must be explicitly configured in .cdsrc.json
|
|
191
|
+
if (!cdsConfig) {
|
|
192
|
+
Logger.error("CF mTLS configuration required. Set ord.authentication.cfMtls in .cdsrc.json");
|
|
193
|
+
return {
|
|
194
|
+
error: "CF mTLS configuration required. Set ord.authentication.cfMtls in .cdsrc.json",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
184
197
|
|
|
185
|
-
|
|
198
|
+
// Production: cfMtls: true → requires CF_MTLS_TRUSTED_CERTS environment variable
|
|
199
|
+
if (cdsConfig === true) {
|
|
200
|
+
if (!process.env.CF_MTLS_TRUSTED_CERTS) {
|
|
201
|
+
Logger.error(
|
|
202
|
+
"CF mTLS enabled with cfMtls: true but CF_MTLS_TRUSTED_CERTS environment variable is not set. " +
|
|
203
|
+
"Set CF_MTLS_TRUSTED_CERTS with JSON: {certs: [...], rootCaDn: [...]} or {configEndpoints: [...], rootCaDn: [...]}",
|
|
204
|
+
);
|
|
205
|
+
return {
|
|
206
|
+
error: "CF_MTLS_TRUSTED_CERTS environment variable required when cfMtls is set to true",
|
|
207
|
+
};
|
|
208
|
+
}
|
|
186
209
|
try {
|
|
187
210
|
config = JSON.parse(process.env.CF_MTLS_TRUSTED_CERTS);
|
|
188
211
|
} catch {
|
|
189
|
-
Logger.error("Failed to parse CF_MTLS_TRUSTED_CERTS");
|
|
212
|
+
Logger.error("Failed to parse CF_MTLS_TRUSTED_CERTS environment variable");
|
|
190
213
|
return {
|
|
191
214
|
error: "Invalid CF_MTLS_TRUSTED_CERTS format. Expected JSON: {certs: [...], rootCaDn: [...], configEndpoints: [...]}",
|
|
192
215
|
};
|
|
193
216
|
}
|
|
194
|
-
} else {
|
|
195
|
-
config = cds.env.ord?.authentication?.cfMtls;
|
|
196
217
|
}
|
|
197
|
-
|
|
198
|
-
if (
|
|
199
|
-
|
|
218
|
+
// Development: cfMtls: { ... } → use inline config from .cdsrc.json
|
|
219
|
+
else if (typeof cdsConfig === "object") {
|
|
220
|
+
config = cdsConfig;
|
|
221
|
+
}
|
|
222
|
+
// Invalid configuration type
|
|
223
|
+
else {
|
|
224
|
+
Logger.error("Invalid cfMtls configuration. Expected true or object with certs/rootCaDn/configEndpoints");
|
|
200
225
|
return {
|
|
201
|
-
error: "
|
|
226
|
+
error: "Invalid cfMtls configuration. Expected true or object",
|
|
202
227
|
};
|
|
203
228
|
}
|
|
204
229
|
|
|
@@ -268,7 +293,7 @@ async function createCfMtlsConfig(cds, Logger) {
|
|
|
268
293
|
if (certs.length === 0) {
|
|
269
294
|
Logger.error(
|
|
270
295
|
"CF mTLS requires at least one certificate pair. " +
|
|
271
|
-
"Provide via certs array or configEndpoints in CF_MTLS_TRUSTED_CERTS or
|
|
296
|
+
"Provide via certs array or configEndpoints in CF_MTLS_TRUSTED_CERTS or ord.authentication.cfMtls",
|
|
272
297
|
);
|
|
273
298
|
return {
|
|
274
299
|
error: "CF mTLS requires at least one certificate pair",
|
|
@@ -278,7 +303,7 @@ async function createCfMtlsConfig(cds, Logger) {
|
|
|
278
303
|
if (rootCaDn.length === 0) {
|
|
279
304
|
Logger.error(
|
|
280
305
|
"CF mTLS requires at least one root CA. " +
|
|
281
|
-
"Provide via rootCaDn array in CF_MTLS_TRUSTED_CERTS or
|
|
306
|
+
"Provide via rootCaDn array in CF_MTLS_TRUSTED_CERTS or ord.authentication.cfMtls",
|
|
282
307
|
);
|
|
283
308
|
return {
|
|
284
309
|
error: "CF mTLS requires at least one root CA",
|
package/lib/build.js
CHANGED
|
@@ -6,6 +6,8 @@ const cliProgress = require("cli-progress");
|
|
|
6
6
|
const { BUILD_DEFAULT_PATH, ORD_SERVICE_NAME, ORD_DOCUMENT_FILE_NAME } = require("./constants");
|
|
7
7
|
const { isMCPPluginInPackageJson } = require("./mcpAdapter");
|
|
8
8
|
|
|
9
|
+
const { BuildError } = cds.build;
|
|
10
|
+
|
|
9
11
|
module.exports = class OrdBuildPlugin extends cds.build.Plugin {
|
|
10
12
|
static taskDefaults = { src: cds.env.folders.srv };
|
|
11
13
|
|
|
@@ -15,54 +17,46 @@ module.exports = class OrdBuildPlugin extends cds.build.Plugin {
|
|
|
15
17
|
}
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
let totalFiles = resObj.reduce((total, resource) => {
|
|
20
|
-
if (!resource.ordId.includes(ORD_SERVICE_NAME) && resource.resourceDefinitions) {
|
|
21
|
-
return total + resource.resourceDefinitions.length;
|
|
22
|
-
}
|
|
23
|
-
return total;
|
|
24
|
-
}, 0);
|
|
25
|
-
|
|
20
|
+
_createProgressBar(totalFiles) {
|
|
26
21
|
const progressBar = new cliProgress.SingleBar({
|
|
27
22
|
format: "Processing resourcesFiles [{bar}] {percentage}% | {value}/{total} | ETA: {eta}s",
|
|
28
23
|
barCompleteChar: "█",
|
|
29
24
|
barIncompleteChar: "░",
|
|
30
25
|
stopOnComplete: true,
|
|
31
26
|
});
|
|
27
|
+
progressBar.start(totalFiles, 0);
|
|
28
|
+
return progressBar;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_countTotalFiles(resObj) {
|
|
32
|
+
return resObj.reduce((total, resource) => {
|
|
33
|
+
if (!resource.ordId.includes(ORD_SERVICE_NAME) && resource.resourceDefinitions) {
|
|
34
|
+
return total + resource.resourceDefinitions.length;
|
|
35
|
+
}
|
|
36
|
+
return total;
|
|
37
|
+
}, 0);
|
|
38
|
+
}
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
async _writeResourcesFiles(resObj, model, promises) {
|
|
41
|
+
const totalFiles = this._countTotalFiles(resObj);
|
|
42
|
+
const progressBar = this._createProgressBar(totalFiles);
|
|
34
43
|
let completed = 0;
|
|
35
|
-
progressBar.start(totalFiles, 0);
|
|
36
44
|
|
|
37
45
|
try {
|
|
38
46
|
for (const resource of resObj) {
|
|
39
|
-
// Generate if has service definitions OR has MCP plugin
|
|
40
47
|
const shouldGenerate = resource.resourceDefinitions || isMCPPluginInPackageJson();
|
|
41
48
|
if (!shouldGenerate) continue;
|
|
49
|
+
|
|
42
50
|
for (const resourceDefinition of resource.resourceDefinitions) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
this.write(response)
|
|
50
|
-
.to(fileName)
|
|
51
|
-
.catch((err) => {
|
|
52
|
-
warnings.push(`Error writing file ${fileName}: ${err.message}`);
|
|
53
|
-
}),
|
|
54
|
-
);
|
|
55
|
-
} catch (error) {
|
|
56
|
-
warnings.push(`Error getting metadata for ${resourceDefinition.url}: ${error.message}`);
|
|
57
|
-
}
|
|
58
|
-
completed++;
|
|
59
|
-
progressBar.update(completed);
|
|
51
|
+
const { _, response } = await getMetadata(resourceDefinition.url, model); // eslint-disable-line no-unused-vars
|
|
52
|
+
const fileName = path
|
|
53
|
+
.join(resource.ordId, resourceDefinition.url.split("/").pop())
|
|
54
|
+
.replace(/:/g, "_");
|
|
55
|
+
promises.push(this.write(response).to(fileName));
|
|
56
|
+
progressBar.update(++completed);
|
|
60
57
|
}
|
|
61
58
|
}
|
|
62
59
|
await Promise.all(promises);
|
|
63
|
-
} catch (error) {
|
|
64
|
-
warnings.push("Failed to process resources: " + error.message);
|
|
65
|
-
throw error;
|
|
66
60
|
} finally {
|
|
67
61
|
progressBar.stop();
|
|
68
62
|
}
|
|
@@ -74,9 +68,7 @@ module.exports = class OrdBuildPlugin extends cds.build.Plugin {
|
|
|
74
68
|
for (const resource of resources || []) {
|
|
75
69
|
if (resource.resourceDefinitions) {
|
|
76
70
|
for (const resourceDefinition of resource.resourceDefinitions) {
|
|
77
|
-
|
|
78
|
-
url = this._createRelativePath(url);
|
|
79
|
-
resourceDefinition.url = url;
|
|
71
|
+
resourceDefinition.url = this._createRelativePath(resourceDefinition.url);
|
|
80
72
|
}
|
|
81
73
|
}
|
|
82
74
|
}
|
|
@@ -91,25 +83,29 @@ module.exports = class OrdBuildPlugin extends cds.build.Plugin {
|
|
|
91
83
|
_createRelativePath(url) {
|
|
92
84
|
let relative = url.split("/ord/v1").pop();
|
|
93
85
|
if (relative.startsWith("/")) relative = relative.slice(1);
|
|
94
|
-
|
|
95
|
-
return path.join(...relative.split("/"));
|
|
86
|
+
return path.join(...relative.replace(/:/g, "_").split("/"));
|
|
96
87
|
}
|
|
97
88
|
|
|
98
89
|
async build() {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
90
|
+
try {
|
|
91
|
+
const model = await this.model();
|
|
92
|
+
const ordDocument = ord(model);
|
|
93
|
+
const postProcessedOrdDocument = this.postProcess(ordDocument);
|
|
102
94
|
|
|
103
|
-
|
|
104
|
-
|
|
95
|
+
const promises = [];
|
|
96
|
+
promises.push(this.write(postProcessedOrdDocument).to(ORD_DOCUMENT_FILE_NAME));
|
|
105
97
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
98
|
+
if (ordDocument.apiResources?.length > 0) {
|
|
99
|
+
await this._writeResourcesFiles(ordDocument.apiResources, model, promises);
|
|
100
|
+
}
|
|
109
101
|
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
if (ordDocument.eventResources?.length > 0) {
|
|
103
|
+
await this._writeResourcesFiles(ordDocument.eventResources, model, promises);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return Promise.all(promises);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
throw new BuildError(`ORD build failed: ${error.message}`);
|
|
112
109
|
}
|
|
113
|
-
return Promise.all(promises);
|
|
114
110
|
}
|
|
115
111
|
};
|
package/lib/constants.js
CHANGED
|
@@ -77,6 +77,8 @@ const LEVEL = Object.freeze({
|
|
|
77
77
|
|
|
78
78
|
const OPEN_RESOURCE_DISCOVERY_VERSION = "1.12";
|
|
79
79
|
|
|
80
|
+
const OPENAPI_SERVERS_ANNOTATION = "@OpenAPI.servers";
|
|
81
|
+
|
|
80
82
|
const ORD_EXTENSIONS_PREFIX = "@ORD.Extensions.";
|
|
81
83
|
|
|
82
84
|
const ORD_ODM_ENTITY_NAME_ANNOTATION = "@ODM.entityName";
|
|
@@ -156,6 +158,7 @@ module.exports = {
|
|
|
156
158
|
LEVEL,
|
|
157
159
|
MCP_CUSTOM_TYPE,
|
|
158
160
|
OPEN_RESOURCE_DISCOVERY_VERSION,
|
|
161
|
+
OPENAPI_SERVERS_ANNOTATION,
|
|
159
162
|
ORD_ACCESS_STRATEGY,
|
|
160
163
|
ORD_DOCUMENT_FILE_NAME,
|
|
161
164
|
ORD_EXTENSIONS_PREFIX,
|
package/lib/metaData.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
const cds = require("@sap/cds/lib");
|
|
2
2
|
const { compile: openapi } = require("@cap-js/openapi");
|
|
3
3
|
const { compile: asyncapi } = require("@cap-js/asyncapi");
|
|
4
|
-
const { COMPILER_TYPES } = require("./constants");
|
|
4
|
+
const { COMPILER_TYPES, OPENAPI_SERVERS_ANNOTATION } = require("./constants");
|
|
5
5
|
const Logger = require("./logger");
|
|
6
6
|
const { interopCSN } = require("./interopCsn.js");
|
|
7
7
|
const cdsc = require("@sap/cds-compiler/lib/main");
|
|
8
8
|
const { isMCPPluginReady, buildMcpServerDefinition } = require("./mcpAdapter");
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Read @OpenAPI.servers annotation from service definition
|
|
12
|
+
* @param {object} csn - The CSN model
|
|
13
|
+
* @param {string} serviceName - The service name
|
|
14
|
+
* @returns {string|undefined} - JSON string of servers array or undefined
|
|
15
|
+
*/
|
|
16
|
+
const _getServersFromAnnotation = (csn, serviceName) => {
|
|
17
|
+
const servers = csn?.definitions?.[serviceName]?.[OPENAPI_SERVERS_ANNOTATION];
|
|
18
|
+
const isValidServers = Array.isArray(servers) && servers.length > 0;
|
|
19
|
+
return isValidServers ? JSON.stringify(servers) : undefined;
|
|
20
|
+
};
|
|
21
|
+
|
|
10
22
|
const getMetadata = async (url, model = null) => {
|
|
11
23
|
const parts = url
|
|
12
24
|
?.split("/")
|
|
@@ -23,7 +35,16 @@ const getMetadata = async (url, model = null) => {
|
|
|
23
35
|
switch (compilerType) {
|
|
24
36
|
case COMPILER_TYPES.oas3:
|
|
25
37
|
try {
|
|
26
|
-
|
|
38
|
+
// Check for service-level @OpenAPI.servers annotation
|
|
39
|
+
const serversFromAnnotation = _getServersFromAnnotation(csn, serviceName);
|
|
40
|
+
const openapiOptions = { ...options, ...(compileOptions?.openapi || {}) };
|
|
41
|
+
|
|
42
|
+
// Service-level annotation takes precedence over global config
|
|
43
|
+
if (serversFromAnnotation) {
|
|
44
|
+
openapiOptions["openapi:servers"] = serversFromAnnotation;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
responseFile = openapi(csn, openapiOptions);
|
|
27
48
|
} catch (error) {
|
|
28
49
|
Logger.error("OpenApi error:", error.message);
|
|
29
50
|
throw error;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js/ord",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "CAP Plugin for generating ORD document.",
|
|
5
5
|
"repository": "cap-js/ord",
|
|
6
6
|
"author": "SAP SE (https://www.sap.com)",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"test": "jest __tests__/unit --ci --collectCoverage",
|
|
24
24
|
"test:integration:basic": "jest __tests__/integration/basic-auth.test.js --testPathIgnorePatterns=/node_modules/ --forceExit",
|
|
25
25
|
"test:integration:mtls": "jest __tests__/integration/mtls-auth.test.js --testPathIgnorePatterns=/node_modules/ --forceExit",
|
|
26
|
+
"test:integration:build": "jest __tests__/integration/cds-build.test.js --testPathIgnorePatterns=/node_modules/ --forceExit",
|
|
26
27
|
"update-snapshot": "jest --ci --updateSnapshot",
|
|
27
28
|
"cds:version": "cds v -i"
|
|
28
29
|
},
|
|
@@ -30,7 +31,7 @@
|
|
|
30
31
|
"eslint": "^9.2.0",
|
|
31
32
|
"express": "^4",
|
|
32
33
|
"jest": "^30.0.0",
|
|
33
|
-
"prettier": "3.
|
|
34
|
+
"prettier": "3.8.1",
|
|
34
35
|
"supertest": "^7.0.0",
|
|
35
36
|
"@cap-js/sqlite": "^2",
|
|
36
37
|
"@sap/cds-dk": ">=8.9.5"
|