forked from oaeproject/unity.ac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateGauth.js
148 lines (119 loc) · 4.57 KB
/
updateGauth.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
/*!
* Copyright 2015 Apereo Foundation (AF) Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
var _ = require('lodash');
var csv = require('csv');
var fs = require('fs');
var path = require('path');
var read = require('read');
var util = require('util');
var RestAPI = require('oae-rest');
var RestContext = require('oae-rest/lib/model').RestContext;
var UnityAPI = require('./api');
var argv = require('yargs')
.usage('Usage: $0 --file path/to/file.csv')
.alias('h', 'help')
.describe('h', 'Show help information')
.alias('u', 'url')
.describe('u', 'The URL of the global admin url (including the protocol)')
.alias('a', 'admin')
.describe('a', 'The username of the global administrator')
.alias('f', 'file')
.describe('f', 'The path to the CSV file')
.alias('c', 'concurrency')
.default('c', 1)
.describe('c', 'The number of threads to run')
.demand(['u', 'a', 'f'])
.argv;
read({'prompt': 'Password: ', 'silent': true}, function(err, password) {
if (err) {
console.log('Error: %s', err.stack);
process.exit(1);
}
var restCtx = new RestContext(argv.url, {
'username': argv.admin,
'userPassword': password,
'strictSSL': false
});
// List the errors
require('oae-rest/lib/util').on('error', function(err, body, response) {
console.log('Error %d: - %s', err.code, body);
});
console.log('Parsing CSV data...');
UnityAPI.getGoogleCSVData(argv.file, function(err, records) {
if (err) {
process.exit(1);
}
console.log('Getting all current tenants...');
RestAPI.Tenants.getTenants(restCtx, function(err, tenants) {
if (err) {
console.log('Failed to get all the tenants');
process.exit(1);
}
console.log('Begin updating Google auth for tenants...');
var concurrency = parseInt(argv.c, 10);
if (_.isNaN(concurrency)) {
console.log('Invalid argument concurrency argument: "%s"', argv.c);
process.exit(1);
}
for (var i = 0; i < concurrency; i++) {
updateTenants(restCtx, tenants, records, function() {
console.log('All done');
});
}
});
});
});
var updateTenants = function(restCtx, tenants, records, callback) {
if (_.isEmpty(records)) {
return callback();
}
var record = records.pop();
console.log('%s', record.alias);
if (!tenants[record.alias]) {
// can't do it if tenant does not exist
console.log(' Ignoring bacause tenant does not exist.');
// Move on to the next one
return setImmediate(updateTenants, restCtx, tenants, records, callback);
} else if (!record.googlekey || !record.googlesecret || !record.email) {
// Google auth requires this data
console.log(' Ignoring because of missing data.');
// Move on to the next one
return setImmediate(updateTenants, restCtx, tenants, records, callback);
}
// set gogole auth
setGoogleAuth(restCtx, tenants[record.alias], record, function(err, tenant) {
if (err) {
console.log(' Failed to update the tenant');
console.log(err);
process.exit(1);
}
// Move on to the next one
updateTenants(restCtx, tenants, records, callback);
});
};
var setGoogleAuth = function(restCtx, tenant, record, callback) {
console.log(' Setting configuration');
var update = {};
// set to turn google auth on
update['oae-authentication/google/key'] = record.googlekey;
update['oae-authentication/google/secret'] = record.googlesecret;
update['oae-authentication/google/domains'] = record.email;
update['oae-authentication/google/enabled'] = true;
// and set local auth to off
update['oae-authentication/local/enabled'] = false;
update['oae-authentication/local/allowAccountCreation'] = false;
// run update
RestAPI.Config.updateConfig(restCtx, tenant.alias, update, callback);
};