SUPPORT-8706: add condition
This commit is contained in:
parent
71b7d9b9f8
commit
71915d4616
7 changed files with 96 additions and 21 deletions
|
|
@ -0,0 +1,19 @@
|
|||
package ru.micord.ervu.audit.config;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
public class AuditDisableCondition implements Condition {
|
||||
private static final String AUDIT_ENABLED_PROPERTY_NAME = "audit.kafka.enabled";
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
Environment env = context.getEnvironment();
|
||||
return !Boolean.parseBoolean(env.getProperty(AUDIT_ENABLED_PROPERTY_NAME, "false"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package ru.micord.ervu.audit.config;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
public class AuditEnabledCondition implements Condition {
|
||||
private static final String AUDIT_ENABLED_PROPERTY_NAME = "audit.kafka.enabled";
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
Environment env = context.getEnvironment();
|
||||
return Boolean.parseBoolean(env.getProperty(AUDIT_ENABLED_PROPERTY_NAME, "false"));
|
||||
}
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import org.apache.kafka.common.config.SaslConfigs;
|
|||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.kafka.config.TopicBuilder;
|
||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
||||
|
|
@ -22,6 +23,7 @@ import org.springframework.kafka.core.ProducerFactory;
|
|||
* @author Adel Kalimullin
|
||||
*/
|
||||
@Configuration
|
||||
@Conditional(AuditEnabledCondition.class)
|
||||
public class AuditKafkaConfig {
|
||||
@Value("${audit.kafka.bootstrap.servers}")
|
||||
private String bootstrapServers;
|
||||
|
|
|
|||
|
|
@ -4,20 +4,20 @@ package ru.micord.ervu.audit.service.impl;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.micord.ervu.audit.config.AuditEnabledCondition;
|
||||
import ru.micord.ervu.audit.service.AuditKafkaPublisher;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
@Service
|
||||
@Conditional(AuditEnabledCondition.class)
|
||||
public class BaseAuditKafkaPublisher implements AuditKafkaPublisher {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BaseAuditKafkaPublisher.class);
|
||||
private final KafkaTemplate<String, String> kafkaTemplate;
|
||||
@Value("${audit.kafka.enabled}")
|
||||
private boolean auditEnabled;
|
||||
|
||||
public BaseAuditKafkaPublisher(
|
||||
@Qualifier("auditTemplate") KafkaTemplate<String, String> kafkaTemplate) {
|
||||
|
|
@ -26,18 +26,13 @@ public class BaseAuditKafkaPublisher implements AuditKafkaPublisher {
|
|||
|
||||
@Override
|
||||
public void publishEvent(String topic, String message) {
|
||||
if (auditEnabled) {
|
||||
kafkaTemplate.send(topic, message)
|
||||
.addCallback(
|
||||
result -> {
|
||||
},
|
||||
ex -> LOGGER.error("Failed to send message to topic {}: {}", topic, ex.getMessage(),
|
||||
ex
|
||||
)
|
||||
);
|
||||
}
|
||||
else {
|
||||
LOGGER.info("Audit is disabled. Event not published.");
|
||||
}
|
||||
kafkaTemplate.send(topic, message)
|
||||
.addCallback(
|
||||
result -> {
|
||||
},
|
||||
ex -> LOGGER.error("Failed to send message to topic {}: {}", topic, ex.getMessage(),
|
||||
ex
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import ervu.model.fileupload.FileInfo;
|
||||
import ervu.model.fileupload.UploadOrgInfo;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.micord.ervu.audit.config.AuditEnabledCondition;
|
||||
import ru.micord.ervu.audit.constants.AuditConstants;
|
||||
import ru.micord.ervu.audit.model.*;
|
||||
import ru.micord.ervu.audit.service.AuditKafkaPublisher;
|
||||
|
|
@ -28,6 +30,7 @@ import ru.micord.ervu.util.NetworkUtils;
|
|||
* @author Adel Kalimullin
|
||||
*/
|
||||
@Service
|
||||
@Conditional(AuditEnabledCondition.class)
|
||||
public class BaseAuditService implements AuditService {
|
||||
private final AuditKafkaPublisher auditKafkaPublisher;
|
||||
private final JwtTokenService jwtTokenService;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package ru.micord.ervu.audit.service.impl;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import ervu.model.fileupload.FileInfo;
|
||||
import ervu.model.fileupload.UploadOrgInfo;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.micord.ervu.audit.config.AuditDisableCondition;
|
||||
import ru.micord.ervu.audit.model.AuditActionRequest;
|
||||
import ru.micord.ervu.audit.service.AuditService;
|
||||
import ru.micord.ervu.kafka.model.OrgInfo;
|
||||
|
||||
/**
|
||||
* @author Adel Kalimullin
|
||||
*/
|
||||
@Service
|
||||
@Conditional(AuditDisableCondition.class)
|
||||
public class StubAuditService implements AuditService {
|
||||
@Override
|
||||
public void processActionEvent(HttpServletRequest request,
|
||||
AuditActionRequest auditActionRequest) {}
|
||||
|
||||
@Override
|
||||
public void processAuthEvent(HttpServletRequest request, OrgInfo orgInfo, String prnOid,
|
||||
String status, String eventType) {}
|
||||
|
||||
@Override
|
||||
public void processUploadEvent(UploadOrgInfo uploadOrgInfo, FileInfo fileInfo) {}
|
||||
|
||||
@Override
|
||||
public void processDownloadEvent(HttpServletRequest request, long fileSize, String fileName,
|
||||
int formatRegistry, String status, String s3FileUrl) {}
|
||||
}
|
||||
|
|
@ -399,6 +399,7 @@ public class EsiaAuthService {
|
|||
public String logout(HttpServletRequest request, HttpServletResponse response) {
|
||||
OrgInfo orgInfo = null;
|
||||
String userId = null;
|
||||
String status = null;
|
||||
try {
|
||||
userId = jwtTokenService.getUserAccountId(request);
|
||||
String accessToken = EsiaAuthInfoStore.getAccessToken(userId);
|
||||
|
|
@ -411,19 +412,21 @@ public class EsiaAuthService {
|
|||
URL url = new URL(logoutUrl);
|
||||
Map<String, String> params = mapOf(
|
||||
"client_id", esiaConfig.getClientId(),
|
||||
"redirect_url", redirectUrl);
|
||||
auditService.processAuthEvent(request, orgInfo, userId, AuditConstants.SUCCESS_STATUS_TYPE,
|
||||
AuditConstants.LOGOUT_EVENT_TYPE
|
||||
"redirect_url", redirectUrl
|
||||
);
|
||||
status = AuditConstants.SUCCESS_STATUS_TYPE;
|
||||
return buildUrl(url, params);
|
||||
}
|
||||
catch (Exception e) {
|
||||
status = AuditConstants.FAILURE_STATUS_TYPE;
|
||||
throw new EsiaException(e);
|
||||
}
|
||||
finally {
|
||||
if (orgInfo != null) {
|
||||
auditService.processAuthEvent(request, orgInfo, userId, AuditConstants.FAILURE_STATUS_TYPE,
|
||||
auditService.processAuthEvent(request, orgInfo, userId, status,
|
||||
AuditConstants.LOGOUT_EVENT_TYPE
|
||||
);
|
||||
}
|
||||
throw new EsiaException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue