Repository navigation
ATLAS-5430: Add SPIFFE header authentication for service-to-service calls. #786
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
145 changes: 145 additions & 0 deletions
145
common/src/main/java/org/apache/atlas/utils/SpiffeIdUtil.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,145 @@ | ||
| /** | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.atlas.utils; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Helper for validating a SPIFFE ID and extracting the service-account identity from it. | ||
| * | ||
| * <p>A SPIFFE ID is expected in the exact form {@code spiffe://<trust-domain>/ns/<namespace>/sa/<service-account>}; | ||
| * for service-to-service authentication the receiving server trusts the workload | ||
| * identity carried in a header (e.g. {@code x-awc-source-workload-id}) and uses | ||
| * the trailing service-account segment as the authenticated principal. | ||
| */ | ||
| public final class SpiffeIdUtil { | ||
| private static final String HEADER_NAMES_SEP = ","; | ||
|
|
||
| /** | ||
| * Matches {@code spiffe://<trust-domain>/ns/<namespace>/sa/<service-account>} using the character | ||
| * sets from the SPIFFE-ID specification: | ||
| * <ul> | ||
| * <li>trust-domain: lowercase letters, digits, {@code .}, {@code -}, {@code _} (the spec requires | ||
| * the trust domain to be lowercase); | ||
| * <li>namespace and service-account: ASCII letters (any case), digits, {@code .}, {@code -}, {@code _} | ||
| * (SPIFFE path segments are case-sensitive). | ||
| * </ul> | ||
| * All segments must be non-empty. This is an allow-list of safe characters, so whitespace, control | ||
| * characters and {@code /} are excluded, keeping the extracted principal well-formed. Per the spec, | ||
| * the namespace and service-account path segments must not be the relative modifiers {@code .} or | ||
| * {@code ..} (enforced by the leading negative look-aheads). The service-account is exposed via the | ||
| * named group {@code sa}. | ||
| */ | ||
| private static final Pattern SPIFFE_ID_PATTERN = Pattern.compile( | ||
| "^spiffe://[a-z0-9._-]+/ns/(?!\\.{1,2}/)[A-Za-z0-9._-]+/sa/(?<sa>(?!\\.{1,2}$)[A-Za-z0-9._-]+)$"); | ||
|
|
||
| private SpiffeIdUtil() { | ||
| // to block instantiation | ||
| } | ||
|
|
||
| /** | ||
| * Parses SPIFFE header names from {@link org.apache.commons.configuration2.Configuration#getStringArray(String)}. | ||
| * Atlas {@link org.apache.atlas.ApplicationProperties} uses a comma list delimiter, so a property such as | ||
| * {@code x-source, x-upstream} must be read with {@code getStringArray}; {@code getString} returns only the | ||
| * first element. | ||
| * | ||
| * @param configValues values from configuration (may be {@code null} or empty) | ||
| * @return an ordered, immutable list of trimmed, non-empty header names | ||
| */ | ||
| public static List<String> parseHeaderNames(String[] configValues) { | ||
| if (configValues == null || configValues.length == 0) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| List<String> ret = new ArrayList<>(); | ||
|
|
||
| for (String configValue : configValues) { | ||
| ret.addAll(parseHeaderNamesFromSingleConfigValue(configValue)); | ||
| } | ||
|
|
||
| return Collections.unmodifiableList(ret); | ||
| } | ||
|
|
||
| /** | ||
| * Parses one configuration entry that may itself contain comma-separated header names (for tests and | ||
| * programmatic use). Prefer {@link #parseHeaderNames(String[])} with {@code getStringArray} at runtime. | ||
| */ | ||
| public static List<String> parseHeaderNames(String configValue) { | ||
| return parseHeaderNamesFromSingleConfigValue(configValue); | ||
| } | ||
|
|
||
| private static List<String> parseHeaderNamesFromSingleConfigValue(String configValue) { | ||
| if (StringUtils.isBlank(configValue)) { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| List<String> ret = new ArrayList<>(); | ||
|
|
||
| for (String name : configValue.split(HEADER_NAMES_SEP)) { | ||
| String trimmed = StringUtils.trimToNull(name); | ||
|
|
||
| if (trimmed != null) { | ||
| ret.add(trimmed); | ||
| } | ||
| } | ||
|
|
||
| return Collections.unmodifiableList(ret); | ||
| } | ||
|
|
||
| /** | ||
| * Validates that the given value is a well-formed SPIFFE ID in the exact form | ||
| * {@code spiffe://<trust-domain>/ns/<namespace>/sa/<service-account>} with non-empty segments. | ||
| * | ||
| * @param spiffeId the raw SPIFFE ID value from the request header | ||
| * @return {@code true} if the value matches the expected SPIFFE ID format; {@code false} otherwise | ||
| */ | ||
| public static boolean isValidSpiffeId(String spiffeId) { | ||
| return matchSpiffeId(spiffeId) != null; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the service-account name from a SPIFFE ID such as | ||
| * {@code spiffe://my-cluster/ns/service-namespace/sa/service-sa}. The value must match the exact | ||
| * SPIFFE ID format (see {@link #isValidSpiffeId(String)}); otherwise {@code null} is returned. | ||
| * | ||
| * @param spiffeId the raw SPIFFE ID value from the request header | ||
| * @return the service-account segment (e.g. {@code service-sa}), or | ||
| * {@code null} if the input is blank or not a well-formed SPIFFE ID | ||
| */ | ||
| public static String extractServiceAccount(String spiffeId) { | ||
| Matcher matcher = matchSpiffeId(spiffeId); | ||
|
|
||
| return matcher != null ? matcher.group("sa") : null; | ||
| } | ||
|
|
||
| private static Matcher matchSpiffeId(String spiffeId) { | ||
| if (StringUtils.isBlank(spiffeId)) { | ||
| return null; | ||
| } | ||
|
|
||
| Matcher matcher = SPIFFE_ID_PATTERN.matcher(spiffeId.trim()); | ||
|
|
||
| return matcher.matches() ? matcher : null; | ||
| } | ||
| } | ||
105 changes: 105 additions & 0 deletions
105
common/src/test/java/org/apache/atlas/utils/SpiffeIdUtilTest.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,105 @@ | ||
| /** | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.atlas.utils; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertFalse; | ||
| import static org.testng.Assert.assertNull; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
| public class SpiffeIdUtilTest { | ||
| private static final String VALID = "spiffe://my-cluster/ns/service-namespace/sa/service-sa"; | ||
|
|
||
| @Test | ||
| public void isValidSpiffeId_acceptsExactFormat() { | ||
| assertTrue(SpiffeIdUtil.isValidSpiffeId(VALID)); | ||
| assertTrue(SpiffeIdUtil.isValidSpiffeId(" " + VALID + " ")); | ||
| } | ||
|
|
||
| @Test | ||
| public void isValidSpiffeId_rejectsMalformed() { | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId(null)); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId(" ")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("not-a-spiffe-id")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("https://my-cluster/ns/n/sa/s")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("spiffe://my-cluster/sa/service-sa")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("spiffe://my-cluster/ns/n")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("spiffe://my-cluster/ns//sa/s")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("spiffe:///ns/n/sa/s")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("spiffe://my-cluster/ns/n/sa/")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId(VALID + "/")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId(VALID + "/extra")); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId("spiffe://my-cluster/sa/s/ns/n")); | ||
| } | ||
|
|
||
| @Test | ||
| public void extractServiceAccount_returnsSaOnlyForValidId() { | ||
| assertEquals(SpiffeIdUtil.extractServiceAccount(VALID), "service-sa"); | ||
| assertEquals(SpiffeIdUtil.extractServiceAccount(" " + VALID + " "), "service-sa"); | ||
| } | ||
|
|
||
| @Test | ||
| public void extractServiceAccount_returnsNullForMalformedId() { | ||
| assertNull(SpiffeIdUtil.extractServiceAccount(null)); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount("")); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount("not-a-spiffe-id")); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount("spiffe://my-cluster/sa/service-sa")); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount("spiffe://my-cluster/ns/n/sa/")); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount(VALID + "/extra")); | ||
| } | ||
|
|
||
| @Test | ||
| public void rejectsSpecValidButNonConformingSpiffeId() { | ||
| String workloadPath = "spiffe://example.org/workload/frontend"; | ||
| String nsOnly = "spiffe://example.org/ns/prod"; | ||
|
|
||
| assertFalse(SpiffeIdUtil.isValidSpiffeId(workloadPath)); | ||
| assertFalse(SpiffeIdUtil.isValidSpiffeId(nsOnly)); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount(workloadPath)); | ||
| assertNull(SpiffeIdUtil.extractServiceAccount(nsOnly)); | ||
| } | ||
|
|
||
| @Test | ||
| public void parseHeaderNames_handlesSingleAndMultiple() { | ||
| assertTrue(SpiffeIdUtil.parseHeaderNames((String) null).isEmpty()); | ||
| assertTrue(SpiffeIdUtil.parseHeaderNames(" ").isEmpty()); | ||
| assertEquals(SpiffeIdUtil.parseHeaderNames("x-awc-source-workload-id"), | ||
| Collections.singletonList("x-awc-source-workload-id")); | ||
|
|
||
| List<String> names = SpiffeIdUtil.parseHeaderNames(" x-source , x-upstream ,"); | ||
|
|
||
| assertEquals(names, Arrays.asList("x-source", "x-upstream")); | ||
| } | ||
|
|
||
| @Test | ||
| public void parseHeaderNames_fromStringArray_matchesGetStringArraySemantics() { | ||
| assertTrue(SpiffeIdUtil.parseHeaderNames((String[]) null).isEmpty()); | ||
| assertTrue(SpiffeIdUtil.parseHeaderNames(new String[0]).isEmpty()); | ||
|
|
||
| List<String> names = SpiffeIdUtil.parseHeaderNames(new String[] {"x-source", "x-upstream"}); | ||
|
|
||
| assertEquals(names, Arrays.asList("x-source", "x-upstream")); | ||
| } | ||
| } |
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
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
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.