Skip to content

Commit

Permalink
feat: backend changes for admin-ui to call licenseSpring apis via. SCAN
Browse files Browse the repository at this point in the history
  • Loading branch information
duttarnab committed Apr 5, 2023
1 parent acfb944 commit 8ae7d33
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,60 @@ 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 LicenseConfiguration(String apiKey, String productCode, String sharedKey) {
this.apiKey = apiKey;
this.productCode = productCode;
this.sharedKey = sharedKey;
public String getHardwareId() {
return hardwareId;
}

public String getApiKey() {
return apiKey;
public void setHardwareId(String hardwareId) {
this.hardwareId = hardwareId;
}

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

public String getProductCode() {
return productCode;
public void setLicenseKey(String licenseKey) {
this.licenseKey = licenseKey;
}

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

public String getSharedKey() {
return sharedKey;
public void setScanApiHostname(String scanApiHostname) {
this.scanApiHostname = scanApiHostname;
}

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

public String getHardwareId() {
return hardwareId;
public void setScanApiClientId(String scanApiClientId) {
this.scanApiClientId = scanApiClientId;
}

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

public String getLicenseKey() {
return licenseKey;
public void setScanApiClientSecret(String scanApiClientSecret) {
this.scanApiClientSecret = scanApiClientSecret;
}

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

public void setScanAuthServerHostname(String scanAuthServerHostname) {
this.scanAuthServerHostname = scanAuthServerHostname;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public Response isActive() {
licenseResponse = licenseDetailsService.checkLicense();
log.info("Active license present (true/false): {}", licenseResponse.isApiResult());
return Response.ok(licenseResponse).build();

} catch (Exception e) {
log.error(ErrorResponse.CHECK_LICENSE_ERROR.getDescription(), e);
return Response.serverError().entity(licenseResponse).build();
Expand All @@ -89,6 +90,7 @@ public Response activateLicense(@Valid @NotNull LicenseRequest licenseRequest) {
licenseResponse = licenseDetailsService.activateLicense(licenseRequest);
log.info("License activated (true/false): {}", licenseResponse.isApiResult());
return Response.ok(licenseResponse).build();

} catch (Exception e) {
log.error(ErrorResponse.ACTIVATE_LICENSE_ERROR.getDescription(), e);
return Response.serverError().entity(licenseResponse).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,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 @@ -148,37 +136,32 @@ private LicenseConfiguration addPropertiesToLicenseConfiguration(AdminConf appCo

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 Exception {
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 +173,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

0 comments on commit 8ae7d33

Please sign in to comment.