Skip to content

Commit 40fa057

Browse files
authored
Merge pull request #22 from IBM/iam_credential_file_fix
Credential file IAM fix
2 parents 6577ca1 + 6307b0c commit 40fa057

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ To find the correct version to specify in your dependency definitions, consult t
1414
<dependency>
1515
<groupId>com.ibm.cloud</groupId>
1616
<artifactId>sdk-core</artifactId>
17-
<version>1.1.0</version>
17+
<version>3.0.3</version>
1818
</dependency>
1919
```
2020

2121
##### Gradle
2222

2323
```gradle
24-
'com.ibm.cloud:sdk-core:1.1.0'
24+
'com.ibm.cloud:sdk-core:3.0.3'
2525
```
2626
# Issues
2727

ibm-credentials.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
NATURAL_LANGUAGE_CLASSIFIER_APIKEY=123456789
1+
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=123456789
22
NATURAL_LANGUAGE_CLASSIFIER_URL=https://gateway.watsonplatform.net/natural-language-classifier/api

src/main/java/com/ibm/cloud/sdk/core/service/exception/ServiceResponseException.java

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class ServiceResponseException extends RuntimeException {
3434
private static final String ERRORS_KEY = "errors";
3535
private static final String MESSAGE_STRING = "message";
3636
private static final String ERROR_STRING = "error";
37+
private static final String ERROR_MESSAGE = "errorMessage";
3738

3839
private static final Type debuggingInfoType = new TypeToken<Map<String, Object>>() { }.getType();
3940

@@ -65,6 +66,8 @@ public ServiceResponseException(int statusCode, Response response) {
6566
this.message = jsonObject.remove(ERROR_STRING).getAsString();
6667
} else if (jsonObject.has(MESSAGE_STRING)) {
6768
this.message = jsonObject.remove(MESSAGE_STRING).getAsString();
69+
} else if (jsonObject.has(ERROR_MESSAGE)) {
70+
this.message = jsonObject.remove(ERROR_MESSAGE).getAsString();
6871
}
6972
this.debuggingInfo = GsonSingleton.getGson().fromJson(jsonObject, debuggingInfoType);
7073
} catch (final Exception e) {

src/main/java/com/ibm/cloud/sdk/core/service/security/IamTokenManager.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ibm.cloud.sdk.core.http.HttpMediaType;
1818
import com.ibm.cloud.sdk.core.http.RequestBuilder;
1919
import com.ibm.cloud.sdk.core.http.ResponseConverter;
20+
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
2021
import com.ibm.cloud.sdk.core.util.CredentialUtils;
2122
import com.ibm.cloud.sdk.core.util.ResponseConverterUtils;
2223
import okhttp3.Call;
@@ -163,7 +164,7 @@ private boolean isAccessTokenExpired() {
163164
* if it has been at least 7 days and 1 hour since the last token was
164165
* retrieved.
165166
*
166-
* @returns whether the current managed refresh token is expired or not
167+
* @return whether the current managed refresh token is expired or not
167168
*/
168169
private boolean isRefreshTokenExpired() {
169170
if (tokenData.getExpiration() == null) {
@@ -193,6 +194,12 @@ public void run() {
193194

194195
try {
195196
okhttp3.Response response = call.execute();
197+
198+
// handle possible errors
199+
if (response.code() >= 400) {
200+
throw new ServiceResponseException(response.code(), response);
201+
}
202+
196203
returnToken[0] = converter.convert(response);
197204
} catch (IOException e) {
198205
LOG.severe(ERROR_MESSAGE);

src/main/java/com/ibm/cloud/sdk/core/util/CredentialUtils.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ public boolean isEmpty() {
148148
private static final String PASSWORD = "password";
149149
private static final String OLD_APIKEY = "api_key";
150150
private static final String URL = "url";
151-
private static final String IAM_APIKEY = "apikey";
151+
private static final String IAM_APIKEY = "iam_apikey";
152+
// this value was used previously for IAM API keys as well
153+
private static final String APIKEY = "apikey";
152154
private static final String IAM_URL = "iam_url";
153155

154156
private CredentialUtils() {
@@ -444,6 +446,7 @@ private static ServiceCredentials setCredentialFields(String serviceName, List<S
444446
case URL:
445447
serviceCredentials.url = credentialValue;
446448
break;
449+
case APIKEY:
447450
case IAM_APIKEY:
448451
serviceCredentials.iamApiKey = credentialValue;
449452
break;

0 commit comments

Comments
 (0)