Skip to content

Commit 7bfcd9e

Browse files
Support PluginConfigVariable for Confluence/Jira bearer token. (#6856)
* Change bearer_token from String to PluginConfigVariable for secrets manager integration Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com> * Use instanceof for safe cast, guard refresh with isUpdatable, consolidate token validation into helper method Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com> * Remove unused UUID import from AtlassianAuthFactoryTest Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com> * Remove isUpdatable guard and call refresh directly per reviewer feedback Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com> --------- Signed-off-by: Srikanth Padakanti <srikanth_padakanti@apple.com>
1 parent 1703bb8 commit 7bfcd9e

6 files changed

Lines changed: 66 additions & 28 deletions

File tree

data-prepper-plugins/saas-source-plugins/atlassian-commons/src/main/java/org/opensearch/dataprepper/plugins/source/atlassian/configuration/AuthenticationConfig.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
package org.opensearch.dataprepper.plugins.source.atlassian.configuration;
1212

13+
import com.fasterxml.jackson.annotation.JsonIgnore;
1314
import com.fasterxml.jackson.annotation.JsonProperty;
1415
import jakarta.validation.Valid;
1516
import jakarta.validation.constraints.AssertTrue;
1617
import lombok.Getter;
18+
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
1719

1820
import static org.opensearch.dataprepper.plugins.source.atlassian.utils.Constants.BASIC;
1921
import static org.opensearch.dataprepper.plugins.source.atlassian.utils.Constants.BEARER_TOKEN;
@@ -31,24 +33,38 @@ public class AuthenticationConfig {
3133
private Oauth2Config oauth2Config;
3234

3335
@JsonProperty("bearer_token")
34-
private String bearerToken;
36+
private PluginConfigVariable bearerToken;
37+
38+
@JsonIgnore
39+
public String getBearerTokenValue() {
40+
if (bearerToken == null || bearerToken.getValue() == null) {
41+
return null;
42+
}
43+
final Object value = bearerToken.getValue();
44+
if (value instanceof String) {
45+
return (String) value;
46+
}
47+
return value.toString();
48+
}
3549

3650
@AssertTrue(message = "Authentication config should have exactly one of basic, oauth2, or bearer_token")
3751
private boolean isValidAuthenticationConfig() {
3852
boolean hasBasic = basicConfig != null;
3953
boolean hasOauth = oauth2Config != null;
40-
boolean hasBearer = bearerToken != null && !bearerToken.isEmpty();
54+
final String tokenValue = getBearerTokenValue();
55+
boolean hasBearer = tokenValue != null && !tokenValue.isEmpty();
4156
int count = (hasBasic ? 1 : 0) + (hasOauth ? 1 : 0) + (hasBearer ? 1 : 0);
4257
return count == 1;
4358
}
4459

4560
public String getAuthType() {
4661
if (basicConfig != null) {
4762
return BASIC;
48-
} else if (bearerToken != null && !bearerToken.isEmpty()) {
63+
}
64+
final String tokenValue = getBearerTokenValue();
65+
if (tokenValue != null && !tokenValue.isEmpty()) {
4966
return BEARER_TOKEN;
50-
} else {
51-
return OAUTH2;
5267
}
68+
return OAUTH2;
5369
}
5470
}

data-prepper-plugins/saas-source-plugins/atlassian-commons/src/main/java/org/opensearch/dataprepper/plugins/source/atlassian/rest/auth/AtlassianBearerTokenAuthConfig.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@
1010

1111
package org.opensearch.dataprepper.plugins.source.atlassian.rest.auth;
1212

13+
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
1314
import org.opensearch.dataprepper.plugins.source.atlassian.AtlassianSourceConfig;
1415

16+
import java.util.Objects;
17+
1518
public class AtlassianBearerTokenAuthConfig implements AtlassianAuthConfig {
1619

17-
private String accountUrl;
18-
private final String bearerToken;
20+
private final String accountUrl;
21+
private final PluginConfigVariable bearerTokenVariable;
1922

2023
public AtlassianBearerTokenAuthConfig(AtlassianSourceConfig sourceConfig) {
21-
this.bearerToken = sourceConfig.getAuthenticationConfig().getBearerToken();
22-
accountUrl = sourceConfig.getAccountUrl();
23-
if (!accountUrl.endsWith("/")) {
24-
accountUrl += "/";
24+
Objects.requireNonNull(sourceConfig, "sourceConfig must not be null");
25+
this.bearerTokenVariable = sourceConfig.getAuthenticationConfig().getBearerToken();
26+
Objects.requireNonNull(bearerTokenVariable, "bearer_token must not be null");
27+
String url = sourceConfig.getAccountUrl();
28+
if (!url.endsWith("/")) {
29+
url += "/";
2530
}
31+
this.accountUrl = url;
2632
}
2733

2834
@Override
@@ -31,11 +37,15 @@ public String getUrl() {
3137
}
3238

3339
public String getBearerToken() {
34-
return bearerToken;
40+
final Object value = bearerTokenVariable.getValue();
41+
if (value instanceof String) {
42+
return (String) value;
43+
}
44+
return value != null ? value.toString() : null;
3545
}
3646

3747
@Override
3848
public void renewCredentials() {
39-
// static token, no renewal needed
49+
bearerTokenVariable.refresh();
4050
}
4151
}

data-prepper-plugins/saas-source-plugins/atlassian-commons/src/test/java/org/opensearch/dataprepper/plugins/source/atlassian/rest/auth/AtlassianAuthFactoryTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.opensearch.dataprepper.plugins.source.atlassian.configuration.AuthenticationConfig;
2121
import org.opensearch.dataprepper.plugins.source.atlassian.configuration.Oauth2Config;
2222

23-
import java.util.UUID;
2423

2524

2625
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -47,6 +46,9 @@ public class AtlassianAuthFactoryTest {
4746
@Mock
4847
private PluginConfigVariable refreshTokenPluginConfigVariable;
4948

49+
@Mock
50+
private PluginConfigVariable bearerTokenVariable;
51+
5052
private AtlassianAuthFactory confluenceAuthFactory;
5153

5254
@BeforeEach
@@ -73,10 +75,9 @@ void testGetObjectBasicAuth() {
7375

7476
@Test
7577
void testGetObjectBearerToken() {
76-
final String token = UUID.randomUUID().toString();
7778
when(sourceConfig.getAuthType()).thenReturn(BEARER_TOKEN);
7879
when(sourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
79-
when(authenticationConfig.getBearerToken()).thenReturn(token);
80+
when(authenticationConfig.getBearerToken()).thenReturn(bearerTokenVariable);
8081
when(sourceConfig.getAccountUrl()).thenReturn("https://confluence.opensearch.org");
8182
assertInstanceOf(AtlassianBearerTokenAuthConfig.class, confluenceAuthFactory.getObject());
8283
}

data-prepper-plugins/saas-source-plugins/atlassian-commons/src/test/java/org/opensearch/dataprepper/plugins/source/atlassian/rest/auth/AtlassianBearerTokenAuthConfigTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
import org.junit.jupiter.api.extension.ExtendWith;
1616
import org.mockito.Mock;
1717
import org.mockito.junit.jupiter.MockitoExtension;
18+
import org.opensearch.dataprepper.model.plugin.PluginConfigVariable;
1819
import org.opensearch.dataprepper.plugins.source.atlassian.AtlassianSourceConfig;
1920
import org.opensearch.dataprepper.plugins.source.atlassian.configuration.AuthenticationConfig;
2021

2122
import java.util.UUID;
2223

2324
import static org.hamcrest.MatcherAssert.assertThat;
2425
import static org.hamcrest.Matchers.equalTo;
25-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
26+
import static org.mockito.Mockito.verify;
2627
import static org.mockito.Mockito.when;
2728

2829
@ExtendWith(MockitoExtension.class)
@@ -34,6 +35,9 @@ class AtlassianBearerTokenAuthConfigTest {
3435
@Mock
3536
private AuthenticationConfig authenticationConfig;
3637

38+
@Mock
39+
private PluginConfigVariable bearerTokenVariable;
40+
3741
private String token;
3842

3943
@BeforeEach
@@ -45,7 +49,7 @@ void setUp() {
4549
void testGetUrl() {
4650
when(sourceConfig.getAccountUrl()).thenReturn("https://confluence.opensearch.org");
4751
when(sourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
48-
when(authenticationConfig.getBearerToken()).thenReturn(token);
52+
when(authenticationConfig.getBearerToken()).thenReturn(bearerTokenVariable);
4953

5054
AtlassianBearerTokenAuthConfig config = new AtlassianBearerTokenAuthConfig(sourceConfig);
5155
assertThat(config.getUrl(), equalTo("https://confluence.opensearch.org/"));
@@ -55,7 +59,7 @@ void testGetUrl() {
5559
void testGetUrlWithTrailingSlash() {
5660
when(sourceConfig.getAccountUrl()).thenReturn("https://confluence.opensearch.org/");
5761
when(sourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
58-
when(authenticationConfig.getBearerToken()).thenReturn(token);
62+
when(authenticationConfig.getBearerToken()).thenReturn(bearerTokenVariable);
5963

6064
AtlassianBearerTokenAuthConfig config = new AtlassianBearerTokenAuthConfig(sourceConfig);
6165
assertThat(config.getUrl(), equalTo("https://confluence.opensearch.org/"));
@@ -65,19 +69,22 @@ void testGetUrlWithTrailingSlash() {
6569
void testGetBearerToken() {
6670
when(sourceConfig.getAccountUrl()).thenReturn("https://confluence.opensearch.org");
6771
when(sourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
68-
when(authenticationConfig.getBearerToken()).thenReturn(token);
72+
when(authenticationConfig.getBearerToken()).thenReturn(bearerTokenVariable);
73+
when(bearerTokenVariable.getValue()).thenReturn(token);
6974

7075
AtlassianBearerTokenAuthConfig config = new AtlassianBearerTokenAuthConfig(sourceConfig);
7176
assertThat(config.getBearerToken(), equalTo(token));
7277
}
7378

7479
@Test
75-
void testRenewCredentialsIsNoOp() {
80+
void testRenewCredentials_calls_refresh() {
7681
when(sourceConfig.getAccountUrl()).thenReturn("https://confluence.opensearch.org");
7782
when(sourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
78-
when(authenticationConfig.getBearerToken()).thenReturn(token);
83+
when(authenticationConfig.getBearerToken()).thenReturn(bearerTokenVariable);
7984

8085
AtlassianBearerTokenAuthConfig config = new AtlassianBearerTokenAuthConfig(sourceConfig);
81-
assertDoesNotThrow(config::renewCredentials);
86+
config.renewCredentials();
87+
88+
verify(bearerTokenVariable).refresh();
8289
}
8390
}

data-prepper-plugins/saas-source-plugins/confluence-source/src/main/java/org/opensearch/dataprepper/plugins/source/confluence/utils/ConfluenceConfigHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public static boolean validateConfig(ConfluenceSourceConfig config) {
105105
}
106106

107107
if (BEARER_TOKEN.equals(authType)) {
108-
String bearerToken = config.getAuthenticationConfig().getBearerToken();
109-
if (bearerToken == null || bearerToken.isEmpty()) {
108+
final String tokenValue = config.getAuthenticationConfig().getBearerTokenValue();
109+
if (tokenValue == null || tokenValue.isEmpty()) {
110110
throw new RuntimeException("Bearer token is required for BearerToken AuthType");
111111
}
112112
}

data-prepper-plugins/saas-source-plugins/confluence-source/src/test/java/org/opensearch/dataprepper/plugins/source/confluence/ConfluenceConfigHelperTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class ConfluenceConfigHelperTest {
7070
@Mock
7171
PluginConfigVariable refreshTokenPluginConfigVariable;
7272

73+
@Mock
74+
PluginConfigVariable bearerTokenVariable;
75+
7376
@Test
7477
void testInitialization() {
7578
ConfluenceConfigHelper confluenceConfigHelper = new ConfluenceConfigHelper();
@@ -161,7 +164,7 @@ void testValidateConfigBearerToken_nullToken_throwsException() {
161164
when(confluenceSourceConfig.getAccountUrl()).thenReturn("https://opensearch.org");
162165
when(confluenceSourceConfig.getAuthType()).thenReturn(BEARER_TOKEN);
163166
when(confluenceSourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
164-
when(authenticationConfig.getBearerToken()).thenReturn(null);
167+
when(authenticationConfig.getBearerTokenValue()).thenReturn(null);
165168
assertThrows(RuntimeException.class, () -> ConfluenceConfigHelper.validateConfig(confluenceSourceConfig));
166169
}
167170

@@ -170,7 +173,7 @@ void testValidateConfigBearerToken_emptyToken_throwsException() {
170173
when(confluenceSourceConfig.getAccountUrl()).thenReturn("https://opensearch.org");
171174
when(confluenceSourceConfig.getAuthType()).thenReturn(BEARER_TOKEN);
172175
when(confluenceSourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
173-
when(authenticationConfig.getBearerToken()).thenReturn("");
176+
when(authenticationConfig.getBearerTokenValue()).thenReturn("");
174177
assertThrows(RuntimeException.class, () -> ConfluenceConfigHelper.validateConfig(confluenceSourceConfig));
175178
}
176179

@@ -179,7 +182,8 @@ void testValidateConfigBearerToken_validToken_succeeds() {
179182
when(confluenceSourceConfig.getAccountUrl()).thenReturn("https://opensearch.org");
180183
when(confluenceSourceConfig.getAuthType()).thenReturn(BEARER_TOKEN);
181184
when(confluenceSourceConfig.getAuthenticationConfig()).thenReturn(authenticationConfig);
182-
when(authenticationConfig.getBearerToken()).thenReturn(UUID.randomUUID().toString());
185+
when(authenticationConfig.getBearerTokenValue()).thenReturn(UUID.randomUUID().toString());
186+
when(confluenceSourceConfig.isAllowLocalAddress()).thenReturn(false);
183187
assertDoesNotThrow(() -> ConfluenceConfigHelper.validateConfig(confluenceSourceConfig));
184188
}
185189
}

0 commit comments

Comments
 (0)