Skip to content
Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GcsFileSystemModule
public void configure(Binder binder)
{
configBinder(binder).bindConfig(GcsFileSystemConfig.class);
binder.bind(GcsStorageFactory.class).to(DefaultGcsStorageFactory.class).in(Scopes.SINGLETON);
binder.bind(GcsStorageFactory.class).in(Scopes.SINGLETON);
binder.bind(GcsFileSystemFactory.class).in(Scopes.SINGLETON);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,78 @@
*/
package io.trino.filesystem.gcs;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import io.trino.spi.security.ConnectorIdentity;

public interface GcsStorageFactory
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Optional;

import static com.google.common.base.Strings.nullToEmpty;
import static java.nio.charset.StandardCharsets.UTF_8;

public class GcsStorageFactory
{
Storage create(ConnectorIdentity identity);
public static final String GCS_OAUTH_KEY = "gcs.oauth";
public static final List<String> DEFAULT_SCOPES = ImmutableList.of("https://www.googleapis.com/auth/cloud-platform");
private final String projectId;
private final boolean useGcsAccessToken;
private final Optional<GoogleCredentials> jsonGoogleCredential;

@Inject
public GcsStorageFactory(GcsFileSystemConfig config)
throws IOException
{
config.validate();
projectId = config.getProjectId();
useGcsAccessToken = config.isUseGcsAccessToken();
String jsonKey = config.getJsonKey();
String jsonKeyFilePath = config.getJsonKeyFilePath();
if (jsonKey != null) {
try (InputStream inputStream = new ByteArrayInputStream(jsonKey.getBytes(UTF_8))) {
jsonGoogleCredential = Optional.of(GoogleCredentials.fromStream(inputStream).createScoped(DEFAULT_SCOPES));
}
}
else if (jsonKeyFilePath != null) {
try (FileInputStream inputStream = new FileInputStream(jsonKeyFilePath)) {
jsonGoogleCredential = Optional.of(GoogleCredentials.fromStream(inputStream).createScoped(DEFAULT_SCOPES));
}
}
else {
jsonGoogleCredential = Optional.empty();
}
}

public Storage create(ConnectorIdentity identity)
{
try {
GoogleCredentials credentials;
if (useGcsAccessToken) {
String accessToken = nullToEmpty(identity.getExtraCredentials().get(GCS_OAUTH_KEY));
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(accessToken.getBytes(UTF_8))) {
credentials = GoogleCredentials.fromStream(inputStream).createScoped(DEFAULT_SCOPES);
}
}
else {
credentials = jsonGoogleCredential.orElseThrow(() -> new VerifyException("GCS credentials not configured"));
}
StorageOptions.Builder storageOptionsBuilder = StorageOptions.newBuilder();
if (projectId != null) {
storageOptionsBuilder.setProjectId(projectId);
}
return storageOptionsBuilder.setCredentials(credentials).build().getService();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.TestInstance;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -57,7 +54,7 @@ protected void initialize(String gcpCredentialKey)
// For gcp testing this corresponds to the Cluster Storage Admin and Cluster Storage Object Admin roles
byte[] jsonKeyBytes = Base64.getDecoder().decode(gcpCredentialKey);
GcsFileSystemConfig config = new GcsFileSystemConfig().setJsonKey(new String(jsonKeyBytes, UTF_8));
GcsStorageFactory storageFactory = new TestingGcsStorageFactory(config);
GcsStorageFactory storageFactory = new GcsStorageFactory(config);
this.gcsFileSystemFactory = new GcsFileSystemFactory(config, storageFactory);
this.storage = storageFactory.create(ConnectorIdentity.ofUser("test"));
String bucket = RemoteStorageHelper.generateBucketName();
Expand All @@ -69,17 +66,20 @@ protected void initialize(String gcpCredentialKey)

@AfterAll
void tearDown()
throws Exception
{
try {
RemoteStorageHelper.forceDelete(storage, rootLocation.host().get(), 5, TimeUnit.SECONDS);
gcsFileSystemFactory.stop();
storage.delete(rootLocation.host().get());
}
finally {
fileSystem = null;
storage = null;
rootLocation = null;
gcsFileSystemFactory = null;
try {
gcsFileSystemFactory.stop();
}
finally {
gcsFileSystemFactory = null;
}
}
}

Expand Down Expand Up @@ -126,25 +126,4 @@ protected final boolean supportsRenameFile()
{
return false;
}

private static class TestingGcsStorageFactory
implements GcsStorageFactory
{
private final Storage storage;

public TestingGcsStorageFactory(GcsFileSystemConfig config)
{
requireNonNull(config, "config is null");
InputStream inputStream = new ByteArrayInputStream(config.getJsonKey().getBytes(UTF_8));
// Note: the default project id from the credentials file will be used. See StorageOptions.setProjectId()
RemoteStorageHelper helper = RemoteStorageHelper.create(null, inputStream);
this.storage = helper.getOptions().getService();
}

@Override
public Storage create(ConnectorIdentity identity)
{
return storage;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
package io.trino.filesystem.gcs;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.TestInstance;

import java.io.IOException;

@Disabled // TODO Re-enable once fixed the initialization failure https://github.com/trinodb/trino/issues/19785
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class TestGcsFileSystemGcs
extends AbstractTestGcsFileSystem
Expand Down