aboutsummaryrefslogtreecommitdiffstats
path: root/aai-resources/src/main/java/org/onap/aai/interceptors/pre/TwoWaySslAuthorization.java
blob: d4e8c4f64a224472a58aa08acac65542b90a5256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.interceptors.pre;

import java.io.IOException;
import java.security.cert.X509Certificate;
import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Priority;
import javax.security.auth.x500.X500Principal;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.onap.aai.auth.AAIAuthCore;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.interceptors.AAIContainerFilter;
import org.onap.aai.interceptors.AAIHeaderProperties;
import org.onap.aai.logging.ErrorLogHelper;
import org.onap.aai.restcore.HttpMethod;
import org.onap.aai.util.AAIConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

@PreMatching
@Priority(AAIRequestFilterPriority.AUTHORIZATION)
@Profile("two-way-ssl")
public class TwoWaySslAuthorization extends AAIContainerFilter implements ContainerRequestFilter {

	@Autowired
	private HttpServletRequest httpServletRequest;

	@Autowired
	private AAIAuthCore aaiAuthCore;

	@Override
	public void filter(ContainerRequestContext requestContext) {

		Optional<Response> oResp;

		String uri = requestContext.getUriInfo().getAbsolutePath().getPath();
		String httpMethod = getHttpMethod(requestContext);

		List<MediaType> acceptHeaderValues = requestContext.getAcceptableMediaTypes();

		Optional<String> authUser = getUser(this.httpServletRequest);

		if (authUser.isPresent()) {
			oResp = this.authorize(uri, httpMethod, acceptHeaderValues, authUser.get(),
					this.getHaProxyUser(this.httpServletRequest), getCertIssuer(this.httpServletRequest));
			if (oResp.isPresent()) {
				requestContext.abortWith(oResp.get());
				return;
			}
		} else {
			AAIException aaie = new AAIException("AAI_9107");
			requestContext
					.abortWith(Response
							.status(aaie.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper
									.getRESTAPIErrorResponseWithLogging(acceptHeaderValues, aaie, new ArrayList<>()))
							.build());
		}

	}

	private String getCertIssuer(HttpServletRequest hsr) {
		String issuer =  hsr.getHeader("X-AAI-SSL-Issuer");
		if (issuer != null && !issuer.isEmpty()) {
			// the haproxy header replaces the ', ' with '/' and reverses on the '/' need to undo that.
			List<String> broken = Arrays.asList(issuer.split("/"));
			broken = broken.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
			Collections.reverse(broken);
			issuer = String.join(", ", broken);
		} else {
			if (hsr.getAttribute("javax.servlet.request.cipher_suite") != null) {
				X509Certificate[] certChain = (X509Certificate[]) hsr.getAttribute("javax.servlet.request.X509Certificate");
				if (certChain != null && certChain.length > 0) {
					X509Certificate clientCert = certChain[0];
					issuer = clientCert.getIssuerX500Principal().getName();
				}
			}
		}
		return issuer;
	}

	private String getHttpMethod(ContainerRequestContext requestContext) {
		String httpMethod = requestContext.getMethod();
		if ("POST".equalsIgnoreCase(httpMethod)
				&& "PATCH".equals(requestContext.getHeaderString(AAIHeaderProperties.HTTP_METHOD_OVERRIDE))) {
			httpMethod = HttpMethod.MERGE_PATCH.toString();
		}
		if (httpMethod.equalsIgnoreCase(HttpMethod.MERGE_PATCH.toString()) || "patch".equalsIgnoreCase(httpMethod)) {
			httpMethod = HttpMethod.PUT.toString();
		}
		return httpMethod;
	}

	private Optional<String> getUser(HttpServletRequest hsr) {
		String authUser = null;
		if (hsr.getAttribute("javax.servlet.request.cipher_suite") != null) {
			X509Certificate[] certChain = (X509Certificate[]) hsr.getAttribute("javax.servlet.request.X509Certificate");

			/*
			 * If the certificate is null or the certificate chain length is zero Then
			 * retrieve the authorization in the request header Authorization Check that it
			 * is not null and that it starts with Basic and then strip the basic portion to
			 * get the base64 credentials Check if this is contained in the AAIBasicAuth
			 * Singleton class If it is, retrieve the username associated with that
			 * credentials and set to authUser Otherwise, get the principal from certificate
			 * and use that authUser
			 */

			if (certChain == null || certChain.length == 0) {

				String authorization = hsr.getHeader("Authorization");

				if (authorization != null && authorization.startsWith("Basic ")) {
					authUser = authorization.replace("Basic ", "");
				}

			} else {
				X509Certificate clientCert = certChain[0];
				X500Principal subjectDN = clientCert.getSubjectX500Principal();
				authUser = subjectDN.toString().toLowerCase();
			}
		}

		return Optional.ofNullable(authUser);
	}

	private String getHaProxyUser(HttpServletRequest hsr) {
		String haProxyUser;
		if (Objects.isNull(hsr.getHeader("X-AAI-SSL-Client-CN")) 
				|| Objects.isNull(hsr.getHeader("X-AAI-SSL-Client-OU"))
				|| Objects.isNull(hsr.getHeader("X-AAI-SSL-Client-O"))
				|| Objects.isNull(hsr.getHeader("X-AAI-SSL-Client-L"))
				|| Objects.isNull(hsr.getHeader("X-AAI-SSL-Client-ST"))
				|| Objects.isNull(hsr.getHeader("X-AAI-SSL-Client-C"))) {
			haProxyUser = "";
		} else {
			haProxyUser = String.format("CN=%s, OU=%s, O=\"%s\", L=%s, ST=%s, C=%s",
					Objects.toString(hsr.getHeader("X-AAI-SSL-Client-CN"), ""),
					Objects.toString(hsr.getHeader("X-AAI-SSL-Client-OU"), ""),
					Objects.toString(hsr.getHeader("X-AAI-SSL-Client-O"), ""),
					Objects.toString(hsr.getHeader("X-AAI-SSL-Client-L"), ""),
					Objects.toString(hsr.getHeader("X-AAI-SSL-Client-ST"), ""),
					Objects.toString(hsr.getHeader("X-AAI-SSL-Client-C"), "")).toLowerCase();
		}
		return haProxyUser;
	}

	private Optional<Response> authorize(String uri, String httpMethod, List<MediaType> acceptHeaderValues,
			String authUser, String haProxyUser, String issuer) {
		Response response = null;
		try {
			if (!aaiAuthCore.authorize(authUser, uri, httpMethod, haProxyUser, issuer)) {
				throw new AAIException("AAI_9101", "Request on " + httpMethod + " " + uri + " status is not OK");
			}
		} catch (AAIException e) {
			response = Response.status(e.getErrorObject().getHTTPResponseCode())
					.entity(ErrorLogHelper.getRESTAPIErrorResponseWithLogging(acceptHeaderValues, e, new ArrayList<>()))
					.build();
		}
		return Optional.ofNullable(response);
	}

}