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 13, 2024
1 parent 231b974 commit 5cdf54b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Module>
{
private final Class<? extends Identity> identityType;

@Inject
public JsonIdentityProvider(Class<? extends Identity> identityType)
{
this.identityType = identityType;
}

@Override
public Module get()
{
return new SimpleModule().addAbstractTypeMapping(Identity.class, identityType);
}
}
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,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;
Expand All @@ -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;
Expand All @@ -46,14 +42,12 @@ public class HttpCredentialsProvider
private final Map<String, String> httpHeaders;

@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.httpHeaders = ImmutableMap.copyOf(config.getHttpHeaders());
this.jsonCodec = jsonCodec;
}

@Override
Expand Down

0 comments on commit 5cdf54b

Please sign in to comment.