Skip to content

Commit

Permalink
Merge pull request #496 from bhagyasakalanka/audit-log-remote
Browse files Browse the repository at this point in the history
Improve server config  api to get remote server logger config data
  • Loading branch information
bhagyasakalanka authored Oct 13, 2023
2 parents 6d790a1 + 9e2c126 commit 9d9b423
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,22 @@ public enum ErrorMessage {
"Unable to update remote logging configs.",
"Server encountered an " +
"error while updating the remote logging configs."),
ERROR_CODE_ERROR_GETTING_REMOTE_LOGGING_CONFIGS("65008",
"Unable to get remote logging configs.",
"Server encountered an " +
"error while getting the remote logging configs."),


ERROR_CODE_INVALID_TENANT_DOMAIN_FOR_REMOTE_LOGGING_CONFIG("60506",
"Invalid tenant domain for accessing remote logging config service",
"Remote logging configuration service is only supported for super tenant."),

ERROR_CODE_INVALID_LOG_TYPE_FOR_REMOTE_LOGGING_CONFIG("60507",
"Invalid log type provided remote logging config service",
"Remote logging configuration service only supports AUDIT or CARBON.");
"Remote logging configuration service only supports AUDIT or CARBON."),
ERROR_CODE_REMOTE_LOGGING_CONFIG_NOT_FOUND("60508",
"Resource not found.",
"Unable to find a resource matching the provided log type %s.");


private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -475,28 +474,12 @@ public void resetRemoteServerConfig() {
public void resetRemoteServerConfig(String logType) {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
if (!StringUtils.equalsIgnoreCase(tenantDomain, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Resetting remote server configuration service is not available for %s",
tenantDomain));
}
throw handleException(Response.Status.BAD_REQUEST, Constants.ErrorMessage
.ERROR_CODE_INVALID_TENANT_DOMAIN_FOR_REMOTE_LOGGING_CONFIG, null);
}
validateTenantDomain(tenantDomain, "Resetting remote server configuration service is not available for %s");

RemoteServerLoggerData remoteServerLoggerData = new RemoteServerLoggerData();

switch (logType.toUpperCase(Locale.ENGLISH)) {
case Constants.AUDIT:
remoteServerLoggerData.setAuditLogType(true);
break;
case Constants.CARBON:
remoteServerLoggerData.setCarbonLogType(true);
break;
default:
throw handleException(Response.Status.BAD_REQUEST, Constants.ErrorMessage
.ERROR_CODE_INVALID_LOG_TYPE_FOR_REMOTE_LOGGING_CONFIG, null);
}
validateLogType(logType);
remoteServerLoggerData.setLogType(logType);

try {
ConfigsServiceHolder.getInstance().getRemoteLoggingConfigService()
Expand All @@ -508,6 +491,26 @@ public void resetRemoteServerConfig(String logType) {
}
}

private void validateTenantDomain(String tenantDomain, String message) {

if (!StringUtils.equalsIgnoreCase(tenantDomain, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
if (log.isDebugEnabled()) {
log.debug(String.format(message,
tenantDomain));
}
throw handleException(Response.Status.BAD_REQUEST, Constants.ErrorMessage
.ERROR_CODE_INVALID_TENANT_DOMAIN_FOR_REMOTE_LOGGING_CONFIG, null);
}
}

private void validateLogType(String logType) {

if (!Constants.AUDIT.equals(logType) && !Constants.CARBON.equals(logType)) {
throw handleException(Response.Status.BAD_REQUEST, Constants.ErrorMessage
.ERROR_CODE_INVALID_LOG_TYPE_FOR_REMOTE_LOGGING_CONFIG, null);
}
}

/**
* Update remote server logging configurations. Each list item should correspond to specific log type.
*
Expand Down Expand Up @@ -555,27 +558,11 @@ private RemoteLoggingConfig getRemoteLoggingConfig(RemoteLoggingConfigListItem l
public void updateRemoteLoggingConfig(String logType, RemoteLoggingConfig remoteLoggingConfig) {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
if (!StringUtils.equalsIgnoreCase(tenantDomain, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Resetting remote server configuration service is not available for %s",
tenantDomain));
}
throw handleException(Response.Status.BAD_REQUEST, Constants.ErrorMessage
.ERROR_CODE_INVALID_TENANT_DOMAIN_FOR_REMOTE_LOGGING_CONFIG, null);
}
validateTenantDomain(tenantDomain, "Resetting remote server configuration service is not available for %s");

RemoteServerLoggerData remoteServerLoggerData = getRemoteServerLoggerData(remoteLoggingConfig);
switch (logType) {
case Constants.AUDIT:
remoteServerLoggerData.setAuditLogType(true);
break;
case Constants.CARBON:
remoteServerLoggerData.setCarbonLogType(true);
break;
default:
throw handleException(Response.Status.BAD_REQUEST, Constants.ErrorMessage
.ERROR_CODE_INVALID_LOG_TYPE_FOR_REMOTE_LOGGING_CONFIG, null);
}
validateLogType(logType);
remoteServerLoggerData.setLogType(logType);

try {
ConfigsServiceHolder.getInstance().getRemoteLoggingConfigService()
Expand Down Expand Up @@ -1181,4 +1168,28 @@ private APIError handleNotFoundError() {
Response.Status status = Response.Status.NOT_FOUND;
return new APIError(status, errorResponse);
}

public RemoteServerLoggerData getRemoteServerConfig(String logType) {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
validateTenantDomain(tenantDomain, "Getting remote server configuration service is not available for %s");
try {
return ConfigsServiceHolder.getInstance().getRemoteLoggingConfigService().getRemoteServerConfig(logType);
} catch (ConfigurationException e) {
throw handleException(Response.Status.INTERNAL_SERVER_ERROR,
Constants.ErrorMessage.ERROR_CODE_ERROR_GETTING_REMOTE_LOGGING_CONFIGS, null);
}
}

public List<RemoteServerLoggerData> getRemoteServerConfigs() {

String tenantDomain = ContextLoader.getTenantDomainFromContext();
validateTenantDomain(tenantDomain, "Listing remote server configuration service is not available for %s");
try {
return ConfigsServiceHolder.getInstance().getRemoteLoggingConfigService().getRemoteServerConfigs();
} catch (ConfigurationException e) {
throw handleException(Response.Status.INTERNAL_SERVER_ERROR,
Constants.ErrorMessage.ERROR_CODE_ERROR_GETTING_REMOTE_LOGGING_CONFIGS, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import org.wso2.carbon.identity.api.server.configs.v1.model.RemoteLoggingConfig;
import org.wso2.carbon.identity.api.server.configs.v1.model.RemoteLoggingConfigListItem;
import org.wso2.carbon.identity.api.server.configs.v1.model.ScimConfig;
import org.wso2.carbon.logging.service.data.RemoteServerLoggerData;

import java.util.List;
import java.util.stream.Collectors;

import javax.ws.rs.core.Response;

Expand Down Expand Up @@ -86,13 +88,23 @@ public Response getPrivatKeyJWTValidationConfiguration() {
@Override
public Response getRemoteLoggingConfig(String logType) {

return Response.status(Response.Status.NOT_IMPLEMENTED).build();
RemoteServerLoggerData remoteServerLoggerResponseData =
configManagementService.getRemoteServerConfig(logType);
if (remoteServerLoggerResponseData != null) {
return Response.ok().entity(createRemoteLoggingConfig(remoteServerLoggerResponseData)).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

@Override
public Response getRemoteLoggingConfigs() {

return Response.status(Response.Status.NOT_IMPLEMENTED).build();
List<RemoteServerLoggerData> remoteServerLoggerResponseData =
configManagementService.getRemoteServerConfigs();
return Response.ok()
.entity(remoteServerLoggerResponseData.stream().map(this::createRemoteLoggingConfigListItem)
.collect(Collectors.toList())).build();
}

@Override
Expand Down Expand Up @@ -162,4 +174,38 @@ public Response updateRemoteLoggingConfigs(List<RemoteLoggingConfigListItem> rem
configManagementService.updateRemoteLoggingConfigs(remoteLoggingConfigListItem);
return Response.accepted().build();
}

private RemoteLoggingConfigListItem createRemoteLoggingConfigListItem(
RemoteServerLoggerData remoteServerLoggerData) {

RemoteLoggingConfigListItem remoteLoggingConfigListItem = new RemoteLoggingConfigListItem();
remoteLoggingConfigListItem.setRemoteUrl(remoteServerLoggerData.getUrl());
remoteLoggingConfigListItem.setConnectTimeoutMillis(remoteServerLoggerData.getConnectTimeoutMillis());
remoteLoggingConfigListItem.setVerifyHostname(remoteServerLoggerData.isVerifyHostname());
remoteLoggingConfigListItem.setUsername(remoteServerLoggerData.getUsername());
remoteLoggingConfigListItem.setPassword(remoteServerLoggerData.getPassword());
remoteLoggingConfigListItem.setKeystoreLocation(remoteServerLoggerData.getKeystoreLocation());
remoteLoggingConfigListItem.setKeystorePassword(remoteServerLoggerData.getKeystorePassword());
remoteLoggingConfigListItem.setTruststoreLocation(remoteServerLoggerData.getTruststoreLocation());
remoteLoggingConfigListItem.setTruststorePassword(remoteServerLoggerData.getTruststorePassword());
remoteLoggingConfigListItem.setLogType(
RemoteLoggingConfigListItem.LogTypeEnum.valueOf(remoteServerLoggerData.getLogType()));
return remoteLoggingConfigListItem;
}

private RemoteLoggingConfig createRemoteLoggingConfig(
RemoteServerLoggerData remoteServerLoggerData) {

RemoteLoggingConfig remoteLoggingConfig = new RemoteLoggingConfig();
remoteLoggingConfig.setRemoteUrl(remoteServerLoggerData.getUrl());
remoteLoggingConfig.setConnectTimeoutMillis(remoteServerLoggerData.getConnectTimeoutMillis());
remoteLoggingConfig.setVerifyHostname(remoteServerLoggerData.isVerifyHostname());
remoteLoggingConfig.setUsername(remoteServerLoggerData.getUsername());
remoteLoggingConfig.setPassword(remoteServerLoggerData.getPassword());
remoteLoggingConfig.setKeystoreLocation(remoteServerLoggerData.getKeystoreLocation());
remoteLoggingConfig.setKeystorePassword(remoteServerLoggerData.getKeystorePassword());
remoteLoggingConfig.setTruststoreLocation(remoteServerLoggerData.getTruststoreLocation());
remoteLoggingConfig.setTruststorePassword(remoteServerLoggerData.getTruststorePassword());
return remoteLoggingConfig;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@
<carbon.multitenancy.version>4.9.10</carbon.multitenancy.version>
<org.wso2.carbon.identity.remotefetch.version>0.7.12</org.wso2.carbon.identity.remotefetch.version>
<org.wso2.carbon.identity.oauth2.token.handler.clientauth.jwt.version>2.4.21</org.wso2.carbon.identity.oauth2.token.handler.clientauth.jwt.version>
<org.wso2.carbon.logging.service.version>4.10.6</org.wso2.carbon.logging.service.version>
<org.wso2.carbon.logging.service.version>4.10.7</org.wso2.carbon.logging.service.version>
<org.wso2.carbon.event.publisher.version>5.2.15</org.wso2.carbon.event.publisher.version>
<identity.branding.preference.management.version>1.0.7</identity.branding.preference.management.version>
<apache.felix.scr.ds.annotations.version>1.2.4</apache.felix.scr.ds.annotations.version>
Expand Down

0 comments on commit 9d9b423

Please sign in to comment.