chaitalicod commented on code in PR #786: URL: https://github.com/apache/atlas/pull/786#discussion_r4296572189
########## common/src/main/java/org/apache/atlas/utils/SpiffeIdUtil.java: ########## @@ -0,0 +1,121 @@ +/** + * 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 a configured, comma-separated list of SPIFFE header names into an ordered list of + * trimmed, non-empty names. Supports a single header name as a list of length one. + * + * @param configValue the raw config value (e.g. {@code x-awc-source-workload-id}) + * @return an ordered, immutable list of header names; empty when the input is blank + */ + public static List<String> parseHeaderNames(String configValue) { Review Comment: A comma-separated list of SPIFFE headers silently uses only the first one. parseHeaderNames(configuration.getString(PROP_SPIFFE_HEADER, "")) can't work for more than one header. ApplicationProperties uses DefaultListDelimiterHandler(','), so the value is stored as a list and getString returns just the first element. getString -> [x-source] getStringArray -> [x-source, x-upstream] -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
