XJDKC commented on code in PR #2523: URL: https://github.com/apache/polaris/pull/2523#discussion_r2402418359
########## runtime/service/src/main/java/org/apache/polaris/service/identity/provider/DefaultServiceIdentityProvider.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.polaris.service.identity.provider; + +import com.google.common.annotations.VisibleForTesting; +import jakarta.annotation.Nonnull; +import jakarta.inject.Inject; +import java.util.Map; +import java.util.Optional; +import org.apache.polaris.core.admin.model.AuthenticationParameters; +import org.apache.polaris.core.admin.model.ConnectionConfigInfo; +import org.apache.polaris.core.admin.model.ServiceIdentityInfo; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.identity.ServiceIdentityType; +import org.apache.polaris.core.identity.credential.ServiceIdentityCredential; +import org.apache.polaris.core.identity.dpo.AwsIamServiceIdentityInfoDpo; +import org.apache.polaris.core.identity.dpo.ServiceIdentityInfoDpo; +import org.apache.polaris.core.identity.provider.ServiceIdentityProvider; +import org.apache.polaris.core.secrets.SecretReference; +import org.apache.polaris.service.identity.RealmServiceIdentityConfiguration; +import org.apache.polaris.service.identity.ResolvableServiceIdentityConfiguration; +import org.apache.polaris.service.identity.ServiceIdentityConfiguration; + +/** + * Default implementation of {@link ServiceIdentityProvider} that provides service identity + * credentials from statically configured values. + * + * <p>This implementation loads service identity configurations at startup and uses them to provide + * identity information and credentials on demand. All resolution is done lazily - credentials are + * only created when actually needed for authentication. + */ +public class DefaultServiceIdentityProvider implements ServiceIdentityProvider { + public static final String DEFAULT_REALM_KEY = ServiceIdentityConfiguration.DEFAULT_REALM_KEY; + public static final String DEFAULT_REALM_NSS = "system:default"; + private static final String IDENTITY_INFO_REFERENCE_URN_FORMAT = + "urn:polaris-secret:default-identity-provider:%s:%s"; + + private final String realm; + private final RealmServiceIdentityConfiguration config; + + public DefaultServiceIdentityProvider() { + this.realm = DEFAULT_REALM_KEY; + this.config = null; + } + + @Inject + public DefaultServiceIdentityProvider( + RealmContext realmContext, ServiceIdentityConfiguration serviceIdentityConfiguration) { + ServiceIdentityConfiguration.RealmConfigEntry entry = + serviceIdentityConfiguration.forRealm(realmContext); + this.realm = entry.realm(); + this.config = entry.config(); + } + + @Override + public Optional<ServiceIdentityInfoDpo> allocateServiceIdentity( + @Nonnull ConnectionConfigInfo connectionConfig) { + if (config == null || connectionConfig.getAuthenticationParameters() == null) { + return Optional.empty(); + } + + AuthenticationParameters.AuthenticationTypeEnum authType = + connectionConfig.getAuthenticationParameters().getAuthenticationType(); + + // Map authentication type to service identity type and check if configured + return switch (authType) { + case SIGV4 -> + config.awsIamServiceIdentity().isPresent() + ? Optional.of( + new AwsIamServiceIdentityInfoDpo( + buildIdentityInfoReference(realm, ServiceIdentityType.AWS_IAM))) + : Optional.empty(); + default -> Optional.empty(); + }; + } + + @Override + public Optional<ServiceIdentityInfo> getServiceIdentityInfo( + @Nonnull ServiceIdentityInfoDpo serviceIdentityInfo) { + if (config == null) { + return Optional.empty(); + } + + // Find the configuration matching the reference and return metadata only + SecretReference actualRef = serviceIdentityInfo.getIdentityInfoReference(); + + return config.serviceIdentityConfigurations().stream() + .filter( + identityConfig -> + buildIdentityInfoReference(realm, identityConfig.getType()).equals(actualRef)) Review Comment: > identities do not have to be 1:1 with secrets. It's used to map a connection config to a service identity. For a single service identity, there can be many secrets/credentials. For example, the same IAM user (same ARN) could have 100 different credentials. Polaris Vendor can provide a custom `ServiceIdentityProvider` that implements this 1:n mappings (i.e., given a reference, find all the available credentials, then pick one to return to assume the role) -- 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]
