forked from GoogleCloudPlatform/compute-image-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows_auth_java_sample.java
274 lines (213 loc) · 10.1 KB
/
windows_auth_java_sample.java
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
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache 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://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.
*/
/**
* This package demonstrates how to reset Windows passwords in Java.
*/
package cloud.google.com.windows.example;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.Metadata;
import com.google.api.services.compute.model.Metadata.Items;
import com.google.api.services.compute.model.SerialPortOutput;
import com.google.common.io.BaseEncoding;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import javax.crypto.Cipher;
public class ExampleCode {
public ExampleCode() {}
// Constants used to configure behavior.
private static final String ZONE_NAME = "us-central1-a";
private static final String PROJECT_NAME = "example-project-1234";
private static final String INSTANCE_NAME = "test-instance";
private static final String APPLICATION_NAME = "windows-pw-reset";
// Constants for configuring user name, email, and SSH key expiration.
private static final String USER_NAME = "example_user";
private static final String EMAIL = "[email protected]";
// Keys are one-time use, so the metadata doesn't need to stay around for long.
// 5 minutes chosen to allow for differences between time on the client
// and time on the server.
private static final long EXPIRE_TIME = 300000;
// HttpTransport and JsonFactory used to create the Compute object.
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
public static void main(String[] args) {
ExampleCode ec = new ExampleCode();
try {
// Initialize Transport object.
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// Reset the password.
ec.resetPassword();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void resetPassword() throws Exception {
// Get credentials to setup a connection with the Compute API.
Credential cred = GoogleCredential.getApplicationDefault();
// Create an instance of the Compute API.
Compute compute = new Compute.Builder(httpTransport, JSON_FACTORY, null)
.setApplicationName(APPLICATION_NAME).setHttpRequestInitializer(cred).build();
// Get the instance object to gain access to the instance's metadata.
Instance inst = compute.instances().get(PROJECT_NAME, ZONE_NAME, INSTANCE_NAME).execute();
Metadata metadata = inst.getMetadata();
// Generate the public/private key pair for encryption and decryption.
KeyPair keys = generateKeys();
// Update metadata from instance with new windows-keys entry.
replaceMetadata(metadata, buildKeyMetadata(keys));
// Tell Compute Engine to update the instance metadata with our changes.
compute.instances().setMetadata(PROJECT_NAME, ZONE_NAME, INSTANCE_NAME, metadata).execute();
System.out.println("Updating metadata...");
// Sleep while waiting for metadata to propagate - production code may
// want to monitor the status of the metadata update operation.
Thread.sleep(30000);
System.out.println("Getting serial output...");
// Request the output from serial port 4.
// In production code, this operation should be polled.
SerialPortOutput output = compute.instances()
.getSerialPortOutput(PROJECT_NAME, ZONE_NAME, INSTANCE_NAME).setPort(4).execute();
// Get the last line - this will be a JSON string corresponding to the
// most recent password reset attempt.
String[] entries = output.getContents().split("\n");
String outputEntry = entries[entries.length - 1];
// Parse output using the json-simple library.
JSONParser parser = new JSONParser();
JSONObject passwordDict = (JSONObject) parser.parse(outputEntry);
String encryptedPassword = passwordDict.get("encryptedPassword").toString();
// Output user name and decrypted password.
System.out.println("\nUser name: " + passwordDict.get("userName").toString());
System.out.println("Password: " + decryptPassword(encryptedPassword, keys));
}
private String decryptPassword(String message, KeyPair keys) {
try {
// Add the bouncycastle provider - the built-in providers don't support RSA
// with OAEPPadding.
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// Get the appropriate cipher instance.
Cipher rsa = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");
// Add the private key for decryption.
rsa.init(Cipher.DECRYPT_MODE, keys.getPrivate());
// Decrypt the text.
byte[] rawMessage = Base64.decodeBase64(message);
byte[] decryptedText = rsa.doFinal(rawMessage);
// The password was encoded using UTF8. Transform into string.
return new String(decryptedText, "UTF8");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return "";
}
private void replaceMetadata(Metadata input, JSONObject newMetadataItem) {
// Transform the JSON object into a string that the API can use.
String newItemString = newMetadataItem.toJSONString();
// Get the list containing all of the Metadata entries for this instance.
List<Items> items = input.getItems();
// If the instance has no metadata, items can be returned as null.
if (items == null)
{
items = new LinkedList<Items>();
input.setItems(items);
}
// Find the "windows-keys" entry and update it.
for (Items item : items) {
if (item.getKey().compareTo("windows-keys") == 0) {
// Replace item's value with the new entry.
// To prevent race conditions, production code may want to maintain a
// list where the oldest entries are removed once the 32KB limit is
// reached for the metadata entry.
item.setValue(newItemString);
return;
}
}
// "windows.keys" entry doesn't exist in the metadata - append it.
// This occurs when running password-reset for the first time on an instance.
items.add(new Items().setKey("windows-keys").setValue(newItemString));
}
private KeyPair generateKeys() throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
// Key moduli for encryption/decryption are 2048 bits long.
keyGen.initialize(2048);
return keyGen.genKeyPair();
}
@SuppressWarnings("unchecked")
private JSONObject buildKeyMetadata(KeyPair pair) throws NoSuchAlgorithmException,
InvalidKeySpecException {
// Object used for storing the metadata values.
JSONObject metadataValues = new JSONObject();
// Encode the public key into the required JSON format.
metadataValues.putAll(jsonEncode(pair));
// Add username and email.
metadataValues.put("userName", USER_NAME);
metadataValues.put("email", EMAIL);
// Create the date on which the new keys expire.
Date now = new Date();
Date expireDate = new Date(now.getTime() + EXPIRE_TIME);
// Format the date to match rfc3339.
SimpleDateFormat rfc3339Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
rfc3339Format.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateString = rfc3339Format.format(expireDate);
// Encode the expiration date for the returned JSON dictionary.
metadataValues.put("expireOn", dateString);
return metadataValues;
}
@SuppressWarnings("unchecked")
private JSONObject jsonEncode(KeyPair keys) throws NoSuchAlgorithmException,
InvalidKeySpecException {
KeyFactory factory = KeyFactory.getInstance("RSA");
// Get the RSA spec for key manipulation.
RSAPublicKeySpec pubSpec = factory.getKeySpec(keys.getPublic(), RSAPublicKeySpec.class);
// Extract required parts of the key.
BigInteger modulus = pubSpec.getModulus();
BigInteger exponent = pubSpec.getPublicExponent();
// Grab an encoder for the modulus and exponent to encode using RFC 3548;
// Java SE 7 requires an external library (Google's Guava used here)
// Java SE 8 has a built-in Base64 class that can be used instead. Apache also has an RFC 3548
// encoder.
BaseEncoding stringEncoder = BaseEncoding.base64();
// Strip out the leading 0 byte in the modulus.
byte[] arr = Arrays.copyOfRange(modulus.toByteArray(), 1, modulus.toByteArray().length);
JSONObject returnJson = new JSONObject();
// Encode the modulus, add to returned JSON object.
String modulusString = stringEncoder.encode(arr).replaceAll("\n", "");
returnJson.put("modulus", modulusString);
// Encode exponent, add to returned JSON object.
String exponentString = stringEncoder.encode(exponent.toByteArray()).replaceAll("\n", "");
returnJson.put("exponent", exponentString);
return returnJson;
}
}