From 2c535152fd86476b5b9ebceb940e59c98b83872a Mon Sep 17 00:00:00 2001 From: Pranav Ramachandra Date: Tue, 13 Aug 2024 14:09:16 +0100 Subject: [PATCH] Improved deserializer injection --- .../server/TrinoAwsProxyServerModule.java | 2 + .../credentials/JsonIdentityProvider.java | 38 +++++++++++++++++++ .../file/FileBasedCredentialsModule.java | 3 ++ .../file/FileBasedCredentialsProvider.java | 20 ++++------ .../http/HttpCredentialsModule.java | 3 ++ .../http/HttpCredentialsProvider.java | 10 +---- 6 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/JsonIdentityProvider.java diff --git a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/TrinoAwsProxyServerModule.java b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/TrinoAwsProxyServerModule.java index 67122736..59bfd30e 100644 --- a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/TrinoAwsProxyServerModule.java +++ b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/TrinoAwsProxyServerModule.java @@ -30,6 +30,7 @@ import io.airlift.jaxrs.JaxrsBinder; import io.airlift.log.Logger; import io.trino.aws.proxy.server.credentials.CredentialsController; +import io.trino.aws.proxy.server.credentials.JsonIdentityProvider; import io.trino.aws.proxy.server.credentials.file.FileBasedCredentialsModule; import io.trino.aws.proxy.server.credentials.http.HttpCredentialsModule; import io.trino.aws.proxy.server.remote.RemoteS3Module; @@ -116,6 +117,7 @@ protected void setup(Binder binder) log.info("Using %s identity type", StandardIdentity.class.getSimpleName()); return StandardIdentity.class; }); + newSetBinder(binder, com.fasterxml.jackson.databind.Module.class).addBinding().toProvider(JsonIdentityProvider.class).in(Scopes.SINGLETON); // provided implementations install(new FileBasedCredentialsModule()); diff --git a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/JsonIdentityProvider.java b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/JsonIdentityProvider.java new file mode 100644 index 00000000..90bfc462 --- /dev/null +++ b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/JsonIdentityProvider.java @@ -0,0 +1,38 @@ +/* + * Licensed 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 io.trino.aws.proxy.server.credentials; + +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.google.inject.Inject; +import com.google.inject.Provider; +import io.trino.aws.proxy.spi.credentials.Identity; + +public class JsonIdentityProvider + implements Provider +{ + private final Class identityType; + + @Inject + public JsonIdentityProvider(Class identityType) + { + this.identityType = identityType; + } + + @Override + public Module get() + { + return new SimpleModule().addAbstractTypeMapping(Identity.class, identityType); + } +} diff --git a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsModule.java b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsModule.java index ad897365..1b9beffb 100644 --- a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsModule.java +++ b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsModule.java @@ -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 @@ -34,6 +36,7 @@ protected void setup(Binder binder) innerBinder -> { configBinder(innerBinder).bindConfig(FileBasedCredentialsProviderConfig.class); innerBinder.bind(FileBasedCredentialsProvider.class); + jsonCodecBinder(innerBinder).bindListJsonCodec(Credentials.class); })); } } diff --git a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsProvider.java b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsProvider.java index 1ceb16e1..0bc9a195 100644 --- a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsProvider.java +++ b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/file/FileBasedCredentialsProvider.java @@ -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; @@ -40,18 +36,18 @@ public class FileBasedCredentialsProvider private final Map credentialsStore; @Inject - public FileBasedCredentialsProvider(FileBasedCredentialsProviderConfig config, ObjectMapper objectMapper, Class identityClass) + public FileBasedCredentialsProvider(FileBasedCredentialsProviderConfig config, JsonCodec> jsonCodec) { requireNonNull(config, "Config is null"); - objectMapper = objectMapper.registerModule(new SimpleModule().addAbstractTypeMapping(Identity.class, identityClass)); - this.credentialsStore = buildCredentialsMap(config.getCredentialsFile(), objectMapper); + requireNonNull(jsonCodec, "jsonCodec is null"); + this.credentialsStore = buildCredentialsMap(config.getCredentialsFile(), jsonCodec); } - private Map buildCredentialsMap(File credentialsFile, ObjectMapper objectMapper) + private Map buildCredentialsMap(File credentialsFile, JsonCodec> jsonCodec) { List 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); diff --git a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsModule.java b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsModule.java index bd7b749c..2d4a17c9 100644 --- a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsModule.java +++ b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsModule.java @@ -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 @@ -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); })); } } diff --git a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsProvider.java b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsProvider.java index 7e5696e3..aa73a1fb 100644 --- a/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsProvider.java +++ b/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsProvider.java @@ -13,8 +13,6 @@ */ package io.trino.aws.proxy.server.credentials.http; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Multimaps; import com.google.inject.Inject; @@ -23,10 +21,8 @@ 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; @@ -46,13 +42,11 @@ public class HttpCredentialsProvider private final Map httpHeaders; @Inject - public HttpCredentialsProvider(@ForHttpCredentialsProvider HttpClient httpClient, HttpCredentialsProviderConfig config, ObjectMapper objectMapper, Class identityClass) + public HttpCredentialsProvider(@ForHttpCredentialsProvider HttpClient httpClient, HttpCredentialsProviderConfig config, JsonCodec jsonCodec) { - requireNonNull(objectMapper, "objectMapper is null"); this.httpClient = requireNonNull(httpClient, "httpClient is null"); + this.jsonCodec = requireNonNull(jsonCodec, "jsonCodec 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.httpHeaders = ImmutableMap.copyOf(config.getHttpHeaders()); }