-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgraphman-operation-explode.js
More file actions
274 lines (242 loc) · 11 KB
/
Copy pathgraphman-operation-explode.js
File metadata and controls
274 lines (242 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) 2025 Broadcom Inc. and its subsidiaries. All Rights Reserved.
const utils = require("./graphman-utils");
const butils = require("./graphman-bundle");
module.exports = {
/**
* Explodes bundle into multiple files.
* @param params
* @param params.input name of the input file containing the gateway configuration as bundle
* @param params.output name of the output directory into which the gateway configuration will be exploded
* @param params.options name-value pairs used to customize explode operation
*/
run: function (params) {
if (!params.input) {
throw "--input parameter is missing";
}
if (!params.output) {
throw "--output parameter is missing";
}
const bundle = utils.readFile(params.input);
const outputDir = params.output;
utils.mkDir(outputDir);
utils.info(`exploding to ${outputDir}`);
type1Exploder.explode(bundle, outputDir, params.options);
},
initParams: function (params, config) {
params = Object.assign({
gateway: "default"
}, params);
params.options = Object.assign({
level: 0
}, config.options, params.options);
return params;
},
usage: function () {
console.log("explode --input <input-file>");
console.log(" --output <output-dir>");
console.log(" [--options.<name> <value>,...]");
console.log();
console.log("Explodes bundle into multiple files.");
console.log();
console.log(" --input <input-file>");
console.log(" specify the name of input bundle file that contains gateway configuration");
console.log();
console.log(" --output <output-dir>");
console.log(" specify the name of directory to explode into.");
console.log();
console.log(" --options.<name> <value>");
console.log(" specify options as name-value pair(s) to customize the operation");
console.log(" .level 0|1|2");
console.log(" to decide the level of explode operation");
console.log(" - 0, default level where the individual entities will be exploded into separate files");
console.log(" - 1, wsdl resources and cert and key binary data (in p12 and pem formats) associated with the entities will be exploded into separate files");
console.log(" - 2, policy code will be exploded into separate files");
console.log();
}
}
function explodeFile(path, filename, data) {
utils.writeFile(utils.path(path, filename), data);
return `{{${filename}}}`;
}
function explodeFileBinary(path, filename, data) {
utils.writeFileBinary(utils.path(path, filename), data);
return `{{${filename}}}`;
}
let type1Exploder = (function () {
const BEGIN_CERT_HEADER = '-----BEGIN CERTIFICATE-----';
const END_CERT_HEADER = '-----END CERTIFICATE-----';
const subExploders = [
{
/**
* Explodes key details
* @param outputDir output directory
* @param filename name of the file (without extension)
* @param entity key entity
* @param typeInfo type-information
* @param options explode options
*/
explode: function (outputDir, filename, entity, typeInfo, options) {
if (options.level < 1) return;
if (typeInfo.pluralName !== "keys") return;
// make sure the key details exploded from one of the available (p12, pem)
if (entity.p12) {
entity.p12 = explodeFileBinary(outputDir, filename + ".p12", Buffer.from(entity.p12, 'base64'));
delete entity.pem;
} else if (entity.pem) {
entity.pem = explodeFile(outputDir, filename + ".pem", entity.pem);
}
if (entity.certChain) {
let data = "";
for (let index in entity.certChain) {
data += entity.certChain[index].trim();
data += "\r\n";
}
entity.certChain = explodeFile(outputDir, filename + ".certchain.pem", data);
}
}
},
{
/**
* Explodes trusted cert details
* @param outputDir output directory
* @param filename name of the file (without extension)
* @param entity trusted cert entity
* @param typeInfo type-information
* @param options explode options
*/
explode: function (outputDir, filename, entity, typeInfo, options) {
if (options.level < 1) return;
if (typeInfo.pluralName !== "trustedCerts") return;
if (entity.certBase64) {
let pemData = BEGIN_CERT_HEADER;
pemData += '\r\n' + entity.certBase64;
pemData += '\r\n' + END_CERT_HEADER;
entity.certBase64 = explodeFile(outputDir, filename + ".pem", pemData);
}
}
},
{
/**
* Explodes user cert details
* @param outputDir output directory
* @param filename name of the file (without extension)
* @param entity user entity
* @param typeInfo type-information
* @param options explode options
*/
explode: function (outputDir, filename, entity, typeInfo, options) {
if (options.level < 1) return;
if (typeInfo.pluralName !== "internalUsers" && typeInfo.pluralName !== "federatedUsers") return;
if (entity.certBase64) {
let pemData = BEGIN_CERT_HEADER;
pemData += '\r\n' + entity.certBase64;
pemData += '\r\n' + END_CERT_HEADER;
entity.certBase64 = explodeFile(outputDir, filename + ".pem", pemData);
}
if (entity.sshPublicKey) {
entity.sshPublicKey = explodeFile(outputDir, filename + ".pub", entity.sshPublicKey);
}
}
},
{
/**
* Explodes wsdl details
* @param outputDir output directory
* @param filename name of the file (without extension)
* @param entity any entity containing wsdl details (services)
* @param typeInfo type-information
* @param options explode options
*/
explode: function (outputDir, filename, entity, typeInfo, options) {
if (options.level < 1) return;
if (entity.wsdl) {
entity.wsdl = explodeFile(outputDir, filename + ".wsdl", entity.wsdl);
if (Array.isArray(entity.wsdlResources)) {
entity.wsdlResources.forEach((item, index) =>
item.content = explodeFile(outputDir, `${filename}-wsdl-resource-${index + 1}.xml`, item.content));
}
}
}
},
{
/**
* Explodes policy details
* @param outputDir output directory
* @param filename name of the file (without extension)
* @param entity any entity containing policy details (services, policies)
* @param typeInfo type-information
* @param options explode options
*/
explode: function (outputDir, filename, entity, typeInfo, options) {
if (options.level < 2) return;
// make sure the policy details exploded from one of the available (xml, json, yaml, code)
if (entity.policy) {
this.explodePolicy(outputDir, filename, entity.policy);
}
if (Array.isArray(entity.policyRevisions)) {
entity.policyRevisions.forEach(item =>
this.explodePolicy(outputDir, filename + "-revision-" + item.ordinal, item));
}
},
explodePolicy: function (outputDir, filename, policy) {
if (policy.xml) {
policy.xml = explodeFile(outputDir, filename + ".xml", policy.xml);
delete policy.json;
delete policy.yaml;
delete policy.code;
} else if (policy.json) {
policy.json = explodeFile(outputDir, filename + ".cjson", JSON.parse(policy.json));
delete policy.code;
delete policy.yaml;
} else if (policy.code) {
utils.writeFile(`${outputDir}/${filename}.cjson`, policy.code);
policy.json = explodeFile(outputDir, filename + ".cjson", policy.code);
delete policy.code;
delete policy.yaml;
} else if (policy.yaml) {
policy.yaml = explodeFile(outputDir, filename + ".yaml", policy.yaml);
delete policy.code;
}
}
}
];
return {
explode: function (bundle, outputDir, options) {
butils.forEach(bundle, (key, entities, typeInfo) => {
const list = [];
utils.info(`exploding ${key}`);
entities.forEach(item => {
options.duplicate = list.find(x => butils.isEntityMatches(x, item, typeInfo));
writeEntity(outputDir, key, item, typeInfo, options);
list.push(item);
});
});
if (bundle.properties) {
utils.info(`capturing properties to bundle-properties.json`);
utils.writeFile(`${outputDir}/bundle-properties.json`, bundle.properties);
}
}
};
function timeAwareRandom() {
return Date.now() + "-" + Math.round(Math.random() * 1000)
}
function writeEntity(dir, key, entity, typeInfo, options) {
let displayName = butils.entityName(entity, typeInfo);
if (!displayName) {
displayName = entity.checksum || timeAwareRandom();
utils.warn(" forced to use alternative entity display name for ", entity);
}
if (options.duplicate) {
const originalName = displayName;
displayName += "-" + timeAwareRandom();
utils.info(` ${displayName} (duplicate to ${originalName})`);
} else {
utils.info(` ${displayName}`);
}
const fileSuffix = butils.entityFileSuffixByPluralName(key);
const filename = utils.safeName(displayName) + (fileSuffix ? fileSuffix : "");
const targetDir = entity.folderPath ? utils.safePath(dir, "tree", entity.folderPath) : utils.path(dir, key);
subExploders.forEach(item => item.explode(targetDir, filename, entity, typeInfo, options));
utils.writeFile(`${targetDir}/${filename}.json`, entity);
}
})();