-
Notifications
You must be signed in to change notification settings - Fork 1.1k
RANGER-5723: Plugin SPIFFE outbound auth for audit-server destination #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ramackri
wants to merge
5
commits into
apache:master
Choose a base branch
from
ramackri:RANGER-5723-patch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
143fa62
RANGER-5723: Plugin SPIFFE outbound auth for audit-server destination
dff71f0
RANGER-5723: Remove unused SPIFFE prefix-discovery helpers
3380ce0
Merge branch 'master' into RANGER-5723-patch
242d31b
RANGER-5723: Address PR review comments for SPIFFE outbound auth
ramackri fc9ce57
RANGER-5723: Fix Checkstyle import order in TestRangerRESTClient
ramackri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...r/src/test/java/org/apache/ranger/audit/destination/RangerAuditServerDestinationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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. | ||
| */ | ||
|
|
||
| package org.apache.ranger.audit.destination; | ||
|
|
||
| import org.apache.ranger.plugin.util.PluginHeaderAuthConfig; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Properties; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class RangerAuditServerDestinationTest { | ||
| private static final String AUDIT_DEST_PREFIX = "xasecure.audit.destination.auditserver"; | ||
|
|
||
| @Test | ||
| public void buildSpiffeAuthHeadersUsesAuditDestinationPrefix() { | ||
| Properties props = new Properties(); | ||
| props.setProperty(AUDIT_DEST_PREFIX + ".authn.header.enabled", "true"); | ||
| props.setProperty(AUDIT_DEST_PREFIX + ".authn.header.spiffe", "X-Spiffe-Id"); | ||
| props.setProperty(AUDIT_DEST_PREFIX + ".authn.spiffe.value", | ||
| "spiffe://example.com/ns/default/sa/hive"); | ||
|
|
||
| Map<String, String> headers = PluginHeaderAuthConfig.buildSpiffeAuthHeaders(props, AUDIT_DEST_PREFIX); | ||
|
|
||
| assertEquals(1, headers.size()); | ||
| assertEquals("spiffe://example.com/ns/default/sa/hive", headers.get("X-Spiffe-Id")); | ||
| } | ||
|
|
||
| @Test | ||
| public void buildSpiffeAuthHeadersEmptyWhenAuditDestinationDisabled() { | ||
| Properties props = new Properties(); | ||
| props.setProperty(AUDIT_DEST_PREFIX + ".authn.header.enabled", "false"); | ||
| props.setProperty(AUDIT_DEST_PREFIX + ".authn.spiffe.value", | ||
| "spiffe://example.com/ns/default/sa/hive"); | ||
|
|
||
| Map<String, String> headers = PluginHeaderAuthConfig.buildSpiffeAuthHeaders(props, AUDIT_DEST_PREFIX); | ||
|
|
||
| assertTrue(headers.isEmpty()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
common-utils/src/main/java/org/apache/ranger/plugin/util/PluginHeaderAuthConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.ranger.plugin.util; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
|
|
||
| /** | ||
| * Outbound trusted-header auth for audit-server and other REST clients. | ||
| * | ||
| * <p>Properties are read under a caller-supplied prefix (audit destination example): | ||
| * <pre> | ||
| * xasecure.audit.destination.auditserver.authn.header.enabled=true | ||
| * xasecure.audit.destination.auditserver.authn.header.spiffe=X-Spiffe-Id | ||
| * </pre> | ||
| * SPIFFE ID value is resolved via {@link SpiffeIdentityResolver} under the same | ||
| * prefix (explicit value, identity file, or {@code SPIFFE_ID} environment variable). | ||
| */ | ||
| public final class PluginHeaderAuthConfig { | ||
| public static final String PROP_HEADER_AUTH_ENABLED = "authn.header.enabled"; | ||
| public static final String PROP_HEADER_SPIFFE = "authn.header.spiffe"; | ||
| public static final String DEFAULT_SPIFFE_HEADER_NAME = "X-Spiffe-Id"; | ||
|
|
||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(PluginHeaderAuthConfig.class); | ||
|
|
||
| private PluginHeaderAuthConfig() { | ||
| // to block instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether trusted header auth is enabled for the given config prefix. | ||
| * | ||
| * @param props plugin or site configuration properties | ||
| * @param configPrefix property prefix for header-auth settings | ||
| * @return {@code true} when header auth is enabled | ||
| */ | ||
| public static boolean isHeaderAuthEnabled(final Properties props, | ||
| final String configPrefix) { | ||
| if (props == null || StringUtils.isBlank(configPrefix)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Boolean.parseBoolean( | ||
| props.getProperty(configPrefix + "." + PROP_HEADER_AUTH_ENABLED, | ||
| "false")); | ||
| } | ||
|
|
||
| /** | ||
| * Builds SPIFFE header(s) for outbound REST calls when header auth is enabled. | ||
| * | ||
| * @param props plugin or site configuration properties | ||
| * @param configPrefix prefix such as {@code xasecure.audit.destination.auditserver} | ||
| * @return immutable header map; empty when auth is disabled or misconfigured | ||
| */ | ||
| public static Map<String, String> buildSpiffeAuthHeaders(final Properties props, | ||
| final String configPrefix) { | ||
| if (!isHeaderAuthEnabled(props, configPrefix)) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| List<String> headerNames = SpiffeIdUtil.parseHeaderNames( | ||
| resolveSpiffeHeaderName(props, configPrefix)); | ||
| String spiffeId = SpiffeIdentityResolver.resolve(props, configPrefix); | ||
|
|
||
| if (headerNames.isEmpty()) { | ||
| LOG.warn("Plugin header auth enabled for {} but no SPIFFE header " | ||
| + "name is configured", configPrefix); | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| if (StringUtils.isBlank(spiffeId)) { | ||
| LOG.warn("Plugin header auth enabled for {} but no SPIFFE ID could " | ||
|
ramackri marked this conversation as resolved.
|
||
| + "be resolved", configPrefix); | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| if (!SpiffeIdUtil.isValidSpiffeId(spiffeId)) { | ||
| LOG.warn("Resolved SPIFFE ID for {} is not well-formed", configPrefix); | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| Map<String, String> headers = new LinkedHashMap<>(); | ||
|
|
||
| for (String headerName : headerNames) { | ||
| headers.put(headerName, spiffeId); | ||
| } | ||
|
|
||
| return Collections.unmodifiableMap(headers); | ||
| } | ||
|
|
||
| private static String resolveSpiffeHeaderName(final Properties props, | ||
| final String configPrefix) { | ||
| String headerName = props != null | ||
| ? StringUtils.trimToNull( | ||
| props.getProperty(configPrefix + "." + PROP_HEADER_SPIFFE)) | ||
| : null; | ||
|
|
||
| return headerName != null ? headerName : DEFAULT_SPIFFE_HEADER_NAME; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.