Skip to content

Commit

Permalink
feat(config-api): lock stat endpoint and SAML TR fix (#10755)
Browse files Browse the repository at this point in the history
* test(config-api) scim test failure #10749

Signed-off-by: pujavs <[email protected]>

* test(config-api) scim test failure #10749

Signed-off-by: pujavs <[email protected]>

* feat(config-api): lock stat endpoint and SAML TR documentStore fix

Signed-off-by: pujavs <[email protected]>

---------

Signed-off-by: pujavs <[email protected]>
  • Loading branch information
pujavs authored Jan 28, 2025
1 parent 21d8ef5 commit 11bddd1
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 100 deletions.
12 changes: 6 additions & 6 deletions jans-config-api/docs/jans-config-api-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9335,14 +9335,14 @@ components:
$ref: '#/components/schemas/AttributeValidation'
tooltip:
type: string
userCanEdit:
type: boolean
adminCanView:
type: boolean
adminCanEdit:
type: boolean
userCanView:
type: boolean
adminCanView:
type: boolean
userCanEdit:
type: boolean
userCanAccess:
type: boolean
adminCanAccess:
Expand Down Expand Up @@ -11769,10 +11769,10 @@ components:
ttl:
type: integer
format: int32
persisted:
type: boolean
opbrowserState:
type: string
persisted:
type: boolean
SessionIdAccessMap:
type: object
properties:
Expand Down
6 changes: 3 additions & 3 deletions jans-config-api/plugins/docs/lock-plugin-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ paths:
security:
- oauth2:
- https://jans.io/oauth/lock-config.write
/lock/stat:
/lock/lockStat:
get:
tags:
- Statistics
Expand All @@ -488,12 +488,12 @@ paths:
is mandatory if start_month and end_month parameters are not present.
schema:
type: string
- name: start_month
- name: start-month
in: query
description: Start-Month for which the stat report is to be fetched
schema:
type: string
- name: end_month
- name: end-month
in: query
description: End-Month for which the stat report is to be fetched
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

package io.jans.configapi.plugin.saml.service;

import io.jans.service.document.store.service.DocumentStoreService;
import io.jans.service.document.store.conf.DocumentStoreType;
import io.jans.service.document.store.service.LocalDocumentStoreService;
import io.jans.util.exception.InvalidConfigurationException;
Expand Down Expand Up @@ -38,8 +37,6 @@ public class SamlIdpService {

@Inject
Logger logger;
@Inject
private DocumentStoreService documentStoreService;

@Inject
private LocalDocumentStoreService localDocumentStoreService;
Expand All @@ -58,7 +55,7 @@ public void create() {
}

public boolean isLocalDocumentStoreType() {
return documentStoreService.getProviderType() == DocumentStoreType.LOCAL;
return localDocumentStoreService.getProviderType() == DocumentStoreType.LOCAL;
}

public String saveMetadataFile(String metadataDir, String metadataFileName, String documentStoreModuleName,
Expand All @@ -83,14 +80,13 @@ public String saveMetadataFile(String metadataDir, String metadataFileName, Stri
}

String metadataFile = metadataDir + File.separator + metadataFileName;
logger.info("documentStoreService:{}, metadataFile:{}, localDocumentStoreService:{} ", documentStoreService,
metadataFile, localDocumentStoreService);
logger.info("metadataFile:{}, localDocumentStoreService:{} ", metadataFile, localDocumentStoreService);
try {
String result = documentStoreService.saveDocumentStream(metadataFile, null,
stream, documentStoreModuleName);
String result = localDocumentStoreService.saveDocumentStream(metadataFile, null, stream,
documentStoreModuleName);
logger.info("SAML file saving result:{}", result);

InputStream newFile = documentStoreService.readDocumentAsStream(metadataFile);
InputStream newFile = localDocumentStoreService.readDocumentAsStream(metadataFile);
logger.info("SAML file read newFile:{}", newFile);

if (result != null) {
Expand All @@ -114,16 +110,16 @@ public GluuErrorHandler validateMetadata(String metadataPath)
return new GluuErrorHandler(false, true, validationLog);
}

try (InputStream stream = documentStoreService.readDocumentAsStream(metadataPath)) {
try (InputStream stream = localDocumentStoreService.readDocumentAsStream(metadataPath)) {
return XMLValidator.validateMetadata(stream, samlSchema);
}
}

public boolean renameMetadata(String metadataPath, String destinationMetadataPath) {
logger.debug("Rename metadata file documentStoreService:{},metadataPath:{}, destinationMetadataPath:{}",
documentStoreService, metadataPath, destinationMetadataPath);
localDocumentStoreService, metadataPath, destinationMetadataPath);
try {
return documentStoreService.renameDocument(metadataPath, destinationMetadataPath) != null;
return localDocumentStoreService.renameDocument(metadataPath, destinationMetadataPath) != null;
} catch (Exception ex) {
logger.error("Failed to rename metadata '{}' to '{}'", metadataPath, destinationMetadataPath, ex);
}
Expand All @@ -133,24 +129,24 @@ public boolean renameMetadata(String metadataPath, String destinationMetadataPat

public InputStream getFileFromDocumentStore(String path) {

logger.debug("Get file from DocumentStore. Path: {}",path);
logger.debug("Get file from DocumentStore. Path: {}", path);
try {
return documentStoreService.readDocumentAsStream(path);
}catch(Exception e) {
logger.error("Failed to get file '{}' from DocumentStore",path);
return localDocumentStoreService.readDocumentAsStream(path);
} catch (Exception e) {
logger.error("Failed to get file '{}' from DocumentStore", path);
return null;
}
}

private String getTempMetadataFilename(String metadataFolder, String fileName) {
logger.info("documentStoreService:{}, localDocumentStoreService:{}, metadataFolder:{}, fileName:{}",
documentStoreService, localDocumentStoreService, metadataFolder, fileName);
public String getTempMetadataFilename(String metadataFolder, String fileName) {
logger.info("localDocumentStoreService:{}, metadataFolder:{}, fileName:{}", localDocumentStoreService,
metadataFolder, fileName);
synchronized (SamlIdpService.class) {
String possibleTemp;
do {
possibleTemp = fileName + INumGenerator.generate(2);
logger.debug("possibleTemp:{}", possibleTemp);
} while (documentStoreService.hasDocument(metadataFolder + possibleTemp));
} while (localDocumentStoreService.hasDocument(metadataFolder + possibleTemp));
return possibleTemp;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Set<Class<?>> getClasses() {

classes.add(LockConfigResource.class);
classes.add(AuditResource.class);
classes.add(LockStatResource.class);

return classes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

package io.jans.configapi.plugin.lock.rest;



import com.fasterxml.jackson.databind.JsonNode;

import static io.jans.as.model.util.Util.escapeLog;
import io.jans.configapi.core.model.exception.ApiApplicationException;
import io.jans.configapi.core.rest.BaseResource;
import io.jans.configapi.core.rest.ProtectedApi;
import io.jans.configapi.plugin.lock.service.LockService;
Expand Down Expand Up @@ -66,8 +65,8 @@ public class LockStatResource extends BaseResource {
public Response getStatistics(
@Parameter(description = "Authorization code") @HeaderParam("Authorization") String authorization,
@Parameter(description = "Month for which the stat report is to be fetched. The parameter is mandatory if start_month and end_month parameters are not present.") @QueryParam(value = "month") String month,
@Parameter(description = "Start-Month for which the stat report is to be fetched") @QueryParam(value = "start_month") String startMonth,
@Parameter(description = "End-Month for which the stat report is to be fetched") @QueryParam(value = "end_month") String endMonth,
@Parameter(description = "Start-Month for which the stat report is to be fetched") @QueryParam(value = "start-month") String startMonth,
@Parameter(description = "End-Month for which the stat report is to be fetched") @QueryParam(value = "end-month") String endMonth,
@Parameter(description = "Report format") @QueryParam(value = "format") String format) {
if (StringUtils.isBlank(format)) {
format = "";
Expand All @@ -77,11 +76,15 @@ public Response getStatistics(
if (logger.isInfoEnabled()) {
logger.info(
"LockStatResource::getStatistics() - authorization:{}, month:{}, startMonth:{}, endMonth:{}, format:{}",
escapeLog(authorization), escapeLog(month), escapeLog(startMonth), escapeLog(endMonth), escapeLog(format));
escapeLog(authorization), escapeLog(month), escapeLog(startMonth), escapeLog(endMonth),
escapeLog(format));
}
String url = getIssuer() + STAT_URL;
jsonNode = this.lockService.getStat(url, authorization, month, startMonth, endMonth, format);
logger.info("StatResource::getUserStatistics() - jsonNode:{} ", jsonNode);
} catch (ApiApplicationException aex) {
logger.error(" ApiApplicationException while fetching lock stat is", aex);
throwInternalServerException("Stat Error", aex);
} catch (Exception ex) {
logger.error(" Error while fetching lock stat is", ex);
throwBadRequestException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;

import static io.jans.as.model.util.Util.escapeLog;
import io.jans.configapi.core.model.exception.ApiApplicationException;
import io.jans.configapi.core.service.ConfigHttpService;
import io.jans.model.net.HttpServiceResponse;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MediaType;

import jakarta.ws.rs.core.Response.Status;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;

@ApplicationScoped
public class LockService {

private static final String CONTENT_TYPE = "Content-Type";
private static final String AUTHORIZATION = "Authorization";

Expand All @@ -31,73 +37,95 @@ public class LockService {
ConfigHttpService configHttpService;

public JsonNode getStat(String url, String token, String month, String startMonth, String endMonth, String format)
throws JsonProcessingException {
throws ApiApplicationException, JsonProcessingException {
if (logger.isInfoEnabled()) {
logger.info(
"LockStatResource::getStatistics() - url:{}, token:{}, month:{}, startMonth:{}, endMonth:{}, format:{}",
escapeLog(url), escapeLog(token), escapeLog(month), escapeLog(startMonth), escapeLog(endMonth), escapeLog(format));
escapeLog(url), escapeLog(token), escapeLog(month), escapeLog(startMonth), escapeLog(endMonth),
escapeLog(format));
}

JsonNode jsonNode = null;

// Request headers
Map<String, String> headers = new HashMap<>();
headers.put(CONTENT_TYPE, MediaType.APPLICATION_JSON);
if (StringUtils.isNotBlank(token)) {
headers.put(AUTHORIZATION, token);
}

// Query Param
// Query Parameter
Map<String, String> data = new HashMap<>();
data.put("month", month);
data.put("start-month", startMonth);
data.put("end-month", endMonth);
data.put("format", format);
HttpServiceResponse httpServiceResponse = configHttpService.executeGet(url, headers, data);

logger.info(" stat httpServiceResponse:{}", httpServiceResponse);
if (httpServiceResponse != null) {
jsonNode = getResponseJsonNode(httpServiceResponse, Status.OK);
logger.info(
" stat httpServiceResponse.getHttpResponse():{}, httpServiceResponse.getHttpResponse().getStatusLine():{}, httpServiceResponse.getHttpResponse().getEntity():{}",
httpServiceResponse.getHttpResponse(), httpServiceResponse.getHttpResponse().getStatusLine(),
httpServiceResponse.getHttpResponse().getEntity());
jsonNode = getResponseJsonNode(httpServiceResponse);
}
logger.info(" stat jsonNode:{}", jsonNode);
return jsonNode;
}

public String getResponseEntityString(HttpServiceResponse serviceResponse, Status status) {
public JsonNode getResponseJsonNode(HttpServiceResponse serviceResponse)
throws ApiApplicationException, JsonProcessingException {
JsonNode jsonNode = null;

if (serviceResponse == null) {
return jsonNode;
}

return getResponseJsonNode(getResponseEntityString(serviceResponse), "response");
}

public String getResponseEntityString(HttpServiceResponse serviceResponse) throws ApiApplicationException {
String jsonString = null;

if (serviceResponse == null) {
return jsonString;
}

if (serviceResponse.getHttpResponse() != null && serviceResponse.getHttpResponse().getStatusLine() != null
&& serviceResponse.getHttpResponse().getStatusLine().getStatusCode() == status.getStatusCode()) {
HttpEntity entity = serviceResponse.getHttpResponse().getEntity();
HttpResponse httpResponse = serviceResponse.getHttpResponse();
if (httpResponse != null) {
HttpEntity entity = httpResponse.getEntity();
logger.debug("entity:{}, httpResponse.getStatusLine().getStatusCode():{}", entity,
httpResponse.getStatusLine().getStatusCode());
if (entity == null) {
return jsonString;
}
jsonString = entity.toString();
try {
jsonString = EntityUtils.toString(entity, "UTF-8");
} catch (Exception ex) {
logger.error("Error while getting entity using EntityUtils is ", ex);
}

if (httpResponse.getStatusLine() != null
&& httpResponse.getStatusLine().getStatusCode() == Status.OK.getStatusCode()) {
return jsonString;
} else {
throw new ApiApplicationException(httpResponse.getStatusLine().getStatusCode(), jsonString);
}
}
logger.info(" stat jsonString:{}", jsonString);
return jsonString;
}

public JsonNode getResponseJsonNode(HttpServiceResponse serviceResponse, Status status)
throws JsonProcessingException {
public JsonNode getResponseJsonNode(String jsonSring, String nodeName) throws JsonProcessingException {
JsonNode jsonNode = null;

if (serviceResponse == null) {
if (StringUtils.isBlank(jsonSring)) {
return jsonNode;
}

return getResponseJsonNode(getResponseEntityString(serviceResponse, status));
}

public JsonNode getResponseJsonNode(String jsonSring) throws JsonProcessingException {
JsonNode jsonNode = null;

if (StringUtils.isNotBlank(jsonSring)) {
return jsonNode;
jsonNode = Jackson.asJsonNode(jsonSring);
if (StringUtils.isNotBlank(nodeName) && jsonNode != null && jsonNode.get(nodeName) != null) {
jsonNode = jsonNode.get("response");
}

return Jackson.asJsonNode(jsonSring);
return jsonNode;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private Constants() {
public static final String LOCK = "/lock";
public static final String LOCK_CONFIG = "/lockConfig";
public static final String AUDIT = "/audit";
public static final String LOCK_STAT = "/stat";
public static final String LOCK_STAT = "/lockStat";
public static final String HEALTH = "/health";
public static final String LOG = "/log";
public static final String TELEMETRY = "/telemetry";
Expand Down
Loading

0 comments on commit 11bddd1

Please sign in to comment.