Skip to content

Commit 9e02a6b

Browse files
committed
feat: add support for new service version
Signed-off-by: Lídia Tarcza <100163235+diatrcz@users.noreply.github.com>
1 parent 0d0bedf commit 9e02a6b

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

src/main/java/com/ibm/cloud/sdk/core/security/AuthenticatorBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class AuthenticatorBase {
3737
"iamAccountId must be specified if and only if iamProfileName is specified";
3838
public static final String ERRORMSG_PROP_INVALID_BOOL =
3939
"The %s property must be a valid boolean but was '%s'. Valid values are 'true' and 'false'.";
40+
public static final String ERRORMSG_INVALID_SERVICE_VERSION = "Invalid service version. Supported values are: %s";
4041

4142
/**
4243
* Returns a "Basic" Authorization header value for the specified username and password.

src/main/java/com/ibm/cloud/sdk/core/security/VpcInstanceAuthenticator.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
package com.ibm.cloud.sdk.core.security;
1515

16+
import java.util.ArrayList;
17+
import java.util.List;
1618
import java.util.Map;
1719
import java.util.logging.Level;
1820
import java.util.logging.Logger;
@@ -42,18 +44,22 @@ public class VpcInstanceAuthenticator
4244
private static final String defaultIMSEndpoint = "http://169.254.169.254";
4345
private static final String operationPathCreateAccessToken = "/instance_identity/v1/token";
4446
private static final String operationPathCreateIamToken = "/instance_identity/v1/iam_token";
47+
private static final String operationPathCreateAccessToken2 = "/identity/v1/token";
48+
private static final String operationPathCreateIamToken2 = "/identity/v1/iam_tokens";
4549
private static final String metadataFlavor = "ibm";
4650
private static final String metadataServiceVersion = "2022-03-01";
4751
private static final int instanceIdentityTokenLifetime = 300;
4852

53+
private ArrayList<String> defaultServiceSupportedVersions = new ArrayList<>(List.of("2022-03-01", "2025-08-26"));
54+
55+
4956
// Properties specific to a VpcInstanceAuthenticator.
5057
private String iamProfileCrn;
5158
private String iamProfileId;
5259
private String url;
5360
private String serviceVersion;
5461
private int tokenLifetime;
5562

56-
5763
/**
5864
* This Builder class is used to construct IamAuthenticator instances.
5965
*/
@@ -204,6 +210,11 @@ public void validate() {
204210
throw new IllegalArgumentException(
205211
String.format(ERRORMSG_ATMOST_ONE_PROP_ERROR, "iamProfileCrn", "iamProfileId"));
206212
}
213+
214+
if (!this.defaultServiceSupportedVersions.contains(this.serviceVersion)) {
215+
throw new IllegalArgumentException(
216+
String.format(ERRORMSG_INVALID_SERVICE_VERSION, this.defaultServiceSupportedVersions));
217+
}
207218
}
208219

209220
/**
@@ -314,7 +325,7 @@ private String getImsEndpoint() {
314325
*/
315326
public String getCreateAccessTokenPath() {
316327
if (this.serviceVersion.equals("2025-08-26")) {
317-
return "/identity/v1/token";
328+
return operationPathCreateAccessToken2;
318329
}
319330
return operationPathCreateAccessToken;
320331
}
@@ -326,7 +337,7 @@ public String getCreateAccessTokenPath() {
326337
*/
327338
public String getCreateIamTokenPath() {
328339
if (this.serviceVersion.equals("2025-08-26")) {
329-
return "/identity/v1/iam_tokens";
340+
return operationPathCreateIamToken2;
330341
}
331342
return operationPathCreateIamToken;
332343
}
@@ -400,7 +411,7 @@ public IamToken retrieveIamAccessToken(String instanceIdentityToken) {
400411
RequestBuilder.post(RequestBuilder.resolveRequestUrl(getImsEndpoint(), this.getCreateIamTokenPath()));
401412

402413
// Set the params and request body.
403-
builder.query("version", this.getServiceVersion());
414+
builder.query("version", this.serviceVersion);
404415
builder.header(HttpHeaders.ACCEPT, HttpMediaType.APPLICATION_JSON);
405416
builder.header(HttpHeaders.CONTENT_TYPE, HttpMediaType.APPLICATION_JSON);
406417
builder.header(HttpHeaders.AUTHORIZATION, "Bearer " + instanceIdentityToken);

src/test/java/com/ibm/cloud/sdk/core/test/security/VpcInstanceAuthenticatorTest.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public class VpcInstanceAuthenticatorTest extends BaseServiceUnitTest {
5555
private static final String mockIamProfileCrn = "crn:iam-profile:123";
5656
private static final String mockIamProfileId = "iam-id-123";
5757

58+
private static final String operationPathCreateAccessToken = "/instance_identity/v1/token";
59+
private static final String operationPathCreateIamToken = "/instance_identity/v1/iam_token";
60+
private static final String operationPathCreateAccessToken2 = "/identity/v1/token";
61+
private static final String operationPathCreateIamToken2 = "/identity/v1/iam_tokens";
62+
5863
private static final String mockErrorResponseJson1 =
5964
"{\"errors\": [{\"message\": \"Your create_access_token request was bad.\", \"code\": \"invalid_parameter_value\"}]}";
6065
private static final String mockErrorResponseJson2 =
@@ -633,8 +638,8 @@ public void testVpcAuthServiceVersionDefaults() {
633638
assertEquals(authenticator.getServiceVersion(), "2022-03-01");
634639
assertEquals(authenticator.getTokenLifetime(), 300);
635640

636-
assertEquals("/instance_identity/v1/token", authenticator.getCreateAccessTokenPath());
637-
assertEquals("/instance_identity/v1/iam_token", authenticator.getCreateIamTokenPath());
641+
assertEquals(operationPathCreateAccessToken, authenticator.getCreateAccessTokenPath());
642+
assertEquals(operationPathCreateIamToken, authenticator.getCreateIamTokenPath());
638643
}
639644

640645
@Test
@@ -648,8 +653,8 @@ public void testVpcAuthServiceVersionBuilder() {
648653
assertEquals(authenticator.getServiceVersion(), "2025-08-26");
649654
assertEquals(authenticator.getTokenLifetime(), 600);
650655

651-
assertEquals("/identity/v1/token", authenticator.getCreateAccessTokenPath());
652-
assertEquals("/identity/v1/iam_tokens", authenticator.getCreateIamTokenPath());
656+
assertEquals(operationPathCreateAccessToken2, authenticator.getCreateAccessTokenPath());
657+
assertEquals(operationPathCreateIamToken2, authenticator.getCreateIamTokenPath());
653658
}
654659

655660
@Test
@@ -662,8 +667,8 @@ public void testVpcAuthServiceVersionFromMap() {
662667

663668
assertEquals(authenticator.getServiceVersion(), "2025-08-26");
664669

665-
assertEquals("/identity/v1/token", authenticator.getCreateAccessTokenPath());
666-
assertEquals("/identity/v1/iam_tokens", authenticator.getCreateIamTokenPath());
670+
assertEquals(operationPathCreateAccessToken2, authenticator.getCreateAccessTokenPath());
671+
assertEquals(operationPathCreateIamToken2, authenticator.getCreateIamTokenPath());
667672
}
668673

669674
@Test
@@ -675,19 +680,19 @@ public void testVpcAuthServiceVersionOldVersion() {
675680

676681
assertEquals(authenticator.getServiceVersion(), "2022-03-01");
677682

678-
assertEquals("/instance_identity/v1/token", authenticator.getCreateAccessTokenPath());
679-
assertEquals("/instance_identity/v1/iam_token", authenticator.getCreateIamTokenPath());
683+
assertEquals(operationPathCreateAccessToken, authenticator.getCreateAccessTokenPath());
684+
assertEquals(operationPathCreateIamToken, authenticator.getCreateIamTokenPath());
680685
}
681686

682687
@Test
683688
public void testVpcAuthServiceVersionCustomVersion() {
684-
VpcInstanceAuthenticator authenticator = new VpcInstanceAuthenticator.Builder()
685-
.serviceVersion("2024-01-01")
686-
.build();
687-
assertNotNull(authenticator);
688-
689-
assertEquals(authenticator.getServiceVersion(), "2024-01-01");
690-
assertEquals("/instance_identity/v1/token", authenticator.getCreateAccessTokenPath());
691-
assertEquals("/instance_identity/v1/iam_token", authenticator.getCreateIamTokenPath());
689+
try {
690+
new VpcInstanceAuthenticator.Builder()
691+
.serviceVersion("2024-01-01")
692+
.build();
693+
fail("Expected build() to throw an exception!");
694+
} catch (IllegalArgumentException e) {
695+
assertEquals(e.getMessage(), "Invalid service version. Supported values are: [2022-03-01, 2025-08-26]");
696+
}
692697
}
693698
}

0 commit comments

Comments
 (0)