Skip to content

Commit

Permalink
add support for no-auth, refactor code to put auth strategy in a comm…
Browse files Browse the repository at this point in the history
…on constructor

Signed-off-by: Saad Khan <[email protected]>
  • Loading branch information
khansaad committed Sep 19, 2024
1 parent 9d50dfc commit fd15d34
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1973,10 +1973,8 @@ public void fetchMetricsBasedOnProfileAndDatasource(KruizeObject kruizeObject, T
long interval_end_time_epoc = 0;
long interval_start_time_epoc = 0;
SimpleDateFormat sdf = new SimpleDateFormat(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT, Locale.ROOT);
AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(
dataSourceInfo.getAuthenticationConfig());
// Create the client
GenericRestApiClient client = new GenericRestApiClient(authenticationStrategy);
GenericRestApiClient client = new GenericRestApiClient(dataSourceInfo);

String metricProfileName = kruizeObject.getPerformanceProfile();
PerformanceProfile metricProfile = MetricProfileCollection.getInstance().getMetricProfileCollection().get(metricProfileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class AuthenticationStrategyFactory {
public static AuthenticationStrategy createAuthenticationStrategy(AuthenticationConfig authConfig) {
String type = authConfig.getType();
switch (type) {
case KruizeConstants.AuthenticationConstants.NONE:
return new NoAuthStrategy();
case KruizeConstants.AuthenticationConstants.BASIC:
String username = authConfig.getCredentials().getUsername();
String password = authConfig.getCredentials().getPassword();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/autotune/common/auth/NoAuthStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.autotune.common.auth;

public class NoAuthStrategy implements AuthenticationStrategy {
@Override
public String applyAuthentication() {
// No authentication is applied
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,17 @@ public void addDataSourcesFromConfigFile(String configFileName) {
String serviceName = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_SERVICE_NAME);
String namespace = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_SERVICE_NAMESPACE);
String dataSourceURL = dataSourceObject.getString(KruizeConstants.DataSourceConstants.DATASOURCE_URL);
JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION);
// create the corresponding authentication object
AuthenticationConfig authConfig = AuthenticationConfig.createAuthenticationConfigObject(authenticationObj);
DataSourceInfo datasource = null;
AuthenticationConfig authConfig;
try {
JSONObject authenticationObj = dataSourceObject.optJSONObject(KruizeConstants.AuthenticationConstants.AUTHENTICATION);
// create the corresponding authentication object
authConfig = AuthenticationConfig.createAuthenticationConfigObject(authenticationObj);
} catch (Exception e) {
LOGGER.warn("Auth details are missing for datasource: {}", name);
authConfig = AuthenticationConfig.noAuth();
}

DataSourceInfo datasource;
// Validate input
if (!validateInput(name, provider, serviceName, dataSourceURL, namespace)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ public ArrayList<String> getAppsForLayer(DataSourceInfo dataSource, String query
String queryURL = dataSourceURL + queryEndpoint + query;
LOGGER.debug("Query URL is: {}", queryURL);
try {
AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(
dataSource.getAuthenticationConfig());
// Create the client
GenericRestApiClient genericRestApiClient = new GenericRestApiClient(authenticationStrategy);
GenericRestApiClient genericRestApiClient = new GenericRestApiClient(dataSource);
genericRestApiClient.setBaseURL(dataSourceURL + queryEndpoint);
JSONObject responseJson = genericRestApiClient.fetchMetricsJson("GET", query);
int level = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ public Object getValueForQuery(DataSourceInfo dataSource, String query) {
*/
@Override
public JSONObject getJsonObjectForQuery(DataSourceInfo dataSource, String query) {
AuthenticationStrategy authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(
dataSource.getAuthenticationConfig());
// Create the client
GenericRestApiClient apiClient = new GenericRestApiClient(authenticationStrategy);
GenericRestApiClient apiClient = new GenericRestApiClient(dataSource);
apiClient.setBaseURL(CommonUtils.getBaseDataSourceUrl(
dataSource,
KruizeConstants.SupportedDatasources.PROMETHEUS
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/autotune/common/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ public static double getPercentage(double newer, double older) {

public static DataSourceInfo getDataSourceInfo(String dataSourceName) throws Exception {
DataSourceManager dataSourceManager = new DataSourceManager();
// fetch the datasource from the DB
DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName);
// fetch the datasource from the config file first
DataSourceInfo datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName);
if (isInvalidDataSource(datasource)) {
// fetch the datasource from the config
datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName);
// fetch the datasource from the DB
datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName);
if (isInvalidDataSource(datasource)) {
throw new Exception(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/autotune/utils/GenericRestApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.autotune.utils;

import com.autotune.common.auth.AuthenticationStrategy;
import com.autotune.common.auth.AuthenticationStrategyFactory;
import com.autotune.common.datasource.DataSourceInfo;
import com.autotune.utils.authModels.APIKeysAuthentication;
import com.autotune.utils.authModels.BasicAuthentication;
import com.autotune.utils.authModels.BearerAccessToken;
Expand Down Expand Up @@ -57,11 +59,13 @@ public class GenericRestApiClient {
private AuthenticationStrategy authenticationStrategy;

/**
* Initializes a new instance just by passing baseURL which does not need any authentication.
* @param authenticationStrategy
* constructor to set the authentication based on the datasourceInfo object
* @param dataSourceInfo object containing the datasource details
*/
public GenericRestApiClient(AuthenticationStrategy authenticationStrategy) {
this.authenticationStrategy = authenticationStrategy;
public GenericRestApiClient(DataSourceInfo dataSourceInfo) {
// TODO: add partial URL as well as part of this constructor
this.authenticationStrategy = AuthenticationStrategyFactory.createAuthenticationStrategy(
dataSourceInfo.getAuthenticationConfig());
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/autotune/utils/KruizeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ public static final class AuthenticationConstants {
public static final String AUTHENTICATION_CLIENT_ID = "clientId";
public static final String AUTHENTICATION_CLIENT_SECRET = "clientSecret";
public static final String AUTHENTICATION_GRANT_TYPE = "grantType";
public static final String NONE = "none";
public static final String BASIC = "basic";
public static final String BEARER = "bearer";
public static final String API_KEY = "apikey";
Expand Down

0 comments on commit fd15d34

Please sign in to comment.