FANNG1 commented on code in PR #6021:
URL: https://github.com/apache/gravitino/pull/6021#discussion_r1898550224
##########
core/src/main/java/org/apache/gravitino/credential/CatalogCredentialManager.java:
##########
@@ -51,6 +51,16 @@ public Credential getCredential(String credentialType,
CredentialContext context
return credentialCache.getCredential(credentialCacheKey, cacheKey ->
doGetCredential(cacheKey));
}
+ // Get credential with only one credential provider.
+ public Credential getCredential(CredentialContext context) {
+ if (credentialProviders.size() == 0) {
+ throw new RuntimeException("There are not any credential provider for
the catalog.");
+ } else if (credentialProviders.size() > 1) {
+ throw new RuntimeException("There are multiple credential providers for
the catalog.");
+ }
+ return getCredential(credentialProviders.keySet().iterator().next(),
context);
Review Comment:
ok
##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/CatalogWrapperForREST.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.gravitino.iceberg.service;
+
+import com.google.common.collect.ImmutableSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;
+import org.apache.gravitino.credential.CatalogCredentialManager;
+import org.apache.gravitino.credential.Credential;
+import org.apache.gravitino.credential.CredentialConstants;
+import org.apache.gravitino.credential.CredentialPropertyUtils;
+import org.apache.gravitino.credential.PathBasedCredentialContext;
+import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper;
+import org.apache.gravitino.utils.MapUtils;
+import org.apache.gravitino.utils.PrincipalUtils;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.ServiceUnavailableException;
+import org.apache.iceberg.rest.requests.CreateTableRequest;
+import org.apache.iceberg.rest.responses.LoadTableResponse;
+
+/** Process Iceberg REST specific operations, like credential vending. */
+public class CatalogWrapperForREST extends IcebergCatalogWrapper {
+
+ private final CatalogCredentialManager catalogCredentialManager;
+
+ private final Map<String, String> catalogConfigToClients;
+
+ private static final Set<String> catalogPropertiesToClientKeys =
+ ImmutableSet.of(
+ IcebergConstants.IO_IMPL,
+ IcebergConstants.AWS_S3_REGION,
+ IcebergConstants.ICEBERG_S3_ENDPOINT,
+ IcebergConstants.ICEBERG_OSS_ENDPOINT);
+
+ public CatalogWrapperForREST(String catalogName, IcebergConfig config) {
+ super(config);
+ this.catalogConfigToClients =
+ MapUtils.getFilteredMap(
+ config.getIcebergCatalogProperties(),
+ key -> catalogPropertiesToClientKeys.contains(key));
+ // To compatibility with old version
+ Map<String, String> catalogProperties =
normalizeCredentialProperties(config.getAllConfig());
+ this.catalogCredentialManager = new CatalogCredentialManager(catalogName,
catalogProperties);
+ }
+
+ public LoadTableResponse createTable(
+ Namespace namespace, CreateTableRequest request, boolean
requestCredential) {
+ LoadTableResponse loadTableResponse = super.createTable(namespace,
request);
+ if (requestCredential) {
+ return injectCredentialConfig(
+ TableIdentifier.of(namespace, request.name()), loadTableResponse);
+ }
+ return loadTableResponse;
+ }
+
+ public LoadTableResponse loadTable(TableIdentifier identifier, boolean
requestCredential) {
+ LoadTableResponse loadTableResponse = super.loadTable(identifier);
+ if (requestCredential) {
+ return injectCredentialConfig(identifier, loadTableResponse);
+ }
+ return loadTableResponse;
+ }
+
+ @Override
+ public void close() {
+ if (catalogCredentialManager != null) {
+ catalogCredentialManager.close();
+ }
+ }
+
+ private Map<String, String> getCatalogConfigToClient() {
+ return catalogConfigToClients;
+ }
+
+ private LoadTableResponse injectCredentialConfig(
+ TableIdentifier tableIdentifier, LoadTableResponse loadTableResponse) {
+ TableMetadata tableMetadata = loadTableResponse.tableMetadata();
+ String[] path =
+ Stream.of(
+ tableMetadata.location(),
+ tableMetadata.property(TableProperties.WRITE_DATA_LOCATION,
""),
+
tableMetadata.property(TableProperties.WRITE_METADATA_LOCATION, ""))
+ .filter(StringUtils::isNotBlank)
+ .toArray(String[]::new);
+
+ PathBasedCredentialContext context =
+ new PathBasedCredentialContext(
+ PrincipalUtils.getCurrentUserName(), ImmutableSet.copyOf(path),
Collections.emptySet());
+ Credential credential = catalogCredentialManager.getCredential(context);
+ if (credential == null) {
+ throw new ServiceUnavailableException("Couldn't generate credential,
%s", context);
+ }
+
+ LOG.info(
+ "Generate credential: {} for Iceberg table: {}",
+ credential.credentialType(),
+ tableIdentifier);
+
+ Map<String, String> credentialConfig =
CredentialPropertyUtils.toIcebergProperties(credential);
+ return LoadTableResponse.builder()
+ .withTableMetadata(loadTableResponse.tableMetadata())
+ .addAllConfig(loadTableResponse.config())
+ .addAllConfig(getCatalogConfigToClient())
+ .addAllConfig(credentialConfig)
+ .build();
+ }
+
+ @SuppressWarnings("deprecation")
+ private Map<String, String> normalizeCredentialProperties(Map<String,
String> properties) {
Review Comment:
ok
--
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]