Skip to content

Commit b3d9242

Browse files
Added new endpoints for checking status and security (#94)
1 parent 1f793f3 commit b3d9242

14 files changed

+204
-75
lines changed

src/main/java/com/ericsson/ei/EndpointSecurity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.slf4j.LoggerFactory;
2424
import org.springframework.beans.factory.annotation.Value;
2525
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.http.HttpMethod;
2627
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2728
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2829
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -35,7 +36,7 @@ public class EndpointSecurity extends WebSecurityConfigurerAdapter {
3536

3637
private static Logger LOGGER = (Logger) LoggerFactory.getLogger(EndpointSecurity.class);
3738

38-
@Value("${ldap.enabled}")
39+
@Value("${ldap.enabled:false}")
3940
private boolean ldapEnabled;
4041

4142
@Value("${ldap.url}")
@@ -59,10 +60,16 @@ protected void configure(HttpSecurity http) throws Exception {
5960
LOGGER.info("LDAP security configuration is enabled");
6061
http
6162
.authorizeRequests()
62-
.anyRequest().authenticated()
63+
.antMatchers("/auth/*").authenticated()
64+
.antMatchers(HttpMethod.POST, "/subscriptions").authenticated()
65+
.antMatchers(HttpMethod.PUT, "/subscriptions").authenticated()
66+
.antMatchers(HttpMethod.DELETE, "/subscriptions/*").authenticated()
67+
.anyRequest().permitAll()
6368
.and()
6469
.sessionManagement()
6570
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
71+
.and()
72+
.logout().logoutUrl("/auth/logout").logoutSuccessUrl("/").deleteCookies("SESSION").invalidateHttpSession(true)
6673
.and()
6774
.httpBasic()
6875
.and()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
package com.ericsson.ei.controller;
3+
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
10+
/**
11+
* No description
12+
* (Generated with springmvc-raml-parser v.0.10.11)
13+
*
14+
*/
15+
@RestController
16+
@RequestMapping(value = "/auth", produces = "application/json")
17+
public interface AuthController {
18+
19+
20+
/**
21+
* This call for checking if security is enabled
22+
*
23+
*/
24+
@RequestMapping(value = "", method = RequestMethod.GET)
25+
public ResponseEntity<?> getAuth();
26+
27+
/**
28+
* This call for getting logged in user
29+
*
30+
*/
31+
@RequestMapping(value = "/login", method = RequestMethod.GET)
32+
public ResponseEntity<?> getLogin();
33+
34+
/**
35+
* This call for checking backend status
36+
*
37+
*/
38+
@RequestMapping(value = "/checkStatus", method = RequestMethod.GET)
39+
public ResponseEntity<?> getCheckStatus();
40+
41+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2018 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.controller;
18+
19+
import io.swagger.annotations.Api;
20+
import io.swagger.annotations.ApiOperation;
21+
import org.json.JSONObject;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.ResponseEntity;
27+
import org.springframework.security.core.context.SecurityContextHolder;
28+
import org.springframework.stereotype.Component;
29+
import org.springframework.web.bind.annotation.CrossOrigin;
30+
31+
/**
32+
* Endpoints /auth/login and /auth/checkStatus should be secured in case LDAP is enabled
33+
* Endpoint /auth should be not secured
34+
*/
35+
@Component
36+
@CrossOrigin
37+
@Api(value = "Auth", description = "REST endpoints for authentication and authorization")
38+
public class AuthControllerImpl implements AuthController {
39+
40+
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(AuthControllerImpl.class);
41+
42+
@Value("${ldap.enabled:false}")
43+
private boolean ldapEnabled;
44+
45+
@Override
46+
@CrossOrigin
47+
@ApiOperation(value = "To check is security enabled", response = String.class)
48+
public ResponseEntity<?> getAuth() {
49+
try {
50+
return new ResponseEntity<>(new JSONObject().put("security", ldapEnabled).toString(), HttpStatus.OK);
51+
} catch (Exception e) {
52+
LOGGER.error(e.getMessage(), e);
53+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
54+
}
55+
}
56+
57+
@Override
58+
@CrossOrigin
59+
@ApiOperation(value = "To get login of current user", response = String.class)
60+
public ResponseEntity<?> getLogin() {
61+
try {
62+
String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
63+
return new ResponseEntity<>(new JSONObject().put("user", currentUser).toString(), HttpStatus.OK);
64+
} catch (Exception e) {
65+
LOGGER.error(e.getMessage(), e);
66+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
67+
}
68+
}
69+
70+
@Override
71+
@CrossOrigin
72+
@ApiOperation(value = "To check backend status", response = String.class)
73+
public ResponseEntity<?> getCheckStatus() {
74+
try {
75+
return new ResponseEntity<>(HttpStatus.OK);
76+
} catch (Exception e) {
77+
LOGGER.error(e.getMessage(), e);
78+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
79+
}
80+
}
81+
}

src/main/java/com/ericsson/ei/controller/LoginController.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/com/ericsson/ei/controller/LoginControllerImpl.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/main/java/com/ericsson/ei/controller/QueryAggregatedObjectController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.ericsson.ei.controller;
33

4+
import com.ericsson.ei.controller.model.QueryResponse;
45
import org.springframework.http.ResponseEntity;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RequestMethod;

src/main/java/com/ericsson/ei/controller/QueryMissedNotificationController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.ericsson.ei.controller;
33

4+
import com.ericsson.ei.controller.model.QueryResponse;
45
import org.springframework.http.ResponseEntity;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RequestMethod;

src/main/java/com/ericsson/ei/controller/RuleCheckController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.ericsson.ei.controller;
33

44
import javax.validation.Valid;
5+
import com.ericsson.ei.controller.model.RuleCheckBody;
56
import org.springframework.http.ResponseEntity;
67
import org.springframework.web.bind.annotation.RequestBody;
78
import org.springframework.web.bind.annotation.RequestMapping;

src/main/java/com/ericsson/ei/controller/SubscriptionController.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface SubscriptionController {
2626
*
2727
*/
2828
@RequestMapping(value = "", method = RequestMethod.GET)
29-
public ResponseEntity<List<String>> getSubscriptions();
29+
public ResponseEntity<List<com.ericsson.ei.controller.model.Subscription>> getSubscriptions();
3030

3131
/**
3232
* Takes the subscription rules, the name for subscription and the user name of the person registering this subscription and saves the subscription in subscription database. The name needs to be unique.
@@ -36,34 +36,34 @@ public interface SubscriptionController {
3636
public ResponseEntity<?> createSubscription(
3737
@Valid
3838
@RequestBody
39-
List<String> string);
39+
List<com.ericsson.ei.controller.model.Subscription> subscription);
4040

4141
/**
4242
* Modify an existing Subscription.
4343
*
4444
*/
4545
@RequestMapping(value = "", method = RequestMethod.PUT)
46-
public ResponseEntity<SubscriptionResponse> updateSubscriptions(
46+
public ResponseEntity<com.ericsson.ei.controller.model.SubscriptionResponse> updateSubscriptions(
4747
@Valid
4848
@RequestBody
49-
List<String> string);
49+
List<com.ericsson.ei.controller.model.Subscription> subscription);
5050

5151
/**
5252
* Returns the subscription rules for given subscription name.
5353
*
5454
*/
5555
@RequestMapping(value = "/{subscriptionName}", method = RequestMethod.GET)
56-
public ResponseEntity<List<String>> getSubscriptionById(
56+
public ResponseEntity<List<com.ericsson.ei.controller.model.Subscription>> getSubscriptionById(
5757
@PathVariable(required = false)
58-
java.lang.String subscriptionName);
58+
String subscriptionName);
5959

6060
/**
6161
* Removes the subscription from the database.
6262
*
6363
*/
6464
@RequestMapping(value = "/{subscriptionName}", method = RequestMethod.DELETE)
65-
public ResponseEntity<SubscriptionResponse> deleteSubscriptionById(
65+
public ResponseEntity<com.ericsson.ei.controller.model.SubscriptionResponse> deleteSubscriptionById(
6666
@PathVariable(required = false)
67-
java.lang.String subscriptionName);
67+
String subscriptionName);
6868

6969
}

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public ResponseEntity<SubscriptionResponse> updateSubscriptions(@RequestBody Lis
133133
user = currentUser();
134134
}
135135
Subscription subscription = subscriptions.get(0);
136+
subscription.setUserName(user);
136137
String subscriptionName = subscription.getSubscriptionName();
137138
LOG.info("Subscription :" + subscriptionName + " update started");
138139
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();

0 commit comments

Comments
 (0)