-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.example.js
More file actions
293 lines (274 loc) · 11.5 KB
/
server.example.js
File metadata and controls
293 lines (274 loc) · 11.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require.paths.unshift(__dirname + '/lib');
require.paths.unshift(__dirname + '/lib/xml2js/lib');
require.paths.unshift(__dirname + '/lib/simpledb/lib');
var sys = require('sys'),
http = require('http'),
fs = require('fs'),
conductor = require('conductor'),
aws = require('aws-lib'),
simpledb = require('simpledb/lib/simpledb'),
uuid = require('uuid'),
dt = require('date-util'),
exec = require('child_process').exec;
//read config.json
try {
var configJSON = fs.readFileSync(__dirname + "/config.json");
var config = JSON.parse(configJSON.toString());
conductor.config = config;
} catch(e) {
sys.log("File config.json not found. Try: `cp config.json.sample config.json`");
sys.log(e);
process.exit(1);
}
//setup default variables
var port = (process.env.PORT || config.port) // use env var, otherwise use value from config.json
, simpledbKey = (process.env.AWS_SIMPLEDB_KEY || config.aws_simpledb_key)
, simpledbSecretKey = (process.env.AWS_SIMPLEDB_SECRET || config.aws_simpledb_secret_key);
// uncomment this to enable https on port 443
var https = require('https'), port = 9000;
var sdb = new simpledb.SimpleDB({ keyid: simpledbKey, secret: simpledbSecretKey, secure: true });
// execute callback(true) if username is admin or instance was created by username
var isAllowed = function(instanceId, username, env, callback) {
isEnvAdmin(username, env, function(result) {
if (result == true) { callback( true ); }
else {
sdb.getItem('VncAwsInstanceMetadata', instanceId, {}, function(err, result, meta) {
if (err) sys.log("Exception in isAllowed: " + JSON.stringify(err));
callback( (result.CreatedBy).toLowerCase() == (username).toLowerCase() );
});
}
});
};
// return true if user is admin within env account
var isEnvAdmin = function(username, env, callback) {
var user = username.split('\\')[1]; // remove 'aws\' domain prefix from username
var command = "/home/ec2-user/conductor/getGroups.sh " + user;
exec(command, function(err, stdout, stderr) {
if (err) {
sys.log("Error in isAdmin: ");
console.log(err);
callback( false );
} else {
var test = "memberOf: CN=" + env + "Admins";
if ( (stdout.toLowerCase()).indexOf((test.toLowerCase()) ) >= 0) callback( true );
else callback( false );
}
});
};
// return true if user is 'user' within env account
var isEnvUser = function(username, env, callback) {
isEnvAdmin(username, env, function(result) {
if (result == true) { callback( true ); }
else {
var user = username.split('\\')[1]; // remove 'aws\' domain prefix from username
var command = "/home/ec2-user/conductor/getGroups.sh " + user;
exec(command, function(err, stdout, stderr) {
if (err) {
sys.log("Error in isEnvUser: ");
console.log(err);
callback( false );
} else {
var test = "memberOf: CN=" + env + "Users";
if ( (stdout.toLowerCase()).indexOf( (test.toLowerCase()) ) >= 0) callback( true );
else callback( false );
}
});
}
});
};
// all before functions receive a single parameter
// 1) the query string as a generic object
conductor.beforeCreate = function(q, username, callback) {
// if user is not a member of 'users' group or 'admins' group in q.env account
isEnvUser(username, q.env, function(result) {
if (result == false) {
callback({ httpCode: 403, message: 'You are not authorized to create an instance with that configuration.' });
} else {
// required: q.env, q.displayName (freakin' simpledb library bug won't let me use parens in simpledb query so can't use imageName)
//if (!q.env || !q.imageName || !q.name || !q.description)
// callback({ httpCode: 400, message: 'env, imageName, name, and description are required' });
// optional: q.az, q.instanceType, q.keyPair, q.userData, q.secGroups
// check that requested configuration is allowed
var selectStatement = 'select *' +
' from VncAwsInstanceCreation' +
' where Environment=\'' + q.env + '\'' +
' and DisplayName=\'' + q.displayName + '\'' +
((q.az) ? ' and AvailabilityZone=\'' + q.az + '\'': "") +
((q.instanceType) ? ' and PreferredInstanceSize=\'' + q.instanceType + '\'': "") +
((q.keyPair) ? ' and KeyPair=\'' + q.keyPair + '\'': "");
sdb.select(selectStatement, {}, function(err, result, meta) {
if(!err) {
if(result.length && result.length > 1) { // if more than 1 row returned, fail with error
callback({ httpCode: 400, message: 'Parameters provided identify more than one allowed instance configuration.' });
} else if (result.length && result.length == 0) { // if no rows returned, fail with error
callback({ httpCode: 400, message: 'Parameters provided do not identify any allowed instance configurations.' });
} else if (result.length && result.length == 1) { // if 1 row returned, set attributes of q and continue
// add a bunch of attributes to q so that they can be used to make the RunInstances EC2 api request
q.imageName = result[0]['$ItemName'];
q.imageId = result[0].AmiId;
q.az = result[0].AvailabilityZone;
q.instanceType = result[0].PreferredInstanceSize;
q.keyPair = result[0].KeyPair;
q.secGroups = result[0].SecurityGroups;
callback({});
} else {
callback({ httpCode: 400, message: 'Error querying instance configurations.' });
}
} else {
sys.log("Error reading instance config from SimpleDB:");
console.log(err);
callback({ httpCode: 403, message: 'Error validating instance creation parameters.' });
}
});
}
});
};
conductor.beforeStart = function(q, username, callback) {
isAllowed(q.instanceId, username, q.env, function(result) {
if (!result) callback({ httpCode: 403, message: 'You are not permitted to start instance ' + q.instanceId + '.' });
else callback({});
});
};
// TODO: replace with username of currently authenticated user
conductor.beforeStop = function(q, username, callback) {
isAllowed(q.instanceId, username, q.env, function(result) {
if (!result) callback({ httpCode: 403, message: 'You are not permitted to stop instance ' + q.instanceId + '.' });
else callback({});
});
};
conductor.beforeTerminate = function(q, username, callback) {
isAllowed(q.instanceId, username, q.env, function(result) {
if (!result) callback({ httpCode: 403, message: 'You are not permitted to terminate instance ' + q.instanceId + '.' });
else callback({});
});
};
conductor.beforeAssociateAddress = function(q, username, callback) {
// what type of user is allowed to associate elastic IP addresses with instances?
callback({});
};
// all after functions receive three parameters
// 1) the query string as a generic object
// 2) the http return code as an int
// 3) the http return message as a string
conductor.afterCreate = function(q, httpCode, msg) {
if (httpCode != 200 || msg.indexOf('running') >= 0) return; // don't log to simpledb on failure or if it's already running
var msgObj = JSON.parse(msg); // make it an object so we can access properties instead of doing text parsing
// add item to operation history simpledb domain
sdb.putItem('VncAwsOperationHistory', "" + uuid().toLowerCase(),
{
Message: 'Instance(s): ' + q.instanceId + ' has been successfully created by ' + msgObj.data.user + ' via VNC API.',
Date: (new Date()).format('isoDateTime'),
Environment: q.env
},
function(err, result, meta) {
if (err) {
sys.log("Error writing to VncAwsOperationHistory");
console.log(err);
}
}
);
// add item to meta data simpledb domain
sdb.putItem('VncAwsInstanceMetadata', msgObj.data.instanceId,
{
Name: q.name,
Description: q.description,
CreatedBy: msgObj.data.user,
InstanceConfiguration: q.imageName
},
function(err, result, meta) {
if (err) {
sys.log("Error writing to VncAwsInstanceMetadata:");
console.log(err);
}
}
);
};
conductor.afterStart = function(q, httpCode, msg) {
if (httpCode != 200 || msg.indexOf('running') >= 0) return; // don't log to simpledb on failure or if it's already running
var msgObj = JSON.parse(msg); // make it an object so we can access properties instead of doing text parsing
sdb.putItem('test_VncAwsOperationHistory', "" + uuid().toLowerCase(),
{
Message: 'Instance: ' + q.instanceId + ' has been successfully started by ' + msgObj.data.user + ' via VNC API.',
Date: (new Date()).format('isoDateTime'),
Environment: q.env
},
function(err, result, meta) {
if (err) sys.log(err);
}
);
};
conductor.afterStop = function(q, httpCode, msg) {
if (httpCode != 200 || msg.indexOf('stopped') >= 0) return; // don't log to simpledb on failure or if it's already stopped
var msgObj = JSON.parse(msg); // make it an object so we can access properties instead of doing text parsing
sdb.putItem('test_VncAwsOperationHistory', "" + uuid().toLowerCase(),
{
Message: 'Instance: ' + q.instanceId + ' has been successfully stopped by ' + msgObj.data.user + ' via VNC API.',
Date: (new Date()).format('isoDateTime'),
Environment: q.env
},
function(err, result, meta) {
if (err) sys.log(err);
}
);
};
conductor.afterTerminate = function(q, httpCode, msg) {
if (httpCode != 200 || msg.indexOf('terminated') >= 0) return; // don't log to simpledb on failure or if it's already terminated
var msgObj = JSON.parse(msg); // make it an object so we can access properties instead of doing text parsing
sdb.putItem('test_VncAwsOperationHistory', "" + uuid().toLowerCase(),
{
Message: 'Instance: ' + q.instanceId + ' has been successfully terminated by ' + msgObj.data.user + ' via VNC API.',
Date: (new Date()).format('isoDateTime'),
Environment: q.env
},
function(err, result, meta) {
if (err) sys.log(err);
}
);
};
conductor.afterAssociateAddress = function(q, httpCode, msg) {
if (httpCode != 200 || msg.indexOf('success') < 1) return; // don't log to simpledb on failure
var msgObj = JSON.parse(msg); // make it an object so we can access properties instead of doing text parsing
sdb.putItem('test_VnvAwsOperationHistory', "" + uuid().toLowerCase(),
{
Message: 'ElasticIP: ' + q.ip + ' has been successfully associated to instance ID ' + q.instanceId + ' by ' + msgObj.user + ' via VNC API.',
Date: (new Date()).format('isoDateTime'),
Environment: q.env
},
function(err, result, meta) {
if (err) sys.log(err);
}
);
};
// override default authentication scheme with ldap authentication
<<<<<<< Updated upstream
conductor.authenticationScheme = function(username, password, callback) {
username = username.replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
password = password.replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
var command = "/home/ec2-user/conductor/auth.sh " + username + " " + password;
exec(command, function(err, stdout, stderr) {
if (err) {
// test for invalid credentials
var test = "Invalid credentials";
err = err.toString();
if ( (err.toLowerCase()).indexOf(test.toLowerCase()) >= 0 ) callback(null, false);
// else there was an error but it wasn't invalid credentials. send error back.
else callback(err, null);
} else {
// test for valid credentials
var test = "Result: Success";
if ( (stdout.toLowerCase()).indexOf(test.toLowerCase()) >= 0 ) callback(null, true);
// else credentials must have been invalid
else callback(null, false);
}
});
};*/
// uncomment this to enable https
// also comment out the http.createServer... line below
var sslOptions = {
ca: fs.readFileSync('ca.pem'),
key: fs.readFileSync('ssl.key'),
cert: fs.readFileSync('ssl.crt')
};
https.createServer(sslOptions, conductor.router).listen(port);
//http.createServer(conductor.router).listen(port);
sys.log('Listening on http://0.0.0.0:' + port);