Skip to content

Commit 3617a95

Browse files
authored
feat: backend changes for admin-ui to call licenseSpring apis via. SCAN #4461 (#4462)
* feat: backend changes for admin-ui to call licenseSpring apis via. SCAN #4461 * feat: backend changes for admin-ui to call licenseSpring apis via. SCAN #4461 * fix: remove unused imports * fix: code smells * fix: code smells * fix: code smells
1 parent 7b41c44 commit 3617a95

File tree

4 files changed

+171
-226
lines changed

4 files changed

+171
-226
lines changed

jans-config-api/plugins/admin-ui-plugin/src/main/java/io/jans/ca/plugin/adminui/model/config/LicenseConfiguration.java

+28-29
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,58 @@ public class LicenseConfiguration {
88
@Inject
99
Logger log;
1010

11-
private String apiKey;
12-
private String productCode;
13-
private String sharedKey;
1411
private String hardwareId;
1512
private String licenseKey;
13+
private String scanApiHostname;
14+
private String scanAuthServerHostname;
15+
private String scanApiClientId;
16+
private String scanApiClientSecret;
1617

17-
public LicenseConfiguration() {
18+
public String getHardwareId() {
19+
return hardwareId;
1820
}
1921

20-
public LicenseConfiguration(String apiKey, String productCode, String sharedKey) {
21-
this.apiKey = apiKey;
22-
this.productCode = productCode;
23-
this.sharedKey = sharedKey;
22+
public void setHardwareId(String hardwareId) {
23+
this.hardwareId = hardwareId;
2424
}
2525

26-
public String getApiKey() {
27-
return apiKey;
26+
public String getLicenseKey() {
27+
return licenseKey;
2828
}
2929

30-
public void setApiKey(String apiKey) {
31-
this.apiKey = apiKey;
30+
public void setLicenseKey(String licenseKey) {
31+
this.licenseKey = licenseKey;
3232
}
3333

34-
public String getProductCode() {
35-
return productCode;
34+
public String getScanApiHostname() {
35+
return scanApiHostname;
3636
}
3737

38-
public void setProductCode(String productCode) {
39-
this.productCode = productCode;
38+
public void setScanApiHostname(String scanApiHostname) {
39+
this.scanApiHostname = scanApiHostname;
4040
}
4141

42-
public String getSharedKey() {
43-
return sharedKey;
42+
public String getScanApiClientId() {
43+
return scanApiClientId;
4444
}
4545

46-
public void setSharedKey(String sharedKey) {
47-
this.sharedKey = sharedKey;
46+
public void setScanApiClientId(String scanApiClientId) {
47+
this.scanApiClientId = scanApiClientId;
4848
}
4949

50-
public String getHardwareId() {
51-
return hardwareId;
50+
public String getScanApiClientSecret() {
51+
return scanApiClientSecret;
5252
}
5353

54-
public void setHardwareId(String hardwareId) {
55-
this.hardwareId = hardwareId;
54+
public void setScanApiClientSecret(String scanApiClientSecret) {
55+
this.scanApiClientSecret = scanApiClientSecret;
5656
}
5757

58-
public String getLicenseKey() {
59-
return licenseKey;
58+
public String getScanAuthServerHostname() {
59+
return scanAuthServerHostname;
6060
}
6161

62-
public void setLicenseKey(String licenseKey) {
63-
this.licenseKey = licenseKey;
62+
public void setScanAuthServerHostname(String scanAuthServerHostname) {
63+
this.scanAuthServerHostname = scanAuthServerHostname;
6464
}
65-
6665
}

jans-config-api/plugins/admin-ui-plugin/src/main/java/io/jans/ca/plugin/adminui/service/config/AUIConfigurationService.java

+17-85
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,19 @@
1212
import io.jans.ca.plugin.adminui.model.auth.DCRResponse;
1313
import io.jans.ca.plugin.adminui.model.config.AUIConfiguration;
1414
import io.jans.ca.plugin.adminui.model.config.LicenseConfiguration;
15-
import io.jans.ca.plugin.adminui.model.config.LicenseSpringCredentials;
1615
import io.jans.ca.plugin.adminui.model.exception.ApplicationException;
1716
import io.jans.ca.plugin.adminui.rest.license.LicenseResource;
1817
import io.jans.ca.plugin.adminui.service.BaseService;
1918
import io.jans.ca.plugin.adminui.utils.AppConstants;
20-
import io.jans.ca.plugin.adminui.utils.ClientFactory;
21-
import io.jans.ca.plugin.adminui.utils.CommonUtils;
2219
import io.jans.ca.plugin.adminui.utils.ErrorResponse;
2320
import io.jans.configapi.service.auth.ConfigurationService;
2421
import io.jans.orm.PersistenceEntryManager;
2522
import jakarta.inject.Inject;
2623
import jakarta.inject.Singleton;
27-
import jakarta.json.JsonObject;
28-
import jakarta.ws.rs.client.Entity;
29-
import jakarta.ws.rs.client.Invocation;
30-
import jakarta.ws.rs.core.MediaType;
31-
import jakarta.ws.rs.core.MultivaluedHashMap;
32-
import jakarta.ws.rs.core.MultivaluedMap;
3324
import jakarta.ws.rs.core.Response;
3425
import org.apache.commons.lang3.StringUtils;
3526
import org.slf4j.Logger;
3627

37-
import java.util.Base64;
38-
import java.util.HashMap;
3928
import java.util.Map;
4029

4130
@Singleton
@@ -90,18 +79,6 @@ public AUIConfiguration getAUIConfiguration(String appType) throws Exception {
9079
appConfigurationMap.put(appType, auiConfiguration);
9180
}
9281
}
93-
//check if LicenseConfiguration contains valid values in every request
94-
logger.info("Checking if LicenseConfiguration present.");
95-
if (!appType.equals(AppConstants.APPLICATION_KEY_ADS)) {
96-
LicenseConfiguration lc = appConfigurationMap.get(appType).getLicenseConfiguration();
97-
if (lc == null || Strings.isNullOrEmpty(lc.getApiKey())) {
98-
logger.info("Trying to add properties to LicenseConfiguration.");
99-
AdminConf appConf = entryManager.find(AdminConf.class, AppConstants.ADMIN_UI_CONFIG_DN);
100-
auiConfiguration = appConfigurationMap.get(appType);
101-
auiConfiguration.setLicenseConfiguration(addPropertiesToLicenseConfiguration(appConf));
102-
appConfigurationMap.put(appType, auiConfiguration);
103-
}
104-
}
10582
return appConfigurationMap.get(appType);
10683
} catch (Exception e) {
10784
logger.error(ErrorResponse.ERROR_READING_CONFIG.getDescription());
@@ -141,44 +118,39 @@ private AUIConfiguration addPropertiesToAUIConfiguration(String appType, AdminCo
141118
return auiConfig;
142119
}
143120

144-
private LicenseConfiguration addPropertiesToLicenseConfiguration(AdminConf appConf) throws Exception {
121+
private LicenseConfiguration addPropertiesToLicenseConfiguration(AdminConf appConf) {
145122
LicenseConfiguration licenseConfiguration = new LicenseConfiguration();
146123
try {
147124
LicenseConfig licenseConfig = appConf.getMainSettings().getLicenseConfig();
148125

149126
if (licenseConfig != null) {
150127

151-
LicenseSpringCredentials licenseSpringCredentials = requestLicenseCredentialsFromScan(licenseConfig);
152-
licenseConfiguration.setApiKey(licenseSpringCredentials.getApiKey());
153-
licenseConfiguration.setProductCode(licenseSpringCredentials.getProductCode());
154-
licenseConfiguration.setSharedKey(licenseSpringCredentials.getSharedKey());
128+
validateLicenseClientOnAuthServer(licenseConfig);
155129
licenseConfiguration.setHardwareId(licenseConfig.getLicenseHardwareKey());
156130
licenseConfiguration.setLicenseKey(licenseConfig.getLicenseKey());
131+
licenseConfiguration.setScanApiHostname(licenseConfig.getScanLicenseApiHostname());
132+
licenseConfiguration.setScanAuthServerHostname(licenseConfig.getOidcClient().getOpHost());
133+
licenseConfiguration.setScanApiClientId(licenseConfig.getOidcClient().getClientId());
134+
licenseConfiguration.setScanApiClientSecret(licenseConfig.getOidcClient().getClientSecret());
157135
}
158136
return licenseConfiguration;
159137
} catch (Exception e) {
160-
logger.error(ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
138+
logger.error(ErrorResponse.ERROR_IN_LICENSE_CONFIGURATION_VALIDATION.getDescription());
161139
}
162140
return null;
163141
}
164142

165-
/**
166-
* 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
167-
* to a different REST API endpoint to get some license credentials
168-
*
169-
* @param licenseConfig This is the object that contains the configuration parameters for the license.
170-
*/
171-
private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig licenseConfig) throws Exception {
143+
private void validateLicenseClientOnAuthServer(LicenseConfig licenseConfig) throws ApplicationException {
172144
try {
173145
logger.info("Inside method to request license credentials from SCAN api.");
174-
io.jans.as.client.TokenResponse tokenResponse = generateToken(licenseConfig);
146+
io.jans.as.client.TokenResponse tokenResponse = generateToken(licenseConfig.getOidcClient().getOpHost(), licenseConfig.getOidcClient().getClientId(), licenseConfig.getOidcClient().getClientSecret());
175147
if (tokenResponse == null) {
176148
//try to re-generate clients using old SSA
177149
DCRResponse dcrResponse = executeDCR(licenseConfig.getSsa());
178150
if (dcrResponse == null) {
179151
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_DCR.getDescription());
180152
}
181-
tokenResponse = generateToken(licenseConfig);
153+
tokenResponse = generateToken(licenseConfig.getOidcClient().getOpHost(), licenseConfig.getOidcClient().getClientId(), licenseConfig.getOidcClient().getClientSecret());
182154

183155
if (tokenResponse == null) {
184156
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.TOKEN_GENERATION_ERROR.getDescription());
@@ -190,63 +162,23 @@ private LicenseSpringCredentials requestLicenseCredentialsFromScan(LicenseConfig
190162
lc.setOidcClient(oidcClient);
191163
appConf.getMainSettings().setLicenseConfig(lc);
192164
entryManager.merge(appConf);
193-
licenseConfig = lc;
194-
}
195-
// create request header
196-
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
197-
headers.putSingle("Content-Type", "application/json");
198-
headers.putSingle("Authorization", "Bearer " + tokenResponse.getAccessToken());
199-
200-
logger.info("Trying to get license credentials from SCAN api.");
201-
String licenseCredentailsUrl = (new StringBuffer()).append(licenseConfig.getScanLicenseApiHostname())
202-
.append("/scan/license/credentials").toString();
203-
204-
Invocation.Builder request = ClientFactory.instance().getClientBuilder(licenseCredentailsUrl);
205-
request.headers(headers);
206-
207-
Map<String, String> body = new HashMap<>();
208-
body.put("pubKey", licenseConfig.getCredentialsEncryptionKey().getPublicKey());
209-
210-
Response response = request.post(Entity.entity(body, MediaType.APPLICATION_JSON));
211-
logger.info(" license credentials from scan request status code: {}", response.getStatus());
212-
if (response.getStatus() == 200) {
213-
JsonObject entity = response.readEntity(JsonObject.class);
214-
if (!Strings.isNullOrEmpty(entity.getString("apiKey"))) {
215-
//get license spring credentials
216-
LicenseSpringCredentials licenseSpringCredentials = new LicenseSpringCredentials();
217-
licenseSpringCredentials.setHardwareId(licenseConfig.getLicenseHardwareKey());
218-
219-
String privateKey = (new String(Base64.getDecoder().decode(licenseConfig.getCredentialsEncryptionKey().getPrivateKey())))
220-
.replace("-----BEGIN PRIVATE KEY-----", "")
221-
.replaceAll(System.lineSeparator(), "")
222-
.replace("-----END PRIVATE KEY-----", "");
223-
licenseSpringCredentials.setApiKey(CommonUtils.decode(entity.getString("apiKey"), privateKey));
224-
licenseSpringCredentials.setProductCode(CommonUtils.decode(entity.getString("productCode"), privateKey));
225-
licenseSpringCredentials.setSharedKey(CommonUtils.decode(entity.getString("sharedKey"), privateKey));
226-
227-
return licenseSpringCredentials;
228-
}
229165
}
230-
String errorResponse = response.readEntity(String.class);
231-
logger.error("license Activation error response: {}, code: {}", errorResponse, response.getStatus());
232-
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
233166
} catch (Exception e) {
234-
logger.error(ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
235-
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.LICENSE_SPRING_CREDENTIALS_ERROR.getDescription());
167+
logger.error(ErrorResponse.ERROR_IN_LICENSE_CONFIGURATION_VALIDATION.getDescription());
168+
throw new ApplicationException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ErrorResponse.ERROR_IN_LICENSE_CONFIGURATION_VALIDATION.getDescription());
236169
}
237170
}
238171

239-
private io.jans.as.client.TokenResponse generateToken(LicenseConfig licenseConfig) {
172+
private io.jans.as.client.TokenResponse generateToken(String opHost, String clientId, String clientSecret) {
240173
try {
241174
TokenRequest tokenRequest = new TokenRequest(GrantType.CLIENT_CREDENTIALS);
242-
tokenRequest.setAuthUsername(licenseConfig.getOidcClient().getClientId());
243-
tokenRequest.setAuthPassword(licenseConfig.getOidcClient().getClientSecret());
175+
tokenRequest.setAuthUsername(clientId);
176+
tokenRequest.setAuthPassword(clientSecret);
244177
tokenRequest.setGrantType(GrantType.CLIENT_CREDENTIALS);
245178
tokenRequest.setScope(LicenseResource.SCOPE_LICENSE_READ);
246179

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

0 commit comments

Comments
 (0)