2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.aaf.filters;
22 import java.io.IOException;
23 import java.nio.charset.StandardCharsets;
24 import java.util.regex.Pattern;
26 import javax.servlet.FilterChain;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.apache.commons.io.IOUtils;
32 import org.onap.aai.aaf.auth.ResponseFormatter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.context.annotation.Profile;
38 import org.springframework.stereotype.Component;
42 AafProfiles.AAF_CERT_AUTHENTICATION,
43 AafProfiles.AAF_AUTHENTICATION
45 public class GremlinFilter {
47 private static final Logger LOGGER = LoggerFactory.getLogger(GremlinFilter.class);
49 private static final String ADVANCED = "advanced";
50 private static final String BASIC = "basic";
51 private static final Pattern ECHO_ENDPOINT = Pattern.compile("^.*/util/echo$");
57 private CadiProps cadiProps;
61 @Value("${permission.type}") String type,
62 @Value("${permission.instance}") String instance,
66 this.instance = instance;
67 this.cadiProps = cadiProps;
70 public void doBasicAuthFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
71 PayloadBufferingRequestWrapper requestBufferWrapper = new PayloadBufferingRequestWrapper(request);
73 if(ECHO_ENDPOINT.matcher(request.getRequestURI()).matches()){
74 filterChain.doFilter(requestBufferWrapper, response);
77 String payload = IOUtils.toString(requestBufferWrapper.getInputStream(), StandardCharsets.UTF_8.name());
78 boolean containsWordGremlin = payload.contains("\"gremlin\"");
80 //if the requestBufferWrapper contains the word "gremlin" it's an "advanced" query needing an "advanced" role
81 String permissionBasic = String.format("%s|%s|%s", type, instance, BASIC);
82 String permissionAdvanced = String.format("%s|%s|%s", type, instance, ADVANCED);
86 if(containsWordGremlin){
87 isAuthorized = requestBufferWrapper.isUserInRole(permissionAdvanced);
89 isAuthorized = requestBufferWrapper.isUserInRole(permissionAdvanced) || requestBufferWrapper.isUserInRole(permissionBasic);
93 String name = requestBufferWrapper.getUserPrincipal() != null ? requestBufferWrapper.getUserPrincipal().getName() : "unknown";
94 LOGGER.info("User " + name + " does not have a role for " + (containsWordGremlin ? "gremlin" : "non-gremlin") + " query" );
95 ResponseFormatter.errorResponse(request, response);
97 filterChain.doFilter(requestBufferWrapper,response);