Skip to content

Commit

Permalink
fix(jans-config-api): getting license credentials from SCAN (#4055)
Browse files Browse the repository at this point in the history
* fix: code smell

* fix: code smell

* fix: sonar bugs

* fix: sonar bugs
  • Loading branch information
duttarnab authored Mar 8, 2023
1 parent 12baf1d commit 407d618
Showing 1 changed file with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AUIConfigurationService extends BaseService {
private Map<String, AUIConfiguration> appConfigurationMap;

@Inject
Logger log;
Logger logger;

@Inject
private PersistenceEntryManager entryManager;
Expand All @@ -53,7 +53,7 @@ public class AUIConfigurationService extends BaseService {
@Inject
ConfigurationService configurationService;

public AUIConfiguration getAUIConfiguration() throws ApplicationException {
public AUIConfiguration getAUIConfiguration() throws Exception {
return getAUIConfiguration(null);
}

Expand All @@ -63,8 +63,8 @@ public AUIConfiguration getAUIConfiguration() throws ApplicationException {
* @param appType The application type. This is either "adminUI" or "ads".
* @return The AUIConfiguration object
*/
public AUIConfiguration getAUIConfiguration(String appType) throws ApplicationException {

public AUIConfiguration getAUIConfiguration(String appType) throws Exception {
logger.info("Inside method to read the configuration from the LDAP server and stores it in a map.");
try {
if (Strings.isNullOrEmpty(appType)) {
appType = AppConstants.APPLICATION_KEY_ADMIN_UI;
Expand All @@ -73,6 +73,7 @@ public AUIConfiguration getAUIConfiguration(String appType) throws ApplicationEx
if (appConfigurationMap == null) {
appConfigurationMap = Maps.newHashMap();
}
AUIConfiguration auiConfiguration = null;

if (appConfigurationMap.get(appType) == null) {
AdminConf appConf = null;
Expand All @@ -81,23 +82,29 @@ public AUIConfiguration getAUIConfiguration(String appType) throws ApplicationEx
} else if (appType.equals(AppConstants.APPLICATION_KEY_ADS)) {
appConf = entryManager.find(AdminConf.class, AppConstants.ADS_CONFIG_DN);
}
AUIConfiguration auiConfiguration = addPropertiesToAUIConfiguration(appType, appConf);
auiConfiguration = addPropertiesToAUIConfiguration(appType, appConf);
if (!appType.equals(AppConstants.APPLICATION_KEY_ADS)) {
auiConfiguration.setLicenseConfiguration(addPropertiesToLicenseConfiguration(appConf));
appConfigurationMap.put(appType, auiConfiguration);
}
}


//check if LicenseConfiguration contains valid values in every request
logger.info("Checking if LicenseConfiguration present.");
if (!appType.equals(AppConstants.APPLICATION_KEY_ADS)) {
LicenseConfiguration lc = appConfigurationMap.get(appType).getLicenseConfiguration();
if (lc == null || Strings.isNullOrEmpty(lc.getApiKey())) {
logger.info("Trying to add properties to LicenseConfiguration.");
AdminConf appConf = entryManager.find(AdminConf.class, AppConstants.ADMIN_UI_CONFIG_DN);
auiConfiguration = appConfigurationMap.get(appType);
auiConfiguration.setLicenseConfiguration(addPropertiesToLicenseConfiguration(appConf));
appConfigurationMap.put(appType, auiConfiguration);
}
}
return appConfigurationMap.get(appType);
} catch (ApplicationException e) {
log.error(ErrorResponse.ERROR_READING_CONFIG.getDescription(), e);
throw e;
} catch (Exception e) {
log.error(ErrorResponse.ERROR_READING_CONFIG.getDescription(), e);
logger.error(ErrorResponse.ERROR_READING_CONFIG.getDescription());
throw e;
}

}

public void setAuiConfiguration(AUIConfiguration auiConfiguration) {
Expand Down Expand Up @@ -132,7 +139,7 @@ private AUIConfiguration addPropertiesToAUIConfiguration(String appType, AdminCo
return auiConfig;
}

private LicenseConfiguration addPropertiesToLicenseConfiguration(AdminConf appConf) throws ApplicationException {
private LicenseConfiguration addPropertiesToLicenseConfiguration(AdminConf appConf) throws Exception {
LicenseConfiguration licenseConfiguration = new LicenseConfiguration();
LicenseConfig licenseConfig = appConf.getMainSettings().getLicenseConfig();

Expand All @@ -154,30 +161,26 @@ private LicenseConfiguration addPropertiesToLicenseConfiguration(AdminConf appCo
*
* @param licenseConfig This is the object that contains the configuration parameters for the license.
*/
private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig licenseConfig) throws ApplicationException {
private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig licenseConfig) throws Exception {
try {
log.info("Inside method to request license credentials from SCAN api.");
logger.info("Inside method to request license credentials from SCAN api.");
TokenRequest tokenRequest = new TokenRequest(GrantType.CLIENT_CREDENTIALS);
tokenRequest.setAuthUsername(licenseConfig.getOidcClient().getClientId());
tokenRequest.setAuthPassword(licenseConfig.getOidcClient().getClientSecret());
tokenRequest.setGrantType(GrantType.CLIENT_CREDENTIALS);
tokenRequest.setScope(LicenseResource.SCOPE_LICENSE_READ);

log.info("Truing to get access token from auth server.");
logger.info("Truing to get access token from auth server.");
String scanLicenseApiHostname = (new StringBuffer()).append(licenseConfig.getScanLicenseAuthServerHostname()).append("/jans-auth/restv1/token").toString();
io.jans.as.client.TokenResponse tokenResponse = null;
try {
tokenResponse = getToken(tokenRequest, scanLicenseApiHostname);
} catch (Exception e) {
log.error("Error in generating token from server: {}", scanLicenseApiHostname);
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Error in generating token from server: " + scanLicenseApiHostname);
}
tokenResponse = getToken(tokenRequest, scanLicenseApiHostname);

// create request header
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.putSingle("Content-Type", "application/json");
headers.putSingle("Authorization", "Bearer " + tokenResponse.getAccessToken());

log.info("Trying to get license credentials from SCAN api.");
logger.info("Trying to get license credentials from SCAN api.");
String licenseCredentailsUrl = (new StringBuffer()).append(licenseConfig.getScanLicenseApiHostname())
.append("/scan/license/credentials").toString();

Expand All @@ -188,7 +191,7 @@ private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig
body.put("pubKey", licenseConfig.getCredentialsEncryptionKey().getPublicKey());

Response response = request.post(Entity.entity(body, MediaType.APPLICATION_JSON));
log.info(" license credentials from scan request status code: {}", response.getStatus());
logger.info(" license credentials from scan request status code: {}", response.getStatus());
if (response.getStatus() == 200) {
JsonObject entity = response.readEntity(JsonObject.class);
if (!Strings.isNullOrEmpty(entity.getString("apiKey"))) {
Expand All @@ -200,19 +203,18 @@ private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig
.replace("-----BEGIN PRIVATE KEY-----", "")
.replaceAll(System.lineSeparator(), "")
.replace("-----END PRIVATE KEY-----", "");
;
licenseSpringCredentials.setApiKey(CommonUtils.decode(entity.getString("apiKey"), privateKey));
licenseSpringCredentials.setProductCode(CommonUtils.decode(entity.getString("productCode"), privateKey));
licenseSpringCredentials.setSharedKey(CommonUtils.decode(entity.getString("sharedKey"), privateKey));

log.info(" licenseSpringCredentials.toString(): {}", licenseSpringCredentials.toString());
return licenseSpringCredentials;
}
}
log.error("license Activation error response: {}, code: {}", response.readEntity(String.class), response.getStatus());
String errorResponse = response.readEntity(String.class);
logger.error("license Activation error response: {}, code: {}", errorResponse, response.getStatus());
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
} catch (Exception e) {
log.error(ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription(), e);
logger.error(ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
}
}
Expand Down

0 comments on commit 407d618

Please sign in to comment.