From e80d39197ed815122e0ba10ba2cc0fcb4b706e4b Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Tue, 10 Sep 2024 10:03:30 +0530 Subject: [PATCH 1/8] add authentication support for datasources Signed-off-by: Saad Khan --- .../openshift/kruize-crc-openshift.yaml | 10 +- migrations/kruize_local_ddl.sql | 2 +- .../engine/RecommendationEngine.java | 15 ++- .../datasource/DataSourceCollection.java | 52 +++++++- .../common/datasource/DataSourceInfo.java | 10 +- .../DataSourceMetadataOperator.java | 6 +- .../common/datasource/DataSourceOperator.java | 26 ++-- .../datasource/DataSourceOperatorImpl.java | 40 ++++--- .../auth/APIKeyAuthenticationStrategy.java | 14 +++ .../datasource/auth/AuthenticationConfig.java | 57 +++++++++ .../auth/AuthenticationStrategy.java | 5 + .../auth/AuthenticationStrategyFactory.java | 27 +++++ .../auth/BasicAuthenticationStrategy.java | 19 +++ .../auth/BearerAuthenticationStrategy.java | 26 ++++ .../common/datasource/auth/Credentials.java | 100 ++++++++++++++++ .../auth/OAuth2AuthenticationStrategy.java | 64 ++++++++++ .../PrometheusDataOperatorImpl.java | 46 ++++--- .../autotune/database/helper/DBHelpers.java | 30 ++++- .../database/table/KruizeDataSourceEntry.java | 13 ++ .../handler/MetricCollectionHandler.java | 7 +- .../autotune/utils/GenericRestApiClient.java | 112 ++++-------------- .../com/autotune/utils/KruizeConstants.java | 12 ++ .../java/com/autotune/utils/TrialHelpers.java | 6 +- 23 files changed, 544 insertions(+), 155 deletions(-) create mode 100644 src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/Credentials.java create mode 100644 src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java 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 598334ab1..1200e0091 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 @@ -92,7 +92,7 @@ data: "savetodb": "true", "dbdriver": "jdbc:postgresql://", "plots": "true", - "local": "false", + "local": "true", "logAllHttpReqAndResp": "true", "hibernate": { "dialect": "org.hibernate.dialect.PostgreSQLDialect", @@ -121,7 +121,13 @@ data: "provider": "prometheus", "serviceName": "", "namespace": "", - "url": "https://prometheus-k8s.openshift-monitoring.svc.cluster.local:9091" + "url": "https://prometheus-k8s.openshift-monitoring.svc.cluster.local:9091", + "authentication": { + "type": "bearer", + "credentials": { + "tokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token" + } + } } ] } diff --git a/migrations/kruize_local_ddl.sql b/migrations/kruize_local_ddl.sql index 087ad1bf4..cd6453371 100644 --- a/migrations/kruize_local_ddl.sql +++ b/migrations/kruize_local_ddl.sql @@ -1,4 +1,4 @@ -create table IF NOT EXISTS kruize_datasources (version varchar(255), name varchar(255), provider varchar(255), serviceName varchar(255), namespace varchar(255), url varchar(255), primary key (name)); +create table IF NOT EXISTS kruize_datasources (version varchar(255), name varchar(255), provider varchar(255), serviceName varchar(255), namespace varchar(255), url varchar(255), authentication jsonb, primary key (name)); create table IF NOT EXISTS kruize_dsmetadata (id serial, version varchar(255), datasource_name varchar(255), cluster_name varchar(255), namespace varchar(255), workload_type varchar(255), workload_name varchar(255), container_name varchar(255), container_image_name varchar(255), primary key (id)); alter table kruize_experiments add column metadata_id bigint references kruize_dsmetadata(id), alter column datasource type varchar(255); create table IF NOT EXISTS kruize_metric_profiles (api_version varchar(255), kind varchar(255), metadata jsonb, name varchar(255) not null, k8s_type varchar(255), profile_version float(53) not null, slo jsonb, primary key (name)); 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 3a6a404b6..b5ada63d0 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -20,7 +20,6 @@ import com.autotune.analyzer.utils.AnalyzerConstants; import com.autotune.analyzer.utils.AnalyzerErrorConstants; import com.autotune.common.data.ValidationOutputData; -import com.autotune.common.data.dataSourceQueries.PromQLDataSourceQueries; import com.autotune.common.data.metrics.AggregationFunctions; import com.autotune.common.data.metrics.Metric; import com.autotune.common.data.metrics.MetricAggregationInfoResults; @@ -28,6 +27,9 @@ import com.autotune.common.data.result.ContainerData; import com.autotune.common.data.result.IntervalResults; import com.autotune.common.datasource.DataSourceInfo; +import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.datasource.auth.AuthenticationStrategy; +import com.autotune.common.datasource.auth.AuthenticationStrategyFactory; import com.autotune.common.exceptions.DataSourceNotExist; import com.autotune.common.k8sObjects.K8sObject; import com.autotune.common.utils.CommonUtils; @@ -1619,6 +1621,11 @@ 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); + AuthenticationConfig authenticationConfig = dataSourceInfo.getAuthenticationConfig(); + AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(authenticationConfig); + // Create the client + GenericRestApiClient client = new GenericRestApiClient(authenticationStrategy); + String metricProfileName = kruizeObject.getPerformanceProfile(); PerformanceProfile metricProfile = MetricProfileCollection.getInstance().getMetricProfileCollection().get(metricProfileName); @@ -1671,7 +1678,8 @@ public void fetchMetricsBasedOnProfileAndDatasource(KruizeObject kruizeObject, T URLEncoder.encode(queryToEncode, CHARACTER_ENCODING) ); LOGGER.info(dateMetricsUrl); - JSONObject genericJsonObject = new GenericRestApiClient(dateMetricsUrl).fetchMetricsJson(KruizeConstants.APIMessages.GET, ""); + client.setBaseURL(dateMetricsUrl); + JSONObject genericJsonObject = client.fetchMetricsJson(KruizeConstants.APIMessages.GET, ""); JsonObject jsonObject = new Gson().fromJson(genericJsonObject.toString(), JsonObject.class); JsonArray resultArray = jsonObject.getAsJsonObject(KruizeConstants.JSONKeys.DATA).getAsJsonArray(KruizeConstants.DataSourceConstants.DataSourceQueryJSONKeys.RESULT); // Process fetched metrics @@ -1744,7 +1752,8 @@ public void fetchMetricsBasedOnProfileAndDatasource(KruizeObject kruizeObject, T interval_end_time_epoc, measurementDurationMinutesInDouble.intValue() * KruizeConstants.TimeConv.NO_OF_SECONDS_PER_MINUTE); LOGGER.info(podMetricsUrl); - JSONObject genericJsonObject = new GenericRestApiClient(podMetricsUrl).fetchMetricsJson(KruizeConstants.APIMessages.GET, ""); + client.setBaseURL(podMetricsUrl); + JSONObject genericJsonObject = client.fetchMetricsJson(KruizeConstants.APIMessages.GET, ""); JsonObject jsonObject = new Gson().fromJson(genericJsonObject.toString(), JsonObject.class); JsonArray resultArray = jsonObject.getAsJsonObject(KruizeConstants.JSONKeys.DATA).getAsJsonArray(KruizeConstants.DataSourceConstants.DataSourceQueryJSONKeys.RESULT); // Process fetched metrics diff --git a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java index 3e4737200..13f04796c 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java @@ -15,6 +15,8 @@ *******************************************************************************/ package com.autotune.common.datasource; +import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.datasource.auth.Credentials; import com.autotune.common.exceptions.datasource.*; import com.autotune.common.data.ValidationOutputData; import com.autotune.common.utils.CommonUtils; @@ -84,6 +86,7 @@ public void addDataSource(DataSourceInfo datasource) { final String name = datasource.getName(); final String provider = datasource.getProvider(); final String url = datasource.getUrl().toString(); + AuthenticationConfig authenticationConfig = datasource.getAuthenticationConfig(); ValidationOutputData addedToDB = null; LOGGER.info(KruizeConstants.DataSourceConstants.DataSourceInfoMsgs.ADDING_DATASOURCE + name); @@ -96,7 +99,7 @@ public void addDataSource(DataSourceInfo datasource) { if (provider.equalsIgnoreCase(KruizeConstants.SupportedDatasources.PROMETHEUS)) { LOGGER.info(KruizeConstants.DataSourceConstants.DataSourceInfoMsgs.VERIFYING_DATASOURCE_REACHABILITY + name); DataSourceOperatorImpl op = DataSourceOperatorImpl.getInstance().getOperator(KruizeConstants.SupportedDatasources.PROMETHEUS); - if (op.isServiceable(url) == CommonUtils.DatasourceReachabilityStatus.REACHABLE) { + if (op.isServiceable(url, authenticationConfig) == CommonUtils.DatasourceReachabilityStatus.REACHABLE) { LOGGER.info(KruizeConstants.DataSourceConstants.DataSourceSuccessMsgs.DATASOURCE_SERVICEABLE); // add the data source to DB addedToDB = new ExperimentDBService().addDataSourceToDB(datasource); @@ -153,16 +156,55 @@ 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); - DataSourceInfo datasource = null; + JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.DataSourceConstants.DATASOURCE_AUTHENTICATION); + + DataSourceInfo dataSourceInfo; + AuthenticationConfig authConfig = null; + + // Parse and map authentication methods if they exist + if (authenticationObj != null) { + String type = authenticationObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_TYPE); + JSONObject credentialsObj = authenticationObj.getJSONObject(KruizeConstants.DataSourceConstants.AUTHENTICATION_CREDENTIALS); + + Credentials credentials = new Credentials(); + switch (type.toLowerCase()) { + case "basic": + credentials.setUsername(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_USERNAME)); + credentials.setPassword(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_PASSWORD)); + break; + case "bearer": + credentials.setTokenFilePath(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_TOKEN_FILE)); + break; + case "apikey": + credentials.setApiKey(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_API_KEY)); + credentials.setHeaderName(credentialsObj.optString(KruizeConstants.DataSourceConstants.AUTHENTICATION_HEADER_NAME, "X-API-Key")); + break; + case "oauth2": + credentials.setTokenEndpoint(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_TOKEN_ENDPOINT)); + credentials.setClientId(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_CLIENT_ID)); + credentials.setClientSecret(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_CLIENT_SECRET)); + credentials.setGrantType(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_GRANT_TYPE)); + break; + default: + LOGGER.error("Unsupported authentication type: {}", type); + continue; + } + + authConfig = new AuthenticationConfig(type, credentials); + } + + // Validate input if (!validateInput(name, provider, serviceName, dataSourceURL, namespace)) { continue; } if (dataSourceURL.isEmpty()) { - datasource = new DataSourceInfo(name, provider, serviceName, namespace, null); + dataSourceInfo = new DataSourceInfo(name, provider, serviceName, namespace, null, authConfig); } else { - datasource = new DataSourceInfo(name, provider, serviceName, namespace, new URL(dataSourceURL)); + dataSourceInfo = new DataSourceInfo(name, provider, serviceName, namespace, new URL(dataSourceURL), authConfig); } - addDataSource(datasource); + + // Add the datasource to the system + addDataSource(dataSourceInfo); } } catch (IOException e) { LOGGER.error(e.getMessage()); diff --git a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java index 60fe3d7fb..2d6f3941e 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java @@ -19,6 +19,7 @@ import java.net.URL; import com.autotune.analyzer.utils.AnalyzerConstants; +import com.autotune.common.datasource.auth.AuthenticationConfig; import com.autotune.operator.KruizeDeploymentInfo; import com.autotune.utils.KruizeConstants; import org.slf4j.LoggerFactory; @@ -39,10 +40,11 @@ public class DataSourceInfo { private final String serviceName; private final String namespace; private final URL url; + private final AuthenticationConfig authenticationConfig; private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DataSourceInfo.class); - public DataSourceInfo(String name, String provider, String serviceName, String namespace, URL url) { + public DataSourceInfo(String name, String provider, String serviceName, String namespace, URL url, AuthenticationConfig authenticationConfig) { this.name = name; this.provider = provider; if (null == url) { @@ -52,6 +54,7 @@ public DataSourceInfo(String name, String provider, String serviceName, String n } this.serviceName = serviceName; this.namespace = namespace; + this.authenticationConfig = authenticationConfig; } /** @@ -117,6 +120,10 @@ private URL getDNSBasedUrlForService(String serviceName, String namespace, Strin return dnsUrl; } + public AuthenticationConfig getAuthenticationConfig() { + return authenticationConfig; + } + @Override public String toString() { return "DataSourceInfo{" + @@ -125,6 +132,7 @@ public String toString() { ", serviceName='" + serviceName + '\'' + ", namespace='" + namespace + '\'' + ", url=" + url + + ", authenticationConfig=" + authenticationConfig + '}'; } } diff --git a/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java b/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java index 3fed859c4..aeafa848a 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java @@ -130,7 +130,7 @@ public DataSourceMetadataInfo processQueriesAndPopulateDataSourceMetadataInfo(Da try { String dataSourceName = dataSourceInfo.getName(); String dataSourceUrl = dataSourceInfo.getUrl().toString(); - JsonArray namespacesDataResultArray = op.getResultArrayForQuery(dataSourceUrl, PromQLDataSourceQueries.NAMESPACE_QUERY); + JsonArray namespacesDataResultArray = op.getResultArrayForQuery(dataSourceUrl, PromQLDataSourceQueries.NAMESPACE_QUERY, dataSourceInfo.getAuthenticationConfig()); if (false == op.validateResultArray(namespacesDataResultArray)){ dataSourceMetadataInfo = dataSourceDetailsHelper.createDataSourceMetadataInfoObject(dataSourceName, null); throw new Exception(KruizeConstants.DataSourceConstants.DataSourceMetadataErrorMsgs.NAMESPACE_QUERY_VALIDATION_FAILED); @@ -154,7 +154,7 @@ public DataSourceMetadataInfo processQueriesAndPopulateDataSourceMetadataInfo(Da */ HashMap> datasourceWorkloads = new HashMap<>(); JsonArray workloadDataResultArray = op.getResultArrayForQuery(dataSourceUrl, - PromQLDataSourceQueries.WORKLOAD_QUERY); + PromQLDataSourceQueries.WORKLOAD_QUERY, dataSourceInfo.getAuthenticationConfig()); if (op.validateResultArray(workloadDataResultArray)) { datasourceWorkloads = dataSourceDetailsHelper.getWorkloadInfo(workloadDataResultArray); @@ -173,7 +173,7 @@ public DataSourceMetadataInfo processQueriesAndPopulateDataSourceMetadataInfo(Da */ HashMap> datasourceContainers = new HashMap<>(); JsonArray containerDataResultArray = op.getResultArrayForQuery(dataSourceUrl, - PromQLDataSourceQueries.CONTAINER_QUERY); + PromQLDataSourceQueries.CONTAINER_QUERY, dataSourceInfo.getAuthenticationConfig()); if (op.validateResultArray(containerDataResultArray)) { datasourceContainers = dataSourceDetailsHelper.getContainerInfo(containerDataResultArray); diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperator.java b/src/main/java/com/autotune/common/datasource/DataSourceOperator.java index 91f79a320..cc076248f 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperator.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperator.java @@ -15,6 +15,7 @@ *******************************************************************************/ package com.autotune.common.datasource; +import com.autotune.common.datasource.auth.AuthenticationConfig; import com.autotune.common.utils.CommonUtils; import com.google.gson.JsonArray; import org.json.JSONObject; @@ -47,34 +48,41 @@ public interface DataSourceOperator { /** * Check if a datasource is reachable, implementation of this function * should check and return the reachability status (REACHABLE, NOT_REACHABLE) - * @param dataSourceUrl String containing the url for the datasource + * + * @param dataSourceUrl String containing the url for the datasource + * @param authenticationConfig * @return DatasourceReachabilityStatus */ - CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceUrl); + CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceUrl, AuthenticationConfig authenticationConfig); /** * executes specified query on datasource and returns the result value - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param url String containing the url for the datasource + * @param query String containing the query to be executed + * @param authenticationConfig * @return Object containing the result value for the specified query */ - Object getValueForQuery(String url, String query); + Object getValueForQuery(String url, String query, AuthenticationConfig authenticationConfig); /** * executes specified query on datasource and returns the JSON Object - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param url String containing the url for the datasource + * @param query String containing the query to be executed + * @param authenticationConfig * @return JSONObject for the specified query */ - JSONObject getJsonObjectForQuery(String url, String query); + JSONObject getJsonObjectForQuery(String url, String query, AuthenticationConfig authenticationConfig); /** * executes specified query on datasource and returns the result array * @param url String containing the url for the datasource * @param query String containing the query to be executed + * @param authenticationConfig contains Authentication Config * @return JsonArray containing the result array for the specified query */ - public JsonArray getResultArrayForQuery(String url, String query); + public JsonArray getResultArrayForQuery(String url, String query, AuthenticationConfig authenticationConfig); /** * Validates a JSON array to ensure it is not null, not a JSON null, and has at least one element. diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java index 358ba5ca1..99db90fd9 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java @@ -3,6 +3,9 @@ import com.autotune.analyzer.exceptions.MonitoringAgentNotFoundException; import com.autotune.analyzer.exceptions.TooManyRecursiveCallsException; import com.autotune.analyzer.utils.AnalyzerConstants; +import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.datasource.auth.AuthenticationStrategy; +import com.autotune.common.datasource.auth.AuthenticationStrategyFactory; import com.autotune.common.datasource.prometheus.PrometheusDataOperatorImpl; import com.autotune.common.exceptions.datasource.ServiceNotFound; import com.autotune.common.target.kubernetes.service.KubernetesServices; @@ -12,7 +15,6 @@ import com.autotune.utils.GenericRestApiClient; import com.autotune.utils.KruizeConstants; import com.google.gson.JsonArray; -import com.autotune.utils.authModels.BearerAccessToken; import io.fabric8.kubernetes.api.model.Service; import org.json.JSONArray; import org.json.JSONObject; @@ -71,22 +73,26 @@ public String getDefaultServicePortForProvider() { /** * Check if a datasource is reachable, implementation of this function * should check and return the reachability status (REACHABLE, NOT_REACHABLE) - * @param dataSourceUrl String containing the url for the datasource + * + * @param dataSourceUrl String containing the url for the datasource + * @param authenticationConfig * @return DatasourceReachabilityStatus */ @Override - public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceUrl) { + public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceUrl, AuthenticationConfig authenticationConfig) { return null; } /** * executes specified query on datasource and returns the result value - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param url String containing the url for the datasource + * @param query String containing the query to be executed + * @param authenticationConfig * @return Object containing the result value for the specified query */ @Override - public Object getValueForQuery(String url, String query) { + public Object getValueForQuery(String url, String query, AuthenticationConfig authenticationConfig) { return null; } @@ -100,12 +106,14 @@ public String getQueryEndpoint() { } /** * executes specified query on datasource and returns the JSON Object - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param url String containing the url for the datasource + * @param query String containing the query to be executed + * @param authenticationConfig * @return JSONObject for the specified query */ @Override - public JSONObject getJsonObjectForQuery(String url, String query) { + public JSONObject getJsonObjectForQuery(String url, String query, AuthenticationConfig authenticationConfig) { return null; } @@ -116,7 +124,7 @@ public JSONObject getJsonObjectForQuery(String url, String query) { * @return JsonArray containing the result array for the specified query */ @Override - public JsonArray getResultArrayForQuery(String url, String query) { + public JsonArray getResultArrayForQuery(String url, String query, AuthenticationConfig authenticationConfig) { return null; } @@ -159,10 +167,11 @@ public ArrayList getAppsForLayer(DataSourceInfo dataSource, String query String queryURL = dataSourceURL + queryEndpoint + query; LOGGER.debug("Query URL is: {}", queryURL); try { - GenericRestApiClient genericRestApiClient = new GenericRestApiClient( - dataSourceURL + queryEndpoint, - new BearerAccessToken(this.getToken()) - ); + AuthenticationConfig authenticationConfig = dataSource.getAuthenticationConfig(); + AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(authenticationConfig); + // Create the client + GenericRestApiClient genericRestApiClient = new GenericRestApiClient(authenticationStrategy); + genericRestApiClient.setBaseURL(dataSourceURL + queryEndpoint); JSONObject responseJson = genericRestApiClient.fetchMetricsJson("GET", query); int level = 0; try { @@ -193,7 +202,8 @@ public static DataSourceInfo getMonitoringAgent(String dataSource) throws Monito monitoringAgentEndpoint = getServiceEndpoint(KruizeDeploymentInfo.monitoring_service); } if (dataSource.equals(AnalyzerConstants.PROMETHEUS_DATA_SOURCE)) { - monitoringAgent = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, AnalyzerConstants.PROMETHEUS_DATA_SOURCE, null, null, new URL(monitoringAgentEndpoint)); + AuthenticationConfig authenticationConfig = AuthenticationConfig.noAuth(); + monitoringAgent = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, AnalyzerConstants.PROMETHEUS_DATA_SOURCE, null, null, new URL(monitoringAgentEndpoint), authenticationConfig); } } diff --git a/src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java b/src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java new file mode 100644 index 000000000..71335919a --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java @@ -0,0 +1,14 @@ +package com.autotune.common.datasource.auth; + +public class APIKeyAuthenticationStrategy implements AuthenticationStrategy { + private final String apiKey; + + public APIKeyAuthenticationStrategy(String apiKey) { + this.apiKey = apiKey; + } + + @Override + public String applyAuthentication() { + return "Api-Key " + apiKey; + } +} diff --git a/src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java b/src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java new file mode 100644 index 000000000..fe54c8554 --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java @@ -0,0 +1,57 @@ +package com.autotune.common.datasource.auth; + +import org.apache.http.client.methods.HttpRequestBase; + +import java.util.Base64; + +public class AuthenticationConfig { + private final String type; // "basic", "bearer", "apiKey", "oauth2" + private final Credentials credentials; + + public AuthenticationConfig(String type, Credentials credentials) { + this.type = type; + this.credentials = credentials; + } + + public String getType() { + return type; + } + + public Credentials getCredentials() { + return credentials; + } + + public void applyAuthentication(HttpRequestBase httpRequestBase) { + switch (type) { + case "Basic": + String basicAuth = Base64.getEncoder().encodeToString((credentials.getUsername() + ":" + credentials.getPassword()).getBytes()); + httpRequestBase.setHeader("Authorization", "Basic " + basicAuth); + break; + case "Bearer": + httpRequestBase.setHeader("Authorization", "Bearer " + credentials.getTokenFilePath()); + break; + case "APIKey": + httpRequestBase.setHeader("Authorization", "ApiKey " + credentials.getApiKey()); + break; + case "OAuth2": + // Assume the token is already retrieved and set + httpRequestBase.setHeader("Authorization", "Bearer " + credentials.getTokenFilePath()); + break; + default: + throw new IllegalArgumentException("Unsupported authentication type: " + type); + } + } + + // Static method to return a no-auth config + public static AuthenticationConfig noAuth() { + return new AuthenticationConfig("none", null); // Type "none" or similar to indicate no auth + } + + @Override + public String toString() { + return "AuthenticationConfig{" + + "type='" + type + '\'' + + ", credentials=" + credentials + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java b/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java new file mode 100644 index 000000000..c1854a917 --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java @@ -0,0 +1,5 @@ +package com.autotune.common.datasource.auth; + +public interface AuthenticationStrategy { + String applyAuthentication(); +} diff --git a/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java b/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java new file mode 100644 index 000000000..86deb2cb2 --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java @@ -0,0 +1,27 @@ +package com.autotune.common.datasource.auth; + +public class AuthenticationStrategyFactory { + + public static AuthenticationStrategy createAuthenticationStrategy(AuthenticationConfig authConfig) { + String type = authConfig.getType(); + switch (type) { + case "basic": + String username = authConfig.getCredentials().getUsername(); + String password = authConfig.getCredentials().getPassword(); + return new BasicAuthenticationStrategy(username, password); + case "bearer": + String tokenFilePath = authConfig.getCredentials().getTokenFilePath(); + return new BearerAuthenticationStrategy(tokenFilePath); + case "apiKey": + String apiKey = authConfig.getCredentials().getApiKey(); + return new APIKeyAuthenticationStrategy(apiKey); + case "oauth2": + String tokenEndpoint = authConfig.getCredentials().getTokenEndpoint(); + String clientId = authConfig.getCredentials().getClientId(); + String clientSecret = authConfig.getCredentials().getClientSecret(); + return new OAuth2AuthenticationStrategy(tokenEndpoint, clientId, clientSecret); + default: + throw new IllegalArgumentException("Unknown authentication type: " + type); + } + } +} diff --git a/src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java b/src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java new file mode 100644 index 000000000..2aa024847 --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java @@ -0,0 +1,19 @@ +package com.autotune.common.datasource.auth; + +import java.util.Base64; + +public class BasicAuthenticationStrategy implements AuthenticationStrategy { + private final String username; + private final String password; + + public BasicAuthenticationStrategy(String username, String password) { + this.username = username; + this.password = password; + } + + @Override + public String applyAuthentication() { + String auth = username + ":" + password; + return "Basic " + Base64.getEncoder().encodeToString(auth.getBytes()); + } +} diff --git a/src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java b/src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java new file mode 100644 index 000000000..6be7ce5fe --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java @@ -0,0 +1,26 @@ +package com.autotune.common.datasource.auth; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class BearerAuthenticationStrategy implements AuthenticationStrategy { + private final String tokenFilePath; + + public BearerAuthenticationStrategy(String tokenFilePath) { + this.tokenFilePath = tokenFilePath; + } + + @Override + public String applyAuthentication() { + // Read token from file + try { + BufferedReader reader = new BufferedReader(new FileReader(tokenFilePath)); + String token = reader.readLine(); + reader.close(); + return "Bearer " + token; + } catch (IOException e) { + throw new RuntimeException("Failed to read Bearer token: " + e.getMessage()); + } + } +} diff --git a/src/main/java/com/autotune/common/datasource/auth/Credentials.java b/src/main/java/com/autotune/common/datasource/auth/Credentials.java new file mode 100644 index 000000000..aedb5963b --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/Credentials.java @@ -0,0 +1,100 @@ +package com.autotune.common.datasource.auth; + +public class Credentials { + private String grantType; // OAuth2 + private String clientId; // OAuth2 + private String clientSecret; // OAuth2 + private String username; // Basic auth + private String password; // Basic auth + private String tokenEndpoint; // OAuth2 + private String tokenFilePath; // Bearer token + private String apiKey; // API key + private String headerName; // API key header name + + public String getUsername() { + return username; + } + + public String getGrantType() { + return grantType; + } + + public String getClientSecret() { + return clientSecret; + } + + public String getClientId() { + return clientId; + } + + public String getTokenEndpoint() { + return tokenEndpoint; + } + + public String getHeaderName() { + return headerName; + } + + public String getApiKey() { + return apiKey; + } + + public String getTokenFilePath() { + return tokenFilePath; + } + + public String getPassword() { + return password; + } + + public void setGrantType(String grantType) { + this.grantType = grantType; + } + + public void setClientId(String clientId) { + this.clientId = clientId; + } + + public void setClientSecret(String clientSecret) { + this.clientSecret = clientSecret; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setTokenEndpoint(String tokenEndpoint) { + this.tokenEndpoint = tokenEndpoint; + } + + public void setTokenFilePath(String tokenFilePath) { + this.tokenFilePath = tokenFilePath; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public void setHeaderName(String headerName) { + this.headerName = headerName; + } + + @Override + public String toString() { + return "Credentials{" + + "grantType='" + grantType + '\'' + + ", clientId='" + clientId + '\'' + + ", clientSecret='" + clientSecret + '\'' + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + ", tokenEndpoint='" + tokenEndpoint + '\'' + + ", tokenFilePath='" + tokenFilePath + '\'' + + ", apiKey='" + apiKey + '\'' + + ", headerName='" + headerName + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java b/src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java new file mode 100644 index 000000000..fbe03b289 --- /dev/null +++ b/src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java @@ -0,0 +1,64 @@ +package com.autotune.common.datasource.auth; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class OAuth2AuthenticationStrategy implements AuthenticationStrategy { + private final String tokenEndpoint; + private final String clientId; + private final String clientSecret; + + public OAuth2AuthenticationStrategy(String tokenEndpoint, String clientId, String clientSecret) { + this.tokenEndpoint = tokenEndpoint; + this.clientId = clientId; + this.clientSecret = clientSecret; + } + + @Override + public String applyAuthentication() { + try { + // Fetch the OAuth2 token using client credentials + HttpClient client = HttpClient.newHttpClient(); + + // Create the request body for the OAuth2 token request + String form = "grant_type=client_credentials" + + "&client_id=" + clientId + + "&client_secret=" + clientSecret; + + // Send the POST request to fetch the token + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(tokenEndpoint)) + .header("Content-Type", "application/x-www-form-urlencoded") + .POST(HttpRequest.BodyPublishers.ofString(form)) + .build(); + + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + if (response.statusCode() == 200) { + // Parse the token from the response JSON (assuming GitHub returns a JSON) + String token = parseToken(response.body()); + return "Bearer " + token; // Return the token in Bearer format + } else { + throw new RuntimeException("Failed to fetch OAuth2 token: " + response.body()); + } + } catch (Exception e) { + throw new RuntimeException("Error fetching OAuth2 token", e); + } + } + + // You will need a method to parse the token from the response body + private String parseToken(String responseBody) { + // Parse the response and extract the access token. + // GitHub returns a form-encoded response, so you need to parse the "access_token" + String[] pairs = responseBody.split("&"); + for (String pair : pairs) { + String[] keyValue = pair.split("="); + if (keyValue[0].equals("access_token")) { + return keyValue[1]; + } + } + throw new RuntimeException("No access token found in response"); + } +} 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 d79ece652..782486c5c 100644 --- a/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java @@ -17,6 +17,9 @@ import com.autotune.analyzer.utils.AnalyzerConstants; import com.autotune.common.datasource.DataSourceOperatorImpl; +import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.datasource.auth.AuthenticationStrategy; +import com.autotune.common.datasource.auth.AuthenticationStrategyFactory; import com.autotune.common.utils.CommonUtils; import com.autotune.utils.KruizeConstants; import com.autotune.utils.GenericRestApiClient; @@ -67,18 +70,20 @@ public String getDefaultServicePortForProvider() { /** * Check if a datasource is reachable, implementation of this function * should check and return the reachability status (REACHABLE, NOT_REACHABLE) - * @param dataSourceURL String containing the url for the datasource + * + * @param dataSourceURL String containing the url for the datasource + * @param authenticationConfig * @return DatasourceReachabilityStatus */ @Override - public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceURL) { + public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceURL, AuthenticationConfig authenticationConfig) { String dataSourceStatus; Object queryResult; String query = KruizeConstants.DataSourceConstants.PROMETHEUS_REACHABILITY_QUERY; CommonUtils.DatasourceReachabilityStatus reachabilityStatus; - queryResult = this.getValueForQuery(dataSourceURL, query); + queryResult = this.getValueForQuery(dataSourceURL, query, authenticationConfig); if (queryResult != null){ dataSourceStatus = queryResult.toString(); @@ -96,14 +101,16 @@ public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceU /** * executes specified query on datasource and returns the result value - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param url String containing the url for the datasource + * @param query String containing the query to be executed + * @param authenticationConfig * @return Object containing the result value for the specified query */ @Override - public Object getValueForQuery(String url, String query) { + public Object getValueForQuery(String url, String query, AuthenticationConfig authenticationConfig) { try { - JSONObject jsonObject = getJsonObjectForQuery(url, query); + JSONObject jsonObject = getJsonObjectForQuery(url, query, authenticationConfig); if (null == jsonObject) { return null; @@ -127,18 +134,21 @@ public Object getValueForQuery(String url, String query) { /** * executes specified query on datasource and returns the JSON Object - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param url String containing the url for the datasource + * @param query String containing the query to be executed + * @param authenticationConfig * @return JSONObject for the specified query */ @Override - public JSONObject getJsonObjectForQuery(String url, String query) { - GenericRestApiClient apiClient = new GenericRestApiClient( - CommonUtils.getBaseDataSourceUrl( - url, - KruizeConstants.SupportedDatasources.PROMETHEUS - ) - ); + public JSONObject getJsonObjectForQuery(String url, String query, AuthenticationConfig authenticationConfig) { + AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(authenticationConfig); + // Create the client + GenericRestApiClient apiClient = new GenericRestApiClient(authenticationStrategy); + apiClient.setBaseURL(CommonUtils.getBaseDataSourceUrl( + url, + KruizeConstants.SupportedDatasources.PROMETHEUS + )); if (null == apiClient) { return null; @@ -202,9 +212,9 @@ public String getQueryEndpoint() { */ @Override - public JsonArray getResultArrayForQuery(String url, String query) { + public JsonArray getResultArrayForQuery(String url, String query, AuthenticationConfig authenticationConfig) { try { - JSONObject jsonObject = getJsonObjectForQuery(url, query); + JSONObject jsonObject = getJsonObjectForQuery(url, query, authenticationConfig); if (null == jsonObject) { return null; diff --git a/src/main/java/com/autotune/database/helper/DBHelpers.java b/src/main/java/com/autotune/database/helper/DBHelpers.java index 47f1c0493..63b382484 100644 --- a/src/main/java/com/autotune/database/helper/DBHelpers.java +++ b/src/main/java/com/autotune/database/helper/DBHelpers.java @@ -32,6 +32,7 @@ import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceMetadataOperator; +import com.autotune.common.datasource.auth.AuthenticationConfig; import com.autotune.common.k8sObjects.K8sObject; import com.autotune.database.table.*; import com.autotune.utils.KruizeConstants; @@ -39,6 +40,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.CollectionType; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.slf4j.Logger; @@ -735,13 +737,20 @@ public static List convertKruizeDataSourceToDataSourceObject(Lis int failureCount = 0; for (KruizeDataSourceEntry kruizeDataSource : kruizeDataSourceList) { try { - DataSourceInfo dataSourceInfo = null; + DataSourceInfo dataSourceInfo; + JsonNode authConfigJsonNode = kruizeDataSource.getAuthentication(); + // Define the type for the list of AuthenticationConfig + CollectionType listType = new ObjectMapper().getTypeFactory().constructCollectionType(List.class, AuthenticationConfig.class); + // Convert JsonNode to List + AuthenticationConfig authConfig = new ObjectMapper().treeToValue(authConfigJsonNode, AuthenticationConfig.class); if (kruizeDataSource.getServiceName().isEmpty() && null != kruizeDataSource.getUrl()) { - dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), kruizeDataSource - .getProvider(), null, null, new URL(kruizeDataSource.getUrl())); - } else{ - dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), kruizeDataSource - .getProvider(), kruizeDataSource.getServiceName(), kruizeDataSource.getNamespace(), null); + dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), + kruizeDataSource.getProvider(), null, null, + new URL(kruizeDataSource.getUrl()), authConfig); + } else { + dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), + kruizeDataSource.getProvider(), kruizeDataSource.getServiceName(), + kruizeDataSource.getNamespace(), null, authConfig); } dataSourceInfoList.add(dataSourceInfo); } catch (Exception e) { @@ -763,6 +772,13 @@ public static List convertKruizeDataSourceToDataSourceObject(Lis */ public static KruizeDataSourceEntry convertDataSourceToDataSourceDBObj(DataSourceInfo dataSourceInfo) { KruizeDataSourceEntry kruizeDataSource; + Gson gson = new GsonBuilder() + .disableHtmlEscaping() + .setPrettyPrinting() + .enableComplexMapKeySerialization() + .setDateFormat(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT) + .registerTypeAdapter(Date.class, new GsonUTCDateAdapter()) + .create(); try { kruizeDataSource = new KruizeDataSourceEntry(); kruizeDataSource.setVersion(KruizeConstants.DataSourceConstants.DataSourceMetadataInfoConstants.version); @@ -771,6 +787,8 @@ public static KruizeDataSourceEntry convertDataSourceToDataSourceDBObj(DataSourc kruizeDataSource.setServiceName(dataSourceInfo.getServiceName()); kruizeDataSource.setNamespace(dataSourceInfo.getNamespace()); kruizeDataSource.setUrl(dataSourceInfo.getUrl().toString()); + String authConfigs = gson.toJson(dataSourceInfo.getAuthenticationConfig()); + kruizeDataSource.setAuthentication(new ObjectMapper().readTree(authConfigs)); } catch (Exception e) { kruizeDataSource = null; LOGGER.error("Error while converting DataSource Object to KruizeDataSource table due to {}", e.getMessage()); diff --git a/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java b/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java index 63e5a90d5..433ae966c 100644 --- a/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java +++ b/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java @@ -16,7 +16,10 @@ package com.autotune.database.table; +import com.fasterxml.jackson.databind.JsonNode; import jakarta.persistence.*; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; /** * This is a Java class named KruizeDataSourceEntry annotated with JPA annotations. @@ -42,6 +45,8 @@ public class KruizeDataSourceEntry { private String serviceName; private String namespace; private String url; + @JdbcTypeCode(SqlTypes.JSON) + private JsonNode authentication; public String getVersion() { return version; @@ -90,4 +95,12 @@ public String getUrl() { public void setUrl(String url) { this.url = url; } + + public JsonNode getAuthentication() { + return authentication; + } + + public void setAuthentication(JsonNode authentication) { + this.authentication = authentication; + } } diff --git a/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java b/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java index f078d9845..67c697793 100644 --- a/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java +++ b/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java @@ -37,6 +37,7 @@ import com.autotune.experimentManager.utils.EMUtil; import com.autotune.operator.KruizeDeploymentInfo; import com.autotune.utils.KruizeConstants; +import com.autotune.common.datasource.auth.AuthenticationConfig; import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -121,9 +122,10 @@ public void execute(ExperimentTrial experimentTrial, TrialDetails trialDetails, if (null == ado) { // TODO: Return an error saying unsupported datasource } + AuthenticationConfig noAuthConfig = AuthenticationConfig.noAuth(); String queryResult = (String) ado.getValueForQuery(experimentTrial.getDatasourceInfoHashMap() .get(podMetric.getDatasource()) - .getUrl().toString(), updatedPodQuery); + .getUrl().toString(), updatedPodQuery, noAuthConfig); if (null != queryResult && !queryResult.isEmpty() && !queryResult.isBlank()) { try { queryResult = queryResult.trim(); @@ -160,9 +162,10 @@ public void execute(ExperimentTrial experimentTrial, TrialDetails trialDetails, } if (null != updatedContainerQuery) { LOGGER.debug("Updated Query - " + updatedContainerQuery); + AuthenticationConfig noAuthConfig = AuthenticationConfig.noAuth(); String queryResult = (String) ado.getValueForQuery(experimentTrial.getDatasourceInfoHashMap() .get(containerMetric.getDatasource()) - .getUrl().toString(), updatedContainerQuery); + .getUrl().toString(), updatedContainerQuery, noAuthConfig); if (null != queryResult && !queryResult.isEmpty() && !queryResult.isBlank()) { try { queryResult = queryResult.trim(); diff --git a/src/main/java/com/autotune/utils/GenericRestApiClient.java b/src/main/java/com/autotune/utils/GenericRestApiClient.java index 2626ba7ff..115549679 100644 --- a/src/main/java/com/autotune/utils/GenericRestApiClient.java +++ b/src/main/java/com/autotune/utils/GenericRestApiClient.java @@ -15,10 +15,11 @@ *******************************************************************************/ package com.autotune.utils; +import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.datasource.auth.AuthenticationStrategy; import com.autotune.utils.authModels.APIKeysAuthentication; import com.autotune.utils.authModels.BasicAuthentication; import com.autotune.utils.authModels.BearerAccessToken; -import com.autotune.utils.authModels.OAuth2Config; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; @@ -35,8 +36,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.SSLContext; -import java.io.BufferedReader; -import java.io.FileReader; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -52,113 +51,46 @@ public class GenericRestApiClient { private static final long serialVersionUID = 1L; private static final Logger LOGGER = LoggerFactory.getLogger(GenericRestApiClient.class); - private final String baseURL; + private String baseURL; private BasicAuthentication basicAuthentication; private BearerAccessToken bearerAccessToken; private APIKeysAuthentication apiKeysAuthentication; - private OAuth2Config oAuth2Config; //Yet to implement - private String authHeaderString; + private AuthenticationStrategy authenticationStrategy; /** * Initializes a new instance just by passing baseURL which does not need any authentication. - * @param baseURL + * @param authenticationStrategy */ - public GenericRestApiClient(String baseURL) { - String tokenFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/token"; - String token = null; - try { - // Read the token from the file - BufferedReader reader = new BufferedReader(new FileReader(tokenFilePath)); - token = reader.readLine(); - reader.close(); - this.bearerAccessToken = new BearerAccessToken(token); - this.setAuthHeaderString(this.bearerAccessToken.getAuthHeader()); - // Print the service account token - } catch (Exception e) { - LOGGER.error("Error reading service account token: " + e.getMessage()); - } - - this.baseURL = baseURL; - } - - /** - * Use this constructor to initializes a new instance if RESTAPI need Basic authentication. - * @param baseURL - * @param basicAuthentication - */ - public GenericRestApiClient(String baseURL, BasicAuthentication basicAuthentication) { - this.baseURL = baseURL; - this.basicAuthentication = basicAuthentication; - this.setAuthHeaderString(this.basicAuthentication.getAuthHeader()); + public GenericRestApiClient(AuthenticationStrategy authenticationStrategy) { + this.authenticationStrategy = authenticationStrategy; } /** - * Use this constructor to initializes a new instance if RESTAPI need Bearer authentication. - * @param baseURL - * @param bearerAccessToken - */ - public GenericRestApiClient(String baseURL, BearerAccessToken bearerAccessToken) { - this.baseURL = baseURL; - this.bearerAccessToken = bearerAccessToken; - this.setAuthHeaderString(this.bearerAccessToken.getAuthHeader()); - } - - /** - * Use this constructor to initializes a new instance if RESTAPI need APIKeys authentication. - * @param baseURL - * @param apiKeysAuthentication - */ - public GenericRestApiClient(String baseURL, APIKeysAuthentication apiKeysAuthentication) { - this.baseURL = baseURL; - this.apiKeysAuthentication = apiKeysAuthentication; - this.setAuthHeaderString(this.apiKeysAuthentication.getAuthHeader()); - } - - /** - * Use this constructor to initializes a new instance if RESTAPI need OAuth2 authentication. - * @param baseURL - * @param oAuth2Config - */ - public GenericRestApiClient(String baseURL, OAuth2Config oAuth2Config) { - this.baseURL = baseURL; - this.oAuth2Config = oAuth2Config; - } - - public String getAuthHeaderString() { - return authHeaderString; - } - - public void setAuthHeaderString(String authHeaderString) { - this.authHeaderString = authHeaderString; - } - - /** - * This methode appends aueryString with baseURL and returns response in JSON using specified authentication. + * This method appends aueryString with baseURL and returns response in JSON using specified authentication. * @param methodType Http methods like GET,POST,PATCH etc * @param queryString * @return Json object which contains API response. * @throws IOException */ public JSONObject fetchMetricsJson(String methodType, String queryString) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { - java.lang.System.setProperty("https.protocols", "TLSv1.2"); + System.setProperty("https.protocols", "TLSv1.2"); String jsonOutputInString = ""; - SSLContext sslContext = SSLContexts.custom().loadTrustMaterial((chain, authType) -> true).build(); //overriding the standard certificate verification process and trust all certificate chains regardless of their validity + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial((chain, authType) -> true).build(); // Trust all certificates SSLConnectionSocketFactory sslConnectionSocketFactory = - new SSLConnectionSocketFactory(sslContext, new String[] - {"TLSv1.2" }, null, - NoopHostnameVerifier.INSTANCE); + new SSLConnectionSocketFactory(sslContext, new String[]{"TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE); try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build()) { - HttpRequestBase httpRequestBase = null; + HttpRequestBase httpRequestBase; if (methodType.equalsIgnoreCase("GET")) { - httpRequestBase = new HttpGet(this.baseURL - + URLEncoder.encode(queryString, StandardCharsets.UTF_8) - ); + httpRequestBase = new HttpGet(baseURL + URLEncoder.encode(queryString, StandardCharsets.UTF_8)); + } else { + throw new UnsupportedOperationException("Unsupported method type: " + methodType); } - // Checks if auth string is null and then checks if it's not empty - if (null != this.authHeaderString && !this.authHeaderString.isEmpty()) { - httpRequestBase.setHeader("Authorization", this.authHeaderString); + // Apply authentication + if (authenticationStrategy != null) { + String authHeader = authenticationStrategy.applyAuthentication(); + httpRequestBase.setHeader("Authorization", authHeader); } - LOGGER.debug("Executing request " + httpRequestBase.getRequestLine()); + LOGGER.info("Executing request: {}", httpRequestBase.getRequestLine()); jsonOutputInString = httpclient.execute(httpRequestBase, new StringResponseHandler()); } @@ -180,5 +112,7 @@ public String handleResponse(HttpResponse response) throws IOException { } - + public void setBaseURL(String baseURL) { + this.baseURL = baseURL; + } } diff --git a/src/main/java/com/autotune/utils/KruizeConstants.java b/src/main/java/com/autotune/utils/KruizeConstants.java index 046220d97..cc97ddd0d 100644 --- a/src/main/java/com/autotune/utils/KruizeConstants.java +++ b/src/main/java/com/autotune/utils/KruizeConstants.java @@ -396,6 +396,18 @@ public static class DataSourceConstants { public static final String PROMETHEUS_REACHABILITY_QUERY = "up"; public static final String DATASOURCE_ENDPOINT_WITH_QUERY = "%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s"; public static final String DATE_ENDPOINT_WITH_QUERY = "%s/api/v1/query?query=%s"; + public static final String DATASOURCE_AUTHENTICATION = "authentication"; + public static final String AUTHENTICATION_TYPE = "type"; + public static final String AUTHENTICATION_CREDENTIALS = "credentials"; + public static final String AUTHENTICATION_USERNAME = "username"; + public static final String AUTHENTICATION_PASSWORD = "password"; + public static final String AUTHENTICATION_TOKEN_FILE = "tokenFilePath"; + public static final String AUTHENTICATION_API_KEY = "apiKey"; + public static final String AUTHENTICATION_HEADER_NAME = "header"; + public static final String AUTHENTICATION_TOKEN_ENDPOINT = "tokenEndpoint"; + public static final String AUTHENTICATION_CLIENT_ID = "clientId"; + public static final String AUTHENTICATION_CLIENT_SECRET = "clientSecret"; + public static final String AUTHENTICATION_GRANT_TYPE = "grantType"; private DataSourceConstants() { } diff --git a/src/main/java/com/autotune/utils/TrialHelpers.java b/src/main/java/com/autotune/utils/TrialHelpers.java index 78cb6b430..cbe67505d 100644 --- a/src/main/java/com/autotune/utils/TrialHelpers.java +++ b/src/main/java/com/autotune/utils/TrialHelpers.java @@ -29,6 +29,7 @@ import com.autotune.common.data.metrics.Metric; import com.autotune.common.data.metrics.MetricResults; import com.autotune.common.datasource.DataSourceInfo; +import com.autotune.common.datasource.auth.AuthenticationConfig; import com.autotune.common.trials.*; import com.autotune.experimentManager.exceptions.IncompatibleInputJSONException; import com.autotune.operator.KruizeDeploymentInfo; @@ -145,7 +146,10 @@ public static ExperimentTrial createDefaultExperimentTrial(int trialNumber, trialNumber, trialResultUrl.toString()); - DataSourceInfo datasourceInfo = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, KruizeConstants.SupportedDatasources.PROMETHEUS, null, null, new URL(KruizeDeploymentInfo.monitoring_agent_endpoint)); + AuthenticationConfig authenticationConfig = AuthenticationConfig.noAuth(); + DataSourceInfo datasourceInfo = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, + KruizeConstants.SupportedDatasources.PROMETHEUS, null, null, + new URL(KruizeDeploymentInfo.monitoring_agent_endpoint), authenticationConfig); HashMap datasourceInfoHashMap = new HashMap<>(); datasourceInfoHashMap.put(KruizeDeploymentInfo.monitoring_agent, datasourceInfo); //Change key value as per YAML input DeploymentTracking deploymentTracking = new DeploymentTracking(); From 9b81fd70bc6885570014fd40164d5915f64c65c9 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Thu, 12 Sep 2024 10:31:54 +0530 Subject: [PATCH 2/8] update DB,cloudwatch auth Signed-off-by: Saad Khan --- .../openshift/kruize-crc-openshift.yaml | 18 ++++++++++++++---- .../engine/RecommendationEngine.java | 6 +++--- .../auth/APIKeyAuthenticationStrategy.java | 2 +- .../auth/AuthenticationConfig.java | 2 +- .../auth/AuthenticationStrategy.java | 2 +- .../auth/AuthenticationStrategyFactory.java | 2 +- .../auth/BasicAuthenticationStrategy.java | 2 +- .../auth/BearerAuthenticationStrategy.java | 2 +- .../{datasource => }/auth/Credentials.java | 10 +++++++++- .../auth/OAuth2AuthenticationStrategy.java | 2 +- .../datasource/DataSourceCollection.java | 4 ++-- .../common/datasource/DataSourceInfo.java | 2 +- .../common/datasource/DataSourceOperator.java | 2 +- .../datasource/DataSourceOperatorImpl.java | 6 +++--- .../prometheus/PrometheusDataOperatorImpl.java | 6 +++--- .../autotune/database/helper/DBHelpers.java | 2 +- .../database/init/KruizeHibernateUtil.java | 15 +++++++++++++-- .../handler/MetricCollectionHandler.java | 2 +- .../operator/KruizeDeploymentInfo.java | 10 ++++++---- .../com/autotune/utils/CloudWatchAppender.java | 6 +++--- .../autotune/utils/GenericRestApiClient.java | 3 +-- .../com/autotune/utils/KruizeConstants.java | 4 ++-- .../java/com/autotune/utils/TrialHelpers.java | 2 +- 23 files changed, 71 insertions(+), 41 deletions(-) rename src/main/java/com/autotune/common/{datasource => }/auth/APIKeyAuthenticationStrategy.java (87%) rename src/main/java/com/autotune/common/{datasource => }/auth/AuthenticationConfig.java (97%) rename src/main/java/com/autotune/common/{datasource => }/auth/AuthenticationStrategy.java (63%) rename src/main/java/com/autotune/common/{datasource => }/auth/AuthenticationStrategyFactory.java (96%) rename src/main/java/com/autotune/common/{datasource => }/auth/BasicAuthenticationStrategy.java (91%) rename src/main/java/com/autotune/common/{datasource => }/auth/BearerAuthenticationStrategy.java (94%) rename src/main/java/com/autotune/common/{datasource => }/auth/Credentials.java (92%) rename src/main/java/com/autotune/common/{datasource => }/auth/OAuth2AuthenticationStrategy.java (98%) 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 1200e0091..c5c5583bc 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 @@ -75,10 +75,15 @@ data: "adminUsername": "admin", "hostname": "kruize-db-service", "name": "kruizeDB", - "password": "admin", "port": 5432, "sslMode": "require", - "username": "admin" + "authentication": { + "type": "basic", + "credentials": { + "username": "admin", + "password": "admin" + } + } } } kruizeconfigjson: | @@ -107,12 +112,17 @@ data: }, "logging" : { "cloudwatch": { - "accessKeyId": "", "logGroup": "kruize-logs", "logStream": "kruize-stream", "region": "", - "secretAccessKey": "", "logLevel": "INFO" + "authentication": { + "type": "apiKey", + "credentials": { + "accessKeyId": "", + "secretAccessKey": "" + } + } } }, "datasource": [ 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 b5ada63d0..3ee985c15 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -27,9 +27,9 @@ import com.autotune.common.data.result.ContainerData; import com.autotune.common.data.result.IntervalResults; import com.autotune.common.datasource.DataSourceInfo; -import com.autotune.common.datasource.auth.AuthenticationConfig; -import com.autotune.common.datasource.auth.AuthenticationStrategy; -import com.autotune.common.datasource.auth.AuthenticationStrategyFactory; +import com.autotune.common.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationStrategy; +import com.autotune.common.auth.AuthenticationStrategyFactory; import com.autotune.common.exceptions.DataSourceNotExist; import com.autotune.common.k8sObjects.K8sObject; import com.autotune.common.utils.CommonUtils; diff --git a/src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java b/src/main/java/com/autotune/common/auth/APIKeyAuthenticationStrategy.java similarity index 87% rename from src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java rename to src/main/java/com/autotune/common/auth/APIKeyAuthenticationStrategy.java index 71335919a..e65705c73 100644 --- a/src/main/java/com/autotune/common/datasource/auth/APIKeyAuthenticationStrategy.java +++ b/src/main/java/com/autotune/common/auth/APIKeyAuthenticationStrategy.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; public class APIKeyAuthenticationStrategy implements AuthenticationStrategy { private final String apiKey; diff --git a/src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java similarity index 97% rename from src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java rename to src/main/java/com/autotune/common/auth/AuthenticationConfig.java index fe54c8554..3eaee9bf3 100644 --- a/src/main/java/com/autotune/common/datasource/auth/AuthenticationConfig.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; import org.apache.http.client.methods.HttpRequestBase; diff --git a/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java b/src/main/java/com/autotune/common/auth/AuthenticationStrategy.java similarity index 63% rename from src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java rename to src/main/java/com/autotune/common/auth/AuthenticationStrategy.java index c1854a917..ea5b8d95e 100644 --- a/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategy.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationStrategy.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; public interface AuthenticationStrategy { String applyAuthentication(); diff --git a/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java b/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java similarity index 96% rename from src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java rename to src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java index 86deb2cb2..90caa0d13 100644 --- a/src/main/java/com/autotune/common/datasource/auth/AuthenticationStrategyFactory.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; public class AuthenticationStrategyFactory { diff --git a/src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java b/src/main/java/com/autotune/common/auth/BasicAuthenticationStrategy.java similarity index 91% rename from src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java rename to src/main/java/com/autotune/common/auth/BasicAuthenticationStrategy.java index 2aa024847..8ee6fbe72 100644 --- a/src/main/java/com/autotune/common/datasource/auth/BasicAuthenticationStrategy.java +++ b/src/main/java/com/autotune/common/auth/BasicAuthenticationStrategy.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; import java.util.Base64; diff --git a/src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java b/src/main/java/com/autotune/common/auth/BearerAuthenticationStrategy.java similarity index 94% rename from src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java rename to src/main/java/com/autotune/common/auth/BearerAuthenticationStrategy.java index 6be7ce5fe..28b52b46b 100644 --- a/src/main/java/com/autotune/common/datasource/auth/BearerAuthenticationStrategy.java +++ b/src/main/java/com/autotune/common/auth/BearerAuthenticationStrategy.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; import java.io.BufferedReader; import java.io.FileReader; diff --git a/src/main/java/com/autotune/common/datasource/auth/Credentials.java b/src/main/java/com/autotune/common/auth/Credentials.java similarity index 92% rename from src/main/java/com/autotune/common/datasource/auth/Credentials.java rename to src/main/java/com/autotune/common/auth/Credentials.java index aedb5963b..d0869afe9 100644 --- a/src/main/java/com/autotune/common/datasource/auth/Credentials.java +++ b/src/main/java/com/autotune/common/auth/Credentials.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; public class Credentials { private String grantType; // OAuth2 @@ -11,6 +11,14 @@ public class Credentials { private String apiKey; // API key private String headerName; // API key header name + public Credentials(String username, String password) { + this.username = username; + this.password = password; + } + + public Credentials() { + } + public String getUsername() { return username; } diff --git a/src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java b/src/main/java/com/autotune/common/auth/OAuth2AuthenticationStrategy.java similarity index 98% rename from src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java rename to src/main/java/com/autotune/common/auth/OAuth2AuthenticationStrategy.java index fbe03b289..2423a55c0 100644 --- a/src/main/java/com/autotune/common/datasource/auth/OAuth2AuthenticationStrategy.java +++ b/src/main/java/com/autotune/common/auth/OAuth2AuthenticationStrategy.java @@ -1,4 +1,4 @@ -package com.autotune.common.datasource.auth; +package com.autotune.common.auth; import java.net.URI; import java.net.http.HttpClient; diff --git a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java index 13f04796c..9285ea60d 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java @@ -15,8 +15,8 @@ *******************************************************************************/ package com.autotune.common.datasource; -import com.autotune.common.datasource.auth.AuthenticationConfig; -import com.autotune.common.datasource.auth.Credentials; +import com.autotune.common.auth.AuthenticationConfig; +import com.autotune.common.auth.Credentials; import com.autotune.common.exceptions.datasource.*; import com.autotune.common.data.ValidationOutputData; import com.autotune.common.utils.CommonUtils; diff --git a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java index 2d6f3941e..9affd8fc9 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java @@ -19,7 +19,7 @@ import java.net.URL; import com.autotune.analyzer.utils.AnalyzerConstants; -import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationConfig; import com.autotune.operator.KruizeDeploymentInfo; import com.autotune.utils.KruizeConstants; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperator.java b/src/main/java/com/autotune/common/datasource/DataSourceOperator.java index cc076248f..2f5d6d345 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperator.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperator.java @@ -15,7 +15,7 @@ *******************************************************************************/ package com.autotune.common.datasource; -import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.utils.CommonUtils; import com.google.gson.JsonArray; import org.json.JSONObject; diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java index 99db90fd9..4ce7703c0 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java @@ -3,9 +3,9 @@ import com.autotune.analyzer.exceptions.MonitoringAgentNotFoundException; import com.autotune.analyzer.exceptions.TooManyRecursiveCallsException; import com.autotune.analyzer.utils.AnalyzerConstants; -import com.autotune.common.datasource.auth.AuthenticationConfig; -import com.autotune.common.datasource.auth.AuthenticationStrategy; -import com.autotune.common.datasource.auth.AuthenticationStrategyFactory; +import com.autotune.common.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationStrategy; +import com.autotune.common.auth.AuthenticationStrategyFactory; import com.autotune.common.datasource.prometheus.PrometheusDataOperatorImpl; import com.autotune.common.exceptions.datasource.ServiceNotFound; import com.autotune.common.target.kubernetes.service.KubernetesServices; 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 782486c5c..d119140de 100644 --- a/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java @@ -17,9 +17,9 @@ import com.autotune.analyzer.utils.AnalyzerConstants; import com.autotune.common.datasource.DataSourceOperatorImpl; -import com.autotune.common.datasource.auth.AuthenticationConfig; -import com.autotune.common.datasource.auth.AuthenticationStrategy; -import com.autotune.common.datasource.auth.AuthenticationStrategyFactory; +import com.autotune.common.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationStrategy; +import com.autotune.common.auth.AuthenticationStrategyFactory; import com.autotune.common.utils.CommonUtils; import com.autotune.utils.KruizeConstants; import com.autotune.utils.GenericRestApiClient; diff --git a/src/main/java/com/autotune/database/helper/DBHelpers.java b/src/main/java/com/autotune/database/helper/DBHelpers.java index 63b382484..b0332cf74 100644 --- a/src/main/java/com/autotune/database/helper/DBHelpers.java +++ b/src/main/java/com/autotune/database/helper/DBHelpers.java @@ -32,7 +32,7 @@ import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceMetadataOperator; -import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.k8sObjects.K8sObject; import com.autotune.database.table.*; import com.autotune.utils.KruizeConstants; diff --git a/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java b/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java index e518bde28..426182480 100644 --- a/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java +++ b/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java @@ -16,6 +16,8 @@ package com.autotune.database.init; +import com.autotune.common.auth.AuthenticationConfig; +import com.autotune.common.auth.Credentials; import com.autotune.database.table.*; import com.autotune.operator.KruizeDeploymentInfo; import org.hibernate.Session; @@ -35,14 +37,23 @@ public class KruizeHibernateUtil { public static void buildSessionFactory() { SessionFactory sfTemp = null; try { + // Initialize AuthenticationConfig for the database (for example, with "basic" auth) + AuthenticationConfig dbAuthConfig = new AuthenticationConfig( + KruizeDeploymentInfo.database_authentication_type, + new Credentials(KruizeDeploymentInfo.database_authentication_credentials_username, KruizeDeploymentInfo.database_authentication_credentials_password) + ); + // Fetch the credentials from the AuthenticationConfig + Credentials credentials = dbAuthConfig.getCredentials(); Configuration configuration = new Configuration(); + String dbUsername = credentials.getUsername(); + String dbPassword = credentials.getPassword(); String connectionURL = KruizeDeploymentInfo.settings_db_driver + KruizeDeploymentInfo.database_hostname + ":" + Integer.parseInt(KruizeDeploymentInfo.database_port) + "/" + KruizeDeploymentInfo.database_dbname; configuration.setProperty("hibernate.connection.url", connectionURL); - configuration.setProperty("hibernate.connection.username", KruizeDeploymentInfo.database_username); - configuration.setProperty("hibernate.connection.password", KruizeDeploymentInfo.database_password); + configuration.setProperty("hibernate.connection.username", dbUsername); + configuration.setProperty("hibernate.connection.password", dbPassword); configuration.setProperty("hibernate.dialect", KruizeDeploymentInfo.settings_hibernate_dialect); configuration.setProperty("hibernate.connection.driver_class", KruizeDeploymentInfo.settings_hibernate_connection_driver_class); configuration.setProperty("hibernate.c3p0.min_size", KruizeDeploymentInfo.settings_hibernate_c3p0_min_size); diff --git a/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java b/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java index 67c697793..fac4e9726 100644 --- a/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java +++ b/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java @@ -37,7 +37,7 @@ import com.autotune.experimentManager.utils.EMUtil; import com.autotune.operator.KruizeDeploymentInfo; import com.autotune.utils.KruizeConstants; -import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationConfig; import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java b/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java index 4be00ff62..e42807376 100644 --- a/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java +++ b/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java @@ -38,8 +38,9 @@ */ public class KruizeDeploymentInfo { private static final Logger LOGGER = LoggerFactory.getLogger(KruizeDeploymentInfo.class); - public static String database_username; - public static String database_password; + public static String database_authentication_type; + public static String database_authentication_credentials_username; + public static String database_authentication_credentials_password; public static String database_hostname; public static String database_dbname; public static String database_port; @@ -67,12 +68,13 @@ public class KruizeDeploymentInfo { public static String database_admin_password; public static String database_ssl_mode; - public static String cloudwatch_logs_access_key_id; - public static String cloudwatch_logs_secret_access_key; + public static String cloudwatch_logs_authentication_credentials_access_key_id; + public static String cloudwatch_logs_authentication_credentials_secret_access_key; public static String cloudwatch_logs_log_group; public static String cloudwatch_logs_region; public static String cloudwatch_logs_log_level; public static String cloudwatch_logs_log_stream; + public static String cloudwatch_logs_authentication_type; public static Boolean settings_save_to_db; public static String em_only_mode; diff --git a/src/main/java/com/autotune/utils/CloudWatchAppender.java b/src/main/java/com/autotune/utils/CloudWatchAppender.java index 6ecc184c8..4e0010a71 100644 --- a/src/main/java/com/autotune/utils/CloudWatchAppender.java +++ b/src/main/java/com/autotune/utils/CloudWatchAppender.java @@ -73,7 +73,7 @@ public void append(LogEvent event) { } public static void configureLoggerForCloudWatchLog() { - if (cloudwatch_logs_access_key_id != null && !cloudwatch_logs_access_key_id.isEmpty() && cloudwatch_logs_secret_access_key != null && !cloudwatch_logs_secret_access_key.isEmpty() && cloudwatch_logs_region != null && !cloudwatch_logs_region.isEmpty()) { + if (cloudwatch_logs_authentication_credentials_access_key_id != null && !cloudwatch_logs_authentication_credentials_access_key_id.isEmpty() && cloudwatch_logs_authentication_credentials_secret_access_key != null && !cloudwatch_logs_authentication_credentials_secret_access_key.isEmpty() && cloudwatch_logs_region != null && !cloudwatch_logs_region.isEmpty()) { try { // Define default values for attributes if they are empty or null String cw_logs_log_group = cloudwatch_logs_log_group == null || cloudwatch_logs_log_group.isEmpty() ? "kruize-logs" : cloudwatch_logs_log_group; @@ -84,7 +84,7 @@ public static void configureLoggerForCloudWatchLog() { CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() .region(Region.of(cloudwatch_logs_region)) - .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(cloudwatch_logs_access_key_id, cloudwatch_logs_secret_access_key))) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(cloudwatch_logs_authentication_credentials_access_key_id, cloudwatch_logs_authentication_credentials_secret_access_key))) .build(); if (!logGroupExists(logsClient, cw_logs_log_group)) { @@ -101,7 +101,7 @@ public static void configureLoggerForCloudWatchLog() { Level level = Level.getLevel(cw_logs_log_level_uc); Filter filter = new LogFilter(level); Layout layout = PatternLayout.newBuilder().withPattern(KruizeConstants.Patterns.CLOUDWATCH_LOG_PATTERN).build(); - CloudWatchAppender appender = new CloudWatchAppender("cloudwatchRootAppender", filter, layout, cw_logs_log_group, cw_logs_log_stream, cloudwatch_logs_region,cloudwatch_logs_access_key_id,cloudwatch_logs_secret_access_key); + CloudWatchAppender appender = new CloudWatchAppender("cloudwatchRootAppender", filter, layout, cw_logs_log_group, cw_logs_log_stream, cloudwatch_logs_region, cloudwatch_logs_authentication_credentials_access_key_id, cloudwatch_logs_authentication_credentials_secret_access_key); appender.start(); config.addAppender(appender); diff --git a/src/main/java/com/autotune/utils/GenericRestApiClient.java b/src/main/java/com/autotune/utils/GenericRestApiClient.java index 115549679..a23ea6ef9 100644 --- a/src/main/java/com/autotune/utils/GenericRestApiClient.java +++ b/src/main/java/com/autotune/utils/GenericRestApiClient.java @@ -15,8 +15,7 @@ *******************************************************************************/ package com.autotune.utils; -import com.autotune.common.datasource.auth.AuthenticationConfig; -import com.autotune.common.datasource.auth.AuthenticationStrategy; +import com.autotune.common.auth.AuthenticationStrategy; import com.autotune.utils.authModels.APIKeysAuthentication; import com.autotune.utils.authModels.BasicAuthentication; import com.autotune.utils.authModels.BearerAccessToken; diff --git a/src/main/java/com/autotune/utils/KruizeConstants.java b/src/main/java/com/autotune/utils/KruizeConstants.java index cc97ddd0d..853f5ecf4 100644 --- a/src/main/java/com/autotune/utils/KruizeConstants.java +++ b/src/main/java/com/autotune/utils/KruizeConstants.java @@ -621,8 +621,8 @@ private DateFormats() { public static final class DATABASE_ENV_NAME { public static final String DATABASE_ADMIN_USERNAME = "database_adminusername"; public static final String DATABASE_ADMIN_PASSWORD = "database_adminpassword"; - public static final String DATABASE_USERNAME = "database_username"; - public static final String DATABASE_PASSWORD = "database_password"; + public static final String DATABASE_AUTHENTICATION_CREDENTIALS_USERNAME = "database_authentication_credentials_username"; + public static final String DATABASE_AUTHENTICATION_CREDENTIALS_PASSWORD = "database_authentication_credentials_password"; public static final String DATABASE_HOSTNAME = "database_hostname"; public static final String DATABASE_DBNAME = "database_name"; public static final String DATABASE_PORT = "database_port"; diff --git a/src/main/java/com/autotune/utils/TrialHelpers.java b/src/main/java/com/autotune/utils/TrialHelpers.java index cbe67505d..60d4ad72c 100644 --- a/src/main/java/com/autotune/utils/TrialHelpers.java +++ b/src/main/java/com/autotune/utils/TrialHelpers.java @@ -29,7 +29,7 @@ import com.autotune.common.data.metrics.Metric; import com.autotune.common.data.metrics.MetricResults; import com.autotune.common.datasource.DataSourceInfo; -import com.autotune.common.datasource.auth.AuthenticationConfig; +import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.trials.*; import com.autotune.experimentManager.exceptions.IncompatibleInputJSONException; import com.autotune.operator.KruizeDeploymentInfo; From e4b2e4f3dd46428005bbb674b1b533d48f4360ae Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Thu, 12 Sep 2024 21:19:52 +0530 Subject: [PATCH 3/8] temporarily stashed changes related to common auth, changes to methods passing the datasource URL and other review updates Signed-off-by: Saad Khan --- .../openshift/kruize-crc-openshift.yaml | 20 ++----- .../engine/RecommendationEngine.java | 5 +- .../common/auth/AuthenticationConfig.java | 60 ++++++++++++------- .../auth/AuthenticationStrategyFactory.java | 12 ++-- .../datasource/DataSourceCollection.java | 42 ++----------- .../DataSourceMetadataOperator.java | 11 ++-- .../common/datasource/DataSourceOperator.java | 28 ++++----- .../datasource/DataSourceOperatorImpl.java | 30 +++++----- .../PrometheusDataOperatorImpl.java | 41 +++++++------ .../autotune/common/utils/CommonUtils.java | 7 ++- .../database/init/KruizeHibernateUtil.java | 15 +---- .../handler/MetricCollectionHandler.java | 9 +-- .../operator/KruizeDeploymentInfo.java | 10 ++-- .../autotune/utils/CloudWatchAppender.java | 6 +- .../autotune/utils/GenericRestApiClient.java | 2 +- .../java/com/autotune/utils/HttpUtils.java | 2 +- .../com/autotune/utils/KruizeConstants.java | 38 +++++++----- 17 files changed, 148 insertions(+), 190 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 c5c5583bc..efb882640 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 @@ -75,15 +75,10 @@ data: "adminUsername": "admin", "hostname": "kruize-db-service", "name": "kruizeDB", + "password": "admin", "port": 5432, "sslMode": "require", - "authentication": { - "type": "basic", - "credentials": { - "username": "admin", - "password": "admin" - } - } + "username": "admin" } } kruizeconfigjson: | @@ -97,7 +92,7 @@ data: "savetodb": "true", "dbdriver": "jdbc:postgresql://", "plots": "true", - "local": "true", + "local": "false", "logAllHttpReqAndResp": "true", "hibernate": { "dialect": "org.hibernate.dialect.PostgreSQLDialect", @@ -112,17 +107,12 @@ data: }, "logging" : { "cloudwatch": { + "accessKeyId": "", "logGroup": "kruize-logs", "logStream": "kruize-stream", "region": "", + "secretAccessKey": "", "logLevel": "INFO" - "authentication": { - "type": "apiKey", - "credentials": { - "accessKeyId": "", - "secretAccessKey": "" - } - } } }, "datasource": [ 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 3ee985c15..bde6222cd 100644 --- a/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java +++ b/src/main/java/com/autotune/analyzer/recommendations/engine/RecommendationEngine.java @@ -1621,12 +1621,11 @@ 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); - AuthenticationConfig authenticationConfig = dataSourceInfo.getAuthenticationConfig(); - AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(authenticationConfig); + AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( + dataSourceInfo.getAuthenticationConfig()); // Create the client GenericRestApiClient client = new GenericRestApiClient(authenticationStrategy); - String metricProfileName = kruizeObject.getPerformanceProfile(); PerformanceProfile metricProfile = MetricProfileCollection.getInstance().getMetricProfileCollection().get(metricProfileName); if (null == metricProfile) { diff --git a/src/main/java/com/autotune/common/auth/AuthenticationConfig.java b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java index 3eaee9bf3..ec84e5722 100644 --- a/src/main/java/com/autotune/common/auth/AuthenticationConfig.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java @@ -1,12 +1,15 @@ package com.autotune.common.auth; -import org.apache.http.client.methods.HttpRequestBase; - -import java.util.Base64; +import com.autotune.analyzer.utils.AnalyzerConstants; +import com.autotune.utils.KruizeConstants; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class AuthenticationConfig { private final String type; // "basic", "bearer", "apiKey", "oauth2" private final Credentials credentials; + private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationConfig.class); public AuthenticationConfig(String type, Credentials credentials) { this.type = type; @@ -21,30 +24,43 @@ public Credentials getCredentials() { return credentials; } - public void applyAuthentication(HttpRequestBase httpRequestBase) { - switch (type) { - case "Basic": - String basicAuth = Base64.getEncoder().encodeToString((credentials.getUsername() + ":" + credentials.getPassword()).getBytes()); - httpRequestBase.setHeader("Authorization", "Basic " + basicAuth); - break; - case "Bearer": - httpRequestBase.setHeader("Authorization", "Bearer " + credentials.getTokenFilePath()); - break; - case "APIKey": - httpRequestBase.setHeader("Authorization", "ApiKey " + credentials.getApiKey()); - break; - case "OAuth2": - // Assume the token is already retrieved and set - httpRequestBase.setHeader("Authorization", "Bearer " + credentials.getTokenFilePath()); - break; - default: - throw new IllegalArgumentException("Unsupported authentication type: " + type); + public static AuthenticationConfig createAuthenticationConfigObject(JSONObject authenticationObj) { + // Parse and map authentication methods if they exist + if (authenticationObj != null) { + String type = authenticationObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_TYPE); + JSONObject credentialsObj = authenticationObj.getJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION_CREDENTIALS); + + Credentials credentials = new Credentials(); + switch (type.toLowerCase()) { + case KruizeConstants.AuthenticationConstants.BASIC: + credentials.setUsername(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_USERNAME)); + credentials.setPassword(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_PASSWORD)); + break; + case KruizeConstants.AuthenticationConstants.BEARER: + credentials.setTokenFilePath(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_TOKEN_FILE)); + break; + case KruizeConstants.AuthenticationConstants.API_KEY: + credentials.setApiKey(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_API_KEY)); + credentials.setHeaderName(credentialsObj.optString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_HEADER_NAME, "X-API-Key")); + break; + case KruizeConstants.AuthenticationConstants.OAUTH2: + credentials.setTokenEndpoint(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_TOKEN_ENDPOINT)); + credentials.setClientId(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_CLIENT_ID)); + credentials.setClientSecret(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_CLIENT_SECRET)); + credentials.setGrantType(credentialsObj.getString(KruizeConstants.AuthenticationConstants.AUTHENTICATION_GRANT_TYPE)); + break; + default: + LOGGER.error(KruizeConstants.AuthenticationConstants.UNKNOWN_AUTHENTICATION + "{}", type); + } + + return new AuthenticationConfig(type, credentials); } + return noAuth(); } // Static method to return a no-auth config public static AuthenticationConfig noAuth() { - return new AuthenticationConfig("none", null); // Type "none" or similar to indicate no auth + return new AuthenticationConfig(AnalyzerConstants.NONE, null); } @Override diff --git a/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java b/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java index 90caa0d13..9022aa50f 100644 --- a/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationStrategyFactory.java @@ -1,27 +1,29 @@ package com.autotune.common.auth; +import com.autotune.utils.KruizeConstants; + public class AuthenticationStrategyFactory { public static AuthenticationStrategy createAuthenticationStrategy(AuthenticationConfig authConfig) { String type = authConfig.getType(); switch (type) { - case "basic": + case KruizeConstants.AuthenticationConstants.BASIC: String username = authConfig.getCredentials().getUsername(); String password = authConfig.getCredentials().getPassword(); return new BasicAuthenticationStrategy(username, password); - case "bearer": + case KruizeConstants.AuthenticationConstants.BEARER: String tokenFilePath = authConfig.getCredentials().getTokenFilePath(); return new BearerAuthenticationStrategy(tokenFilePath); - case "apiKey": + case KruizeConstants.AuthenticationConstants.API_KEY: String apiKey = authConfig.getCredentials().getApiKey(); return new APIKeyAuthenticationStrategy(apiKey); - case "oauth2": + case KruizeConstants.AuthenticationConstants.OAUTH2: String tokenEndpoint = authConfig.getCredentials().getTokenEndpoint(); String clientId = authConfig.getCredentials().getClientId(); String clientSecret = authConfig.getCredentials().getClientSecret(); return new OAuth2AuthenticationStrategy(tokenEndpoint, clientId, clientSecret); default: - throw new IllegalArgumentException("Unknown authentication type: " + type); + throw new IllegalArgumentException(KruizeConstants.AuthenticationConstants.UNKNOWN_AUTHENTICATION+ type); } } } diff --git a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java index 9285ea60d..88e5154f1 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java @@ -16,7 +16,6 @@ package com.autotune.common.datasource; import com.autotune.common.auth.AuthenticationConfig; -import com.autotune.common.auth.Credentials; import com.autotune.common.exceptions.datasource.*; import com.autotune.common.data.ValidationOutputData; import com.autotune.common.utils.CommonUtils; @@ -85,8 +84,6 @@ public HashMap getDataSourcesCollection() { public void addDataSource(DataSourceInfo datasource) { final String name = datasource.getName(); final String provider = datasource.getProvider(); - final String url = datasource.getUrl().toString(); - AuthenticationConfig authenticationConfig = datasource.getAuthenticationConfig(); ValidationOutputData addedToDB = null; LOGGER.info(KruizeConstants.DataSourceConstants.DataSourceInfoMsgs.ADDING_DATASOURCE + name); @@ -99,7 +96,7 @@ public void addDataSource(DataSourceInfo datasource) { if (provider.equalsIgnoreCase(KruizeConstants.SupportedDatasources.PROMETHEUS)) { LOGGER.info(KruizeConstants.DataSourceConstants.DataSourceInfoMsgs.VERIFYING_DATASOURCE_REACHABILITY + name); DataSourceOperatorImpl op = DataSourceOperatorImpl.getInstance().getOperator(KruizeConstants.SupportedDatasources.PROMETHEUS); - if (op.isServiceable(url, authenticationConfig) == CommonUtils.DatasourceReachabilityStatus.REACHABLE) { + if (op.isServiceable(datasource) == CommonUtils.DatasourceReachabilityStatus.REACHABLE) { LOGGER.info(KruizeConstants.DataSourceConstants.DataSourceSuccessMsgs.DATASOURCE_SERVICEABLE); // add the data source to DB addedToDB = new ExperimentDBService().addDataSourceToDB(datasource); @@ -156,42 +153,11 @@ 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.DataSourceConstants.DATASOURCE_AUTHENTICATION); + JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION); DataSourceInfo dataSourceInfo; - AuthenticationConfig authConfig = null; - - // Parse and map authentication methods if they exist - if (authenticationObj != null) { - String type = authenticationObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_TYPE); - JSONObject credentialsObj = authenticationObj.getJSONObject(KruizeConstants.DataSourceConstants.AUTHENTICATION_CREDENTIALS); - - Credentials credentials = new Credentials(); - switch (type.toLowerCase()) { - case "basic": - credentials.setUsername(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_USERNAME)); - credentials.setPassword(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_PASSWORD)); - break; - case "bearer": - credentials.setTokenFilePath(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_TOKEN_FILE)); - break; - case "apikey": - credentials.setApiKey(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_API_KEY)); - credentials.setHeaderName(credentialsObj.optString(KruizeConstants.DataSourceConstants.AUTHENTICATION_HEADER_NAME, "X-API-Key")); - break; - case "oauth2": - credentials.setTokenEndpoint(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_TOKEN_ENDPOINT)); - credentials.setClientId(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_CLIENT_ID)); - credentials.setClientSecret(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_CLIENT_SECRET)); - credentials.setGrantType(credentialsObj.getString(KruizeConstants.DataSourceConstants.AUTHENTICATION_GRANT_TYPE)); - break; - default: - LOGGER.error("Unsupported authentication type: {}", type); - continue; - } - - authConfig = new AuthenticationConfig(type, credentials); - } + // create the corresponding authentication object + AuthenticationConfig authConfig = AuthenticationConfig.createAuthenticationConfigObject(authenticationObj); // Validate input if (!validateInput(name, provider, serviceName, dataSourceURL, namespace)) { diff --git a/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java b/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java index aeafa848a..d1079564b 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceMetadataOperator.java @@ -129,8 +129,7 @@ public DataSourceMetadataInfo processQueriesAndPopulateDataSourceMetadataInfo(Da */ try { String dataSourceName = dataSourceInfo.getName(); - String dataSourceUrl = dataSourceInfo.getUrl().toString(); - JsonArray namespacesDataResultArray = op.getResultArrayForQuery(dataSourceUrl, PromQLDataSourceQueries.NAMESPACE_QUERY, dataSourceInfo.getAuthenticationConfig()); + JsonArray namespacesDataResultArray = op.getResultArrayForQuery(dataSourceInfo, PromQLDataSourceQueries.NAMESPACE_QUERY); if (false == op.validateResultArray(namespacesDataResultArray)){ dataSourceMetadataInfo = dataSourceDetailsHelper.createDataSourceMetadataInfoObject(dataSourceName, null); throw new Exception(KruizeConstants.DataSourceConstants.DataSourceMetadataErrorMsgs.NAMESPACE_QUERY_VALIDATION_FAILED); @@ -153,8 +152,8 @@ public DataSourceMetadataInfo processQueriesAndPopulateDataSourceMetadataInfo(Da * TODO - get workload metadata for a given namespace */ HashMap> datasourceWorkloads = new HashMap<>(); - JsonArray workloadDataResultArray = op.getResultArrayForQuery(dataSourceUrl, - PromQLDataSourceQueries.WORKLOAD_QUERY, dataSourceInfo.getAuthenticationConfig()); + JsonArray workloadDataResultArray = op.getResultArrayForQuery(dataSourceInfo, + PromQLDataSourceQueries.WORKLOAD_QUERY); if (op.validateResultArray(workloadDataResultArray)) { datasourceWorkloads = dataSourceDetailsHelper.getWorkloadInfo(workloadDataResultArray); @@ -172,8 +171,8 @@ public DataSourceMetadataInfo processQueriesAndPopulateDataSourceMetadataInfo(Da * TODO - get container metadata for a given workload */ HashMap> datasourceContainers = new HashMap<>(); - JsonArray containerDataResultArray = op.getResultArrayForQuery(dataSourceUrl, - PromQLDataSourceQueries.CONTAINER_QUERY, dataSourceInfo.getAuthenticationConfig()); + JsonArray containerDataResultArray = op.getResultArrayForQuery(dataSourceInfo, + PromQLDataSourceQueries.CONTAINER_QUERY); if (op.validateResultArray(containerDataResultArray)) { datasourceContainers = dataSourceDetailsHelper.getContainerInfo(containerDataResultArray); diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperator.java b/src/main/java/com/autotune/common/datasource/DataSourceOperator.java index 2f5d6d345..f4fa77f86 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperator.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperator.java @@ -15,7 +15,6 @@ *******************************************************************************/ package com.autotune.common.datasource; -import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.utils.CommonUtils; import com.google.gson.JsonArray; import org.json.JSONObject; @@ -49,40 +48,37 @@ public interface DataSourceOperator { * Check if a datasource is reachable, implementation of this function * should check and return the reachability status (REACHABLE, NOT_REACHABLE) * - * @param dataSourceUrl String containing the url for the datasource - * @param authenticationConfig + * @param dataSource DatasourceInfo object containing the datasource details * @return DatasourceReachabilityStatus */ - CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceUrl, AuthenticationConfig authenticationConfig); + CommonUtils.DatasourceReachabilityStatus isServiceable(DataSourceInfo dataSource); /** * executes specified query on datasource and returns the result value * - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return Object containing the result value for the specified query */ - Object getValueForQuery(String url, String query, AuthenticationConfig authenticationConfig); + Object getValueForQuery(DataSourceInfo dataSource, String query); /** * executes specified query on datasource and returns the JSON Object * - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return JSONObject for the specified query */ - JSONObject getJsonObjectForQuery(String url, String query, AuthenticationConfig authenticationConfig); + JSONObject getJsonObjectForQuery(DataSourceInfo dataSource, String query); /** * executes specified query on datasource and returns the result array - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig contains Authentication Config + * + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return JsonArray containing the result array for the specified query */ - public JsonArray getResultArrayForQuery(String url, String query, AuthenticationConfig authenticationConfig); + public JsonArray getResultArrayForQuery(DataSourceInfo dataSource, String query); /** * Validates a JSON array to ensure it is not null, not a JSON null, and has at least one element. diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java index 4ce7703c0..39072cd42 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java @@ -74,25 +74,23 @@ public String getDefaultServicePortForProvider() { * Check if a datasource is reachable, implementation of this function * should check and return the reachability status (REACHABLE, NOT_REACHABLE) * - * @param dataSourceUrl String containing the url for the datasource - * @param authenticationConfig + * @param dataSource DatasourceInfo object containing the datasource details * @return DatasourceReachabilityStatus */ @Override - public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceUrl, AuthenticationConfig authenticationConfig) { + public CommonUtils.DatasourceReachabilityStatus isServiceable(DataSourceInfo dataSource) { return null; } /** * executes specified query on datasource and returns the result value * - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return Object containing the result value for the specified query */ @Override - public Object getValueForQuery(String url, String query, AuthenticationConfig authenticationConfig) { + public Object getValueForQuery(DataSourceInfo dataSource, String query) { return null; } @@ -107,24 +105,24 @@ public String getQueryEndpoint() { /** * executes specified query on datasource and returns the JSON Object * - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return JSONObject for the specified query */ @Override - public JSONObject getJsonObjectForQuery(String url, String query, AuthenticationConfig authenticationConfig) { + public JSONObject getJsonObjectForQuery(DataSourceInfo dataSource, String query) { return null; } /** * executes specified query on datasource and returns the result array - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return JsonArray containing the result array for the specified query */ @Override - public JsonArray getResultArrayForQuery(String url, String query, AuthenticationConfig authenticationConfig) { + public JsonArray getResultArrayForQuery(DataSourceInfo dataSource, String query) { return null; } @@ -167,8 +165,8 @@ public ArrayList getAppsForLayer(DataSourceInfo dataSource, String query String queryURL = dataSourceURL + queryEndpoint + query; LOGGER.debug("Query URL is: {}", queryURL); try { - AuthenticationConfig authenticationConfig = dataSource.getAuthenticationConfig(); - AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(authenticationConfig); + AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( + dataSource.getAuthenticationConfig()); // Create the client GenericRestApiClient genericRestApiClient = new GenericRestApiClient(authenticationStrategy); genericRestApiClient.setBaseURL(dataSourceURL + queryEndpoint); 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 d119140de..c2b8a112e 100644 --- a/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/prometheus/PrometheusDataOperatorImpl.java @@ -16,10 +16,10 @@ package com.autotune.common.datasource.prometheus; import com.autotune.analyzer.utils.AnalyzerConstants; -import com.autotune.common.datasource.DataSourceOperatorImpl; -import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.auth.AuthenticationStrategy; import com.autotune.common.auth.AuthenticationStrategyFactory; +import com.autotune.common.datasource.DataSourceInfo; +import com.autotune.common.datasource.DataSourceOperatorImpl; import com.autotune.common.utils.CommonUtils; import com.autotune.utils.KruizeConstants; import com.autotune.utils.GenericRestApiClient; @@ -71,19 +71,18 @@ public String getDefaultServicePortForProvider() { * Check if a datasource is reachable, implementation of this function * should check and return the reachability status (REACHABLE, NOT_REACHABLE) * - * @param dataSourceURL String containing the url for the datasource - * @param authenticationConfig + * @param dataSource The DataSourceInfo object containing information about the data source * @return DatasourceReachabilityStatus */ @Override - public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceURL, AuthenticationConfig authenticationConfig) { + public CommonUtils.DatasourceReachabilityStatus isServiceable(DataSourceInfo dataSource) { String dataSourceStatus; Object queryResult; String query = KruizeConstants.DataSourceConstants.PROMETHEUS_REACHABILITY_QUERY; CommonUtils.DatasourceReachabilityStatus reachabilityStatus; - queryResult = this.getValueForQuery(dataSourceURL, query, authenticationConfig); + queryResult = this.getValueForQuery(dataSource, query); if (queryResult != null){ dataSourceStatus = queryResult.toString(); @@ -102,15 +101,14 @@ public CommonUtils.DatasourceReachabilityStatus isServiceable(String dataSourceU /** * executes specified query on datasource and returns the result value * - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig + * @param dataSource The DataSourceInfo object containing information about the data source + * @param query String containing the query to be executed * @return Object containing the result value for the specified query */ @Override - public Object getValueForQuery(String url, String query, AuthenticationConfig authenticationConfig) { + public Object getValueForQuery(DataSourceInfo dataSource, String query) { try { - JSONObject jsonObject = getJsonObjectForQuery(url, query, authenticationConfig); + JSONObject jsonObject = getJsonObjectForQuery(dataSource, query); if (null == jsonObject) { return null; @@ -135,18 +133,18 @@ public Object getValueForQuery(String url, String query, AuthenticationConfig au /** * executes specified query on datasource and returns the JSON Object * - * @param url String containing the url for the datasource - * @param query String containing the query to be executed - * @param authenticationConfig + * @param dataSource The DataSourceInfo object containing information about the data source + * @param query String containing the query to be executed * @return JSONObject for the specified query */ @Override - public JSONObject getJsonObjectForQuery(String url, String query, AuthenticationConfig authenticationConfig) { - AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(authenticationConfig); + public JSONObject getJsonObjectForQuery(DataSourceInfo dataSource, String query) { + AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy( + dataSource.getAuthenticationConfig()); // Create the client GenericRestApiClient apiClient = new GenericRestApiClient(authenticationStrategy); apiClient.setBaseURL(CommonUtils.getBaseDataSourceUrl( - url, + dataSource, KruizeConstants.SupportedDatasources.PROMETHEUS )); @@ -196,8 +194,9 @@ public String getQueryEndpoint() { /** * executes specified query on datasource and returns the result array - * @param url String containing the url for the datasource - * @param query String containing the query to be executed + * + * @param dataSource DatasourceInfo object containing the datasource details + * @param query String containing the query to be executed * @return JsonArray containing the result array for the specified query * * Example output JsonArray - @@ -212,9 +211,9 @@ public String getQueryEndpoint() { */ @Override - public JsonArray getResultArrayForQuery(String url, String query, AuthenticationConfig authenticationConfig) { + public JsonArray getResultArrayForQuery(DataSourceInfo dataSource, String query) { try { - JSONObject jsonObject = getJsonObjectForQuery(url, query, authenticationConfig); + JSONObject jsonObject = getJsonObjectForQuery(dataSource, query); if (null == jsonObject) { return null; diff --git a/src/main/java/com/autotune/common/utils/CommonUtils.java b/src/main/java/com/autotune/common/utils/CommonUtils.java index 58eba2810..1f0efa344 100644 --- a/src/main/java/com/autotune/common/utils/CommonUtils.java +++ b/src/main/java/com/autotune/common/utils/CommonUtils.java @@ -16,6 +16,7 @@ package com.autotune.common.utils; +import com.autotune.common.datasource.DataSourceInfo; import com.autotune.utils.KruizeConstants; import java.sql.Timestamp; @@ -247,14 +248,14 @@ public static boolean checkTimeMatch(String timeStrOne, String timeStrTwo) { /** * Get the base datasource URL for running query - * @param url + * @param dataSourceInfo * @param datasource * @return */ - public static String getBaseDataSourceUrl(String url, String datasource) { + public static String getBaseDataSourceUrl(DataSourceInfo dataSourceInfo, String datasource) { if (datasource.equalsIgnoreCase(KruizeConstants.SupportedDatasources.PROMETHEUS)) { return (new StringBuilder()) - .append(url) + .append(dataSourceInfo.getUrl().toString()) .append("/api/v1/query?query=") .toString(); } diff --git a/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java b/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java index 426182480..e518bde28 100644 --- a/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java +++ b/src/main/java/com/autotune/database/init/KruizeHibernateUtil.java @@ -16,8 +16,6 @@ package com.autotune.database.init; -import com.autotune.common.auth.AuthenticationConfig; -import com.autotune.common.auth.Credentials; import com.autotune.database.table.*; import com.autotune.operator.KruizeDeploymentInfo; import org.hibernate.Session; @@ -37,23 +35,14 @@ public class KruizeHibernateUtil { public static void buildSessionFactory() { SessionFactory sfTemp = null; try { - // Initialize AuthenticationConfig for the database (for example, with "basic" auth) - AuthenticationConfig dbAuthConfig = new AuthenticationConfig( - KruizeDeploymentInfo.database_authentication_type, - new Credentials(KruizeDeploymentInfo.database_authentication_credentials_username, KruizeDeploymentInfo.database_authentication_credentials_password) - ); - // Fetch the credentials from the AuthenticationConfig - Credentials credentials = dbAuthConfig.getCredentials(); Configuration configuration = new Configuration(); - String dbUsername = credentials.getUsername(); - String dbPassword = credentials.getPassword(); String connectionURL = KruizeDeploymentInfo.settings_db_driver + KruizeDeploymentInfo.database_hostname + ":" + Integer.parseInt(KruizeDeploymentInfo.database_port) + "/" + KruizeDeploymentInfo.database_dbname; configuration.setProperty("hibernate.connection.url", connectionURL); - configuration.setProperty("hibernate.connection.username", dbUsername); - configuration.setProperty("hibernate.connection.password", dbPassword); + configuration.setProperty("hibernate.connection.username", KruizeDeploymentInfo.database_username); + configuration.setProperty("hibernate.connection.password", KruizeDeploymentInfo.database_password); configuration.setProperty("hibernate.dialect", KruizeDeploymentInfo.settings_hibernate_dialect); configuration.setProperty("hibernate.connection.driver_class", KruizeDeploymentInfo.settings_hibernate_connection_driver_class); configuration.setProperty("hibernate.c3p0.min_size", KruizeDeploymentInfo.settings_hibernate_c3p0_min_size); diff --git a/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java b/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java index fac4e9726..0aa8c41d7 100644 --- a/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java +++ b/src/main/java/com/autotune/experimentManager/handler/MetricCollectionHandler.java @@ -37,7 +37,6 @@ import com.autotune.experimentManager.utils.EMUtil; import com.autotune.operator.KruizeDeploymentInfo; import com.autotune.utils.KruizeConstants; -import com.autotune.common.auth.AuthenticationConfig; import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -122,10 +121,8 @@ public void execute(ExperimentTrial experimentTrial, TrialDetails trialDetails, if (null == ado) { // TODO: Return an error saying unsupported datasource } - AuthenticationConfig noAuthConfig = AuthenticationConfig.noAuth(); String queryResult = (String) ado.getValueForQuery(experimentTrial.getDatasourceInfoHashMap() - .get(podMetric.getDatasource()) - .getUrl().toString(), updatedPodQuery, noAuthConfig); + .get(podMetric.getDatasource()), updatedPodQuery); if (null != queryResult && !queryResult.isEmpty() && !queryResult.isBlank()) { try { queryResult = queryResult.trim(); @@ -162,10 +159,8 @@ public void execute(ExperimentTrial experimentTrial, TrialDetails trialDetails, } if (null != updatedContainerQuery) { LOGGER.debug("Updated Query - " + updatedContainerQuery); - AuthenticationConfig noAuthConfig = AuthenticationConfig.noAuth(); String queryResult = (String) ado.getValueForQuery(experimentTrial.getDatasourceInfoHashMap() - .get(containerMetric.getDatasource()) - .getUrl().toString(), updatedContainerQuery, noAuthConfig); + .get(containerMetric.getDatasource()), updatedContainerQuery); if (null != queryResult && !queryResult.isEmpty() && !queryResult.isBlank()) { try { queryResult = queryResult.trim(); diff --git a/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java b/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java index e42807376..4be00ff62 100644 --- a/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java +++ b/src/main/java/com/autotune/operator/KruizeDeploymentInfo.java @@ -38,9 +38,8 @@ */ public class KruizeDeploymentInfo { private static final Logger LOGGER = LoggerFactory.getLogger(KruizeDeploymentInfo.class); - public static String database_authentication_type; - public static String database_authentication_credentials_username; - public static String database_authentication_credentials_password; + public static String database_username; + public static String database_password; public static String database_hostname; public static String database_dbname; public static String database_port; @@ -68,13 +67,12 @@ public class KruizeDeploymentInfo { public static String database_admin_password; public static String database_ssl_mode; - public static String cloudwatch_logs_authentication_credentials_access_key_id; - public static String cloudwatch_logs_authentication_credentials_secret_access_key; + public static String cloudwatch_logs_access_key_id; + public static String cloudwatch_logs_secret_access_key; public static String cloudwatch_logs_log_group; public static String cloudwatch_logs_region; public static String cloudwatch_logs_log_level; public static String cloudwatch_logs_log_stream; - public static String cloudwatch_logs_authentication_type; public static Boolean settings_save_to_db; public static String em_only_mode; diff --git a/src/main/java/com/autotune/utils/CloudWatchAppender.java b/src/main/java/com/autotune/utils/CloudWatchAppender.java index 4e0010a71..6ecc184c8 100644 --- a/src/main/java/com/autotune/utils/CloudWatchAppender.java +++ b/src/main/java/com/autotune/utils/CloudWatchAppender.java @@ -73,7 +73,7 @@ public void append(LogEvent event) { } public static void configureLoggerForCloudWatchLog() { - if (cloudwatch_logs_authentication_credentials_access_key_id != null && !cloudwatch_logs_authentication_credentials_access_key_id.isEmpty() && cloudwatch_logs_authentication_credentials_secret_access_key != null && !cloudwatch_logs_authentication_credentials_secret_access_key.isEmpty() && cloudwatch_logs_region != null && !cloudwatch_logs_region.isEmpty()) { + if (cloudwatch_logs_access_key_id != null && !cloudwatch_logs_access_key_id.isEmpty() && cloudwatch_logs_secret_access_key != null && !cloudwatch_logs_secret_access_key.isEmpty() && cloudwatch_logs_region != null && !cloudwatch_logs_region.isEmpty()) { try { // Define default values for attributes if they are empty or null String cw_logs_log_group = cloudwatch_logs_log_group == null || cloudwatch_logs_log_group.isEmpty() ? "kruize-logs" : cloudwatch_logs_log_group; @@ -84,7 +84,7 @@ public static void configureLoggerForCloudWatchLog() { CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() .region(Region.of(cloudwatch_logs_region)) - .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(cloudwatch_logs_authentication_credentials_access_key_id, cloudwatch_logs_authentication_credentials_secret_access_key))) + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(cloudwatch_logs_access_key_id, cloudwatch_logs_secret_access_key))) .build(); if (!logGroupExists(logsClient, cw_logs_log_group)) { @@ -101,7 +101,7 @@ public static void configureLoggerForCloudWatchLog() { Level level = Level.getLevel(cw_logs_log_level_uc); Filter filter = new LogFilter(level); Layout layout = PatternLayout.newBuilder().withPattern(KruizeConstants.Patterns.CLOUDWATCH_LOG_PATTERN).build(); - CloudWatchAppender appender = new CloudWatchAppender("cloudwatchRootAppender", filter, layout, cw_logs_log_group, cw_logs_log_stream, cloudwatch_logs_region, cloudwatch_logs_authentication_credentials_access_key_id, cloudwatch_logs_authentication_credentials_secret_access_key); + CloudWatchAppender appender = new CloudWatchAppender("cloudwatchRootAppender", filter, layout, cw_logs_log_group, cw_logs_log_stream, cloudwatch_logs_region,cloudwatch_logs_access_key_id,cloudwatch_logs_secret_access_key); appender.start(); config.addAppender(appender); diff --git a/src/main/java/com/autotune/utils/GenericRestApiClient.java b/src/main/java/com/autotune/utils/GenericRestApiClient.java index a23ea6ef9..30de160ec 100644 --- a/src/main/java/com/autotune/utils/GenericRestApiClient.java +++ b/src/main/java/com/autotune/utils/GenericRestApiClient.java @@ -87,7 +87,7 @@ public JSONObject fetchMetricsJson(String methodType, String queryString) throws // Apply authentication if (authenticationStrategy != null) { String authHeader = authenticationStrategy.applyAuthentication(); - httpRequestBase.setHeader("Authorization", authHeader); + httpRequestBase.setHeader(KruizeConstants.AuthenticationConstants.AUTHORIZATION, authHeader); } LOGGER.info("Executing request: {}", httpRequestBase.getRequestLine()); jsonOutputInString = httpclient.execute(httpRequestBase, new StringResponseHandler()); diff --git a/src/main/java/com/autotune/utils/HttpUtils.java b/src/main/java/com/autotune/utils/HttpUtils.java index e7129ddfc..16858ff2a 100644 --- a/src/main/java/com/autotune/utils/HttpUtils.java +++ b/src/main/java/com/autotune/utils/HttpUtils.java @@ -56,7 +56,7 @@ public static String getDataFromURL(URL url, String bearerToken) { connection = (HttpURLConnection) url.openConnection(); } - connection.setRequestProperty("Authorization", bearerToken); + connection.setRequestProperty(KruizeConstants.AuthenticationConstants.AUTHORIZATION, bearerToken); if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) { result = getDataFromConnection(connection); diff --git a/src/main/java/com/autotune/utils/KruizeConstants.java b/src/main/java/com/autotune/utils/KruizeConstants.java index 853f5ecf4..01d1fe557 100644 --- a/src/main/java/com/autotune/utils/KruizeConstants.java +++ b/src/main/java/com/autotune/utils/KruizeConstants.java @@ -396,18 +396,6 @@ public static class DataSourceConstants { public static final String PROMETHEUS_REACHABILITY_QUERY = "up"; public static final String DATASOURCE_ENDPOINT_WITH_QUERY = "%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s"; public static final String DATE_ENDPOINT_WITH_QUERY = "%s/api/v1/query?query=%s"; - public static final String DATASOURCE_AUTHENTICATION = "authentication"; - public static final String AUTHENTICATION_TYPE = "type"; - public static final String AUTHENTICATION_CREDENTIALS = "credentials"; - public static final String AUTHENTICATION_USERNAME = "username"; - public static final String AUTHENTICATION_PASSWORD = "password"; - public static final String AUTHENTICATION_TOKEN_FILE = "tokenFilePath"; - public static final String AUTHENTICATION_API_KEY = "apiKey"; - public static final String AUTHENTICATION_HEADER_NAME = "header"; - public static final String AUTHENTICATION_TOKEN_ENDPOINT = "tokenEndpoint"; - public static final String AUTHENTICATION_CLIENT_ID = "clientId"; - public static final String AUTHENTICATION_CLIENT_SECRET = "clientSecret"; - public static final String AUTHENTICATION_GRANT_TYPE = "grantType"; private DataSourceConstants() { } @@ -621,8 +609,8 @@ private DateFormats() { public static final class DATABASE_ENV_NAME { public static final String DATABASE_ADMIN_USERNAME = "database_adminusername"; public static final String DATABASE_ADMIN_PASSWORD = "database_adminpassword"; - public static final String DATABASE_AUTHENTICATION_CREDENTIALS_USERNAME = "database_authentication_credentials_username"; - public static final String DATABASE_AUTHENTICATION_CREDENTIALS_PASSWORD = "database_authentication_credentials_password"; + public static final String DATABASE_USERNAME = "database_username"; + public static final String DATABASE_PASSWORD = "database_password"; public static final String DATABASE_HOSTNAME = "database_hostname"; public static final String DATABASE_DBNAME = "database_name"; public static final String DATABASE_PORT = "database_port"; @@ -732,4 +720,26 @@ public static final class KRUIZE_RECOMMENDATION_METRICS { public static final String notification_format_for_METRICS = "%s|%s|%s"; //termname,modelname,type } + + public static final class AuthenticationConstants { + public static final String AUTHENTICATION = "authentication"; + public static final String AUTHENTICATION_TYPE = "type"; + public static final String AUTHENTICATION_CREDENTIALS = "credentials"; + public static final String AUTHENTICATION_USERNAME = "username"; + public static final String AUTHENTICATION_PASSWORD = "password"; + public static final String AUTHENTICATION_TOKEN_FILE = "tokenFilePath"; + public static final String AUTHENTICATION_API_KEY = "apiKey"; + public static final String AUTHENTICATION_HEADER_NAME = "header"; + public static final String AUTHENTICATION_TOKEN_ENDPOINT = "tokenEndpoint"; + 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 BASIC = "basic"; + public static final String BEARER = "bearer"; + public static final String API_KEY = "apikey"; + public static final String OAUTH2 = "oauth2"; + public static final String UNKNOWN_AUTHENTICATION = "Unknown authentication type: "; + public static final String AUTHORIZATION = "Authorization"; + + } } From 1e4e4c15cd189d04ed85b9587a254c76c63d52ab Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Thu, 12 Sep 2024 21:21:43 +0530 Subject: [PATCH 4/8] add missing EOF Signed-off-by: Saad Khan --- .../java/com/autotune/common/auth/AuthenticationConfig.java | 2 +- src/main/java/com/autotune/common/auth/Credentials.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/autotune/common/auth/AuthenticationConfig.java b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java index ec84e5722..650f2851c 100644 --- a/src/main/java/com/autotune/common/auth/AuthenticationConfig.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java @@ -70,4 +70,4 @@ public String toString() { ", credentials=" + credentials + '}'; } -} \ No newline at end of file +} diff --git a/src/main/java/com/autotune/common/auth/Credentials.java b/src/main/java/com/autotune/common/auth/Credentials.java index d0869afe9..4638eb088 100644 --- a/src/main/java/com/autotune/common/auth/Credentials.java +++ b/src/main/java/com/autotune/common/auth/Credentials.java @@ -105,4 +105,4 @@ public String toString() { ", headerName='" + headerName + '\'' + '}'; } -} \ No newline at end of file +} From 25b30f955635e474e528cf18d7554829cdf4bcc0 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Thu, 12 Sep 2024 22:40:40 +0530 Subject: [PATCH 5/8] fix issue occurring while converting authJson to authConfig object Signed-off-by: Saad Khan --- .../com/autotune/common/auth/AuthenticationConfig.java | 7 +++++-- src/main/java/com/autotune/database/helper/DBHelpers.java | 5 +---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/autotune/common/auth/AuthenticationConfig.java b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java index 650f2851c..6eebdf2a5 100644 --- a/src/main/java/com/autotune/common/auth/AuthenticationConfig.java +++ b/src/main/java/com/autotune/common/auth/AuthenticationConfig.java @@ -7,8 +7,8 @@ import org.slf4j.LoggerFactory; public class AuthenticationConfig { - private final String type; // "basic", "bearer", "apiKey", "oauth2" - private final Credentials credentials; + private String type; // "basic", "bearer", "apiKey", "oauth2" + private Credentials credentials; private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationConfig.class); public AuthenticationConfig(String type, Credentials credentials) { @@ -16,6 +16,9 @@ public AuthenticationConfig(String type, Credentials credentials) { this.credentials = credentials; } + public AuthenticationConfig() { + } + public String getType() { return type; } diff --git a/src/main/java/com/autotune/database/helper/DBHelpers.java b/src/main/java/com/autotune/database/helper/DBHelpers.java index b0332cf74..2ee173d19 100644 --- a/src/main/java/com/autotune/database/helper/DBHelpers.java +++ b/src/main/java/com/autotune/database/helper/DBHelpers.java @@ -739,9 +739,7 @@ public static List convertKruizeDataSourceToDataSourceObject(Lis try { DataSourceInfo dataSourceInfo; JsonNode authConfigJsonNode = kruizeDataSource.getAuthentication(); - // Define the type for the list of AuthenticationConfig - CollectionType listType = new ObjectMapper().getTypeFactory().constructCollectionType(List.class, AuthenticationConfig.class); - // Convert JsonNode to List + // Convert JsonNode to AuthenticationConfig AuthenticationConfig authConfig = new ObjectMapper().treeToValue(authConfigJsonNode, AuthenticationConfig.class); if (kruizeDataSource.getServiceName().isEmpty() && null != kruizeDataSource.getUrl()) { dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), @@ -755,7 +753,6 @@ public static List convertKruizeDataSourceToDataSourceObject(Lis dataSourceInfoList.add(dataSourceInfo); } catch (Exception e) { LOGGER.error("Error occurred while converting to dataSourceInfo from DB object : {}", e.getMessage()); - LOGGER.error(e.getMessage()); failureCount++; } } From 83f5afaded96198fa7c32905c357b5b8397f6a26 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Fri, 13 Sep 2024 10:58:42 +0530 Subject: [PATCH 6/8] revert changes in the Datasource table for authentication and corresponding updates Signed-off-by: Saad Khan --- .../datasource/DataSourceCollection.java | 14 ++++----- .../common/datasource/DataSourceInfo.java | 10 ++++--- .../datasource/DataSourceOperatorImpl.java | 3 +- .../autotune/database/helper/DBHelpers.java | 29 +++++-------------- .../database/table/KruizeDataSourceEntry.java | 13 --------- .../java/com/autotune/utils/TrialHelpers.java | 6 +--- 6 files changed, 21 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java index 88e5154f1..858ec6dc2 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceCollection.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceCollection.java @@ -154,23 +154,21 @@ public void addDataSourcesFromConfigFile(String configFileName) { String namespace = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_SERVICE_NAMESPACE); String dataSourceURL = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_URL); JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION); - - DataSourceInfo dataSourceInfo; // create the corresponding authentication object AuthenticationConfig authConfig = AuthenticationConfig.createAuthenticationConfigObject(authenticationObj); - + DataSourceInfo datasource = null; // Validate input if (!validateInput(name, provider, serviceName, dataSourceURL, namespace)) { continue; } if (dataSourceURL.isEmpty()) { - dataSourceInfo = new DataSourceInfo(name, provider, serviceName, namespace, null, authConfig); + datasource = new DataSourceInfo(name, provider, serviceName, namespace, null); } else { - dataSourceInfo = new DataSourceInfo(name, provider, serviceName, namespace, new URL(dataSourceURL), authConfig); + datasource = new DataSourceInfo(name, provider, serviceName, namespace, new URL(dataSourceURL)); } - - // Add the datasource to the system - addDataSource(dataSourceInfo); + // set the authentication config + datasource.setAuthenticationConfig(authConfig); + addDataSource(datasource); } } catch (IOException e) { LOGGER.error(e.getMessage()); diff --git a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java index 9affd8fc9..e3b81f29e 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceInfo.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceInfo.java @@ -40,11 +40,11 @@ public class DataSourceInfo { private final String serviceName; private final String namespace; private final URL url; - private final AuthenticationConfig authenticationConfig; + private AuthenticationConfig authenticationConfig; private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DataSourceInfo.class); - public DataSourceInfo(String name, String provider, String serviceName, String namespace, URL url, AuthenticationConfig authenticationConfig) { + public DataSourceInfo(String name, String provider, String serviceName, String namespace, URL url) { this.name = name; this.provider = provider; if (null == url) { @@ -54,7 +54,6 @@ public DataSourceInfo(String name, String provider, String serviceName, String n } this.serviceName = serviceName; this.namespace = namespace; - this.authenticationConfig = authenticationConfig; } /** @@ -124,6 +123,10 @@ public AuthenticationConfig getAuthenticationConfig() { return authenticationConfig; } + public void setAuthenticationConfig(AuthenticationConfig authenticationConfig) { + this.authenticationConfig = authenticationConfig; + } + @Override public String toString() { return "DataSourceInfo{" + @@ -132,7 +135,6 @@ public String toString() { ", serviceName='" + serviceName + '\'' + ", namespace='" + namespace + '\'' + ", url=" + url + - ", authenticationConfig=" + authenticationConfig + '}'; } } diff --git a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java index 39072cd42..5e102d98b 100644 --- a/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java +++ b/src/main/java/com/autotune/common/datasource/DataSourceOperatorImpl.java @@ -200,8 +200,7 @@ public static DataSourceInfo getMonitoringAgent(String dataSource) throws Monito monitoringAgentEndpoint = getServiceEndpoint(KruizeDeploymentInfo.monitoring_service); } if (dataSource.equals(AnalyzerConstants.PROMETHEUS_DATA_SOURCE)) { - AuthenticationConfig authenticationConfig = AuthenticationConfig.noAuth(); - monitoringAgent = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, AnalyzerConstants.PROMETHEUS_DATA_SOURCE, null, null, new URL(monitoringAgentEndpoint), authenticationConfig); + monitoringAgent = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, AnalyzerConstants.PROMETHEUS_DATA_SOURCE, null, null, new URL(monitoringAgentEndpoint)); } } diff --git a/src/main/java/com/autotune/database/helper/DBHelpers.java b/src/main/java/com/autotune/database/helper/DBHelpers.java index 2ee173d19..47f1c0493 100644 --- a/src/main/java/com/autotune/database/helper/DBHelpers.java +++ b/src/main/java/com/autotune/database/helper/DBHelpers.java @@ -32,7 +32,6 @@ import com.autotune.common.datasource.DataSourceCollection; import com.autotune.common.datasource.DataSourceInfo; import com.autotune.common.datasource.DataSourceMetadataOperator; -import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.k8sObjects.K8sObject; import com.autotune.database.table.*; import com.autotune.utils.KruizeConstants; @@ -40,7 +39,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.type.CollectionType; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.slf4j.Logger; @@ -737,22 +735,18 @@ public static List convertKruizeDataSourceToDataSourceObject(Lis int failureCount = 0; for (KruizeDataSourceEntry kruizeDataSource : kruizeDataSourceList) { try { - DataSourceInfo dataSourceInfo; - JsonNode authConfigJsonNode = kruizeDataSource.getAuthentication(); - // Convert JsonNode to AuthenticationConfig - AuthenticationConfig authConfig = new ObjectMapper().treeToValue(authConfigJsonNode, AuthenticationConfig.class); + DataSourceInfo dataSourceInfo = null; if (kruizeDataSource.getServiceName().isEmpty() && null != kruizeDataSource.getUrl()) { - dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), - kruizeDataSource.getProvider(), null, null, - new URL(kruizeDataSource.getUrl()), authConfig); - } else { - dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), - kruizeDataSource.getProvider(), kruizeDataSource.getServiceName(), - kruizeDataSource.getNamespace(), null, authConfig); + dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), kruizeDataSource + .getProvider(), null, null, new URL(kruizeDataSource.getUrl())); + } else{ + dataSourceInfo = new DataSourceInfo(kruizeDataSource.getName(), kruizeDataSource + .getProvider(), kruizeDataSource.getServiceName(), kruizeDataSource.getNamespace(), null); } dataSourceInfoList.add(dataSourceInfo); } catch (Exception e) { LOGGER.error("Error occurred while converting to dataSourceInfo from DB object : {}", e.getMessage()); + LOGGER.error(e.getMessage()); failureCount++; } } @@ -769,13 +763,6 @@ public static List convertKruizeDataSourceToDataSourceObject(Lis */ public static KruizeDataSourceEntry convertDataSourceToDataSourceDBObj(DataSourceInfo dataSourceInfo) { KruizeDataSourceEntry kruizeDataSource; - Gson gson = new GsonBuilder() - .disableHtmlEscaping() - .setPrettyPrinting() - .enableComplexMapKeySerialization() - .setDateFormat(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT) - .registerTypeAdapter(Date.class, new GsonUTCDateAdapter()) - .create(); try { kruizeDataSource = new KruizeDataSourceEntry(); kruizeDataSource.setVersion(KruizeConstants.DataSourceConstants.DataSourceMetadataInfoConstants.version); @@ -784,8 +771,6 @@ public static KruizeDataSourceEntry convertDataSourceToDataSourceDBObj(DataSourc kruizeDataSource.setServiceName(dataSourceInfo.getServiceName()); kruizeDataSource.setNamespace(dataSourceInfo.getNamespace()); kruizeDataSource.setUrl(dataSourceInfo.getUrl().toString()); - String authConfigs = gson.toJson(dataSourceInfo.getAuthenticationConfig()); - kruizeDataSource.setAuthentication(new ObjectMapper().readTree(authConfigs)); } catch (Exception e) { kruizeDataSource = null; LOGGER.error("Error while converting DataSource Object to KruizeDataSource table due to {}", e.getMessage()); diff --git a/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java b/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java index 433ae966c..63e5a90d5 100644 --- a/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java +++ b/src/main/java/com/autotune/database/table/KruizeDataSourceEntry.java @@ -16,10 +16,7 @@ package com.autotune.database.table; -import com.fasterxml.jackson.databind.JsonNode; import jakarta.persistence.*; -import org.hibernate.annotations.JdbcTypeCode; -import org.hibernate.type.SqlTypes; /** * This is a Java class named KruizeDataSourceEntry annotated with JPA annotations. @@ -45,8 +42,6 @@ public class KruizeDataSourceEntry { private String serviceName; private String namespace; private String url; - @JdbcTypeCode(SqlTypes.JSON) - private JsonNode authentication; public String getVersion() { return version; @@ -95,12 +90,4 @@ public String getUrl() { public void setUrl(String url) { this.url = url; } - - public JsonNode getAuthentication() { - return authentication; - } - - public void setAuthentication(JsonNode authentication) { - this.authentication = authentication; - } } diff --git a/src/main/java/com/autotune/utils/TrialHelpers.java b/src/main/java/com/autotune/utils/TrialHelpers.java index 60d4ad72c..78cb6b430 100644 --- a/src/main/java/com/autotune/utils/TrialHelpers.java +++ b/src/main/java/com/autotune/utils/TrialHelpers.java @@ -29,7 +29,6 @@ import com.autotune.common.data.metrics.Metric; import com.autotune.common.data.metrics.MetricResults; import com.autotune.common.datasource.DataSourceInfo; -import com.autotune.common.auth.AuthenticationConfig; import com.autotune.common.trials.*; import com.autotune.experimentManager.exceptions.IncompatibleInputJSONException; import com.autotune.operator.KruizeDeploymentInfo; @@ -146,10 +145,7 @@ public static ExperimentTrial createDefaultExperimentTrial(int trialNumber, trialNumber, trialResultUrl.toString()); - AuthenticationConfig authenticationConfig = AuthenticationConfig.noAuth(); - DataSourceInfo datasourceInfo = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, - KruizeConstants.SupportedDatasources.PROMETHEUS, null, null, - new URL(KruizeDeploymentInfo.monitoring_agent_endpoint), authenticationConfig); + DataSourceInfo datasourceInfo = new DataSourceInfo(KruizeDeploymentInfo.monitoring_agent, KruizeConstants.SupportedDatasources.PROMETHEUS, null, null, new URL(KruizeDeploymentInfo.monitoring_agent_endpoint)); HashMap datasourceInfoHashMap = new HashMap<>(); datasourceInfoHashMap.put(KruizeDeploymentInfo.monitoring_agent, datasourceInfo); //Change key value as per YAML input DeploymentTracking deploymentTracking = new DeploymentTracking(); From 8150c9ecf82a3d796ef52ca0d855c53d8b2a4573 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Mon, 16 Sep 2024 22:43:33 +0530 Subject: [PATCH 7/8] update manifests with missed URL change Signed-off-by: Saad Khan --- .../openshift/kruize-crc-openshift.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 074fa77d6..d4e72a157 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 @@ -134,7 +134,7 @@ data: "provider": "prometheus", "serviceName": "prometheus-k8s", "namespace": "openshift-monitoring", - "url": "https://prometheus-k8s.openshift-monitoring.svc.cluster.local:9091", + "url": "", "authentication": { "type": "bearer", "credentials": { From 1f2108ba8e5dcc65210b202379e1ffe5a1305902 Mon Sep 17 00:00:00 2001 From: Saad Khan Date: Tue, 17 Sep 2024 15:19:06 +0530 Subject: [PATCH 8/8] update BYODB manifests with the datasource object Signed-off-by: Saad Khan --- .../openshift/kruize-crc-openshift.yaml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/manifests/crc/BYODB-installation/openshift/kruize-crc-openshift.yaml b/manifests/crc/BYODB-installation/openshift/kruize-crc-openshift.yaml index a1f14a0b4..95bb5a997 100644 --- a/manifests/crc/BYODB-installation/openshift/kruize-crc-openshift.yaml +++ b/manifests/crc/BYODB-installation/openshift/kruize-crc-openshift.yaml @@ -68,7 +68,22 @@ data: "secretAccessKey": "", "logLevel": "INFO" } - } + }, + "datasource": [ + { + "name": "prometheus-1", + "provider": "prometheus", + "serviceName": "prometheus-k8s", + "namespace": "openshift-monitoring", + "url": "", + "authentication": { + "type": "bearer", + "credentials": { + "tokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token" + } + } + } + ] } --- apiVersion: apps/v1