diff --git a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java index e14556b82..9abb232c5 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -1973,10 +1973,8 @@ public void fetchMetricsBasedOnProfileAndDatasource(KruizeObject kruizeObject, T long interval_end_time_epoc = 0; long interval_start_time_epoc = 0; SimpleDateFormat sdf = new SimpleDateFormat(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT, Locale.ROOT); - AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( - dataSourceInfo.getAuthenticationConfig()); // Create the client - GenericRestApiClient client = new GenericRestApiClient(authenticationStrategy); + GenericRestApiClient client = new GenericRestApiClient(dataSourceInfo); String metricProfileName = kruizeObject.getPerformanceProfile(); PerformanceProfile metricProfile = MetricProfileCollection.getInstance().getMetricProfileCollection().get(metricProfileName); diff --git a/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java b/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java index 9022aa50f..c81a6075d 100644 --- a/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java @@ -7,6 +7,8 @@ public class AuthenticationStrategyFactory { public static AuthenticationStrategy createAuthenticationStrategy(AuthenticationConfig authConfig) { String type = authConfig.getType(); switch (type) { + case KruizeConstants.AuthenticationConstants.NONE: + return new NoAuthStrategy(); case KruizeConstants.AuthenticationConstants.BASIC: String username = authConfig.getCredentials().getUsername(); String password = authConfig.getCredentials().getPassword(); diff --git a/src/main/java/com/autotune/common/auth/NoAuthStrategy.java b/src/main/java/com/autotune/common/auth/NoAuthStrategy.java new file mode 100644 index 000000000..67c56fa17 --- /dev/null +++ b/src/main/java/com/autotune/common/auth/NoAuthStrategy.java @@ -0,0 +1,9 @@ +package com.autotune.common.auth; + +public class NoAuthStrategy implements AuthenticationStrategy { + @Override + public String applyAuthentication() { + // No authentication is applied + return null; + } +} diff --git a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java index 858ec6dc2..6734adc1c 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java @@ -153,10 +153,17 @@ public void addDataSourcesFromConfigFile(String configFileName) { String serviceName = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_SERVICE_NAME); String namespace = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_SERVICE_NAMESPACE); String dataSourceURL = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_URL); - JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION); - // create the corresponding authentication object - AuthenticationConfig authConfig = AuthenticationConfig.createAuthenticationConfigObject(authenticationObj); - DataSourceInfo datasource = null; + AuthenticationConfig authConfig; + try { + JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION); + // create the corresponding authentication object + authConfig = AuthenticationConfig.createAuthenticationConfigObject(authenticationObj); + } catch (Exception e) { + LOGGER.warn("Auth details are missing for datasource: {}", name); + authConfig = AuthenticationConfig.noAuth(); + } + + DataSourceInfo datasource; // Validate input if (!validateInput(name, provider, serviceName, dataSourceURL, namespace)) { continue; diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java index 5e102d98b..5b10e1184 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java @@ -165,10 +165,8 @@ public ArrayList getAppsForLayer(DataSourceInfo dataSource, String query String queryURL = dataSourceURL + queryEndpoint + query; LOGGER.debug("Query URL is: {}", queryURL); try { - AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( - dataSource.getAuthenticationConfig()); // Create the client - GenericRestApiClient genericRestApiClient = new GenericRestApiClient(authenticationStrategy); + GenericRestApiClient genericRestApiClient = new GenericRestApiClient(dataSource); genericRestApiClient.setBaseURL(dataSourceURL + queryEndpoint); JSONObject responseJson = genericRestApiClient.fetchMetricsJson("GET", query); int level = 0; diff --git a/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java b/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java index 362ca06fd..82865cc19 100644 --- a/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java @@ -143,10 +143,8 @@ public Object getValueForQuery(DataSourceInfo dataSource, String query) { */ @Override public JSONObject getJsonObjectForQuery(DataSourceInfo dataSource, String query) { - AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( - dataSource.getAuthenticationConfig()); // Create the client - GenericRestApiClient apiClient = new GenericRestApiClient(authenticationStrategy); + GenericRestApiClient apiClient = new GenericRestApiClient(dataSource); apiClient.setBaseURL(CommonUtils.getBaseDataSourceUrl( dataSource, KruizeConstants.SupportedDatasources.PROMETHEUS diff --git a/src/main/java/com/autotune/common/utils/CommonUtils.java b/src/main/java/com/autotune/common/utils/CommonUtils.java index 81cc9ef01..384bc5dc3 100644 --- a/src/main/java/com/autotune/common/utils/CommonUtils.java +++ b/src/main/java/com/autotune/common/utils/CommonUtils.java @@ -298,11 +298,11 @@ public static double getPercentage(double newer, double older) { public static DataSourceInfo getDataSourceInfo(String dataSourceName) throws Exception { DataSourceManager dataSourceManager = new DataSourceManager(); - // fetch the datasource from the DB - DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); + // fetch the datasource from the config file first + DataSourceInfo datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); if (isInvalidDataSource(datasource)) { - // fetch the datasource from the config - datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); + // fetch the datasource from the DB + datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); if (isInvalidDataSource(datasource)) { throw new Exception(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO); } diff --git a/src/main/java/com/autotune/utils/GenericRestApiClient.java b/src/main/java/com/autotune/utils/GenericRestApiClient.java index 30de160ec..8a6e6ea8a 100644 --- a/src/main/java/com/autotune/utils/GenericRestApiClient.java +++ b/src/main/java/com/autotune/utils/GenericRestApiClient.java @@ -16,6 +16,8 @@ package com.autotune.utils; import com.autotune.common.auth.AuthenticationStrategy; +import com.autotune.common.auth.AuthenticationStrategyFactory; +import com.autotune.common.datasource.DataSourceInfo; import com.autotune.utils.authModels.APIKeysAuthentication; import com.autotune.utils.authModels.BasicAuthentication; import com.autotune.utils.authModels.BearerAccessToken; @@ -57,11 +59,13 @@ public class GenericRestApiClient { private AuthenticationStrategy authenticationStrategy; /** - * Initializes a new instance just by passing baseURL which does not need any authentication. - * @param authenticationStrategy + * constructor to set the authentication based on the datasourceInfo object + * @param dataSourceInfo object containing the datasource details */ - public GenericRestApiClient(AuthenticationStrategy authenticationStrategy) { - this.authenticationStrategy = authenticationStrategy; + public GenericRestApiClient(DataSourceInfo dataSourceInfo) { + // TODO: add partial URL as well as part of this constructor + this.authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( + dataSourceInfo.getAuthenticationConfig()); } /** diff --git a/src/main/java/com/autotune/utils/KruizeConstants.java b/src/main/java/com/autotune/utils/KruizeConstants.java index 95a086d6d..4180fe902 100644 --- a/src/main/java/com/autotune/utils/KruizeConstants.java +++ b/src/main/java/com/autotune/utils/KruizeConstants.java @@ -738,6 +738,7 @@ public static final class AuthenticationConstants { public static final String AUTHENTICATION_CLIENT_ID = "clientId"; public static final String AUTHENTICATION_CLIENT_SECRET = "clientSecret"; public static final String AUTHENTICATION_GRANT_TYPE = "grantType"; + public static final String NONE = "none"; public static final String BASIC = "basic"; public static final String BEARER = "bearer"; public static final String API_KEY = "apikey";