This repository has been archived by the owner on Jul 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvRA_handler.js
301 lines (262 loc) · 9.6 KB
/
vRA_handler.js
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Example handler that can be used with vRealize 8 as a nodeJS action to load local example functions
// This is a reference file only
// The contents of this file could be loaded as a custom Action within vRealize 8 Orchestrator using nodeJS and then configured with corresponding inputs.
// See `exports.handler` section near the bottom of this file
// -------------------------------
// Chef Infra Server API Functions
// -------------------------------
const https = require('https');
const crypto = require('crypto');
const url = require('url');
function sha1(string) {
return crypto.createHash('sha1').update(string).digest('base64');
}
function chefInfraAPIHeaders(auth, config) {
var bodyHash = sha1(config.body ? JSON.stringify(config.body) : '');
var pathHash = sha1(url.parse(config.uri).pathname);
var timestamp = new Date().toISOString().slice(0, -5) + 'Z';
var user = auth.user;
var headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
Host: config.hostname,
'X-Chef-Version': '17.99.99',
'X-Ops-Content-Hash': bodyHash,
'X-Ops-Sign': 'algorithm=sha1;version=1.0;',
'X-Ops-Timestamp': timestamp,
'X-Ops-UserId': user,
};
// Encrypted Canonical Header contents which are appended to regular Headers as 'X-OPS-AUTHORIZATION-' lines
// https://docs.chef.io/server/api_chef_server/#canonical-header-format-10-using-sha-1
var canonicalHeader = 'Method:' + config.method + '\n' +
'Hashed Path:' + pathHash + '\n' +
'X-Ops-Content-Hash:' + bodyHash + '\n' +
'X-Ops-Timestamp:' + timestamp + '\n' +
'X-Ops-UserId:' + user;
var signedCanonicalReq = crypto.privateEncrypt(auth.key, Buffer.from(canonicalHeader, 'utf-8')).toString('base64');
signedCanonicalReq.match(/.{1,60}/g).forEach(function(hash, line) {
headers['X-OPS-AUTHORIZATION-' + (line + 1)] = hash;
});
return headers;
};
// HTTPS Request Function used by exports
function httpsRequest(httpsRequestOptions, bodyData, callback) {
let data = '';
var req = https.request(httpsRequestOptions, (res) => {
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
// console.log(JSON.parse(data));
callback(JSON.parse(data));
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(JSON.stringify(bodyData));
req.end();
}
// Client Functions
// https://docs.chef.io/server/api_chef_server/#clients
function ClientGet(client, chef_api_server, chef_api_org, chef_api_client_name, chef_api_client_key, callback) {
var chef_api_server_url = url.parse('https://' + chef_api_server + '/organizations/' + chef_api_org + '/clients/' + client);
var httpsRequestOptions = {
hostname: chef_api_server_url.host,
port: chef_api_server_url.port,
path: chef_api_server_url.path,
method: 'GET',
headers: chefInfraAPIHeaders({
user: chef_api_client_name,
key: chef_api_client_key,
},
{
hostname: chef_api_server_url.host,
body: '',
method: 'GET',
uri: chef_api_server_url.path,
}),
};
httpsRequest(httpsRequestOptions, '', function(response){
// console.log(response);
callback(response);
});
};
function ClientCreate(name, chef_api_server, chef_api_org, chef_api_client_name, chef_api_client_key, callback) {
var chef_api_server_url = url.parse('https://' + chef_api_server + '/organizations/' + chef_api_org + '/clients');
var bodyData = {
name: name,
clientname: name,
create_key: true,
validator: false,
};
var httpsRequestOptions = {
hostname: chef_api_server_url.host,
port: chef_api_server_url.port || 443,
path: chef_api_server_url.path,
method: 'POST',
headers: chefInfraAPIHeaders({
user: chef_api_client_name,
key: chef_api_client_key,
},
{
body: bodyData,
hostname: chef_api_server_url.host,
method: 'POST',
uri: chef_api_server_url.path,
}),
};
httpsRequest(httpsRequestOptions, bodyData, function(response){
// console.log(response);
callback(response);
});
};
function ClientDelete(name, chef_api_server, chef_api_org, chef_api_client_name, chef_api_client_key, callback) {
var chef_api_server_url = url.parse('https://' + chef_api_server + '/organizations/' + chef_api_org + '/clients/' + name);
var httpsRequestOptions = {
hostname: chef_api_server_url.host,
port: chef_api_server_url.port,
path: chef_api_server_url.path,
method: 'DELETE',
headers: chefInfraAPIHeaders({
user: chef_api_client_name,
key: chef_api_client_key,
},
{
body: '',
hostname: chef_api_server_url.host,
method: 'DELETE',
uri: chef_api_server_url.path,
}),
};
httpsRequest(httpsRequestOptions, '', function(response){
// console.log(response);
callback(response);
});
};
// Node functions
// https://docs.chef.io/server/api_chef_server/#nodes
function NodeGet(name, chef_api_server, chef_api_org, chef_api_client_name, chef_api_client_key, callback) {
var chef_api_server_url = url.parse('https://' + chef_api_server + '/organizations/' + chef_api_org + '/nodes/' + name);
var httpsRequestOptions = {
hostname: chef_api_server_url.host,
port: chef_api_server_url.port,
path: chef_api_server_url.path,
method: 'GET',
headers: chefInfraAPIHeaders({
user: chef_api_client_name,
key: chef_api_client_key,
},
{
hostname: chef_api_server_url.host,
body: '',
method: 'GET',
uri: chef_api_server_url.path,
}),
};
httpsRequest(httpsRequestOptions, '', function(response){
// console.log(response);
callback(response);
});
};
function NodeCreate(name, runlist, chef_api_server, chef_api_org, chef_api_client_name, chef_api_client_key, callback) {
var chef_api_server_url = url.parse('https://' + chef_api_server + '/organizations/' + chef_api_org + '/nodes');
var bodyData = {
name: name,
json_class: 'Chef::Node',
};
// Assign Policy, if specified, otherwise use run-list
if (runlist.policy_name) {
bodyData.policy_name = runlist.policy_name;
bodyData.policy_group = runlist.policy_group;
} else {
bodyData.run_list = runlist;
}
var httpsRequestOptions = {
hostname: chef_api_server_url.host,
port: chef_api_server_url.port || 443,
path: chef_api_server_url.path,
method: 'POST',
headers: chefInfraAPIHeaders({
user: chef_api_client_name,
key: chef_api_client_key,
},
{
body: bodyData,
hostname: chef_api_server_url.host,
method: 'POST',
uri: chef_api_server_url.path,
}),
};
httpsRequest(httpsRequestOptions, bodyData, function(response){
// console.log(response);
callback(response);
});
};
function NodeDelete(name, chef_api_server, chef_api_org, chef_api_client_name, chef_api_client_key, callback) {
var chef_api_server_url = url.parse('https://' + chef_api_server + '/organizations/' + chef_api_org + '/nodes/' + name);
var httpsRequestOptions = {
hostname: chef_api_server_url.host,
port: chef_api_server_url.port,
path: chef_api_server_url.path,
method: 'DELETE',
headers: chefInfraAPIHeaders({
user: chef_api_client_name,
key: chef_api_client_key,
},
{
body: '',
hostname: chef_api_server_url.host,
method: 'DELETE',
uri: chef_api_server_url.path,
}),
};
httpsRequest(httpsRequestOptions, '', function(response){
// console.log(response);
callback(response);
});
};
// -------------------------------
// End Chef Infra Server API Functions
// -------------------------------
// handler export for vRA to consume
// Assumes the following are fed as inputs to the action:
// action: function from app to call
// target: target object for the function (node, client, etc.)
// target_data: additional data associated with the target (ex: run_list data for creating a node)
// chef_api_user: user or client name to connect to the Chef Infra Server API
// chef_api_key: PEM key data for connection associated with chef_api_user
// chef_server: FQDN of the Chef Infra Server
// chef_org: target Chef Infra Server organization
exports.handler = (context, inputs, callback) => {
const action = inputs.action;
const target = inputs.target;
const target_data = (inputs.target_data || '');
const chef_api_user = inputs.chef_api_user;
const chef_api_key = inputs.chef_api_key;
const chef_server = inputs.chef_server;
const chef_org = inputs.chef_org;
switch (action) {
case 'ClientCreate':
callback(ClientCreate(target, chef_server, chef_org, chef_api_user, chef_api_key, function(response) { console.log(JSON.stringify(response)); }));
break;
case 'ClientGet':
callback(ClientGet(target, chef_server, chef_org, chef_api_user, chef_api_key, function(response) { console.log(JSON.stringify(response)); }));
break;
case 'ClientDelete':
callback(ClientDelete(target, chef_server, chef_org, chef_api_user, chef_api_key, function(response) { console.log(JSON.stringify(response)); }));
break;
case 'NodeCreate':
callback(NodeCreate(target, target_data, chef_server, chef_org, chef_api_user, chef_api_key, function(response) { console.log(JSON.stringify(response)); }));
break;
case 'NodeGet':
callback(NodeGet(target, chef_server, chef_org, chef_api_user, chef_api_key, function(response) { console.log(JSON.stringify(response)); }));
break;
case 'NodeDelete':
callback(NodeDelete(target, chef_server, chef_org, chef_api_user, chef_api_key, function(response) { console.log(JSON.stringify(response)); }));
break;
default:
console.log('No matching action' + action);
break;
};
};