From c4d9abbdab8a65ae5dab4120a491a28aa4da28a4 Mon Sep 17 00:00:00 2001 From: Viren Baraiya Date: Tue, 6 Dec 2022 20:11:57 -0800 Subject: [PATCH 1/2] handle connection resets --- .../io/orkes/conductor/client/ApiClient.java | 109 +++--------------- .../client/http/api/TokenResourceApi.java | 3 +- 2 files changed, 18 insertions(+), 94 deletions(-) diff --git a/src/main/java/io/orkes/conductor/client/ApiClient.java b/src/main/java/io/orkes/conductor/client/ApiClient.java index e15a3198..849317f4 100644 --- a/src/main/java/io/orkes/conductor/client/ApiClient.java +++ b/src/main/java/io/orkes/conductor/client/ApiClient.java @@ -50,8 +50,6 @@ import io.orkes.conductor.client.http.api.TokenResourceApi; import io.orkes.conductor.client.http.auth.ApiKeyAuth; import io.orkes.conductor.client.http.auth.Authentication; -import io.orkes.conductor.client.http.auth.HttpBasicAuth; -import io.orkes.conductor.client.http.auth.OAuth; import io.orkes.conductor.client.model.GenerateTokenRequest; import com.google.common.cache.Cache; @@ -339,55 +337,6 @@ public ApiClient setLenientOnJson(boolean lenientOnJson) { return this; } - /** - * Get authentications (key: authentication name, value: authentication). - * - * @return Map of authentication objects - */ - public Map getAuthentications() { - return authentications; - } - - /** - * Get authentication for the given name. - * - * @param authName The authentication name - * @return The authentication, null if not found - */ - public Authentication getAuthentication(String authName) { - return authentications.get(authName); - } - - /** - * Helper method to set username for the first HTTP basic authentication. - * - * @param username Username - */ - public void setUsername(String username) { - for (Authentication auth : authentications.values()) { - if (auth instanceof HttpBasicAuth) { - ((HttpBasicAuth) auth).setUsername(username); - return; - } - } - throw new RuntimeException("No HTTP basic authentication configured!"); - } - - /** - * Helper method to set password for the first HTTP basic authentication. - * - * @param password Password - */ - public void setPassword(String password) { - for (Authentication auth : authentications.values()) { - if (auth instanceof HttpBasicAuth) { - ((HttpBasicAuth) auth).setPassword(password); - return; - } - } - throw new RuntimeException("No HTTP basic authentication configured!"); - } - /** * Helper method to set API key value for the first API key authentication. * @@ -403,36 +352,6 @@ public void setApiKey(String apiKey) { throw new RuntimeException("No API key authentication configured!"); } - /** - * Helper method to set API key prefix for the first API key authentication. - * - * @param apiKeyPrefix API key prefix - */ - public void setApiKeyPrefix(String apiKeyPrefix) { - for (Authentication auth : authentications.values()) { - if (auth instanceof ApiKeyAuth) { - ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); - return; - } - } - throw new RuntimeException("No API key authentication configured!"); - } - - /** - * Helper method to set access token for the first OAuth2 authentication. - * - * @param accessToken Access token - */ - public void setAccessToken(String accessToken) { - for (Authentication auth : authentications.values()) { - if (auth instanceof OAuth) { - ((OAuth) auth).setAccessToken(accessToken); - return; - } - } - throw new RuntimeException("No OAuth2 authentication configured!"); - } - /** * Set the User-Agent header's value (by adding to the default header map). * @@ -1068,7 +987,9 @@ public Request buildRequest( String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { - updateParamsForAuth(authNames, queryParams, headerParams); + if(!"/token".equalsIgnoreCase(path)) { + updateParamsForAuth(authNames, queryParams, headerParams); + } final String url = buildUrl(path, queryParams, collectionQueryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); @@ -1187,8 +1108,11 @@ public void processHeaderParams(Map headerParams, Request.Builde * @param queryParams List of query parameters * @param headerParams Map of header parameters */ - public void updateParamsForAuth( - String[] authNames, List queryParams, Map headerParams) { + public void updateParamsForAuth(String[] authNames, List queryParams, Map headerParams) { + if(useSecurity() && authentications.isEmpty()) { + LOGGER.debug("No authentication set, will refresh token"); + refreshToken(); + } for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth != null) { @@ -1339,26 +1263,27 @@ private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityExcepti public String getToken() { try { - return tokenCache.get(TOKEN_CACHE_KEY, () -> refreshToken()); + String token = tokenCache.get(TOKEN_CACHE_KEY, () -> refreshToken()); + if(token == null) { + LOGGER.info("null token found...."); + System.exit(1); + } + return token; } catch (ExecutionException e) { return null; } } private String refreshToken() { - if (secretsManager != null) { keyId = secretsManager.getSecret(this.ssmKeyPath); keySecret = secretsManager.getSecret(this.ssmSecretPath); } if (this.keyId == null || this.keySecret == null) { - throw new RuntimeException( - "KeyId and KeySecret must be set in order to get an authentication token"); + throw new RuntimeException("KeyId and KeySecret must be set in order to get an authentication token"); } - GenerateTokenRequest generateTokenRequest = - new GenerateTokenRequest().keyId(this.keyId).keySecret(this.keySecret); - Map response = - TokenResourceApi.generateTokenWithHttpInfo(this, generateTokenRequest).getData(); + GenerateTokenRequest generateTokenRequest = new GenerateTokenRequest().keyId(this.keyId).keySecret(this.keySecret); + Map response = TokenResourceApi.generateTokenWithHttpInfo(this, generateTokenRequest).getData(); String token = response.get("token"); this.setApiKeyHeader(token); return token; diff --git a/src/main/java/io/orkes/conductor/client/http/api/TokenResourceApi.java b/src/main/java/io/orkes/conductor/client/http/api/TokenResourceApi.java index 124fa668..2903a7e2 100644 --- a/src/main/java/io/orkes/conductor/client/http/api/TokenResourceApi.java +++ b/src/main/java/io/orkes/conductor/client/http/api/TokenResourceApi.java @@ -128,8 +128,7 @@ private static com.squareup.okhttp.Call generateTokenValidateBeforeCall( */ public static ApiResponse> generateTokenWithHttpInfo( ApiClient apiClient, GenerateTokenRequest generateTokenRequest) throws ApiException { - com.squareup.okhttp.Call call = - generateTokenValidateBeforeCall(apiClient, generateTokenRequest, null, null); + com.squareup.okhttp.Call call = generateTokenValidateBeforeCall(apiClient, generateTokenRequest, null, null); Type localVarReturnType = new TypeToken>() {}.getType(); return apiClient.execute(call, localVarReturnType); } From a960e8b9b7e28e58e25bd5f414c73e115554cff2 Mon Sep 17 00:00:00 2001 From: Viren Baraiya Date: Tue, 6 Dec 2022 20:13:20 -0800 Subject: [PATCH 2/2] clean up --- src/main/java/io/orkes/conductor/client/ApiClient.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/io/orkes/conductor/client/ApiClient.java b/src/main/java/io/orkes/conductor/client/ApiClient.java index 849317f4..29820ac4 100644 --- a/src/main/java/io/orkes/conductor/client/ApiClient.java +++ b/src/main/java/io/orkes/conductor/client/ApiClient.java @@ -1263,12 +1263,7 @@ private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityExcepti public String getToken() { try { - String token = tokenCache.get(TOKEN_CACHE_KEY, () -> refreshToken()); - if(token == null) { - LOGGER.info("null token found...."); - System.exit(1); - } - return token; + return tokenCache.get(TOKEN_CACHE_KEY, () -> refreshToken()); } catch (ExecutionException e) { return null; }