Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: backend changes for admin-ui to call licenseSpring apis via. SCAN #4461 #4462

Merged
merged 6 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,58 @@ public class LicenseConfiguration {
@Inject
Logger log;

private String apiKey;
private String productCode;
private String sharedKey;
private String hardwareId;
private String licenseKey;
private String scanApiHostname;
private String scanAuthServerHostname;
private String scanApiClientId;
private String scanApiClientSecret;

public LicenseConfiguration() {
public String getHardwareId() {
return hardwareId;
}

public LicenseConfiguration(String apiKey, String productCode, String sharedKey) {
this.apiKey = apiKey;
this.productCode = productCode;
this.sharedKey = sharedKey;
public void setHardwareId(String hardwareId) {
this.hardwareId = hardwareId;
}

public String getApiKey() {
return apiKey;
public String getLicenseKey() {
return licenseKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
public void setLicenseKey(String licenseKey) {
this.licenseKey = licenseKey;
}

public String getProductCode() {
return productCode;
public String getScanApiHostname() {
return scanApiHostname;
}

public void setProductCode(String productCode) {
this.productCode = productCode;
public void setScanApiHostname(String scanApiHostname) {
this.scanApiHostname = scanApiHostname;
}

public String getSharedKey() {
return sharedKey;
public String getScanApiClientId() {
return scanApiClientId;
}

public void setSharedKey(String sharedKey) {
this.sharedKey = sharedKey;
public void setScanApiClientId(String scanApiClientId) {
this.scanApiClientId = scanApiClientId;
}

public String getHardwareId() {
return hardwareId;
public String getScanApiClientSecret() {
return scanApiClientSecret;
}

public void setHardwareId(String hardwareId) {
this.hardwareId = hardwareId;
public void setScanApiClientSecret(String scanApiClientSecret) {
this.scanApiClientSecret = scanApiClientSecret;
}

public String getLicenseKey() {
return licenseKey;
public String getScanAuthServerHostname() {
return scanAuthServerHostname;
}

public void setLicenseKey(String licenseKey) {
this.licenseKey = licenseKey;
public void setScanAuthServerHostname(String scanAuthServerHostname) {
this.scanAuthServerHostname = scanAuthServerHostname;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,19 @@
import io.jans.ca.plugin.adminui.model.auth.DCRResponse;
import io.jans.ca.plugin.adminui.model.config.AUIConfiguration;
import io.jans.ca.plugin.adminui.model.config.LicenseConfiguration;
import io.jans.ca.plugin.adminui.model.config.LicenseSpringCredentials;
import io.jans.ca.plugin.adminui.model.exception.ApplicationException;
import io.jans.ca.plugin.adminui.rest.license.LicenseResource;
import io.jans.ca.plugin.adminui.service.BaseService;
import io.jans.ca.plugin.adminui.utils.AppConstants;
import io.jans.ca.plugin.adminui.utils.ClientFactory;
import io.jans.ca.plugin.adminui.utils.CommonUtils;
import io.jans.ca.plugin.adminui.utils.ErrorResponse;
import io.jans.configapi.service.auth.ConfigurationService;
import io.jans.orm.PersistenceEntryManager;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.json.JsonObject;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Singleton
Expand Down Expand Up @@ -90,18 +79,6 @@ public AUIConfiguration getAUIConfiguration(String appType) throws Exception {
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 (Exception e) {
logger.error(ErrorResponse.ERROR_READING_CONFIG.getDescription());
Expand Down Expand Up @@ -141,44 +118,39 @@ private AUIConfiguration addPropertiesToAUIConfiguration(String appType, AdminCo
return auiConfig;
}

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

if (licenseConfig != null) {

LicenseSpringCredentials licenseSpringCredentials = requestLicenseCredentialsFromScan(licenseConfig);
licenseConfiguration.setApiKey(licenseSpringCredentials.getApiKey());
licenseConfiguration.setProductCode(licenseSpringCredentials.getProductCode());
licenseConfiguration.setSharedKey(licenseSpringCredentials.getSharedKey());
validateLicenseClientOnAuthServer(licenseConfig);
licenseConfiguration.setHardwareId(licenseConfig.getLicenseHardwareKey());
licenseConfiguration.setLicenseKey(licenseConfig.getLicenseKey());
licenseConfiguration.setScanApiHostname(licenseConfig.getScanLicenseApiHostname());
licenseConfiguration.setScanAuthServerHostname(licenseConfig.getOidcClient().getOpHost());
licenseConfiguration.setScanApiClientId(licenseConfig.getOidcClient().getClientId());
licenseConfiguration.setScanApiClientSecret(licenseConfig.getOidcClient().getClientSecret());
}
return licenseConfiguration;
} catch (Exception e) {
logger.error(ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
logger.error(ErrorResponse.ERROR_IN_LICENSE_CONFIGURATION_VALIDATION.getDescription());
}
return null;
}

/**
* It's a function that makes a call to a REST API endpoint to get a token, then uses that token to make another call
* to a different REST API endpoint to get some license credentials
*
* @param licenseConfig This is the object that contains the configuration parameters for the license.
*/
private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig licenseConfig) throws Exception {
private void validateLicenseClientOnAuthServer(LicenseConfig licenseConfig) throws ApplicationException {
try {
logger.info("Inside method to request license credentials from SCAN api.");
io.jans.as.client.TokenResponse tokenResponse = generateToken(licenseConfig);
io.jans.as.client.TokenResponse tokenResponse = generateToken(licenseConfig.getOidcClient().getOpHost(), licenseConfig.getOidcClient().getClientId(), licenseConfig.getOidcClient().getClientSecret());
if (tokenResponse == null) {
//try to re-generate clients using old SSA
DCRResponse dcrResponse = executeDCR(licenseConfig.getSsa());
if (dcrResponse == null) {
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_DCR.getDescription());
}
tokenResponse = generateToken(licenseConfig);
tokenResponse = generateToken(licenseConfig.getOidcClient().getOpHost(), licenseConfig.getOidcClient().getClientId(), licenseConfig.getOidcClient().getClientSecret());

if (tokenResponse == null) {
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.TOKEN_GENERATION_ERROR.getDescription());
Expand All @@ -190,63 +162,23 @@ private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig
lc.setOidcClient(oidcClient);
appConf.getMainSettings().setLicenseConfig(lc);
entryManager.merge(appConf);
licenseConfig = lc;
}
// create request header
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
headers.putSingle("Content-Type", "application/json");
headers.putSingle("Authorization", "Bearer " + tokenResponse.getAccessToken());

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

Invocation.Builder request = ClientFactory.instance().getClientBuilder(licenseCredentailsUrl);
request.headers(headers);

Map<String, String> body = new HashMap<>();
body.put("pubKey", licenseConfig.getCredentialsEncryptionKey().getPublicKey());

Response response = request.post(Entity.entity(body, MediaType.APPLICATION_JSON));
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"))) {
//get license spring credentials
LicenseSpringCredentials licenseSpringCredentials = new LicenseSpringCredentials();
licenseSpringCredentials.setHardwareId(licenseConfig.getLicenseHardwareKey());

String privateKey = (new String(Base64.getDecoder().decode(licenseConfig.getCredentialsEncryptionKey().getPrivateKey())))
.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));

return licenseSpringCredentials;
}
}
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) {
logger.error(ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
logger.error(ErrorResponse.ERROR_IN_LICENSE_CONFIGURATION_VALIDATION.getDescription());
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_LICENSE_CONFIGURATION_VALIDATION.getDescription());
}
}

private io.jans.as.client.TokenResponse generateToken(LicenseConfig licenseConfig) {
private io.jans.as.client.TokenResponse generateToken(String opHost, String clientId, String clientSecret) {
try {
TokenRequest tokenRequest = new TokenRequest(GrantType.CLIENT_CREDENTIALS);
tokenRequest.setAuthUsername(licenseConfig.getOidcClient().getClientId());
tokenRequest.setAuthPassword(licenseConfig.getOidcClient().getClientSecret());
tokenRequest.setAuthUsername(clientId);
tokenRequest.setAuthPassword(clientSecret);
tokenRequest.setGrantType(GrantType.CLIENT_CREDENTIALS);
tokenRequest.setScope(LicenseResource.SCOPE_LICENSE_READ);

logger.info("licenseConfig.toString(): " + licenseConfig.toString());
logger.info("Trying to get access token from auth server.");
String scanLicenseApiHostname = (new StringBuffer()).append(StringUtils.removeEnd(licenseConfig.getOidcClient().getOpHost(), "/"))
logger.info("Trying to get access token from auth server: {}", opHost);
String scanLicenseApiHostname = (new StringBuffer()).append(StringUtils.removeEnd(opHost, "/"))
.append("/jans-auth/restv1/token").toString();
io.jans.as.client.TokenResponse tokenResponse = null;
tokenResponse = getToken(tokenRequest, scanLicenseApiHostname);
Expand Down
Loading