/
ExpressConnection.java
648 lines (571 loc) · 17.5 KB
/
ExpressConnection.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*******************************************************************************
* Copyright (c) 2012 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.openshift.express.internal.core.connection;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.jboss.tools.openshift.common.core.ICredentialsPrompter;
import org.jboss.tools.openshift.common.core.connection.AbstractConnection;
import org.jboss.tools.openshift.common.core.connection.ConnectionType;
import org.jboss.tools.openshift.common.core.connection.IConnection;
import org.jboss.tools.openshift.common.core.utils.StringUtils;
import org.jboss.tools.openshift.common.core.utils.UrlUtils;
import org.jboss.tools.openshift.express.client.ClientSystemProperties;
import org.jboss.tools.openshift.express.core.util.ExpressConnectionUtils;
import org.jboss.tools.openshift.express.internal.core.ExpressCoreActivator;
import org.jboss.tools.openshift.express.internal.core.preferences.ExpressCorePreferences;
import org.jboss.tools.openshift.express.internal.core.util.ExpressResourceLabelUtils;
import org.jboss.tools.openshift.internal.common.core.UsageStats;
import org.jboss.tools.openshift.internal.common.core.security.OpenShiftSecureStorageKey;
import org.jboss.tools.openshift.internal.common.core.security.SecureStore;
import org.jboss.tools.openshift.internal.common.core.security.SecureStoreException;
import com.openshift.client.ApplicationScale;
import com.openshift.client.ConnectionBuilder;
import com.openshift.client.IApplication;
import com.openshift.client.IDomain;
import com.openshift.client.IGearProfile;
import com.openshift.client.IHttpClient.ISSLCertificateCallback;
import com.openshift.client.IOpenShiftSSHKey;
import com.openshift.client.IQuickstart;
import com.openshift.client.ISSHPublicKey;
import com.openshift.client.IUser;
import com.openshift.client.OpenShiftException;
import com.openshift.client.OpenShiftUnknonwSSHKeyTypeException;
import com.openshift.client.cartridge.ICartridge;
import com.openshift.client.cartridge.IEmbeddableCartridge;
import com.openshift.client.cartridge.IStandaloneCartridge;
import com.openshift.internal.client.utils.StreamUtils;
/**
* @author Rob Stryker
* @author Xavier Coulon
* @author Andre Dietisheim
*/
public class ExpressConnection extends AbstractConnection {
private static final String SECURE_STORAGE_PASSWORD = "pass";
/*
* Hard-code the openshift UI activator id, due to backwards compatability issues
*/
private static final String SECURE_STORAGE_BASEKEY = "org.jboss.tools.openshift.express.ui";
private String username;
private String password;
private IUser user;
private boolean isDomainLoaded;
private boolean rememberPassword;
private boolean promptPasswordEnabled = true;
private boolean didPromptForPassword;
private boolean passwordLoaded;
private ICredentialsPrompter passwordPrompter;
private ISSLCertificateCallback sslCallback;
public ExpressConnection(String host, ISSLCertificateCallback callback) {
this(null, null, UrlUtils.getScheme(host), UrlUtils.cutScheme(host), false, null, callback);
}
public ExpressConnection(String username, String host) {
this(username, null, host, false, null);
}
public ExpressConnection(String username, String host, ICredentialsPrompter prompter, ISSLCertificateCallback sslCallback) {
this(username, null, UrlUtils.getScheme(host), UrlUtils.cutScheme(host), false, null, sslCallback);
this.passwordPrompter = prompter;
}
public ExpressConnection(String username, String password, String host, boolean rememberPassword, ISSLCertificateCallback sslCallback) {
this(username, password, UrlUtils.getScheme(host), UrlUtils.cutScheme(host), rememberPassword, null, sslCallback);
}
protected ExpressConnection(String username, String password, String scheme, String host, boolean rememberPassword, IUser user, ISSLCertificateCallback sslCallback) {
super(scheme, host);
this.username = username;
this.password = password;
this.rememberPassword = rememberPassword;
this.sslCallback = sslCallback;
setUser(user);
}
protected void setUser(IUser user) {
this.user = user;
}
@Override
public String getUsername() {
return username;
}
@Override
public void setUsername(String username) {
firePropertyChange(PROPERTY_USERNAME, this.username, this.username = username);
clearUser();
}
@Override
public String getPassword() {
loadPassword();
return password;
}
@Override
public void setPassword(String password) {
firePropertyChange(PROPERTY_PASSWORD, this.password, this.password = password);
this.passwordLoaded = true;
clearUser();
}
@Override
public String getHost() {
if (isDefaultHost()) {
return ExpressConnectionUtils.getDefaultHostUrl();
}
return super.getHost();
}
@Override
public String getScheme() {
if (isDefaultHost()) {
return UrlUtils.getScheme(ExpressConnectionUtils.getDefaultHostUrl());
}
return UrlUtils.getScheme(super.getHost());
}
@Override
public boolean isDefaultHost() {
return isDefaultHost(super.getHost());
}
private boolean isDefaultHost(String host) {
return host == null
|| UrlUtils.cutScheme(host).isEmpty();
}
@Override
public boolean isRememberPassword() {
return rememberPassword;
}
@Override
public final void setRememberPassword(boolean rememberPassword) {
firePropertyChange(PROPERTY_REMEMBER_PASSWORD, this.rememberPassword, this.rememberPassword = rememberPassword);
}
public boolean canPromptForPassword() {
return this.didPromptForPassword == false
&& promptPasswordEnabled;
}
@Override
public void enablePromptCredentials(boolean enable) {
this.promptPasswordEnabled = enable;
}
@Override
public boolean isEnablePromptCredentials() {
return promptPasswordEnabled;
}
public void setSSLCertificateCallback(ISSLCertificateCallback callback) {
this.sslCallback = callback;
}
/**
* Connects to OpenShift. Will do nothing if this user is already
* connected.
*
* @return <code>true</code> if connect succeeed, <code>false</code>
* otherwise
* @throws OpenShiftException
*/
@Override
public boolean connect() throws OpenShiftException {
if (isConnected()) {
save();
return true;
}
if (createUser()) {
/* JBIDE-15847: user may get rewritten in case of kerberos */
updateUsername(user);
save();
return true;
} else {
return false;
}
}
/**
* Creates an OpenShift user instance for this user. Prompts for
* credentials if needed.
*
* @return <code>true</code> if user could get created, <code>false</code>
* otherwise.
*/
protected boolean createUser() {
loadPassword();
if (password == null) {
return promptForCredentials();
} else {
setClientTimeout();
IUser user = doCreateUser();
setUser(user);
return true;
}
}
protected IUser doCreateUser() {
final String userId = ExpressCoreActivator.PLUGIN_ID + " " + ExpressCoreActivator.getDefault().getBundle().getVersion();
try {
return new ConnectionBuilder(getHost())
.credentials(username, password)
.clientId(userId)
.sslCertificateCallback(sslCallback)
.create()
.getUser();
} catch (IOException e) {
throw new OpenShiftException("Could not connect for user {0} - {1}", username, getHost());
}
}
private void setClientTimeout() {
int timeout = ExpressCorePreferences.INSTANCE
.getClientReadTimeout(ClientSystemProperties.getReadTimeoutSeconds());
ClientSystemProperties.setReadTimeoutSeconds(timeout);
}
/**
* Attempts to load the password from the secure storage, only at first time
* it is called.
*/
private void loadPassword() {
if (StringUtils.isEmpty(password)
&& !passwordLoaded) {
this.password = getPassword(getSecureStore(getHost(), getUsername()));
this.passwordLoaded = true;
this.rememberPassword = (password != null);
}
}
private boolean hasUser() {
return user != null;
}
protected void clearUser() {
this.user = null;
}
private String updateUsername(IUser user) {
if (!user.getRhlogin().equals(username)) {
ExpressCoreActivator.getDefault().getLog().log(
new Status(Status.WARNING, ExpressCoreActivator.PLUGIN_ID,
NLS.bind("User {0} was logged in as {1}", username, user.getRhlogin())));
}
this.username = user.getRhlogin();
return username;
}
private boolean promptForCredentials() {
if (passwordPrompter == null) {
return false;
}
try {
passwordPrompter.promptAndAuthenticate(this, null);
didPromptForPassword = true;
} catch (Exception e) {
ExpressCoreActivator.pluginLog().logError("Failed to retrieve User's password", e);
}
return hasUser();
}
public IApplication createApplication(final String applicationName, final IStandaloneCartridge standaloneCartridge,
final ApplicationScale scale, final IGearProfile gearProfile, final IDomain domain)
throws OpenShiftException {
if (connect()) {
return domain.createApplication(applicationName, standaloneCartridge, scale, gearProfile);
} else {
return null;
}
}
/**
* Creates a new domain with the given id
*
* @param id
* the domain id
* @return the created domain
* @throws OpenShiftException
* @throws SocketTimeoutException
*/
public IDomain createDomain(String id) throws OpenShiftException {
if (connect()) {
return user.createDomain(id);
} else {
return null;
}
}
public List<IStandaloneCartridge> getStandaloneCartridges() throws OpenShiftException {
if (connect()) {
return user.getConnection().getStandaloneCartridges();
} else {
return null;
}
}
public List<IEmbeddableCartridge> getEmbeddableCartridges() throws OpenShiftException {
if (connect()) {
return user.getConnection().getEmbeddableCartridges();
} else {
return null;
}
}
public List<ICartridge> getCartridges() throws OpenShiftException {
if (connect()) {
return user.getConnection().getCartridges();
} else {
return null;
}
}
public List<IQuickstart> getQuickstarts() throws OpenShiftException {
if (connect()) {
return user.getConnection().getQuickstarts();
} else {
return null;
}
}
public void load() {
getDomains();
}
public IApplication getApplication(String name, IDomain domain) throws OpenShiftException {
if (domain == null) {
return null;
}
return domain.getApplicationByName(name);
}
public boolean hasApplication(String name, IDomain domain) throws OpenShiftException {
return getApplication(name, domain) != null;
}
public IDomain getDefaultDomain() throws OpenShiftException {
if (connect()) {
return user.getDefaultDomain();
} else {
return null;
}
}
public IDomain getDomain(String id) throws OpenShiftException {
if (StringUtils.isEmpty(id)) {
return null;
}
// trigger authentication, domain loading
getDomains();
//if authentication failed, user is null
if(!isConnected()) {
return null;
}
return user.getDomain(id);
}
public IDomain getFirstDomain() throws OpenShiftException {
if (!connect()) {
return null;
} else {
List<IDomain> domains = getDomains();
if (domains == null
|| domains.isEmpty()) {
return null;
}
return domains.get(0);
}
}
public List<IDomain> getDomains() throws OpenShiftException {
if (!connect()) {
return Collections.emptyList();
} else {
List<IDomain> domains = user.getDomains();
isDomainLoaded = true;
return domains;
}
}
public void destroy(IDomain domain, boolean force) {
if (connect()) {
domain.destroy(force);
}
}
public boolean isLoaded() throws OpenShiftException {
return isDomainLoaded;
}
public boolean hasApplicationOfType(IStandaloneCartridge type) throws OpenShiftException {
if (hasDomain()) {
return user.getDefaultDomain().hasApplicationByCartridge(type);
}
return false;
}
public boolean hasDomain() throws OpenShiftException {
if (connect()) {
return user.hasDomain();
} else {
return false;
}
}
public boolean hasSSHKeys() throws OpenShiftException {
if (connect()) {
return !user.getSSHKeys().isEmpty();
} else {
return false;
}
}
@Override
public void refresh() throws OpenShiftException {
isDomainLoaded = false;
if (connect()) {
user.refresh();
}
}
@Override
public boolean isConnected() {
return hasUser();
}
public List<IOpenShiftSSHKey> getSSHKeys() throws OpenShiftException {
if (connect()) {
return user.getSSHKeys();
} else {
return Collections.emptyList();
}
}
public IOpenShiftSSHKey getSSHKeyByPublicKey(String publicKey) throws OpenShiftUnknonwSSHKeyTypeException,
OpenShiftException {
if (connect()) {
return user.getSSHKeyByPublicKey(publicKey);
} else {
return null;
}
}
public IOpenShiftSSHKey putSSHKey(String name, ISSHPublicKey key) throws OpenShiftException {
if (connect()) {
return user.putSSHKey(name, key);
} else {
return null;
}
}
public boolean hasSSHKeyName(String name) throws OpenShiftException {
if (connect()) {
return user.hasSSHKeyName(name);
} else {
return false;
}
}
public boolean hasSSHPublicKey(String publicKey) {
if (connect()) {
return user.hasSSHPublicKey(publicKey);
} else {
return false;
}
}
public void save() {
String username = getUsername();
if (!StringUtils.isEmpty(username)) {
ExpressCorePreferences.INSTANCE.saveLastUsername(username);
saveOrClearPassword(username, getHost(), getPassword());
}
}
private void saveOrClearPassword(String username, String host, String password) {
SecureStore store = getSecureStore(host, username);
if (store != null
&& !StringUtils.isEmpty(username)) {
try {
if (isRememberPassword()
&& !StringUtils.isEmpty(password)) {
store.put(SECURE_STORAGE_PASSWORD, password);
} else {
store.remove(SECURE_STORAGE_PASSWORD);
}
} catch (SecureStoreException e) {
firePropertyChange(SecureStoreException.ID, null, e);
//ExpressCoreActivator.pluginLog().logError(e.getMessage(), e);
}
}
}
private String getPassword(SecureStore store) {
String password = null;
if (store != null
&& !StringUtils.isEmpty(getUsername())) {
try {
password = store.get(SECURE_STORAGE_PASSWORD);
} catch (SecureStoreException e) {
ExpressCoreActivator.pluginLog().logError(e.getMessage(), e);
}
}
return password;
}
/**
* Returns a secure store for the current host and username
*/
private SecureStore getSecureStore(final String host, final String username) {
return new SecureStore(new OpenShiftSecureStorageKey(SECURE_STORAGE_BASEKEY, host, username));
}
public String getId() {
StringBuilder builder = new StringBuilder(username);
builder
.append(" at ")
.append(getHost());
if (isDefaultHost()) {
builder.append(" (default)");
}
return builder.toString();
}
@Override
public boolean canConnect() throws IOException {
// TODO: move to client library
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(getHost() + "/broker/rest/api").openConnection();
connection.setConnectTimeout(2 * 1000);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Accept", "application/json");
InputStream in = connection.getInputStream();
StreamUtils.readToString(in);
return connection.getResponseCode() == 200;
} catch (MalformedURLException e) {
return false;
} catch (SocketTimeoutException e) {
throw e;
} catch (IOException e) {
if (connection != null
// can throw IOException (ex. UnknownHostException)
&& connection.getResponseCode() != -1) {
return false;
} else {
throw e;
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
@Override
public IConnection clone() {
return new ExpressConnection(getUsername(), getPassword(), getScheme(), getHost(), isRememberPassword(), null, sslCallback);
}
@Override
public void update(IConnection connection) {
Assert.isLegal(connection instanceof ExpressConnection);
ExpressConnection otherConnection = (ExpressConnection) connection;
setUsername(otherConnection.getUsername());
setPassword(otherConnection.getPassword());
setRememberPassword(otherConnection.isRememberPassword());
setUser(otherConnection.user);
this.sslCallback = otherConnection.sslCallback;
}
@Override
public ConnectionType getType() {
return ConnectionType.Express;
}
@Override
public void notifyUsage() {
UsageStats.getInstance().newV2Connection(getHost());
}
@Override
public String toString() {
return ExpressResourceLabelUtils.toString(this);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (!(obj instanceof ExpressConnection))
return false;
ExpressConnection other = (ExpressConnection) obj;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}