Skip to content

Commit

Permalink
[#5620] feat(fileset): Support credential vending for fileset catalog (
Browse files Browse the repository at this point in the history
…#5682)

### What changes were proposed in this pull request?

Support credential vending for fileset catalog

1. add `credential-providers` properties for the fileset catalog,
schema, and fileset.
2. try to get `credential-providers` from the order of fileset, schema,
and catalog.
3. The user could set multi-credential providers


### Why are the changes needed?

Fix: #5620 

### Does this PR introduce _any_ user-facing change?

will add document after this PR is merged

### How was this patch tested?
Add IT and test with local setup Gravitino server
  • Loading branch information
FANNG1 authored Dec 26, 2024
1 parent f7656be commit 082bbdc
Show file tree
Hide file tree
Showing 24 changed files with 867 additions and 89 deletions.
2 changes: 2 additions & 0 deletions bundles/aws-bundle/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation(libs.aws.iam)
implementation(libs.aws.policy)
implementation(libs.aws.sts)
implementation(libs.commons.lang3)
implementation(libs.hadoop3.aws)
implementation(project(":catalogs:catalog-common")) {
exclude("*")
Expand All @@ -46,6 +47,7 @@ dependencies {
tasks.withType(ShadowJar::class.java) {
isZip64 = true
configurations = listOf(project.configurations.runtimeClasspath.get())
relocate("org.apache.commons", "org.apache.gravitino.aws.shaded.org.apache.commons")
archiveClassifier.set("")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

public class CredentialConstants {
public static final String CREDENTIAL_PROVIDER_TYPE = "credential-provider-type";
public static final String CREDENTIAL_PROVIDERS = "credential-providers";
public static final String S3_TOKEN_CREDENTIAL_PROVIDER = "s3-token";
public static final String S3_TOKEN_EXPIRE_IN_SECS = "s3-token-expire-in-secs";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.gravitino.catalog.hadoop.fs.LocalFileSystemProvider;
import org.apache.gravitino.connector.BaseCatalogPropertiesMetadata;
import org.apache.gravitino.connector.PropertyEntry;
import org.apache.gravitino.credential.config.CredentialConfig;

public class HadoopCatalogPropertiesMetadata extends BaseCatalogPropertiesMetadata {

Expand Down Expand Up @@ -84,6 +85,7 @@ public class HadoopCatalogPropertiesMetadata extends BaseCatalogPropertiesMetada
// The following two are about authentication.
.putAll(KERBEROS_PROPERTY_ENTRIES)
.putAll(AuthenticationConfig.AUTHENTICATION_PROPERTY_ENTRIES)
.putAll(CredentialConfig.CREDENTIAL_PROPERTY_ENTRIES)
.build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.gravitino.catalog.hadoop.authentication.kerberos.KerberosConfig;
import org.apache.gravitino.connector.BasePropertiesMetadata;
import org.apache.gravitino.connector.PropertyEntry;
import org.apache.gravitino.credential.config.CredentialConfig;

public class HadoopFilesetPropertiesMetadata extends BasePropertiesMetadata {

Expand All @@ -32,6 +33,7 @@ protected Map<String, PropertyEntry<?>> specificPropertyEntries() {
ImmutableMap.Builder<String, PropertyEntry<?>> builder = ImmutableMap.builder();
builder.putAll(KerberosConfig.KERBEROS_PROPERTY_ENTRIES);
builder.putAll(AuthenticationConfig.AUTHENTICATION_PROPERTY_ENTRIES);
builder.putAll(CredentialConfig.CREDENTIAL_PROPERTY_ENTRIES);
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.gravitino.catalog.hadoop.authentication.kerberos.KerberosConfig;
import org.apache.gravitino.connector.BasePropertiesMetadata;
import org.apache.gravitino.connector.PropertyEntry;
import org.apache.gravitino.credential.config.CredentialConfig;

public class HadoopSchemaPropertiesMetadata extends BasePropertiesMetadata {

Expand All @@ -49,6 +50,7 @@ public class HadoopSchemaPropertiesMetadata extends BasePropertiesMetadata {
false /* hidden */))
.putAll(KerberosConfig.KERBEROS_PROPERTY_ENTRIES)
.putAll(AuthenticationConfig.AUTHENTICATION_PROPERTY_ENTRIES)
.putAll(CredentialConfig.CREDENTIAL_PROPERTY_ENTRIES)
.build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
package org.apache.gravitino.catalog.hadoop;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.security.auth.Subject;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.Entity;
import org.apache.gravitino.EntityStore;
Expand All @@ -38,6 +43,9 @@
import org.apache.gravitino.connector.CatalogOperations;
import org.apache.gravitino.connector.HasPropertyMetadata;
import org.apache.gravitino.connector.SupportsSchemas;
import org.apache.gravitino.connector.credential.PathContext;
import org.apache.gravitino.connector.credential.SupportsPathBasedCredentials;
import org.apache.gravitino.credential.CredentialUtils;
import org.apache.gravitino.exceptions.FilesetAlreadyExistsException;
import org.apache.gravitino.exceptions.NoSuchCatalogException;
import org.apache.gravitino.exceptions.NoSuchEntityException;
Expand All @@ -50,13 +58,14 @@
import org.apache.gravitino.file.FilesetChange;
import org.apache.gravitino.meta.FilesetEntity;
import org.apache.gravitino.meta.SchemaEntity;
import org.apache.gravitino.utils.NameIdentifierUtil;
import org.apache.gravitino.utils.PrincipalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("removal")
public class SecureHadoopCatalogOperations
implements CatalogOperations, SupportsSchemas, FilesetCatalog {
implements CatalogOperations, SupportsSchemas, FilesetCatalog, SupportsPathBasedCredentials {

public static final Logger LOG = LoggerFactory.getLogger(SecureHadoopCatalogOperations.class);

Expand All @@ -66,6 +75,8 @@ public class SecureHadoopCatalogOperations

private UserContext catalogUserContext;

private Map<String, String> catalogProperties;

public SecureHadoopCatalogOperations() {
this.hadoopCatalogOperations = new HadoopCatalogOperations();
}
Expand All @@ -74,6 +85,20 @@ public SecureHadoopCatalogOperations(EntityStore store) {
this.hadoopCatalogOperations = new HadoopCatalogOperations(store);
}

@Override
public void initialize(
Map<String, String> config, CatalogInfo info, HasPropertyMetadata propertiesMetadata)
throws RuntimeException {
hadoopCatalogOperations.initialize(config, info, propertiesMetadata);
this.catalogUserContext =
UserContext.getUserContext(
NameIdentifier.of(info.namespace(), info.name()),
config,
hadoopCatalogOperations.getHadoopConf(),
info);
this.catalogProperties = info.properties();
}

@VisibleForTesting
public HadoopCatalogOperations getBaseHadoopCatalogOperations() {
return hadoopCatalogOperations;
Expand Down Expand Up @@ -163,19 +188,6 @@ public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmpty
}
}

@Override
public void initialize(
Map<String, String> config, CatalogInfo info, HasPropertyMetadata propertiesMetadata)
throws RuntimeException {
hadoopCatalogOperations.initialize(config, info, propertiesMetadata);
catalogUserContext =
UserContext.getUserContext(
NameIdentifier.of(info.namespace(), info.name()),
config,
hadoopCatalogOperations.getHadoopConf(),
info);
}

@Override
public Fileset alterFileset(NameIdentifier ident, FilesetChange... changes)
throws NoSuchFilesetException, IllegalArgumentException {
Expand Down Expand Up @@ -245,6 +257,29 @@ public void testConnection(
hadoopCatalogOperations.testConnection(catalogIdent, type, provider, comment, properties);
}

@Override
public List<PathContext> getPathContext(NameIdentifier filesetIdentifier) {
Fileset fileset = loadFileset(filesetIdentifier);
String path = fileset.storageLocation();
Preconditions.checkState(
StringUtils.isNotBlank(path), "The location of fileset should not be empty.");

Set<String> providers =
CredentialUtils.getCredentialProvidersByOrder(
() -> fileset.properties(),
() -> {
Namespace namespace = filesetIdentifier.namespace();
NameIdentifier schemaIdentifier =
NameIdentifierUtil.ofSchema(
namespace.level(0), namespace.level(1), namespace.level(2));
return loadSchema(schemaIdentifier).properties();
},
() -> catalogProperties);
return providers.stream()
.map(provider -> new PathContext(path, provider))
.collect(Collectors.toList());
}

/**
* Add the user to the subject so that we can get the last user in the subject. Hadoop catalog
* uses this method to pass api user from the client side, so that we can get the user in the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* 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.filesystem.hadoop.integration.test;

import static org.apache.gravitino.catalog.hadoop.HadoopCatalogPropertiesMetadata.FILESYSTEM_PROVIDERS;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import org.apache.gravitino.Catalog;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.client.GravitinoMetalake;
import org.apache.gravitino.credential.Credential;
import org.apache.gravitino.credential.CredentialConstants;
import org.apache.gravitino.credential.S3SecretKeyCredential;
import org.apache.gravitino.credential.S3TokenCredential;
import org.apache.gravitino.file.Fileset;
import org.apache.gravitino.integration.test.util.BaseIT;
import org.apache.gravitino.integration.test.util.GravitinoITUtils;
import org.apache.gravitino.storage.S3Properties;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@EnabledIfEnvironmentVariable(named = "GRAVITINO_TEST_CLOUD_IT", matches = "true")
public class FilesetCatalogCredentialIT extends BaseIT {

private static final Logger LOG = LoggerFactory.getLogger(FilesetCatalogCredentialIT.class);

public static final String BUCKET_NAME = System.getenv("S3_BUCKET_NAME");
public static final String S3_ACCESS_KEY = System.getenv("S3_ACCESS_KEY_ID");
public static final String S3_SECRET_KEY = System.getenv("S3_SECRET_ACCESS_KEY");
public static final String S3_ROLE_ARN = System.getenv("S3_ROLE_ARN");

private String metalakeName = GravitinoITUtils.genRandomName("gvfs_it_metalake");
private String catalogName = GravitinoITUtils.genRandomName("catalog");
private String schemaName = GravitinoITUtils.genRandomName("schema");
private GravitinoMetalake metalake;

@BeforeAll
public void startIntegrationTest() {
// Do nothing
}

@BeforeAll
public void startUp() throws Exception {
copyBundleJarsToHadoop("aws-bundle");
// Need to download jars to gravitino server
super.startIntegrationTest();

metalakeName = GravitinoITUtils.genRandomName("gvfs_it_metalake");
catalogName = GravitinoITUtils.genRandomName("catalog");
schemaName = GravitinoITUtils.genRandomName("schema");

Assertions.assertFalse(client.metalakeExists(metalakeName));
metalake = client.createMetalake(metalakeName, "metalake comment", Collections.emptyMap());
Assertions.assertTrue(client.metalakeExists(metalakeName));

Map<String, String> properties = Maps.newHashMap();
properties.put(FILESYSTEM_PROVIDERS, "s3");
properties.put(
CredentialConstants.CREDENTIAL_PROVIDERS,
S3TokenCredential.S3_TOKEN_CREDENTIAL_TYPE
+ ","
+ S3SecretKeyCredential.S3_SECRET_KEY_CREDENTIAL_TYPE);
properties.put(
CredentialConstants.CREDENTIAL_PROVIDER_TYPE,
S3SecretKeyCredential.S3_SECRET_KEY_CREDENTIAL_TYPE);
properties.put(S3Properties.GRAVITINO_S3_ACCESS_KEY_ID, S3_ACCESS_KEY);
properties.put(S3Properties.GRAVITINO_S3_SECRET_ACCESS_KEY, S3_SECRET_KEY);
properties.put(S3Properties.GRAVITINO_S3_ENDPOINT, "s3.ap-southeast-2.amazonaws.com");
properties.put(S3Properties.GRAVITINO_S3_REGION, "ap-southeast-2");
properties.put(S3Properties.GRAVITINO_S3_ROLE_ARN, S3_ROLE_ARN);

Catalog catalog =
metalake.createCatalog(
catalogName, Catalog.Type.FILESET, "hadoop", "catalog comment", properties);
Assertions.assertTrue(metalake.catalogExists(catalogName));

catalog.asSchemas().createSchema(schemaName, "schema comment", properties);
Assertions.assertTrue(catalog.asSchemas().schemaExists(schemaName));
}

@AfterAll
public void tearDown() throws IOException {
Catalog catalog = metalake.loadCatalog(catalogName);
catalog.asSchemas().dropSchema(schemaName, true);
metalake.dropCatalog(catalogName, true);
client.dropMetalake(metalakeName, true);

if (client != null) {
client.close();
client = null;
}

try {
closer.close();
} catch (Exception e) {
LOG.error("Exception in closing CloseableGroup", e);
}
}

protected String genStorageLocation(String fileset) {
return String.format("s3a://%s/%s", BUCKET_NAME, fileset);
}

@Test
void testGetCatalogCredential() {
Catalog catalog = metalake.loadCatalog(catalogName);
Credential[] credentials = catalog.supportsCredentials().getCredentials();
Assertions.assertEquals(1, credentials.length);
Assertions.assertTrue(credentials[0] instanceof S3SecretKeyCredential);
}

@Test
void testGetFilesetCredential() {
String filesetName = GravitinoITUtils.genRandomName("test_fileset_credential");
NameIdentifier filesetIdent = NameIdentifier.of(schemaName, filesetName);
Catalog catalog = metalake.loadCatalog(catalogName);
String storageLocation = genStorageLocation(filesetName);
catalog
.asFilesetCatalog()
.createFileset(
filesetIdent,
"fileset comment",
Fileset.Type.MANAGED,
storageLocation,
ImmutableMap.of(
CredentialConstants.CREDENTIAL_PROVIDERS,
S3TokenCredential.S3_TOKEN_CREDENTIAL_TYPE));

Fileset fileset = catalog.asFilesetCatalog().loadFileset(filesetIdent);
Credential[] credentials = fileset.supportsCredentials().getCredentials();
Assertions.assertEquals(1, credentials.length);
Assertions.assertTrue(credentials[0] instanceof S3TokenCredential);
}
}
Loading

0 comments on commit 082bbdc

Please sign in to comment.