Skip to content

Commit

Permalink
Merge pull request #1026 from watson-developer-cloud/handle-bad-crede…
Browse files Browse the repository at this point in the history
…ntial-chars

Gracefully handle common errors in credential values
  • Loading branch information
lpatino10 authored Jan 9, 2019
2 parents 9681f5d + 0a30e16 commit 0b12911
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 308 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 6.11.1
current_version = 6.11.2
commit = True
message = [skip ci] docs: Update version numbers from {current_version} -> {new_version}

Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ All the services:
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>6.11.1</version>
<version>6.11.2</version>
</dependency>
```

Expand All @@ -71,7 +71,7 @@ Only Discovery:
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>discovery</artifactId>
<version>6.11.1</version>
<version>6.11.2</version>
</dependency>
```

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

```gradle
'com.ibm.watson.developer_cloud:java-sdk:6.11.1'
'com.ibm.watson.developer_cloud:java-sdk:6.11.2'
```

Only Assistant:

```gradle
'com.ibm.watson.developer_cloud:assistant:6.11.1'
'com.ibm.watson.developer_cloud:assistant:6.11.2'
```

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

```gradle
'com.ibm.watson.developer_cloud:speech-to-text:6.11.2-SNAPSHOT'
'com.ibm.watson.developer_cloud:speech-to-text:6.11.3-SNAPSHOT'
```

##### JAR
Expand Down Expand Up @@ -348,7 +348,7 @@ Gradle:

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


[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
[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
4 changes: 2 additions & 2 deletions assistant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>assistant</artifactId>
<version>6.11.1</version>
<version>6.11.2</version>
</dependency>
```

##### Gradle
```gradle
'com.ibm.watson.developer_cloud:assistant:6.11.1'
'com.ibm.watson.developer_cloud:assistant:6.11.2'
```

## Usage
Expand Down
4 changes: 2 additions & 2 deletions conversation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ Conversation will be removed in the next major release. Please migrate to Assist
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>conversation</artifactId>
<version>6.11.1</version>
<version>6.11.2</version>
</dependency>
```

##### Gradle
```gradle
'com.ibm.watson.developer_cloud:conversation:6.11.1'
'com.ibm.watson.developer_cloud:conversation:6.11.2'
```

## Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,31 @@ public abstract class WatsonService {
*/
public WatsonService(final String name) {
this.name = name;
String iamApiKey = CredentialUtils.getIAMKey(name);
String iamUrl = CredentialUtils.getIAMUrl(name);
if (iamApiKey != null) {
setCredentialFields(CredentialUtils.getCredentialsFromVcap(name));
client = configureHttpClient();
}

/**
* Calls appropriate methods to set credential values based on parsed ServiceCredentials object.
*
* @param serviceCredentials object containing parsed credential values
*/
private void setCredentialFields(CredentialUtils.ServiceCredentials serviceCredentials) {
setEndPoint(serviceCredentials.getUrl());

if ((serviceCredentials.getUsername() != null) && (serviceCredentials.getPassword() != null)) {
setUsernameAndPassword(serviceCredentials.getUsername(), serviceCredentials.getPassword());
} else if (serviceCredentials.getOldApiKey() != null) {
setApiKey(serviceCredentials.getOldApiKey());
}

if (serviceCredentials.getIamApiKey() != null) {
IamOptions iamOptions = new IamOptions.Builder()
.apiKey(iamApiKey)
.url(iamUrl)
.apiKey(serviceCredentials.getIamApiKey())
.url(serviceCredentials.getIamUrl())
.build();
tokenManager = new IamTokenManager(iamOptions);
this.tokenManager = new IamTokenManager(iamOptions);
}
apiKey = CredentialUtils.getAPIKey(name);
String url = CredentialUtils.getAPIUrl(name);
if ((url != null) && !url.isEmpty()) {
// The VCAP_SERVICES will typically contain a url. If present use it.
setEndPoint(url);
}

client = configureHttpClient();
}

/**
Expand Down Expand Up @@ -338,6 +346,11 @@ public String getName() {
* @param apiKey the new API key
*/
public void setApiKey(String apiKey) {
if (CredentialUtils.hasBadStartOrEndChar(apiKey)) {
throw new IllegalArgumentException("The API key shouldn't start or end with curly brackets or quotes. Please "
+ "remove any surrounding {, }, or \" characters.");
}

if (this.endPoint.equals(this.defaultEndPoint)) {
this.endPoint = "https://gateway-a.watsonplatform.net/visual-recognition/api";
}
Expand Down Expand Up @@ -373,6 +386,11 @@ protected void setAuthentication(final Builder builder) {
* @param endPoint the new end point. Will be ignored if empty or null
*/
public void setEndPoint(final String endPoint) {
if (CredentialUtils.hasBadStartOrEndChar(endPoint)) {
throw new IllegalArgumentException("The URL shouldn't start or end with curly brackets or quotes. Please "
+ "remove any surrounding {, }, or \" characters.");
}

if ((endPoint != null) && !endPoint.isEmpty()) {
String newEndPoint = endPoint.endsWith("/") ? endPoint.substring(0, endPoint.length() - 1) : endPoint;
if (this.endPoint == null) {
Expand All @@ -389,6 +407,11 @@ public void setEndPoint(final String endPoint) {
* @param password the password
*/
public void setUsernameAndPassword(final String username, final String password) {
if (CredentialUtils.hasBadStartOrEndChar(username) || CredentialUtils.hasBadStartOrEndChar(password)) {
throw new IllegalArgumentException("The username and password shouldn't start or end with curly brackets or "
+ "quotes. Please remove any surrounding {, }, or \" characters.");
}

// we'll perform the token exchange for users UNLESS they're on ICP
if (username.equals(APIKEY_AS_USERNAME) && !password.startsWith(ICP_PREFIX)) {
IamOptions iamOptions = new IamOptions.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.ibm.watson.developer_cloud.http.HttpMediaType;
import com.ibm.watson.developer_cloud.http.RequestBuilder;
import com.ibm.watson.developer_cloud.http.ResponseConverter;
import com.ibm.watson.developer_cloud.util.CredentialUtils;
import com.ibm.watson.developer_cloud.util.ResponseConverterUtils;
import okhttp3.Call;
import okhttp3.FormBody;
Expand Down Expand Up @@ -44,7 +45,13 @@ public class IamTokenManager {
private static final String REFRESH_TOKEN = "refresh_token";

public IamTokenManager(IamOptions options) {
this.apiKey = options.getApiKey();
if (options.getApiKey() != null) {
if (CredentialUtils.hasBadStartOrEndChar(options.getApiKey())) {
throw new IllegalArgumentException("The IAM API key shouldn't start or end with curly brackets or quotes. "
+ "Please remove any surrounding {, }, or \" characters.");
}
this.apiKey = options.getApiKey();
}
this.url = (options.getUrl() != null) ? options.getUrl() : DEFAULT_IAM_URL;
this.userManagedAccessToken = options.getAccessToken();
tokenData = new IamToken();
Expand Down
Loading

0 comments on commit 0b12911

Please sign in to comment.