From 1063af8d7fa338e4235b0b62be623f116cb20f72 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Wed, 18 Sep 2024 00:03:40 +0530 Subject: [PATCH 1/4] fix issues occurred while hitting Metadata APIs Signed-off-by: Saad Khan --- .../recommendations/engine/RecommendationEngine.java | 3 ++- .../com/autotune/analyzer/services/CreateExperiment.java | 3 ++- .../com/autotune/analyzer/services/DSMetadataService.java | 5 +++-- .../java/com/autotune/common/datasource/DataSourceInfo.java | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) 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 577030b30..3128a74b2 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -24,6 +24,7 @@ import com.autotune.common.data.result.ContainerData; import com.autotune.common.data.result.IntervalResults; import com.autotune.common.data.result.NamespaceData; +import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.auth.AuthenticationStrategy; @@ -1771,7 +1772,7 @@ private String getResults(Map mainKruizeExperimentMAP, Kru } } else if (kruizeObject.getExperiment_usecase_type().isLocal_monitoring()) { // get data from the provided datasource in case of local monitoring - DataSourceInfo dataSourceInfo = new ExperimentDBService().loadDataSourceFromDBByName(dataSource); + DataSourceInfo dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource); if (dataSourceInfo == null) { throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.MISSING_DATASOURCE_INFO); } diff --git a/src/main/java/com/autotune/analyzer/services/CreateExperiment.java b/src/main/java/com/autotune/analyzer/services/CreateExperiment.java index 4a35ecefa..e938b9d06 100644 --- a/src/main/java/com/autotune/analyzer/services/CreateExperiment.java +++ b/src/main/java/com/autotune/analyzer/services/CreateExperiment.java @@ -180,7 +180,8 @@ protected void doDelete(HttpServletRequest request, HttpServletResponse response sendSuccessResponse(response, "Experiment deleted successfully."); } } catch (Exception e) { - sendErrorResponse(inputData, response, e, HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); + LOGGER.error("Exception occurred while deleting the experiment: {}", e.getMessage()); + sendErrorResponse(inputData, response, null, HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } } diff --git a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java index e60f1e046..562ce82e2 100644 --- a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java +++ b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java @@ -23,6 +23,7 @@ import com.autotune.analyzer.utils.GsonUTCDateAdapter; import com.autotune.common.data.ValidationOutputData; import com.autotune.common.data.dataSourceMetadata.DataSourceMetadataInfo; +import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceManager; import com.autotune.common.datasource.DataSourceMetadataValidation; @@ -114,8 +115,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) if (validationOutputData.isSuccess()) { String dataSourceName = metadataAPIObject.getDataSourceName(); - - DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); + // fetch the DatasourceInfo object from the DataSourceCollection based on datasource name + DataSourceInfo datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); if (datasource == null) { sendErrorResponse( diff --git a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java index e3b81f29e..eed8a2855 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java @@ -135,6 +135,7 @@ public String toString() { ", serviceName='" + serviceName + '\'' + ", namespace='" + namespace + '\'' + ", url=" + url + + ", authenticationConfig=" + authenticationConfig + '}'; } } From e798581659a0e3a6323004874bb0416fe2cb09c8 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Wed, 18 Sep 2024 12:52:36 +0530 Subject: [PATCH 2/4] update datasource validation to check with DB first Signed-off-by: Saad Khan --- .../openshift/kruize-crc-openshift.yaml | 13 --------- .../engine/RecommendationEngine.java | 12 ++++++-- .../analyzer/services/DSMetadataService.java | 29 +++++++++++-------- .../autotune/common/utils/CommonUtils.java | 6 ++++ .../com/autotune/utils/KruizeConstants.java | 1 + 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/manifests/crc/default-db-included-installation/openshift/kruize-crc-openshift.yaml b/manifests/crc/default-db-included-installation/openshift/kruize-crc-openshift.yaml index d4e72a157..915fb5170 100644 --- a/manifests/crc/default-db-included-installation/openshift/kruize-crc-openshift.yaml +++ b/manifests/crc/default-db-included-installation/openshift/kruize-crc-openshift.yaml @@ -469,16 +469,3 @@ spec: - name: nginx-config-volume configMap: name: nginx-config ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRoleBinding -metadata: - name: kruize-monitoring-view -subjects: - - kind: ServiceAccount - name: kruize-sa - namespace: openshift-tuning -roleRef: - kind: ClusterRole - name: cluster-monitoring-view - apiGroup: rbac.authorization.k8s.io 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 3128a74b2..eb098db15 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -1772,9 +1772,15 @@ private String getResults(Map mainKruizeExperimentMAP, Kru } } else if (kruizeObject.getExperiment_usecase_type().isLocal_monitoring()) { // get data from the provided datasource in case of local monitoring - DataSourceInfo dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource); - if (dataSourceInfo == null) { - throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.MISSING_DATASOURCE_INFO); + // fetch the DatasourceInfo object from the DB based on datasource name + DataSourceInfo dataSourceInfo = new ExperimentDBService().loadDataSourceFromDBByName(dataSource); + // If the DB result is invalid, check the DataSourceCollection + if (CommonUtils.isInvalidDataSource(dataSourceInfo)) { + dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource); + // If DataSourceCollection is also invalid, return with error + if (CommonUtils.isInvalidDataSource(dataSourceInfo)) { + throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO); + } } // Fetch metrics dynamically from Metric Profile based on the datasource fetchMetricsBasedOnProfileAndDatasource(kruizeObject, interval_end_time, intervalStartTime, dataSourceInfo); diff --git a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java index 562ce82e2..4342ece87 100644 --- a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java +++ b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java @@ -27,6 +27,7 @@ import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceManager; import com.autotune.common.datasource.DataSourceMetadataValidation; +import com.autotune.common.utils.CommonUtils; import com.autotune.database.service.ExperimentDBService; import com.autotune.utils.KruizeConstants; import com.autotune.utils.KruizeSupportedTypes; @@ -115,18 +116,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) if (validationOutputData.isSuccess()) { String dataSourceName = metadataAPIObject.getDataSourceName(); - // fetch the DatasourceInfo object from the DataSourceCollection based on datasource name - DataSourceInfo datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); - - if (datasource == null) { - sendErrorResponse( - inputData, - response, - new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN), - HttpServletResponse.SC_BAD_REQUEST, - String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName) - ); - return; + // fetch the DatasourceInfo object from the DB based on datasource name + DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); + // If the DB result is invalid, check the DataSourceCollection + if (CommonUtils.isInvalidDataSource(datasource)) { + datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); + // If DataSourceCollection is also invalid, return with error + if (CommonUtils.isInvalidDataSource(datasource)) { + sendErrorResponse( + inputData, + response, + new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN), + HttpServletResponse.SC_BAD_REQUEST, + String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName) + ); + return; + } } DataSourceMetadataInfo metadataInfo = dataSourceManager.importMetadataFromDataSource(datasource); diff --git a/src/main/java/com/autotune/common/utils/CommonUtils.java b/src/main/java/com/autotune/common/utils/CommonUtils.java index 1f0efa344..b2acf15b5 100644 --- a/src/main/java/com/autotune/common/utils/CommonUtils.java +++ b/src/main/java/com/autotune/common/utils/CommonUtils.java @@ -293,4 +293,10 @@ public static double getPercentage(double newer, double older) { return ((newer - older)/older) * 100; } + + // Helper method to validate the DataSourceInfo object + public static boolean isInvalidDataSource(DataSourceInfo datasource) { + return datasource == null || datasource.getAuthenticationConfig() == null || + datasource.getAuthenticationConfig().toString().isEmpty(); + } } diff --git a/src/main/java/com/autotune/utils/KruizeConstants.java b/src/main/java/com/autotune/utils/KruizeConstants.java index c27cfe9e0..95a086d6d 100644 --- a/src/main/java/com/autotune/utils/KruizeConstants.java +++ b/src/main/java/com/autotune/utils/KruizeConstants.java @@ -446,6 +446,7 @@ public static class DataSourceErrorMsgs { public static final String SERVICE_NOT_FOUND = "Can not find service with specified name."; public static final String ENDPOINT_NOT_FOUND = "Service endpoint not found."; public static final String MISSING_DATASOURCE_INFO = "Datasource is missing, add a valid Datasource"; + public static final String INVALID_DATASOURCE_INFO = "Datasource is either missing or is invalid"; private DataSourceErrorMsgs() { } } From 9d50dfc9acda93dcfcfc962ef5b2366063d8b42d Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Wed, 18 Sep 2024 16:44:28 +0530 Subject: [PATCH 3/4] refactor code to get datasourceInfo from a common util method Signed-off-by: Saad Khan --- .../engine/RecommendationEngine.java | 19 ++++-------- .../analyzer/services/DSMetadataService.java | 30 ++++++++----------- .../autotune/common/utils/CommonUtils.java | 16 ++++++++++ 3 files changed, 34 insertions(+), 31 deletions(-) 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 eb098db15..e14556b82 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -24,9 +24,7 @@ import com.autotune.common.data.result.ContainerData; import com.autotune.common.data.result.IntervalResults; import com.autotune.common.data.result.NamespaceData; -import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; -import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.auth.AuthenticationStrategy; import com.autotune.common.auth.AuthenticationStrategyFactory; import com.autotune.common.exceptions.DataSourceNotExist; @@ -46,9 +44,6 @@ import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.sql.Timestamp; @@ -1772,15 +1767,11 @@ private String getResults(Map mainKruizeExperimentMAP, Kru } } else if (kruizeObject.getExperiment_usecase_type().isLocal_monitoring()) { // get data from the provided datasource in case of local monitoring - // fetch the DatasourceInfo object from the DB based on datasource name - DataSourceInfo dataSourceInfo = new ExperimentDBService().loadDataSourceFromDBByName(dataSource); - // If the DB result is invalid, check the DataSourceCollection - if (CommonUtils.isInvalidDataSource(dataSourceInfo)) { - dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource); - // If DataSourceCollection is also invalid, return with error - if (CommonUtils.isInvalidDataSource(dataSourceInfo)) { - throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO); - } + DataSourceInfo dataSourceInfo; + try { + dataSourceInfo = CommonUtils.getDataSourceInfo(dataSource); + } catch (Exception e) { + throw new DataSourceNotExist(e.getMessage()); } // Fetch metrics dynamically from Metric Profile based on the datasource fetchMetricsBasedOnProfileAndDatasource(kruizeObject, interval_end_time, intervalStartTime, dataSourceInfo); diff --git a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java index 4342ece87..904ada6ad 100644 --- a/src/main/java/com/autotune/analyzer/services/DSMetadataService.java +++ b/src/main/java/com/autotune/analyzer/services/DSMetadataService.java @@ -23,7 +23,6 @@ import com.autotune.analyzer.utils.GsonUTCDateAdapter; import com.autotune.common.data.ValidationOutputData; import com.autotune.common.data.dataSourceMetadata.DataSourceMetadataInfo; -import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceManager; import com.autotune.common.datasource.DataSourceMetadataValidation; @@ -116,22 +115,19 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) if (validationOutputData.isSuccess()) { String dataSourceName = metadataAPIObject.getDataSourceName(); - // fetch the DatasourceInfo object from the DB based on datasource name - DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); - // If the DB result is invalid, check the DataSourceCollection - if (CommonUtils.isInvalidDataSource(datasource)) { - datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); - // If DataSourceCollection is also invalid, return with error - if (CommonUtils.isInvalidDataSource(datasource)) { - sendErrorResponse( - inputData, - response, - new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN), - HttpServletResponse.SC_BAD_REQUEST, - String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName) - ); - return; - } + // fetch the DatasourceInfo object based on datasource name + DataSourceInfo datasource; + try { + datasource = CommonUtils.getDataSourceInfo(dataSourceName); + } catch (Exception e) { + sendErrorResponse( + inputData, + response, + new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN), + HttpServletResponse.SC_BAD_REQUEST, + String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName) + ); + return; } DataSourceMetadataInfo metadataInfo = dataSourceManager.importMetadataFromDataSource(datasource); diff --git a/src/main/java/com/autotune/common/utils/CommonUtils.java b/src/main/java/com/autotune/common/utils/CommonUtils.java index b2acf15b5..81cc9ef01 100644 --- a/src/main/java/com/autotune/common/utils/CommonUtils.java +++ b/src/main/java/com/autotune/common/utils/CommonUtils.java @@ -16,7 +16,9 @@ package com.autotune.common.utils; +import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; +import com.autotune.common.datasource.DataSourceManager; import com.autotune.utils.KruizeConstants; import java.sql.Timestamp; @@ -294,6 +296,20 @@ public static double getPercentage(double newer, double older) { return ((newer - older)/older) * 100; } + public static DataSourceInfo getDataSourceInfo(String dataSourceName) throws Exception { + DataSourceManager dataSourceManager = new DataSourceManager(); + // fetch the datasource from the DB + DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName); + if (isInvalidDataSource(datasource)) { + // fetch the datasource from the config + datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName); + if (isInvalidDataSource(datasource)) { + throw new Exception(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO); + } + } + return datasource; + } + // Helper method to validate the DataSourceInfo object public static boolean isInvalidDataSource(DataSourceInfo datasource) { return datasource == null || datasource.getAuthenticationConfig() == null || From fd15d3422133dfacdde18c7b8ffbc5a1c0c215c1 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Thu, 19 Sep 2024 17:48:50 +0530 Subject: [PATCH 4/4] add support for no-auth, refactor code to put auth strategy in a common constructor Signed-off-by: Saad Khan --- .../engine/RecommendationEngine.java | 4 +--- .../auth/AuthenticationStrategyFactory.java | 2 ++ .../com/autotune/common/auth/NoAuthStrategy.java | 9 +++++++++ .../common/datasource/DataSourceCollection.java | 15 +++++++++++---- .../common/datasource/DataSourceOperatorImpl.java | 4 +--- .../prometheus/PrometheusDataOperatorImpl.java | 4 +--- .../com/autotune/common/utils/CommonUtils.java | 8 ++++---- .../com/autotune/utils/GenericRestApiClient.java | 12 ++++++++---- .../java/com/autotune/utils/KruizeConstants.java | 1 + 9 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/autotune/common/auth/NoAuthStrategy.java 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";