Skip to content

Commit eebccab

Browse files
authored
feat(*): remove obsolete methods from BaseService (#40)
Fixes: https://github.ibm.com/arf/planning-sdk-squad/issues/904 BREAKING CHANGE: This PR removes support for the standalone "apikey" authentication path in the BaseService class, including the removal of "setApiKey()". Going forward, authentication should be configured on a service instance using generated service stors or the BaseService.setAuthenticator() method.
1 parent 80b8099 commit eebccab

File tree

5 files changed

+95
-55
lines changed

5 files changed

+95
-55
lines changed

src/main/java/com/ibm/cloud/sdk/core/security/icp4d/ICP4DAuthenticator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
/**
3434
* This class implements support for the ICP4D authentication mechanism.
3535
*/
36+
@SuppressWarnings("unused")
3637
public class ICP4DAuthenticator implements Authenticator {
3738
private static final Logger LOG = Logger.getLogger(ICP4DAuthenticator.class.getName());
3839

src/main/java/com/ibm/cloud/sdk/core/security/icp4d/ICP4DConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public String authenticationType() {
4040

4141
@Override
4242
public void validate() {
43-
if (StringUtils.isEmpty(url)) {
44-
throw new IllegalArgumentException("You must provide a URL.");
45-
}
46-
47-
// If the user specifies their own access token, then username/password are not required.
43+
// If the user specifies their own access token, then no other properties are required.
4844
if (StringUtils.isNotEmpty(userManagedAccessToken)) {
4945
return;
5046
}
5147

48+
if (StringUtils.isEmpty(url)) {
49+
throw new IllegalArgumentException("You must provide a URL.");
50+
}
51+
5252
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
5353
throw new IllegalArgumentException(
5454
"You must provide both username and password values.");

src/main/java/com/ibm/cloud/sdk/core/service/BaseService.java

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 IBM Corp. All Rights Reserved.
2+
* Copyright 2017, 2019 IBM Corp. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -63,11 +63,9 @@ public abstract class BaseService {
6363
private static final String APIKEY_AS_USERNAME = "apikey";
6464
private static final String ICP_PREFIX = "icp-";
6565
private static final Logger LOG = Logger.getLogger(BaseService.class.getName());
66-
private String apiKey;
6766
private String username;
6867
private String password;
6968
private String endPoint;
70-
private String defaultEndPoint;
7169
private final String name;
7270
private Authenticator authenticator;
7371

@@ -131,8 +129,6 @@ private void setCredentialFields(CredentialUtils.ServiceCredentials serviceCrede
131129

132130
if ((serviceCredentials.getUsername() != null) && (serviceCredentials.getPassword() != null)) {
133131
setUsernameAndPassword(serviceCredentials.getUsername(), serviceCredentials.getPassword());
134-
} else if (serviceCredentials.getOldApiKey() != null) {
135-
setApiKey(serviceCredentials.getOldApiKey());
136132
}
137133

138134
if (serviceCredentials.getIamApiKey() != null) {
@@ -240,18 +236,6 @@ protected final <T> ServiceCall<T> createServiceCall(final Request request, fina
240236
return new WatsonServiceCall<>(call, converter);
241237
}
242238

243-
/**
244-
* Gets the API key.
245-
*
246-
*
247-
* @return the API key
248-
* @deprecated
249-
*/
250-
@Deprecated
251-
protected String getApiKey() {
252-
return apiKey;
253-
}
254-
255239
/**
256240
* Gets the username.
257241
*
@@ -270,6 +254,7 @@ protected String getUsername() {
270254
*
271255
* @return the password
272256
*/
257+
@Deprecated
273258
protected String getPassword() {
274259
return password;
275260
}
@@ -305,28 +290,13 @@ public String getName() {
305290
}
306291

307292
/**
308-
* Sets the API key.
293+
* Sets the authentication information on the specified request builder.
294+
* This method will invoke the configured Authenticator instance to perform the
295+
* authentication needed for that authentication type. This could involve adding an
296+
* Authorization header, or perhaps adding a specific query param to the request URL.
309297
*
310-
* @param apiKey the new API key
311-
* @deprecated Use setAuthenticator(AuthenticatorConfig) instead
312-
*/
313-
@Deprecated
314-
public void setApiKey(String apiKey) {
315-
if (CredentialUtils.hasBadStartOrEndChar(apiKey)) {
316-
throw new IllegalArgumentException("The API key shouldn't start or end with curly brackets or quotes. Please "
317-
+ "remove any surrounding {, }, or \" characters.");
318-
}
319-
320-
if (this.endPoint.equals(this.defaultEndPoint)) {
321-
this.endPoint = "https://gateway-a.watsonplatform.net/visual-recognition/api";
322-
}
323-
this.apiKey = apiKey;
324-
}
325-
326-
/**
327-
* Sets the authentication. Okhttp3 compliant.
328-
*
329-
* @param builder the new authentication
298+
* @param builder the request builder that represents the outgoing requst on which
299+
* the authentication information should be set.
330300
*/
331301
protected void setAuthentication(final Builder builder) {
332302
if (this.skipAuthentication) {
@@ -353,15 +323,12 @@ public void setEndPoint(final String endPoint) {
353323

354324
if ((endPoint != null) && !endPoint.isEmpty()) {
355325
String newEndPoint = endPoint.endsWith("/") ? endPoint.substring(0, endPoint.length() - 1) : endPoint;
356-
if (this.endPoint == null) {
357-
this.defaultEndPoint = newEndPoint;
358-
}
359326
this.endPoint = newEndPoint;
360327
}
361328
}
362329

363330
/**
364-
* Sets the username and password.
331+
* Sets the username and password on this instance.
365332
*
366333
* @param username the username
367334
* @param password the password
@@ -374,13 +341,19 @@ public void setUsernameAndPassword(final String username, final String password)
374341
+ "quotes. Please remove any surrounding {, }, or \" characters.");
375342
}
376343

377-
// we'll perform the token exchange for users UNLESS they're on ICP
344+
// These fields are set to provide compatibility with the getUsername() and getPassword() methods.
345+
this.username = username;
346+
this.password = password;
347+
348+
// If this method is being called to set the IAM api key, we'll configure the IAM authenticator.
349+
// Note that we only do this if the password does NOT indicate ICP.
378350
if (username.equals(APIKEY_AS_USERNAME) && !password.startsWith(ICP_PREFIX)) {
379351
IamOptions iamOptions = new IamOptions.Builder()
380352
.apiKey(password)
381353
.build();
382354
setAuthenticator(iamOptions);
383355
} else {
356+
// Otherwise, we'll just use the username and password to configure basic auth.
384357
BasicAuthConfig basicAuthConfig = new BasicAuthConfig.Builder()
385358
.username(username)
386359
.password(password)
@@ -418,15 +391,19 @@ public void setIamCredentials(IamOptions iamOptions) {
418391
}
419392

420393
/**
421-
* Initializes a new Authenticator instance based on the input AuthenticatorConfig instance and sets it as
422-
* the current authenticator on the BaseService instance.
394+
* Initializes a new Authenticator instance based on the input AuthenticatorConfig instance,
395+
* and sets it as the current authenticator on this BaseService instance.
396+
* The "skipAuthentication" flag is updated appropriately by this method.
423397
* @param authConfig the AuthenticatorConfig instance containing the authentication configuration
424398
*/
425-
protected void setAuthenticator(AuthenticatorConfig authConfig) {
399+
public void setAuthenticator(AuthenticatorConfig authConfig) {
426400
try {
427-
this.authenticator = AuthenticatorFactory.getAuthenticator(authConfig);
428-
if (authenticator instanceof NoauthAuthenticator) {
429-
setSkipAuthentication(true);
401+
if (authConfig == null) {
402+
this.authenticator = null;
403+
this.skipAuthentication = false;
404+
} else {
405+
this.authenticator = AuthenticatorFactory.getAuthenticator(authConfig);
406+
this.skipAuthentication = (Authenticator.AUTHTYPE_NOAUTH.equals(authConfig.authenticationType()));
430407
}
431408
} catch (Exception e) {
432409
throw new RuntimeException(e);
@@ -503,14 +480,30 @@ protected <T> T processServiceCall(final ResponseConverter<T> converter, final R
503480
* Sets the skip authentication.
504481
*
505482
* @param skipAuthentication the new skip authentication
483+
*
484+
* @deprecated Use setAuthenticator(new NoauthConfig()) instead
506485
*/
486+
@Deprecated
507487
public void setSkipAuthentication(final boolean skipAuthentication) {
508488
this.skipAuthentication = skipAuthentication;
509489
if (this.skipAuthentication) {
510490
this.authenticator = new NoauthAuthenticator((NoauthConfig) null);
491+
} else {
492+
// If we're setting skipAuthentication to false, make sure we
493+
// clear out the authenticator if it's currently set to "noauth".
494+
if (Authenticator.AUTHTYPE_NOAUTH.equals(this.authenticator.authenticationType())) {
495+
this.authenticator = null;
496+
}
511497
}
512498
}
513499

500+
/**
501+
* Returns true iff this instance has been configured to bypass authentication for outgoing requests.
502+
* @return true to indicate authentication is being bypassed, false otherwise.
503+
*
504+
* @deprecated
505+
*/
506+
@Deprecated
514507
public boolean isSkipAuthentication() {
515508
return this.skipAuthentication;
516509
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 IBM Corp. All Rights Reserved.
2+
* Copyright 2018, 2019 IBM Corp. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at

src/test/java/com/ibm/cloud/sdk/core/test/service/AuthenticationTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.junit.Test;
1717

1818
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNull;
1920
import static org.junit.Assert.assertTrue;
2021

2122
@SuppressWarnings("deprecation")
@@ -107,6 +108,51 @@ public void testSkipAuthentication() {
107108
assertTrue(service.isSkipAuthentication());
108109
}
109110

111+
@Test
112+
public void testDefaultServiceCtor() {
113+
TestService service = new TestService();
114+
assertFalse(service.isSkipAuthentication());
115+
assertNull(service.authenticator());
116+
}
117+
118+
@Test
119+
public void testSetAuthenticator() {
120+
// Test use of setAuthenticator() on existing service instance.
121+
TestService service = new TestService();
122+
assertFalse(service.isSkipAuthentication());
123+
assertNull(service.authenticator());
124+
125+
service.setAuthenticator(new NoauthConfig());
126+
assertTrue(service.authenticator() instanceof NoauthAuthenticator);
127+
assertTrue(service.isSkipAuthentication());
128+
129+
service.setAuthenticator(null);
130+
assertNull(service.authenticator());
131+
assertFalse(service.isSkipAuthentication());
132+
133+
BasicAuthConfig baConfig = new BasicAuthConfig.Builder()
134+
.username("user")
135+
.password("pw")
136+
.build();
137+
service.setAuthenticator(baConfig);
138+
assertTrue(service.authenticator() instanceof BasicAuthenticator);
139+
assertFalse(service.isSkipAuthentication());
140+
141+
IamOptions iamConfig = new IamOptions.Builder()
142+
.apiKey("myapikey")
143+
.build();
144+
service.setAuthenticator(iamConfig);
145+
assertTrue(service.authenticator() instanceof IamTokenManager);
146+
assertFalse(service.isSkipAuthentication());
147+
148+
ICP4DConfig icp4dConfig = new ICP4DConfig.Builder()
149+
.userManagedAccessToken("myuseraccesstoken")
150+
.build();
151+
service.setAuthenticator(icp4dConfig);
152+
assertTrue(service.authenticator() instanceof ICP4DAuthenticator);
153+
assertFalse(service.isSkipAuthentication());
154+
}
155+
110156
@Test
111157
public void multiAuthenticationWithMultiBindSameServiceOnVcapService() {
112158

0 commit comments

Comments
 (0)