Skip to content

Commit

Permalink
Improved deserializer injection
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavr12 committed Aug 9, 2024
1 parent a0ff43f commit fcc8598
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
5 changes: 5 additions & 0 deletions trino-aws-proxy-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.aws.proxy.spi.plugin;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;
Expand All @@ -27,6 +28,7 @@
import io.trino.aws.proxy.spi.plugin.config.S3SecurityFacadeProviderConfig;
import io.trino.aws.proxy.spi.security.S3SecurityFacadeProvider;

import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static com.google.inject.multibindings.OptionalBinder.newOptionalBinder;
import static io.airlift.configuration.ConditionalModule.conditionalModule;
import static io.airlift.configuration.ConfigurationAwareModule.combine;
Expand Down Expand Up @@ -56,6 +58,8 @@ static <T extends Identity> void bindIdentityType(Binder binder, Class<T> type)
log.info("Using %s identity type", type.getSimpleName());
return type;
});
newSetBinder(binder, com.fasterxml.jackson.databind.Module.class).addBinding()
.toInstance(new SimpleModule().addAbstractTypeMapping(Identity.class, type));
}

static <Implementation> Module optionalPluginModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import com.google.inject.Binder;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.trino.aws.proxy.spi.credentials.Credentials;

import static io.airlift.configuration.ConfigBinder.configBinder;
import static io.airlift.json.JsonCodecBinder.jsonCodecBinder;
import static io.trino.aws.proxy.spi.plugin.TrinoAwsProxyServerBinding.credentialsProviderModule;

public class FileBasedCredentialsModule
Expand All @@ -34,6 +36,7 @@ protected void setup(Binder binder)
innerBinder -> {
configBinder(innerBinder).bindConfig(FileBasedCredentialsProviderConfig.class);
innerBinder.bind(FileBasedCredentialsProvider.class);
jsonCodecBinder(innerBinder).bindListJsonCodec(Credentials.class);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
*/
package io.trino.aws.proxy.server.credentials.file;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.io.Files;
import com.google.inject.Inject;
import io.airlift.json.JsonCodec;
import io.trino.aws.proxy.spi.credentials.Credentials;
import io.trino.aws.proxy.spi.credentials.CredentialsProvider;
import io.trino.aws.proxy.spi.credentials.Identity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
Expand All @@ -40,18 +36,17 @@ public class FileBasedCredentialsProvider
private final Map<String, Credentials> credentialsStore;

@Inject
public FileBasedCredentialsProvider(FileBasedCredentialsProviderConfig config, ObjectMapper objectMapper, Class<? extends Identity> identityClass)
public FileBasedCredentialsProvider(FileBasedCredentialsProviderConfig config, JsonCodec<List<Credentials>> jsonCodec)
{
requireNonNull(config, "Config is null");
objectMapper = objectMapper.registerModule(new SimpleModule().addAbstractTypeMapping(Identity.class, identityClass));
this.credentialsStore = buildCredentialsMap(config.getCredentialsFile(), objectMapper);
this.credentialsStore = buildCredentialsMap(config.getCredentialsFile(), jsonCodec);
}

private Map<String, Credentials> buildCredentialsMap(File credentialsFile, ObjectMapper objectMapper)
private Map<String, Credentials> buildCredentialsMap(File credentialsFile, JsonCodec<List<Credentials>> jsonCodec)
{
List<Credentials> credentialsList;
try (InputStream inputStream = new FileInputStream(credentialsFile)) {
credentialsList = objectMapper.readValue(inputStream, new TypeReference<>() {});
try {
credentialsList = jsonCodec.fromJson(Files.toByteArray(credentialsFile));
}
catch (IOException e) {
throw new UncheckedIOException("Failed to read credentials file", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

import com.google.inject.Binder;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.trino.aws.proxy.spi.credentials.Credentials;

import static io.airlift.configuration.ConfigBinder.configBinder;
import static io.airlift.http.client.HttpClientBinder.httpClientBinder;
import static io.airlift.json.JsonCodecBinder.jsonCodecBinder;
import static io.trino.aws.proxy.spi.plugin.TrinoAwsProxyServerBinding.credentialsProviderModule;

public class HttpCredentialsModule
Expand All @@ -37,6 +39,7 @@ protected void setup(Binder binder)
configBinder(innerBinder).bindConfig(HttpCredentialsProviderConfig.class);
innerBinder.bind(HttpCredentialsProvider.class);
httpClientBinder(innerBinder).bindHttpClient(HTTP_CREDENTIALS_PROVIDER_HTTP_CLIENT_NAME, ForHttpCredentialsProvider.class);
jsonCodecBinder(innerBinder).bindJsonCodec(Credentials.class);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
*/
package io.trino.aws.proxy.server.credentials.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.inject.Inject;
import io.airlift.http.client.FullJsonResponseHandler.JsonResponse;
import io.airlift.http.client.HttpClient;
import io.airlift.http.client.HttpStatus;
import io.airlift.http.client.Request;
import io.airlift.json.JsonCodec;
import io.airlift.json.JsonCodecFactory;
import io.trino.aws.proxy.spi.credentials.Credentials;
import io.trino.aws.proxy.spi.credentials.CredentialsProvider;
import io.trino.aws.proxy.spi.credentials.Identity;
import jakarta.ws.rs.core.UriBuilder;

import java.net.URI;
Expand All @@ -42,13 +38,11 @@ public class HttpCredentialsProvider
private final URI httpCredentialsProviderEndpoint;

@Inject
public HttpCredentialsProvider(@ForHttpCredentialsProvider HttpClient httpClient, HttpCredentialsProviderConfig config, ObjectMapper objectMapper, Class<? extends Identity> identityClass)
public HttpCredentialsProvider(@ForHttpCredentialsProvider HttpClient httpClient, HttpCredentialsProviderConfig config, JsonCodec<Credentials> jsonCodec)
{
requireNonNull(objectMapper, "objectMapper is null");
this.httpClient = requireNonNull(httpClient, "httpClient is null");
this.httpCredentialsProviderEndpoint = config.getEndpoint();
ObjectMapper adjustedObjectMapper = objectMapper.registerModule(new SimpleModule().addAbstractTypeMapping(Identity.class, identityClass));
this.jsonCodec = new JsonCodecFactory(() -> adjustedObjectMapper).jsonCodec(Credentials.class);
this.jsonCodec = jsonCodec;
}

@Override
Expand Down

0 comments on commit fcc8598

Please sign in to comment.