Skip to content

Commit 0b12911

Browse files
authored
Merge pull request #1026 from watson-developer-cloud/handle-bad-credential-chars
Gracefully handle common errors in credential values
2 parents 9681f5d + 0a30e16 commit 0b12911

File tree

18 files changed

+219
-308
lines changed

18 files changed

+219
-308
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 6.11.1
2+
current_version = 6.11.2
33
commit = True
44
message = [skip ci] docs: Update version numbers from {current_version} -> {new_version}
55

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ All the services:
6161
<dependency>
6262
<groupId>com.ibm.watson.developer_cloud</groupId>
6363
<artifactId>java-sdk</artifactId>
64-
<version>6.11.1</version>
64+
<version>6.11.2</version>
6565
</dependency>
6666
```
6767

@@ -71,7 +71,7 @@ Only Discovery:
7171
<dependency>
7272
<groupId>com.ibm.watson.developer_cloud</groupId>
7373
<artifactId>discovery</artifactId>
74-
<version>6.11.1</version>
74+
<version>6.11.2</version>
7575
</dependency>
7676
```
7777

@@ -80,13 +80,13 @@ Only Discovery:
8080
All the services:
8181

8282
```gradle
83-
'com.ibm.watson.developer_cloud:java-sdk:6.11.1'
83+
'com.ibm.watson.developer_cloud:java-sdk:6.11.2'
8484
```
8585

8686
Only Assistant:
8787

8888
```gradle
89-
'com.ibm.watson.developer_cloud:assistant:6.11.1'
89+
'com.ibm.watson.developer_cloud:assistant:6.11.2'
9090
```
9191

9292
##### Development snapshots
@@ -109,7 +109,7 @@ And then reference the snapshot version on your app module gradle
109109
Only Speech to Text:
110110

111111
```gradle
112-
'com.ibm.watson.developer_cloud:speech-to-text:6.11.2-SNAPSHOT'
112+
'com.ibm.watson.developer_cloud:speech-to-text:6.11.3-SNAPSHOT'
113113
```
114114

115115
##### JAR
@@ -348,7 +348,7 @@ Gradle:
348348

349349
```sh
350350
cd java-sdk
351-
gradle jar # build jar file (build/libs/watson-developer-cloud-6.11.1.jar)
351+
gradle jar # build jar file (build/libs/watson-developer-cloud-6.11.2.jar)
352352
gradle test # run tests
353353
gradle check # performs quality checks on source files and generates reports
354354
gradle testReport # run tests and generate the aggregated test report (build/reports/allTests)
@@ -401,4 +401,4 @@ or [Stack Overflow](http://stackoverflow.com/questions/ask?tags=ibm-watson).
401401
[ibm-cloud-onboarding]: http://console.bluemix.net/registration?target=/developer/watson&cm_sp=WatsonPlatform-WatsonServices-_-OnPageNavLink-IBMWatson_SDKs-_-Java
402402

403403

404-
[jar]: https://github.com/watson-developer-cloud/java-sdk/releases/download/java-sdk-6.11.1/java-sdk-6.11.1-jar-with-dependencies.jar
404+
[jar]: https://github.com/watson-developer-cloud/java-sdk/releases/download/java-sdk-6.11.2/java-sdk-6.11.2-jar-with-dependencies.jar

assistant/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<dependency>
88
<groupId>com.ibm.watson.developer_cloud</groupId>
99
<artifactId>assistant</artifactId>
10-
<version>6.11.1</version>
10+
<version>6.11.2</version>
1111
</dependency>
1212
```
1313

1414
##### Gradle
1515
```gradle
16-
'com.ibm.watson.developer_cloud:assistant:6.11.1'
16+
'com.ibm.watson.developer_cloud:assistant:6.11.2'
1717
```
1818

1919
## Usage

conversation/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ Conversation will be removed in the next major release. Please migrate to Assist
1010
<dependency>
1111
<groupId>com.ibm.watson.developer_cloud</groupId>
1212
<artifactId>conversation</artifactId>
13-
<version>6.11.1</version>
13+
<version>6.11.2</version>
1414
</dependency>
1515
```
1616

1717
##### Gradle
1818
```gradle
19-
'com.ibm.watson.developer_cloud:conversation:6.11.1'
19+
'com.ibm.watson.developer_cloud:conversation:6.11.2'
2020
```
2121

2222
## Usage

core/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,31 @@ public abstract class WatsonService {
117117
*/
118118
public WatsonService(final String name) {
119119
this.name = name;
120-
String iamApiKey = CredentialUtils.getIAMKey(name);
121-
String iamUrl = CredentialUtils.getIAMUrl(name);
122-
if (iamApiKey != null) {
120+
setCredentialFields(CredentialUtils.getCredentialsFromVcap(name));
121+
client = configureHttpClient();
122+
}
123+
124+
/**
125+
* Calls appropriate methods to set credential values based on parsed ServiceCredentials object.
126+
*
127+
* @param serviceCredentials object containing parsed credential values
128+
*/
129+
private void setCredentialFields(CredentialUtils.ServiceCredentials serviceCredentials) {
130+
setEndPoint(serviceCredentials.getUrl());
131+
132+
if ((serviceCredentials.getUsername() != null) && (serviceCredentials.getPassword() != null)) {
133+
setUsernameAndPassword(serviceCredentials.getUsername(), serviceCredentials.getPassword());
134+
} else if (serviceCredentials.getOldApiKey() != null) {
135+
setApiKey(serviceCredentials.getOldApiKey());
136+
}
137+
138+
if (serviceCredentials.getIamApiKey() != null) {
123139
IamOptions iamOptions = new IamOptions.Builder()
124-
.apiKey(iamApiKey)
125-
.url(iamUrl)
140+
.apiKey(serviceCredentials.getIamApiKey())
141+
.url(serviceCredentials.getIamUrl())
126142
.build();
127-
tokenManager = new IamTokenManager(iamOptions);
143+
this.tokenManager = new IamTokenManager(iamOptions);
128144
}
129-
apiKey = CredentialUtils.getAPIKey(name);
130-
String url = CredentialUtils.getAPIUrl(name);
131-
if ((url != null) && !url.isEmpty()) {
132-
// The VCAP_SERVICES will typically contain a url. If present use it.
133-
setEndPoint(url);
134-
}
135-
136-
client = configureHttpClient();
137145
}
138146

139147
/**
@@ -338,6 +346,11 @@ public String getName() {
338346
* @param apiKey the new API key
339347
*/
340348
public void setApiKey(String apiKey) {
349+
if (CredentialUtils.hasBadStartOrEndChar(apiKey)) {
350+
throw new IllegalArgumentException("The API key shouldn't start or end with curly brackets or quotes. Please "
351+
+ "remove any surrounding {, }, or \" characters.");
352+
}
353+
341354
if (this.endPoint.equals(this.defaultEndPoint)) {
342355
this.endPoint = "https://gateway-a.watsonplatform.net/visual-recognition/api";
343356
}
@@ -373,6 +386,11 @@ protected void setAuthentication(final Builder builder) {
373386
* @param endPoint the new end point. Will be ignored if empty or null
374387
*/
375388
public void setEndPoint(final String endPoint) {
389+
if (CredentialUtils.hasBadStartOrEndChar(endPoint)) {
390+
throw new IllegalArgumentException("The URL shouldn't start or end with curly brackets or quotes. Please "
391+
+ "remove any surrounding {, }, or \" characters.");
392+
}
393+
376394
if ((endPoint != null) && !endPoint.isEmpty()) {
377395
String newEndPoint = endPoint.endsWith("/") ? endPoint.substring(0, endPoint.length() - 1) : endPoint;
378396
if (this.endPoint == null) {
@@ -389,6 +407,11 @@ public void setEndPoint(final String endPoint) {
389407
* @param password the password
390408
*/
391409
public void setUsernameAndPassword(final String username, final String password) {
410+
if (CredentialUtils.hasBadStartOrEndChar(username) || CredentialUtils.hasBadStartOrEndChar(password)) {
411+
throw new IllegalArgumentException("The username and password shouldn't start or end with curly brackets or "
412+
+ "quotes. Please remove any surrounding {, }, or \" characters.");
413+
}
414+
392415
// we'll perform the token exchange for users UNLESS they're on ICP
393416
if (username.equals(APIKEY_AS_USERNAME) && !password.startsWith(ICP_PREFIX)) {
394417
IamOptions iamOptions = new IamOptions.Builder()

core/src/main/java/com/ibm/watson/developer_cloud/service/security/IamTokenManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ibm.watson.developer_cloud.http.HttpMediaType;
1818
import com.ibm.watson.developer_cloud.http.RequestBuilder;
1919
import com.ibm.watson.developer_cloud.http.ResponseConverter;
20+
import com.ibm.watson.developer_cloud.util.CredentialUtils;
2021
import com.ibm.watson.developer_cloud.util.ResponseConverterUtils;
2122
import okhttp3.Call;
2223
import okhttp3.FormBody;
@@ -44,7 +45,13 @@ public class IamTokenManager {
4445
private static final String REFRESH_TOKEN = "refresh_token";
4546

4647
public IamTokenManager(IamOptions options) {
47-
this.apiKey = options.getApiKey();
48+
if (options.getApiKey() != null) {
49+
if (CredentialUtils.hasBadStartOrEndChar(options.getApiKey())) {
50+
throw new IllegalArgumentException("The IAM API key shouldn't start or end with curly brackets or quotes. "
51+
+ "Please remove any surrounding {, }, or \" characters.");
52+
}
53+
this.apiKey = options.getApiKey();
54+
}
4855
this.url = (options.getUrl() != null) ? options.getUrl() : DEFAULT_IAM_URL;
4956
this.userManagedAccessToken = options.getAccessToken();
5057
tokenData = new IamToken();

0 commit comments

Comments
 (0)