Skip to content

Commit 1b741e0

Browse files
vishnu-alapatiVishnu Alapati
andauthored
Print the User Information in catalina files while the log level is INFO (#276)
* Print the User Information in catalina files while the log level is INFO --------- Co-authored-by: Vishnu Alapati <zalavis@seliiuts03320.seli.gic.ericsson.se>
1 parent 3be9ff8 commit 1b741e0

File tree

4 files changed

+89
-13
lines changed

4 files changed

+89
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2.1.1
22
- Implemented the changes to log the eventId and HTTPStatus while the level is INFO.
3+
- Implemented the changes to print the user information while the log level is INFO.
34

45
## 2.1.0
56
- Implemented new routing key template for Sepia.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.ericsson.eiffel.remrem.publish.config;
2+
3+
import java.io.IOException;
4+
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.http.HttpStatus;
12+
import org.springframework.security.authentication.BadCredentialsException;
13+
import org.springframework.security.core.AuthenticationException;
14+
import org.springframework.security.web.AuthenticationEntryPoint;
15+
import org.springframework.stereotype.Component;
16+
17+
@Component
18+
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
19+
20+
private static final Logger LOGGER = LoggerFactory.getLogger(
21+
CustomAuthenticationEntryPoint.class);
22+
23+
@Override
24+
public void commence(HttpServletRequest request, HttpServletResponse response,
25+
AuthenticationException authException) throws IOException, ServletException {
26+
if (authException instanceof BadCredentialsException) {
27+
LOGGER.warn("Bad Credentials {}", HttpStatus.UNAUTHORIZED);
28+
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid credentials");
29+
}
30+
}
31+
}

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/config/SecurityConfig.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.beans.factory.annotation.Value;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24-
import org.springframework.context.annotation.Bean;
2524
import org.springframework.context.annotation.Configuration;
2625
import org.springframework.context.annotation.Profile;
2726
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
@@ -72,14 +71,20 @@ public Integer getTimeOut() {
7271
return ldapTimeOut;
7372
}
7473

74+
@Autowired
75+
private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
76+
7577
@Autowired
7678
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
7779
final String jasyptKey = RabbitMqPropertiesConfig.readJasyptKeyFile(jasyptKeyFilePath);
7880
if (managerPassword.startsWith("{ENC(") && managerPassword.endsWith("}")) {
79-
managerPassword = DecryptionUtils.decryptString(managerPassword.substring(1, managerPassword.length() - 1), jasyptKey);
81+
managerPassword = DecryptionUtils.decryptString(
82+
managerPassword.substring(1, managerPassword.length() - 1), jasyptKey);
8083
}
8184
LOGGER.debug("LDAP server url: " + ldapUrl);
82-
auth.ldapAuthentication().userSearchFilter(userSearchFilter).contextSource(ldapContextSource());
85+
auth.ldapAuthentication()
86+
.userSearchFilter(userSearchFilter)
87+
.contextSource(ldapContextSource());
8388
}
8489

8590
public BaseLdapPathContextSource ldapContextSource() {
@@ -98,6 +103,14 @@ public BaseLdapPathContextSource ldapContextSource() {
98103
@Override
99104
protected void configure(HttpSecurity http) throws Exception {
100105
LOGGER.debug("LDAP authentication enabled");
101-
http.authorizeRequests().anyRequest().authenticated().and().httpBasic().and().csrf().disable();
106+
http.authorizeRequests()
107+
.anyRequest()
108+
.authenticated()
109+
.and()
110+
.httpBasic()
111+
.authenticationEntryPoint(customAuthenticationEntryPoint)
112+
.and()
113+
.csrf()
114+
.disable();
102115
}
103116
}

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616

1717
import java.util.EnumSet;
1818
import java.util.Map;
19+
1920
import org.slf4j.LoggerFactory;
2021
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.beans.factory.annotation.Value;
2224
import org.springframework.context.annotation.ComponentScan;
2325
import org.springframework.http.HttpEntity;
2426
import org.springframework.http.HttpHeaders;
2527
import org.springframework.http.HttpStatus;
2628
import org.springframework.http.MediaType;
2729
import org.springframework.http.ResponseEntity;
30+
import org.springframework.security.core.Authentication;
31+
import org.springframework.security.core.context.SecurityContextHolder;
32+
import org.springframework.security.core.userdetails.UserDetails;
2833
import org.springframework.web.bind.annotation.RequestBody;
2934
import org.springframework.web.bind.annotation.RequestMapping;
3035
import org.springframework.web.bind.annotation.RequestMethod;
@@ -35,13 +40,13 @@
3540
import org.springframework.web.client.RestTemplate;
3641

3742
import com.ericsson.eiffel.remrem.protocol.MsgService;
43+
import com.ericsson.eiffel.remrem.publish.exception.RemRemPublishException;
3844
import com.ericsson.eiffel.remrem.publish.helper.PublishUtils;
3945
import com.ericsson.eiffel.remrem.publish.helper.RMQHelper;
4046
import com.ericsson.eiffel.remrem.publish.service.EventTemplateHandler;
47+
import com.ericsson.eiffel.remrem.publish.service.GenerateURLTemplate;
4148
import com.ericsson.eiffel.remrem.publish.service.MessageService;
4249
import com.ericsson.eiffel.remrem.publish.service.SendResult;
43-
import com.ericsson.eiffel.remrem.publish.service.GenerateURLTemplate;
44-
import com.ericsson.eiffel.remrem.publish.exception.RemRemPublishException;
4550
import com.fasterxml.jackson.databind.JsonNode;
4651
import com.google.gson.JsonElement;
4752
import com.google.gson.JsonObject;
@@ -73,6 +78,9 @@ public class ProducerController {
7378
@Autowired
7479
private GenerateURLTemplate generateURLTemplate;
7580

81+
@Value("${activedirectory.publish.enabled}")
82+
private boolean isAuthenticationEnabled;
83+
7684
private RestTemplate restTemplate = new RestTemplate();
7785

7886
private JsonParser parser = new JsonParser();
@@ -87,6 +95,18 @@ public void setRestTemplate(RestTemplate restTemplate) {
8795
this.restTemplate = restTemplate;
8896
}
8997

98+
public void logUserName() {
99+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
100+
// Check if the user is authenticated
101+
if (authentication != null && authentication.isAuthenticated()) {
102+
// Get the UserDetails object, which contains user information
103+
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
104+
// Get the username of the authenticated user
105+
String username = userDetails.getUsername();
106+
log.info("User name: {} ", username);
107+
}
108+
}
109+
90110
@SuppressWarnings({ "rawtypes", "unchecked" })
91111
@ApiOperation(value = "To publish eiffel event to message bus", response = String.class)
92112
@ApiResponses(value = { @ApiResponse(code = 200, message = "Event sent successfully"),
@@ -96,22 +116,30 @@ public void setRestTemplate(RestTemplate restTemplate) {
96116
@ApiResponse(code = 503, message = "Service Unavailable") })
97117
@RequestMapping(value = "/producer/msg", method = RequestMethod.POST)
98118
@ResponseBody
99-
public ResponseEntity send(@ApiParam(value = "message protocol", required = true) @RequestParam(value = "mp") final String msgProtocol,
100-
@ApiParam(value = "user domain") @RequestParam(value = "ud", required = false) final String userDomain,
101-
@ApiParam(value = "tag") @RequestParam(value = "tag", required = false) final String tag,
102-
@ApiParam(value = "routing key") @RequestParam(value = "rk", required = false) final String routingKey,
103-
@ApiParam(value = "eiffel event", required = true) @RequestBody final JsonElement body) {
119+
public ResponseEntity send(
120+
@ApiParam(value = "message protocol", required = true) @RequestParam(value = "mp") final String msgProtocol,
121+
@ApiParam(value = "user domain") @RequestParam(value = "ud", required = false) final String userDomain,
122+
@ApiParam(value = "tag") @RequestParam(value = "tag", required = false) final String tag,
123+
@ApiParam(value = "routing key") @RequestParam(value = "rk", required = false) final String routingKey,
124+
@ApiParam(value = "eiffel event", required = true) @RequestBody final JsonElement body) {
125+
if(isAuthenticationEnabled) {
126+
logUserName();
127+
}
128+
104129
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices);
105130
log.debug("mp: " + msgProtocol);
106131
log.debug("body: " + body);
107-
log.debug("user domain suffix: " + userDomain + " tag: " + tag + " Routing Key: " + routingKey);
132+
log.debug("user domain suffix: " + userDomain + " tag: " + tag + " Routing Key: "
133+
+ routingKey);
134+
108135
if (msgService != null && msgProtocol != null) {
109136
try {
110137
rmqHelper.rabbitMqPropertiesInit(msgProtocol);
111138
} catch (RemRemPublishException e) {
112139
return new ResponseEntity(e.getMessage(), HttpStatus.NOT_FOUND);
113140
}
114-
} synchronized(this) {
141+
}
142+
synchronized (this) {
115143
SendResult result = messageService.send(body, msgService, userDomain, tag, routingKey);
116144
log.info("HTTP Status: {}", messageService.getHttpStatus().value());
117145
return new ResponseEntity(result, messageService.getHttpStatus());
@@ -166,6 +194,9 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r
166194
+ "event fields from the input event data that does not validate successfully, "
167195
+ "and add those removed field information into customData/remremGenerateFailures") @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
168196
@ApiParam(value = "JSON message", required = true) @RequestBody final JsonObject bodyJson) {
197+
if (isAuthenticationEnabled) {
198+
logUserName();
199+
}
169200

170201
String bodyJsonOut = null;
171202
if(parseData) {

0 commit comments

Comments
 (0)