This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
/
mozdef.js
225 lines (197 loc) · 8.21 KB
/
mozdef.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
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (c) 2014 Mozilla Corporation
*/
import { Meteor } from 'meteor/meteor';
import '/imports/models.js';
if (Meteor.isServer) {
Meteor.startup(function () {
// Since we only connect to localhost or to ourselves, adding a hack to bypass cert validation
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// We don't use websockets, turn 'em off to make for a faster startup
process.env.DISABLE_WEBSOCKETS = "1";
console.log("MozDef starting")
// important to set this so meteor knows it's source URL
// set to what the browser thinks you are coming from (i.e. localhost, or actual servername)
Meteor.absoluteUrl.defaultOptions.rootUrl = mozdef.rootURL + ':' + mozdef.port
// figure out what features are enabled
console.log("updating features");
features.remove({});
var featuresFile = Assets.getText("features.txt");
var featuresObject = featuresFile.split("\n");
var featuresRemoved = mozdef.removeFeatures.split(',').map(function(item) {
return item.trim();
});
console.log(featuresRemoved);
featuresObject.forEach(function (featureItem) {
feature = models.feature();
feature.name = featureItem.split(" ")[0];
feature.url = featureItem.split(" ")[1]
if ( featuresRemoved.includes(feature.name) ){
feature.enabled=false;
}
features.insert(feature);
});
console.log('settings', mozdef);
// in addition to the Meteor.settings we use put deployment
// settings in settings.js to make it easier to deploy
// and to allow clients to get access to deployment-specific settings.
// settings are considered in the following order:
// env variables (Object: Meteor.settings.public.mozdef)
// settings.js (Object: mozdef)
// mozdefsettings collection (subscribed table)
// and resolved via the client-side getSetting() function
// or by use of the mozdef.settingKey object.
mozdefsettings.remove({});
mozdefsettings.insert({
key: 'rootURL',
value: mozdef.rootURL + ':' + mozdef.port
});
mozdefsettings.insert({
key: 'rootAPI',
value: mozdef.rootAPI
});
mozdefsettings.insert({
key: 'kibanaURL',
value: mozdef.kibanaURL
});
mozdefsettings.insert({
key: 'authenticationType',
value: mozdef.authenticationType
});
mozdefsettings.insert({
key: 'enableClientAccountCreation',
value: mozdef.enableClientAccountCreation
});
// allow local account creation?
// http://docs.meteor.com/#/full/accounts_config
// https://docs.meteor.com/api/accounts-multi.html#AccountsCommon-config
// https://github.com/meteor/meteor/blob/master/packages/accounts-base/accounts_common.js#L124
// newer meteor uses a key of forbidClientAccountCreation, so
// we invert the enableClientAccountCreation mozdef setting
Accounts._options.forbidClientAccountCreation = !mozdef.enableClientAccountCreation;
mozdefsettings.insert({
key: 'forbidClientAccountCreation',
value: !mozdef.enableClientAccountCreation
});
registerLoginMethod();
//update veris if missing:
console.log("checking the veris framework reference enumeration");
console.log('tags: ' + veris.find().count());
if (veris.find().count() == 0) {
console.log("updating the veris collection");
var verisFile = Assets.getText("veris.dotformat.txt");
var verisObject = verisFile.split("\n");
verisObject.forEach(function (verisItem) {
veris.insert({ tag: verisItem });
});
}
}); //end startup
} //end is server
function registerLoginMethod() {
// Setup a single login method to allow
var authenticationType = mozdef.authenticationType;
console.log("Authentication Type: ", authenticationType);
switch (authenticationType) {
case 'meteor-password':
registerLoginViaPassword();
break;
case 'oidc':
registerLoginViaHeader();
break;
default:
// non-breaking default of header
// todo: evaluate defaulting to meteor-password
registerLoginViaHeader();
break;
}
}
function registerLoginViaPassword() {
console.log("Setting up Password Authentication");
// We need to attach the users email address to the user.profile
Accounts.onCreateUser(function (options, user) {
if (user.emails.count <= 0) {
console.log("No user emails")
return user;
}
var email = user.emails[0].address;
if (typeof (email) === "undefined") {
console.log("User Email address not defined.")
return user;
}else{
// set the username to the primary email
user.username=email;
}
if (typeof (user.profile) === "undefined") {
// user has no profile - this is the usual case
user.profile = {
"email": email
};
} else {
// overwrite any email that may be attached
// to the current user profile. this should
// not happen as we are currently creating
// the user.
user.profile['email'] = email;
}
// set any other profile information here.
return user
});
}
function registerLoginViaHeader() {
Accounts.registerLoginHandler("headerLogin", function (loginRequest) {
// there are multiple login handlers in meteor.
// a login request goes through all these handlers to find it's login hander
// in our login handler, we only consider login requests which are via a header
// ideally we would use a header unique to the installation like HTTP_OIDC_CLAIM_ID_TOKEN_EMAIL
// however sockJS whitelists only certain headers
// https://github.com/sockjs/sockjs-node/blob/8b03b3b1e7be14ee5746847f517029cb3ce30ca7/src/transport.coffee#L132
// choose one that is passed on and set it in your http server config
// we default to the 'via' header
var headerName = 'via';
//grab the email from the header
var userEmail = this.connection.httpHeaders[headerName];
//our authentication logic
//check for user email header
if (userEmail == undefined) {
console.log('refused login request due to missing http header')
// throw an error
return new {
error: handleError("SSO Login failure: email not found in the 'via' http header")
};
}
console.log('target header:', userEmail);
console.log('handling login request', loginRequest);
//we create a user if needed, and get the userId
var userId = null;
var user = Meteor.users.findOne({ profile: { email: userEmail } });
if (user) {
userId = user._id;
} else {
console.log('creating user:', userEmail)
userId = Meteor.users.insert({
profile: { email: userEmail },
username: userEmail,
emails: [{ address: userEmail, "verified": true }],
createdAt: new Date()
});
}
// per https://github.com/meteor/meteor/blob/devel/packages/accounts-base/accounts_server.js#L263
// generating and storing the stamped login token is optional
// so we just return the userId and let the accounts module do it's thing
return {
userId: userId
}
});
}
function handleError(msg) {
const error = new Meteor.Error(
403,
Accounts._options.ambiguousErrorMessages
? "Login failure. Please check your login credentials."
: msg
);
return error;
}