23ff0c6d40d5c0e817507da6632f11efaac114e0
[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 package org.onap.aai.aaf.filters;
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.apache.commons.io.IOUtils;
25 import org.onap.aai.aaf.auth.ResponseFormatter;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.context.annotation.Profile;
29 import org.springframework.stereotype.Component;
30
31 import javax.servlet.FilterChain;
32 import javax.servlet.ServletException;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 import java.io.IOException;
36 import java.nio.charset.StandardCharsets;
37 import java.util.regex.Pattern;
38
39 @Component
40 @Profile({
41     AafProfiles.AAF_CERT_AUTHENTICATION,
42     AafProfiles.AAF_AUTHENTICATION
43 })
44 public class GremlinFilter {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(GremlinFilter.class);
47
48     private static final String ADVANCED = "advanced";
49     private static final String BASIC = "basic";
50     private static final Pattern ECHO_ENDPOINT = Pattern.compile("^.*/util/echo$");
51
52     String type;
53
54     String instance;
55
56     private CadiProps cadiProps;
57
58     @Autowired
59     public GremlinFilter(
60         @Value("${permission.type}") String type,
61         @Value("${permission.instance}") String instance,
62         CadiProps cadiProps
63     ) {
64         this.type = type;
65         this.instance = instance;
66         this.cadiProps = cadiProps;
67     }
68
69     public void doBasicAuthFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
70         PayloadBufferingRequestWrapper requestBufferWrapper = new PayloadBufferingRequestWrapper(request);
71
72         if(ECHO_ENDPOINT.matcher(request.getRequestURI()).matches()){
73             filterChain.doFilter(requestBufferWrapper, response);
74         }
75
76         String payload = IOUtils.toString(requestBufferWrapper.getInputStream(), StandardCharsets.UTF_8.name());
77         boolean containsWordGremlin = payload.contains("\"gremlin\"");
78
79         //if the requestBufferWrapper contains the word "gremlin" it's an "advanced" query needing an "advanced" role
80         String permissionBasic = String.format("%s|%s|%s", type, instance, BASIC);
81         String permissionAdvanced = String.format("%s|%s|%s", type, instance, ADVANCED);
82
83         boolean isAuthorized;
84
85         if(containsWordGremlin){
86             isAuthorized = requestBufferWrapper.isUserInRole(permissionAdvanced);
87         }else{
88             isAuthorized = requestBufferWrapper.isUserInRole(permissionAdvanced) || requestBufferWrapper.isUserInRole(permissionBasic);
89         }
90
91         if(!isAuthorized){
92             String name = requestBufferWrapper.getUserPrincipal() != null ? requestBufferWrapper.getUserPrincipal().getName() : "unknown";
93             LOGGER.info("User " + name + " does not have a role for " + (containsWordGremlin ? "gremlin" : "non-gremlin") + " query" );
94             ResponseFormatter.errorResponse(request, response);
95         } else {
96             filterChain.doFilter(requestBufferWrapper,response);
97         }
98     }
99 }