-
Notifications
You must be signed in to change notification settings - Fork 163
/
upgrades.js
284 lines (255 loc) · 10.3 KB
/
upgrades.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
/*
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/asl/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
var pjson = require('./package.json');
var common = require('./common');
var async = require('async');
var dynamoClient;
var debug = (process.env['DEBUG'] === 'true');
var log_level = process.env['LOG_LEVEL'] || 'info';
const winston = require('winston');
const logger = winston.createLogger({
level: debug === true ? 'debug' : log_level,
transports: [new winston.transports.Console({
format: winston.format.simple()
})]
});
function v1_v2(err, s3Info, configPre, forwardCallback) {
if (configPre.clusterEndpoint) {
logger.warn("Upgrading to Version 2.x multi-cluster configuration");
// bind the current config items into the new config map
var clusterConfig = {
M: {}
};
clusterConfig.M.clusterEndpoint = configPre.clusterEndpoint;
clusterConfig.M.clusterPort = configPre.clusterPort;
clusterConfig.M.clusterDB = configPre.clusterDB;
clusterConfig.M.connectUser = configPre.connectUser;
clusterConfig.M.connectPassword = configPre.connectPassword;
clusterConfig.M.targetTable = configPre.targetTable;
clusterConfig.M.truncateTarget = configPre.truncateTarget;
// update dynamo, adding the new config map as 'loadClusters' and
// removing the old values
var updateRequest = {
Key: {
s3Prefix: {
S: s3Info.prefix
}
},
TableName: configTable,
UpdateExpression: "SET #loadCluster = :newLoadCluster, lastUpdate = :updateTime, #ver = :version "
+ "REMOVE clusterEndpoint, clusterPort, clusterDB, connectUser, connectPassword, targetTable, truncateTarget",
ExpressionAttributeValues: {
":newLoadCluster": {
L: [clusterConfig]
},
":updateTime": {
S: common.getFormattedDate()
},
":version": {
S: pjson.version
}
},
ExpressionAttributeNames: {
"#ver": 'version',
"#loadCluster": 'loadClusters'
},
/*
* current can't be the target version, or someone else has done an
* upgrade
*/
ConditionExpression: "(attribute_not_exists(#ver) or #ver != :version) and attribute_not_exists(#loadCluster)",
// add the ALL_NEW return values so we have the get the config after
// update
ReturnValues: "ALL_NEW"
};
logger.debug(JSON.stringify(updateRequest));
dynamoClient.updateItem(updateRequest, function (err, data) {
if (err) {
if (err.code === conditionCheckFailed) {
// no problem - configuration was upgraded by someone else
// while we were running - requery and return
var dynamoLookup = {
Key: {
s3Prefix: {
S: s3Info.prefix
}
},
TableName: configTable,
ConsistentRead: true
};
dynamoClient.getItem(dynamoLookup, function (err, data) {
forwardCallback(null, s3Info, data.Item);
});
} else {
// unknown error - return the original configuration with
// the
// error
forwardCallback(err, s3Info, configPre);
}
} else {
// update was OK - go ahead with the new item returned by the
// upgrade call
forwardCallback(null, s3Info, data.Attributes);
}
});
} else {
// no upgrade required
forwardCallback(null, s3Info, configPre);
}
}
exports.v1_v2 = v1_v2;
function fixEncryptedItemEncoding(err, s3Info, configPre, forwardCallback) {
var willDoUpgrade = false;
// convert the encrypted items from the previous native storage format to
// base64 encoding
loadClusters = [];
var newMasterSymmetricKey;
var newSecretKeyForS3;
var updateExpressions = [];
var expressionAttributeValues = {
":updateTime": {
S: common.getFormattedDate()
},
":version": {
S: pjson.version
}
};
if (configPre.masterSymmetricKey && configPre.masterSymmetricKey.S.indexOf('[') > -1) {
willDoUpgrade = true;
newMasterSymmetricKey = Buffer.from(JSON.parse(configPre.masterSymmetricKey.S)).toString('base64');
updateExpressions.push("masterSymmetricKey = :masterSymmetricKey");
expressionAttributeValues[":masterSymmetricKey"] = {
S: newMasterSymmetricKey
};
}
if (configPre.secretKeyForS3 && configPre.secretKeyForS3.S.indexOf('[') > -1) {
willDoUpgrade = true;
newSecretKeyForS3 = Buffer.from(JSON.parse(configPre.secretKeyForS3.S)).toString('base64');
updateExpressions.push("secretKeyForS3 = :s3secretAccessKey");
expressionAttributeValues[":s3secretAccessKey"] = {
S: newSecretKeyForS3
};
}
// upgrade each cluster entry's password for redshift
if (configPre && configPre.loadClusters && configPre.loadClusters.L) {
configPre.loadClusters.L.map(function (item) {
if (item.M.connectPassword.S.indexOf('[') > -1) {
willDoUpgrade = true;
item.M.connectPassword.S = Buffer.from(JSON.parse(item.M.connectPassword.S)).toString('base64');
}
loadClusters.push(item);
});
}
if (loadClusters.length > 0) {
expressionAttributeValues[":newLoadClusters"] = {
L: loadClusters
};
}
if (willDoUpgrade) {
var updateRequest = {
Key: {
s3Prefix: {
S: s3Info.prefix
}
},
TableName: configTable,
UpdateExpression: "SET #loadClusters = :newLoadClusters, lastUpdate = :updateTime, #ver = :version "
+ (updateExpressions.length > 0 ? "," + updateExpressions.join(',') : ""),
ExpressionAttributeValues: expressionAttributeValues,
ExpressionAttributeNames: {
"#ver": 'version',
"#loadClusters": 'loadClusters'
},
/*
* current can't be the target version, or someone else has done an
* upgrade
*/
ConditionExpression: " #ver <> :version",
// add the ALL_NEW return values so we have the
// get the config after update
ReturnValues: "ALL_NEW"
};
logger.info("Upgrading to 2.4.x Base64 encoded encryption values");
logger.debug(JSON.stringify(updateRequest));
dynamoClient.updateItem(updateRequest, function (err, data) {
if (err) {
if (err.code === conditionCheckFailed) {
// no problem - configuration was upgraded by someone else
// while we were running - requery and return
var dynamoLookup = {
Key: {
s3Prefix: {
S: s3Info.prefix
}
},
TableName: configTable,
ConsistentRead: true
};
dynamoClient.getItem(dynamoLookup, function (err, data) {
forwardCallback(null, s3Info, data.Item);
});
} else {
// unknown error - return the original configuration with
// the error
forwardCallback(err, s3Info, configPre);
}
} else {
// update was OK - go ahead with the new item returned by the
// upgrade call
forwardCallback(null, s3Info, data.Attributes);
}
});
} else {
forwardCallback(null, s3Info, configPre)
}
}
exports.fixEncryptedItemEncoding = fixEncryptedItemEncoding;
function upgradeAll(dynamoDB, s3Info, configPre, finalCallback) {
dynamoClient = dynamoDB;
// add required upgrades here
var upgrades = [];
/*
* upgrades.push(function(callback) { exports.v1_v2(null, s3Info, configPre,
* callback); });
*/
upgrades.push(function (callback) {
exports.fixEncryptedItemEncoding(null, s3Info, configPre, callback);
});
// run all the upgrades in order
async.waterfall(upgrades, function (err, s3Info, finalConfig) {
if (err) {
logger.error(err);
} else {
// upgrade the config to the current version
let params = {
TableName: configTable,
Key: {
s3Prefix: {
S: s3Info.prefix
}
},
UpdateExpression: 'set version = :ver',
ExpressionAttributeValues: {
":ver": {S: '' + pjson.version}
},
ReturnValues: 'ALL_NEW'
}
logger.info(`Upgrading Config ${s3Info.prefix} to version ${pjson.version}`);
logger.debug(JSON.stringify(params));
dynamoClient.updateItem(params, function (err, data) {
if (err) {
logger.error(err);
finalCallback(err, s3Info);
} else {
// run the final callback
finalCallback(err, s3Info, finalConfig);
}
});
}
});
}
exports.upgradeAll = upgradeAll;