Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.unitycatalog.spark.auth;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import com.google.auth.oauth2.AccessToken;
import com.google.cloud.hadoop.util.AccessTokenProvider;
import io.unitycatalog.server.service.credential.gcp.GcpCredentialsGenerator;
import java.time.Instant;
import java.util.Date;
import java.util.Map;

public class GcsCredRenewITTest extends BaseCredRenewITTest {
private static final String SCHEME = "gs";
private static final String CREDENTIALS_GENERATOR_CLASS = GcsCredGenerator.class.getName();

@Override
protected void setUpProperties() {
super.setUpProperties();
serverProperties.put("gcs.bucketPath.0", "gs://" + BUCKET_NAME);
serverProperties.put("gcs.jsonKeyFilePath.0", "");
serverProperties.put("gcs.credentialsGenerator.0", CREDENTIALS_GENERATOR_CLASS);
}

@Override
protected String scheme() {
return SCHEME;
}

@Override
protected Map<String, String> catalogExtraProps() {
return Map.of(
"fs.gs.impl", GcsCredFileSystem.class.getName(),
"fs.gs.impl.disable.cache", "true");
}

public static class GcsCredGenerator extends TimeBasedCredGenerator<AccessToken>
implements GcpCredentialsGenerator {
@Override
protected AccessToken newTimeBasedCred(long ts) {
Instant expiration = Instant.ofEpochMilli(ts + DEFAULT_INTERVAL_MILLIS);
return AccessToken.newBuilder()
.setTokenValue(String.format("testing-renew://gs://%s#%d", BUCKET_NAME, ts))
.setExpirationTime(Date.from(expiration))
.build();
}
}

public static class GcsCredFileSystem extends CredRenewFileSystem<AccessTokenProvider> {
@Override
protected String scheme() {
return String.format("%s:", SCHEME);
}

@Override
protected AccessTokenProvider createProvider() {
String clazz = getConf().get("fs.gs.auth.access.token.provider");
assertThat(clazz).isEqualTo(GcsVendedTokenProvider.class.getName());

try {
AccessTokenProvider provider =
(AccessTokenProvider) Class.forName(clazz).getDeclaredConstructor().newInstance();
provider.setConf(getConf());
return provider;
} catch (Exception e) {
throw new RuntimeException("Failed to initialize GCS token provider", e);
}
}

@Override
protected void assertCredentials(AccessTokenProvider provider, long ts) {
AccessTokenProvider.AccessToken token = provider.getAccessToken();
assertThat(token).isNotNull();
assertThat(token.getToken())
.isEqualTo(String.format("testing-renew://gs://%s#%d", BUCKET_NAME, ts));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,54 @@
import io.unitycatalog.server.utils.ServerProperties;
import java.io.IOException;
import java.net.URI;
import java.sql.Date;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.SneakyThrows;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.iceberg.Files;

public class GcpCredentialVendor {

public static final List<String> INITIAL_SCOPES =
List.of("https://www.googleapis.com/auth/cloud-platform");

private final Map<String, String> gcsConfigurations;
private final Map<String, GcsStorageConfig> gcsConfigurations;
private final Map<String, GcpCredentialsGenerator> credentialGenerators =
new ConcurrentHashMap<>();

public GcpCredentialVendor(ServerProperties serverProperties) {
this.gcsConfigurations = serverProperties.getGcsConfigurations();
}

@SneakyThrows
public AccessToken vendGcpToken(CredentialContext credentialContext) {
String serviceAccountKeyJsonFilePath =
gcsConfigurations.get(credentialContext.getStorageBase());

GoogleCredentials creds;
if (serviceAccountKeyJsonFilePath != null && !serviceAccountKeyJsonFilePath.isEmpty()) {
if (serviceAccountKeyJsonFilePath.startsWith("testing://")) {
// allow pass-through of a dummy value for integration testing
return AccessToken.newBuilder()
.setTokenValue(serviceAccountKeyJsonFilePath)
.setExpirationTime(Date.from(Instant.ofEpochMilli(253370790000000L)))
.build();
}
creds =
ServiceAccountCredentials.fromStream(
Files.localInput(serviceAccountKeyJsonFilePath).newStream());
} else {
try {
creds = GoogleCredentials.getApplicationDefault();
} catch (IOException e) {
throw new BaseException(ErrorCode.FAILED_PRECONDITION, "GCS credentials not found.", e);
String storageBase = credentialContext.getStorageBase();
GcsStorageConfig storageConfig = gcsConfigurations.get(storageBase);

GcpCredentialsGenerator generator =
credentialGenerators.computeIfAbsent(storageBase, key -> createGenerator(storageConfig));
return generator.generate(credentialContext);
}

private GcpCredentialsGenerator createGenerator(GcsStorageConfig storageConfig) {
if (storageConfig != null) {
String generatorClass = storageConfig.getCredentialsGenerator();
if (generatorClass != null && !generatorClass.isEmpty()) {
try {
return (GcpCredentialsGenerator)
Class.forName(generatorClass).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate GCS credentials generator " + generatorClass, e);
}
}
}

return downscopeGcpCreds(creds.createScoped(INITIAL_SCOPES), credentialContext)
.refreshAccessToken();
String jsonKeyFilePath = storageConfig != null ? storageConfig.getJsonKeyFilePath() : null;
if (jsonKeyFilePath != null && jsonKeyFilePath.isEmpty()) {
jsonKeyFilePath = null;
}

return new ServiceAccountCredentialsGenerator(jsonKeyFilePath);
}

OAuth2Credentials downscopeGcpCreds(GoogleCredentials credentials, CredentialContext context) {
Expand Down Expand Up @@ -118,4 +120,28 @@ List<String> resolvePrivilegesToRoles(Set<CredentialContext.Privilege> privilege
}
return List.of();
}

private final class ServiceAccountCredentialsGenerator implements GcpCredentialsGenerator {
private final String jsonKeyFilePath;

private ServiceAccountCredentialsGenerator(String jsonKeyFilePath) {
this.jsonKeyFilePath = jsonKeyFilePath;
}

@Override
public AccessToken generate(CredentialContext context) {
try {
GoogleCredentials creds;
if (jsonKeyFilePath != null && !jsonKeyFilePath.isEmpty()) {
creds =
ServiceAccountCredentials.fromStream(Files.localInput(jsonKeyFilePath).newStream());
} else {
creds = GoogleCredentials.getApplicationDefault();
}
return downscopeGcpCreds(creds.createScoped(INITIAL_SCOPES), context).refreshAccessToken();
} catch (IOException e) {
throw new BaseException(ErrorCode.FAILED_PRECONDITION, "GCS credentials not found.", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.unitycatalog.server.service.credential.gcp;

import com.google.auth.oauth2.AccessToken;
import io.unitycatalog.server.service.credential.CredentialContext;

/**
* Pluggable contract for generating GCS OAuth tokens. Allows production code to load custom
* implementations for integration tests without coupling to {@link GcpCredentialVendor}.
*/
public interface GcpCredentialsGenerator {
AccessToken generate(CredentialContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.unitycatalog.server.service.credential.gcp;

import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

@Getter
@Builder
@ToString
public class GcsStorageConfig {
private final String bucketPath;
private final String jsonKeyFilePath;
private final String credentialsGenerator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.unitycatalog.server.exception.ErrorCode;
import io.unitycatalog.server.service.credential.aws.S3StorageConfig;
import io.unitycatalog.server.service.credential.azure.ADLSStorageConfig;
import io.unitycatalog.server.service.credential.gcp.GcsStorageConfig;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -120,16 +121,23 @@ public Map<String, S3StorageConfig> getS3Configurations() {
return s3BucketConfigMap;
}

public Map<String, String> getGcsConfigurations() {
Map<String, String> gcsConfigMap = new HashMap<>();
public Map<String, GcsStorageConfig> getGcsConfigurations() {
Map<String, GcsStorageConfig> gcsConfigMap = new HashMap<>();
int i = 0;
while (true) {
String bucketPath = getProperty("gcs.bucketPath." + i);
String jsonKeyFilePath = getProperty("gcs.jsonKeyFilePath." + i);
if (bucketPath == null || jsonKeyFilePath == null) {
if (bucketPath == null) {
break;
}
gcsConfigMap.put(bucketPath, jsonKeyFilePath);
String jsonKeyFilePath = getProperty("gcs.jsonKeyFilePath." + i);
String credentialsGenerator = getProperty("gcs.credentialsGenerator." + i);
gcsConfigMap.put(
bucketPath,
GcsStorageConfig.builder()
.bucketPath(bucketPath)
.jsonKeyFilePath(jsonKeyFilePath)
.credentialsGenerator(credentialsGenerator)
.build());
i++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io.unitycatalog.server.service.credential.azure.ADLSStorageConfig;
import io.unitycatalog.server.service.credential.azure.AzureCredentialVendor;
import io.unitycatalog.server.service.credential.gcp.GcpCredentialVendor;
import io.unitycatalog.server.service.credential.gcp.GcsStorageConfig;
import io.unitycatalog.server.service.credential.gcp.StaticTestingCredentialsGenerator;
import io.unitycatalog.server.utils.ServerProperties;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -113,18 +115,27 @@ public void testGenerateAzureTemporaryCredentials() {

@Test
public void testGenerateGcpTemporaryCredentials() {
// Test mode used
// Test mode using a static generator supplied by the test suite
when(serverProperties.getGcsConfigurations())
.thenReturn(Map.of("gs://uctest", "testing://test"));
.thenReturn(
Map.of(
"gs://uctest",
GcsStorageConfig.builder()
.bucketPath("gs://uctest")
.jsonKeyFilePath("")
.credentialsGenerator(StaticTestingCredentialsGenerator.class.getName())
.build()));
GcpCredentialVendor gcpCredentialVendor = new GcpCredentialVendor(serverProperties);
credentialsOperations = new CloudCredentialVendor(null, null, gcpCredentialVendor);
TemporaryCredentials gcpTemporaryCredentials =
credentialsOperations.vendCredential(
"gs://uctest/abc/xyz", Set.of(CredentialContext.Privilege.UPDATE));
assertThat(gcpTemporaryCredentials.getGcpOauthToken().getOauthToken()).isNotNull();

// Use default creds
when(serverProperties.getGcsConfigurations()).thenReturn(Map.of("gs://uctest", ""));
// Use default creds (expected to fail without real GCP credentials)
when(serverProperties.getGcsConfigurations())
.thenReturn(
Map.of("gs://uctest", GcsStorageConfig.builder().bucketPath("gs://uctest").build()));
gcpCredentialVendor = new GcpCredentialVendor(serverProperties);
credentialsOperations = new CloudCredentialVendor(null, null, gcpCredentialVendor);
assertThatThrownBy(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.unitycatalog.server.service.credential.gcp;

import com.google.auth.oauth2.AccessToken;
import io.unitycatalog.server.service.credential.CredentialContext;
import java.time.Instant;
import java.util.Date;

/**
* Test-only generator that issues a non-expiring static token.
*
* <p>Used by {@link io.unitycatalog.server.service.credential.CloudCredentialVendorTest} to verify
* basic GCS credential vending without requiring real GCP service accounts.
*/
public class StaticTestingCredentialsGenerator implements GcpCredentialsGenerator {
private final AccessToken staticToken;

public StaticTestingCredentialsGenerator() {
this.staticToken =
AccessToken.newBuilder()
.setTokenValue("testing://static-token")
.setExpirationTime(Date.from(Instant.ofEpochMilli(Long.MAX_VALUE)))
.build();
}

@Override
public AccessToken generate(CredentialContext context) {
return staticToken;
}
}