forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionSecretKeyChanger.java
More file actions
executable file
·371 lines (339 loc) · 15.3 KB
/
EncryptionSecretKeyChanger.java
File metadata and controls
executable file
·371 lines (339 loc) · 15.3 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-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.
package com.cloud.utils.crypt;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.jasypt.properties.EncryptableProperties;
import com.cloud.utils.PropertiesUtil;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
/*
* EncryptionSecretKeyChanger updates Management Secret Key / DB Secret Key or both.
* DB secret key is validated against the key in db.properties
* db.properties is updated with values encrypted using new MS secret key
* DB data migrated using new DB secret key
*/
public class EncryptionSecretKeyChanger {
private StandardPBEStringEncryptor oldEncryptor = new StandardPBEStringEncryptor();
private StandardPBEStringEncryptor newEncryptor = new StandardPBEStringEncryptor();
private static final String keyFile = "/etc/cloudstack/management/key";
public static void main(String[] args) {
List<String> argsList = Arrays.asList(args);
Iterator<String> iter = argsList.iterator();
String oldMSKey = null;
String oldDBKey = null;
String newMSKey = null;
String newDBKey = null;
//Parse command-line args
while (iter.hasNext()) {
String arg = iter.next();
// Old MS Key
if (arg.equals("-m")) {
oldMSKey = iter.next();
}
// Old DB Key
if (arg.equals("-d")) {
oldDBKey = iter.next();
}
// New MS Key
if (arg.equals("-n")) {
newMSKey = iter.next();
}
// New DB Key
if (arg.equals("-e")) {
newDBKey = iter.next();
}
}
if (oldMSKey == null || oldDBKey == null) {
System.out.println("Existing MS secret key or DB secret key is not provided");
usage();
return;
}
if (newMSKey == null && newDBKey == null) {
System.out.println("New MS secret key and DB secret are both not provided");
usage();
return;
}
final File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
final Properties dbProps;
EncryptionSecretKeyChanger keyChanger = new EncryptionSecretKeyChanger();
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
keyChanger.initEncryptor(encryptor, oldMSKey);
dbProps = new EncryptableProperties(encryptor);
PropertiesConfiguration backupDBProps = null;
System.out.println("Parsing db.properties file");
try(FileInputStream db_prop_fstream = new FileInputStream(dbPropsFile);) {
dbProps.load(db_prop_fstream);
backupDBProps = new PropertiesConfiguration(dbPropsFile);
} catch (FileNotFoundException e) {
System.out.println("db.properties file not found while reading DB secret key" + e.getMessage());
} catch (IOException e) {
System.out.println("Error while reading DB secret key from db.properties" + e.getMessage());
} catch (ConfigurationException e) {
e.printStackTrace();
}
String dbSecretKey = null;
try {
dbSecretKey = dbProps.getProperty("db.cloud.encrypt.secret");
} catch (EncryptionOperationNotPossibleException e) {
System.out.println("Failed to decrypt existing DB secret key from db.properties. " + e.getMessage());
return;
}
if (!oldDBKey.equals(dbSecretKey)) {
System.out.println("Incorrect MS Secret Key or DB Secret Key");
return;
}
System.out.println("Secret key provided matched the key in db.properties");
final String encryptionType = dbProps.getProperty("db.cloud.encryption.type");
if (newMSKey == null) {
System.out.println("No change in MS Key. Skipping migrating db.properties");
} else {
if (!keyChanger.migrateProperties(dbPropsFile, dbProps, newMSKey, newDBKey)) {
System.out.println("Failed to update db.properties");
return;
} else {
//db.properties updated successfully
if (encryptionType.equals("file")) {
//update key file with new MS key
try (FileWriter fwriter = new FileWriter(keyFile);
BufferedWriter bwriter = new BufferedWriter(fwriter);)
{
bwriter.write(newMSKey);
} catch (IOException e) {
System.out.println("Failed to write new secret to file. Please update the file manually");
}
}
}
}
boolean success = false;
if (newDBKey == null || newDBKey.equals(oldDBKey)) {
System.out.println("No change in DB Secret Key. Skipping Data Migration");
} else {
EncryptionSecretKeyChecker.initEncryptorForMigration(oldMSKey);
try {
success = keyChanger.migrateData(oldDBKey, newDBKey);
} catch (Exception e) {
System.out.println("Error during data migration");
e.printStackTrace();
success = false;
}
}
if (success) {
System.out.println("Successfully updated secret key(s)");
} else {
System.out.println("Data Migration failed. Reverting db.properties");
//revert db.properties
try {
backupDBProps.save();
} catch (ConfigurationException e) {
e.printStackTrace();
}
if (encryptionType.equals("file")) {
//revert secret key in file
try (FileWriter fwriter = new FileWriter(keyFile);
BufferedWriter bwriter = new BufferedWriter(fwriter);)
{
bwriter.write(oldMSKey);
} catch (IOException e) {
System.out.println("Failed to revert to old secret to file. Please update the file manually");
}
}
}
}
private boolean migrateProperties(File dbPropsFile, Properties dbProps, String newMSKey, String newDBKey) {
System.out.println("Migrating db.properties..");
StandardPBEStringEncryptor msEncryptor = new StandardPBEStringEncryptor();
;
initEncryptor(msEncryptor, newMSKey);
try {
PropertiesConfiguration newDBProps = new PropertiesConfiguration(dbPropsFile);
if (newDBKey != null && !newDBKey.isEmpty()) {
newDBProps.setProperty("db.cloud.encrypt.secret", "ENC(" + msEncryptor.encrypt(newDBKey) + ")");
}
String prop = dbProps.getProperty("db.cloud.password");
if (prop != null && !prop.isEmpty()) {
newDBProps.setProperty("db.cloud.password", "ENC(" + msEncryptor.encrypt(prop) + ")");
}
prop = dbProps.getProperty("db.usage.password");
if (prop != null && !prop.isEmpty()) {
newDBProps.setProperty("db.usage.password", "ENC(" + msEncryptor.encrypt(prop) + ")");
}
newDBProps.save(dbPropsFile.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println("Migrating db.properties Done.");
return true;
}
private boolean migrateData(String oldDBKey, String newDBKey) {
System.out.println("Begin Data migration");
initEncryptor(oldEncryptor, oldDBKey);
initEncryptor(newEncryptor, newDBKey);
System.out.println("Initialised Encryptors");
TransactionLegacy txn = TransactionLegacy.open("Migrate");
txn.start();
try {
Connection conn;
try {
conn = txn.getConnection();
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to migrate encrypted data in the database", e);
}
migrateConfigValues(conn);
migrateHostDetails(conn);
migrateVNCPassword(conn);
migrateUserCredentials(conn);
txn.commit();
} finally {
txn.close();
}
System.out.println("End Data migration");
return true;
}
private void initEncryptor(StandardPBEStringEncryptor encryptor, String secretKey) {
encryptor.setAlgorithm("PBEWithMD5AndDES");
SimpleStringPBEConfig stringConfig = new SimpleStringPBEConfig();
stringConfig.setPassword(secretKey);
encryptor.setConfig(stringConfig);
}
private String migrateValue(String value) {
if (value == null || value.isEmpty()) {
return value;
}
String decryptVal = oldEncryptor.decrypt(value);
return newEncryptor.encrypt(decryptVal);
}
private void migrateConfigValues(Connection conn) {
System.out.println("Begin migrate config values");
try(PreparedStatement select_pstmt = conn.prepareStatement("select name, value from configuration where category in ('Hidden', 'Secure')");
ResultSet rs = select_pstmt.executeQuery();
PreparedStatement update_pstmt = conn.prepareStatement("update configuration set value=? where name=?");
) {
while (rs.next()) {
String name = rs.getString(1);
String value = rs.getString(2);
if (value == null || value.isEmpty()) {
continue;
}
String encryptedValue = migrateValue(value);
update_pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
update_pstmt.setString(2, name);
update_pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to update configuration values ", e);
} catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable to update configuration values ", e);
}
System.out.println("End migrate config values");
}
private void migrateHostDetails(Connection conn) {
System.out.println("Begin migrate host details");
try( PreparedStatement sel_pstmt = conn.prepareStatement("select id, value from host_details where name = 'password'");
ResultSet rs = sel_pstmt.executeQuery();
PreparedStatement pstmt = conn.prepareStatement("update host_details set value=? where id=?");
) {
while (rs.next()) {
long id = rs.getLong(1);
String value = rs.getString(2);
if (value == null || value.isEmpty()) {
continue;
}
String encryptedValue = migrateValue(value);
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
pstmt.setLong(2, id);
pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable update host_details values ", e);
} catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable update host_details values ", e);
}
System.out.println("End migrate host details");
}
private void migrateVNCPassword(Connection conn) {
System.out.println("Begin migrate VNC password");
try(PreparedStatement select_pstmt = conn.prepareStatement("select id, vnc_password from vm_instance");
ResultSet rs = select_pstmt.executeQuery();
PreparedStatement pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?");
) {
while (rs.next()) {
long id = rs.getLong(1);
String value = rs.getString(2);
if (value == null || value.isEmpty()) {
continue;
}
String encryptedValue = migrateValue(value);
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
pstmt.setLong(2, id);
pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
} catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
}
System.out.println("End migrate VNC password");
}
private void migrateUserCredentials(Connection conn) {
System.out.println("Begin migrate user credentials");
try(PreparedStatement select_pstmt = conn.prepareStatement("select id, secret_key from user");
ResultSet rs = select_pstmt.executeQuery();
PreparedStatement pstmt = conn.prepareStatement("update user set secret_key=? where id=?");
) {
while (rs.next()) {
long id = rs.getLong(1);
String secretKey = rs.getString(2);
if (secretKey == null || secretKey.isEmpty()) {
continue;
}
String encryptedSecretKey = migrateValue(secretKey);
pstmt.setBytes(1, encryptedSecretKey.getBytes("UTF-8"));
pstmt.setLong(2, id);
pstmt.executeUpdate();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Unable update user secret key ", e);
} catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable update user secret key ", e);
}
System.out.println("End migrate user credentials");
}
private static void usage() {
System.out.println("Usage: \tEncryptionSecretKeyChanger \n" + "\t\t-m <Mgmt Secret Key> \n" + "\t\t-d <DB Secret Key> \n" + "\t\t-n [New Mgmt Secret Key] \n"
+ "\t\t-e [New DB Secret Key]");
}
}