|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.ranger.audit.security; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.apache.ranger.audit.server.AuditServerConfig; |
| 24 | +import org.apache.ranger.audit.server.AuditServerConstants; |
| 25 | +import org.apache.ranger.plugin.util.SpiffeIdUtil; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 29 | +import org.springframework.security.core.Authentication; |
| 30 | +import org.springframework.security.core.GrantedAuthority; |
| 31 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 32 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 33 | +import org.springframework.security.core.userdetails.User; |
| 34 | +import org.springframework.security.core.userdetails.UserDetails; |
| 35 | +import org.springframework.security.web.authentication.WebAuthenticationDetails; |
| 36 | +import org.springframework.web.filter.GenericFilterBean; |
| 37 | + |
| 38 | +import javax.annotation.PostConstruct; |
| 39 | +import javax.servlet.FilterChain; |
| 40 | +import javax.servlet.ServletException; |
| 41 | +import javax.servlet.ServletRequest; |
| 42 | +import javax.servlet.ServletResponse; |
| 43 | +import javax.servlet.http.HttpServletRequest; |
| 44 | + |
| 45 | +import java.io.IOException; |
| 46 | +import java.util.Collections; |
| 47 | +import java.util.List; |
| 48 | + |
| 49 | +/** |
| 50 | + * Authenticates audit REST requests using identity headers set by a trusted proxy. The resolved |
| 51 | + * principal is still subject to the per-service allowed users check in the audit REST API. |
| 52 | + */ |
| 53 | +public class AuditHeaderPreAuthFilter extends GenericFilterBean { |
| 54 | + private static final Logger LOG = LoggerFactory.getLogger(AuditHeaderPreAuthFilter.class); |
| 55 | + |
| 56 | + public static final String PROP_HEADER_AUTH_ENABLED = AuditServerConstants.PROP_PREFIX_AUDIT_SERVER + "authn.header.enabled"; |
| 57 | + public static final String PROP_USERNAME_HEADER_NAME = AuditServerConstants.PROP_PREFIX_AUDIT_SERVER + "authn.header.username"; |
| 58 | + public static final String PROP_SPIFFE_HEADER_NAME = AuditServerConstants.PROP_PREFIX_AUDIT_SERVER + "authn.header.spiffe"; |
| 59 | + |
| 60 | + private static final String DEFAULT_AUDIT_ROLE = "ROLE_USER"; |
| 61 | + |
| 62 | + private boolean headerAuthEnabled; |
| 63 | + private String userNameHeaderName; |
| 64 | + private List<String> spiffeHeaderNames; |
| 65 | + |
| 66 | + @PostConstruct |
| 67 | + protected void initialize() { |
| 68 | + AuditServerConfig auditConfig = AuditServerConfig.getInstance(); |
| 69 | + |
| 70 | + headerAuthEnabled = auditConfig.getBoolean(PROP_HEADER_AUTH_ENABLED, false); |
| 71 | + |
| 72 | + if (headerAuthEnabled) { |
| 73 | + userNameHeaderName = StringUtils.trimToNull(auditConfig.get(PROP_USERNAME_HEADER_NAME)); |
| 74 | + spiffeHeaderNames = SpiffeIdUtil.parseHeaderNames(auditConfig.get(PROP_SPIFFE_HEADER_NAME)); |
| 75 | + |
| 76 | + if (userNameHeaderName == null && spiffeHeaderNames.isEmpty()) { |
| 77 | + LOG.warn("Disabling header-based authentication, as neither {} nor {} is set", PROP_USERNAME_HEADER_NAME, PROP_SPIFFE_HEADER_NAME); |
| 78 | + |
| 79 | + headerAuthEnabled = false; |
| 80 | + } else { |
| 81 | + LOG.info("Header-based authentication is enabled: usernameHeader={}, spiffeHeaders={}", userNameHeaderName, spiffeHeaderNames); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
| 88 | + if (headerAuthEnabled) { |
| 89 | + Authentication existingAuthn = SecurityContextHolder.getContext().getAuthentication(); |
| 90 | + |
| 91 | + if (existingAuthn == null || !existingAuthn.isAuthenticated()) { |
| 92 | + HttpServletRequest httpRequest = (HttpServletRequest) request; |
| 93 | + String username = resolvePrincipal(httpRequest); |
| 94 | + |
| 95 | + if (username != null) { |
| 96 | + List<GrantedAuthority> grantedAuths = Collections.singletonList(new SimpleGrantedAuthority(DEFAULT_AUDIT_ROLE)); |
| 97 | + UserDetails principal = new User(username, "", grantedAuths); |
| 98 | + UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(principal, "", grantedAuths); |
| 99 | + |
| 100 | + authToken.setDetails(new WebAuthenticationDetails(httpRequest)); |
| 101 | + |
| 102 | + SecurityContextHolder.getContext().setAuthentication(authToken); |
| 103 | + |
| 104 | + LOG.debug("Authenticated request using trusted headers for user={}", username); |
| 105 | + } else { |
| 106 | + LOG.debug("No trusted identity header found in the request!"); |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + LOG.debug("Header-based authentication is disabled!"); |
| 111 | + } |
| 112 | + |
| 113 | + chain.doFilter(request, response); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Resolves the principal from trusted headers. The username header (user identity) takes |
| 118 | + * precedence; when it is absent, the SPIFFE header (service identity) is used and the |
| 119 | + * full SPIFFE ID becomes the principal (SPIFFE IDs are used as usernames in Ranger). |
| 120 | + */ |
| 121 | + private String resolvePrincipal(HttpServletRequest httpRequest) { |
| 122 | + String ret = userNameHeaderName != null ? StringUtils.trimToNull(httpRequest.getHeader(userNameHeaderName)) : null; |
| 123 | + |
| 124 | + if (ret == null) { |
| 125 | + for (String spiffeHeaderName : spiffeHeaderNames) { |
| 126 | + String spiffeId = StringUtils.trimToNull(httpRequest.getHeader(spiffeHeaderName)); |
| 127 | + |
| 128 | + if (SpiffeIdUtil.isValidSpiffeId(spiffeId)) { |
| 129 | + LOG.debug("Resolved SPIFFE ID '{}' from header '{}'", spiffeId, spiffeHeaderName); |
| 130 | + |
| 131 | + ret = spiffeId; |
| 132 | + |
| 133 | + break; |
| 134 | + } else if (spiffeId != null) { |
| 135 | + LOG.warn("SPIFFE header '{}' value is not a well-formed SPIFFE ID", spiffeHeaderName); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return ret; |
| 141 | + } |
| 142 | +} |
0 commit comments