Skip to content

Commit cdc64d1

Browse files
committed
Set correct ssl factory when using OAUTH
1 parent f9ede52 commit cdc64d1

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

src/main/java/io/cdap/plugin/http/common/http/OAuthUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public static AccessToken getAccessToken(BaseHttpConfig config) throws IOExcepti
8383
// get accessToken from service account
8484
return OAuthUtil.getAccessTokenByServiceAccount(config);
8585
case OAUTH2:
86+
if (config instanceof BaseHttpSourceConfig) {
87+
try (CloseableHttpClient client = HttpClients.custom()
88+
.setSSLSocketFactory(new SSLConnectionSocketFactoryCreator((BaseHttpSourceConfig) config).create())
89+
.build()) {
90+
return getAccessToken(client, config);
91+
}
92+
}
8693
try (CloseableHttpClient client = HttpClients.createDefault()) {
8794
return getAccessToken(client, config);
8895
}

src/main/java/io/cdap/plugin/http/source/batch/HttpBatchSourceConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import io.cdap.cdap.etl.api.FailureCollector;
2121
import io.cdap.plugin.http.common.http.AuthType;
2222
import io.cdap.plugin.http.common.http.HttpClient;
23+
import io.cdap.plugin.http.common.http.KeyStoreType;
2324
import io.cdap.plugin.http.common.http.OAuthUtil;
25+
import io.cdap.plugin.http.common.http.SSLConnectionSocketFactoryCreator;
2426
import io.cdap.plugin.http.source.common.BaseHttpSourceConfig;
2527
import org.apache.http.HttpEntity;
2628
import org.apache.http.HttpHost;
@@ -70,7 +72,8 @@ private void validateOAuth2Credentials(FailureCollector collector) {
7072
!containsMacro(PROPERTY_PROXY_PASSWORD) && !containsMacro(PROPERTY_PROXY_USERNAME) &&
7173
!containsMacro(PROPERTY_PROXY_URL) && !containsMacro(PROPERTY_OAUTH2_CLIENT_AUTHENTICATION) &&
7274
!containsMacro(PROPERTY_OAUTH2_GRANT_TYPE)) {
73-
HttpClientBuilder httpclientBuilder = HttpClients.custom();
75+
HttpClientBuilder httpclientBuilder = HttpClients.custom()
76+
.setSSLSocketFactory(new SSLConnectionSocketFactoryCreator(this).create());
7477
if (!Strings.isNullOrEmpty(getProxyUrl())) {
7578
HttpHost proxyHost = HttpHost.create(getProxyUrl());
7679
if (!Strings.isNullOrEmpty(getProxyUsername()) && !Strings.isNullOrEmpty(getProxyPassword())) {
@@ -140,6 +143,7 @@ private HttpBatchSourceConfig(HttpBatchSourceConfigBuilder builder) {
140143
this.readTimeout = builder.readTimeout;
141144
this.paginationType = builder.paginationType;
142145
this.verifyHttps = builder.verifyHttps;
146+
this.keystoreType = builder.keystoreType;
143147
this.authType = builder.authType;
144148
this.authUrl = builder.authUrl;
145149
this.clientId = builder.clientId;
@@ -180,6 +184,7 @@ public static class HttpBatchSourceConfigBuilder {
180184
private Integer readTimeout;
181185
private String paginationType;
182186
private String verifyHttps;
187+
private String keystoreType;
183188
private String authType;
184189
private String authUrl;
185190
private String tokenUrl;
@@ -345,6 +350,11 @@ public HttpBatchSourceConfigBuilder setAuthType(String authType) {
345350
return this;
346351
}
347352

353+
public HttpBatchSourceConfigBuilder setKeystoreType(KeyStoreType keystoreTypeObj) {
354+
this.keystoreType = keystoreTypeObj.getValue();
355+
return this;
356+
}
357+
348358
public HttpBatchSourceConfig build() {
349359
return new HttpBatchSourceConfig(this);
350360
}

src/test/java/io/cdap/plugin/http/source/common/HttpBatchSourceConfigTest.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.cdap.cdap.etl.api.validation.InvalidConfigPropertyException;
2222
import io.cdap.cdap.etl.mock.validation.MockFailureCollector;
2323
import io.cdap.plugin.http.common.http.HttpClient;
24+
import io.cdap.plugin.http.common.http.KeyStoreType;
2425
import io.cdap.plugin.http.common.http.OAuthUtil;
2526
import io.cdap.plugin.http.common.pagination.BaseHttpPaginationIterator;
2627
import io.cdap.plugin.http.common.pagination.PaginationIteratorFactory;
@@ -53,7 +54,7 @@
5354
@RunWith(PowerMockRunner.class)
5455
@PrepareForTest({PaginationIteratorFactory.class, HttpClientBuilder.class, HttpClients.class, OAuthUtil.class,
5556
HttpHost.class, EntityUtils.class, HttpClient.class})
56-
@PowerMockIgnore("javax.management.*")
57+
@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*"})
5758
public class HttpBatchSourceConfigTest {
5859

5960
@Mock
@@ -74,8 +75,8 @@ public void testMissingKeyValue() {
7475
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder()
7576
.setReferenceName("test").setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:")
7677
.setFormat("JSON").setAuthType("none").setErrorHandling(StringUtils.EMPTY)
77-
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120)
78-
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("true").build();
78+
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
79+
.setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).build();
7980
config.validate(collector);
8081
}
8182

@@ -85,7 +86,7 @@ public void testEmptySchemaKeyValue() {
8586
.setReferenceName("test").setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth")
8687
.setFormat("JSON").setAuthType("none").setErrorHandling(StringUtils.EMPTY)
8788
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120)
88-
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("true").build();
89+
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).build();
8990
config.validateSchema();
9091
}
9192

@@ -96,8 +97,8 @@ public void testValidateOAuth2() throws Exception {
9697
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
9798
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
9899
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
99-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
100-
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
100+
.setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2")
101+
.setClientId("id").setClientSecret("secret").setRefreshToken("token").setScopes("scope")
101102
.setTokenUrl("https//:token").setRetryPolicy("exponential")
102103
.setOauth2GrantType("refresh_token").build();
103104
PowerMockito.mockStatic(PaginationIteratorFactory.class);
@@ -124,9 +125,10 @@ public void testValidateOAuth2CredentialsWithProxy() throws IOException {
124125
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder()
125126
.setReferenceName("test").setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth")
126127
.setFormat("JSON").setAuthType("none").setErrorHandling(StringUtils.EMPTY)
127-
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120)
128-
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id").
129-
setClientSecret("secret").setRefreshToken("token").setScopes("scope").setTokenUrl("https//:token").setRetryPolicy(
128+
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
129+
.setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2")
130+
.setClientId("id").setClientSecret("secret").setRefreshToken("token").setScopes("scope")
131+
.setTokenUrl("https//:token").setRetryPolicy(
130132
"exponential").setProxyUrl("https://proxy").setProxyUsername("proxyuser").setProxyPassword("proxypassword")
131133
.setOauth2GrantType("refresh_token").build();
132134
HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
@@ -156,10 +158,10 @@ public void testValidateCredentialsOAuth2WithInvalidAccessTokenRequest() throws
156158
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder()
157159
.setReferenceName("test").setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth")
158160
.setFormat("JSON").setErrorHandling(StringUtils.EMPTY)
159-
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120)
160-
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id").
161-
setClientSecret("secret").setRefreshToken("token").setScopes("scope").setTokenUrl("https//:token").setRetryPolicy(
162-
"exponential").setOauth2GrantType("refresh_token").build();
161+
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
162+
.setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2")
163+
.setClientId("id").setClientSecret("secret").setRefreshToken("token").setScopes("scope")
164+
.setTokenUrl("https//:token").setRetryPolicy("exponential").setOauth2GrantType("refresh_token").build();
163165
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
164166
CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
165167
Mockito.when(httpClientMock.execute(Mockito.any())).thenReturn(httpResponse);
@@ -193,10 +195,9 @@ public void testBasicAuthWithValidResponse() throws IOException {
193195
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder()
194196
.setReferenceName("test").setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth")
195197
.setFormat("JSON").setAuthType("none").setErrorHandling(StringUtils.EMPTY)
196-
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120)
197-
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("true").setAuthType("basicAuth").setUsername(
198-
"username").setPassword("password").setRetryPolicy(
199-
"exponential").build();
198+
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
199+
.setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("basicAuth")
200+
.setUsername("username").setPassword("password").setRetryPolicy("exponential").build();
200201
Mockito.when(httpClient.executeHTTP(Mockito.any())).thenReturn(response);
201202
Mockito.when(response.getStatusLine()).thenReturn(statusLine);
202203
Mockito.when(statusLine.getStatusCode()).thenReturn(200);
@@ -210,10 +211,9 @@ public void testValidConfigWithInvalidResponse() throws IOException {
210211
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder()
211212
.setReferenceName("test").setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth")
212213
.setFormat("JSON").setAuthType("none").setErrorHandling(StringUtils.EMPTY)
213-
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120)
214-
.setReadTimeout(120).setPaginationType("NONE").setVerifyHttps("true").setAuthType("basicAuth").setUsername(
215-
"username").setPassword("password").setRetryPolicy(
216-
"exponential").build();
214+
.setRetryPolicy(StringUtils.EMPTY).setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
215+
.setPaginationType("NONE").setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("basicAuth")
216+
.setUsername("username").setPassword("password").setRetryPolicy("exponential").build();
217217
Mockito.when(httpClient.executeHTTP(Mockito.any())).thenReturn(response);
218218
Mockito.when(response.getStatusLine()).thenReturn(statusLine);
219219
Mockito.when(statusLine.getStatusCode()).thenReturn(400);
@@ -232,8 +232,8 @@ public void testValidateOAuth2WithClientCredentialsAndBodyAuthentication() throw
232232
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
233233
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
234234
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
235-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
236-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
235+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
236+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
237237
.setClientSecret("secret").setScopes("scope").setTokenUrl("https//:token")
238238
.setRetryPolicy("exponential").setOauth2GrantType("client_credentials")
239239
.setOauth2ClientAuthentication("body").build();
@@ -264,8 +264,8 @@ public void testValidateOAuth2CredentialsWithProxyWithClientCredentialsAndBodyAu
264264
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
265265
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
266266
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
267-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
268-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
267+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
268+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
269269
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
270270
.setTokenUrl("https//:token").setRetryPolicy("exponential").setProxyUrl("https://proxy")
271271
.setProxyUsername("proxyuser").setProxyPassword("proxypassword")
@@ -298,8 +298,8 @@ public void testValidateCredentialsOAuth2WithInvalidAccessTokenRequestForClientC
298298
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
299299
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
300300
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
301-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
302-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
301+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
302+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
303303
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
304304
.setTokenUrl("https//:token").setRetryPolicy("exponential")
305305
.setOauth2GrantType("client_credentials").setOauth2ClientAuthentication("body").build();
@@ -339,8 +339,8 @@ public void testValidateOAuth2WithClientCredentialsAndRequestParamAuthentication
339339
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
340340
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
341341
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
342-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
343-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
342+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
343+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
344344
.setClientSecret("secret").setScopes("scope").setTokenUrl("https//:token")
345345
.setRetryPolicy("exponential").setOauth2GrantType("client_credentials")
346346
.setOauth2ClientAuthentication("request_parameter").build();
@@ -371,8 +371,8 @@ public void testValidateOAuth2CredentialsWithProxyWithClientCredentialsAndReques
371371
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
372372
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
373373
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
374-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
375-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
374+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
375+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
376376
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
377377
.setTokenUrl("https//:token").setRetryPolicy("exponential").setProxyUrl("https://proxy")
378378
.setProxyUsername("proxyuser").setProxyPassword("proxypassword")
@@ -406,8 +406,8 @@ public void testValidateCredentialsOAuth2WithInvalidAccessTokenRequestForClientC
406406
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
407407
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
408408
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
409-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
410-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
409+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
410+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
411411
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
412412
.setTokenUrl("https//:token").setRetryPolicy("exponential")
413413
.setOauth2GrantType("client_credentials").setOauth2ClientAuthentication("request_parameter")
@@ -448,8 +448,8 @@ public void testValidateOAuth2WithClientCredentialsAndBasicAuthHeaderAuthenticat
448448
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
449449
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
450450
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
451-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
452-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
451+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
452+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
453453
.setClientSecret("secret").setScopes("scope").setTokenUrl("https//:token")
454454
.setRetryPolicy("exponential").setOauth2GrantType("client_credentials")
455455
.setOauth2ClientAuthentication("basic_auth_header").build();
@@ -480,8 +480,8 @@ public void testValidateOAuth2CredentialsWithProxyWithClientCredentialsAndBasicA
480480
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
481481
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
482482
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
483-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
484-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
483+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
484+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
485485
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
486486
.setTokenUrl("https//:token").setRetryPolicy("exponential").setProxyUrl("https://proxy")
487487
.setProxyUsername("proxyuser").setProxyPassword("proxypassword")
@@ -515,8 +515,8 @@ public void testValidateCredentialsOAuth2WithInvalidAccessTokenRequestForClientC
515515
HttpBatchSourceConfig config = HttpBatchSourceConfig.builder().setReferenceName("test")
516516
.setUrl("http://localhost").setHttpMethod("GET").setHeaders("Auth:auth").setFormat("JSON")
517517
.setErrorHandling(StringUtils.EMPTY).setRetryPolicy(StringUtils.EMPTY)
518-
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120)
519-
.setPaginationType("NONE").setVerifyHttps("true").setAuthType("oAuth2").setClientId("id")
518+
.setMaxRetryDuration(600L).setConnectTimeout(120).setReadTimeout(120).setPaginationType("NONE")
519+
.setVerifyHttps("false").setKeystoreType(KeyStoreType.JKS).setAuthType("oAuth2").setClientId("id")
520520
.setClientSecret("secret").setRefreshToken("token").setScopes("scope")
521521
.setTokenUrl("https//:token").setRetryPolicy("exponential")
522522
.setOauth2GrantType("client_credentials").setOauth2ClientAuthentication("basic_auth_header")

0 commit comments

Comments
 (0)