Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-aaf-auth / src / main / java / org / onap / aai / aaf / filters / GremlinFilter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.aaf.filters;
22
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.regex.Pattern;
26
27 import javax.servlet.FilterChain;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.apache.commons.io.IOUtils;
33 import org.onap.aai.aaf.auth.ResponseFormatter;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 @Profile({AafProfiles.AAF_CERT_AUTHENTICATION, AafProfiles.AAF_AUTHENTICATION})
43 public class GremlinFilter {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(GremlinFilter.class);
46
47     private static final String ADVANCED = "advanced";
48     private static final String BASIC = "basic";
49     private static final Pattern ECHO_ENDPOINT = Pattern.compile("^.*/util/echo$");
50
51     String type;
52
53     String instance;
54
55     private CadiProps cadiProps;
56
57     @Autowired
58     public GremlinFilter(@Value("${permission.type}") String type, @Value("${permission.instance}") String instance,
59             CadiProps cadiProps) {
60         this.type = type;
61         this.instance = instance;
62         this.cadiProps = cadiProps;
63     }
64
65     public void doBasicAuthFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
66             throws IOException, ServletException {
67         PayloadBufferingRequestWrapper requestBufferWrapper = new PayloadBufferingRequestWrapper(request);
68
69         if (ECHO_ENDPOINT.matcher(request.getRequestURI()).matches()) {
70             filterChain.doFilter(requestBufferWrapper, response);
71         }
72
73         String payload = IOUtils.toString(requestBufferWrapper.getInputStream(), StandardCharsets.UTF_8.name());
74         boolean containsWordGremlin = payload.contains("\"gremlin\"");
75
76         // if the requestBufferWrapper contains the word "gremlin" it's an "advanced" query needing an "advanced" role
77         String permissionBasic = String.format("%s|%s|%s", type, instance, BASIC);
78         String permissionAdvanced = String.format("%s|%s|%s", type, instance, ADVANCED);
79
80         boolean isAuthorized;
81
82         if (containsWordGremlin) {
83             isAuthorized = requestBufferWrapper.isUserInRole(permissionAdvanced);
84         } else {
85             isAuthorized = requestBufferWrapper.isUserInRole(permissionAdvanced)
86                     || requestBufferWrapper.isUserInRole(permissionBasic);
87         }
88
89         if (!isAuthorized) {
90             String name =
91                     requestBufferWrapper.getUserPrincipal() != null ? requestBufferWrapper.getUserPrincipal().getName()
92                             : "unknown";
93             LOGGER.info("User " + name + " does not have a role for "
94                     + (containsWordGremlin ? "gremlin" : "non-gremlin") + " query");
95             ResponseFormatter.errorResponse(request, response);
96         } else {
97             filterChain.doFilter(requestBufferWrapper, response);
98         }
99     }
100 }