Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http credentials provider #136

Merged
merged 1 commit into from
Aug 7, 2024
Merged

Conversation

pranavr12
Copy link
Contributor

The Http credentials provider can be used to fetch the remote credentials and identity from a HTTP service

trino-aws-proxy/pom.xml Outdated Show resolved Hide resolved
public Optional<Credentials> credentials(String emulatedAccessKey, Optional<String> session)
{
Request request = prepareGet()
.setUri(UriBuilder.fromUri(httpCredentialsProviderConfig.getEndpoint()).path(emulatedAccessKey).build())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to set the Content-Type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently a get request, Should we set the contenttype ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp - never mind

@Randgalt
Copy link
Member

Randgalt commented Aug 6, 2024

Looks good - a few things to address

requireNonNull(config, "Config is null");
requireNonNull(httpClient, "httpClient is null");
requireNonNull(objectMapper, "objectMapper is null");
ObjectMapper adjustedObjectMapper = objectMapper.registerModule(new SimpleModule().addAbstractTypeMapping(Identity.class, identityClass));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are mapping abstract types, it is not possible to have the jsonCodecBinder registered in the module. However, once the abstract type has been mapped at a higher level, we could register the jsonCodecBinder. I'll address that in a separate PR since this change affects the FileBasedCredentialsProvider as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Inject
public HttpCredentialsProvider(@ForHttpCredentialsProvider HttpClient httpClient, HttpCredentialsProviderConfig config, ObjectMapper objectMapper, Class<? extends Identity> identityClass)
{
requireNonNull(config, "Config is null");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're accessing a field in config already so we don't need the requireNonNull.

I'd however suggest using requireNonNull to check that config.getEndpoint() does not return null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since config used in the constructor, it's good to have the requireNonNull for config as well.
The Config class already has the @NonNull for the endpoint - https://github.com/pranavr12/aws-proxy/blob/http-creds/trino-aws-proxy/src/main/java/io/trino/aws/proxy/server/credentials/http/HttpCredentialsProviderConfig.java#L25.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't usually add requireNonNull on configs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed requireNonNull on config

Comment on lines 62 to 61
Request request = prepareGet()
.setUri(UriBuilder.fromUri(httpCredentialsProviderEndpoint).path(emulatedAccessKey).build())
.build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add the session as a query parameter if present? I know we don't have a use for this currently but its nice to have

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added session as a query param with tests

@pranavr12 pranavr12 force-pushed the http-creds branch 3 times, most recently from 26886b5 to beaea8b Compare August 6, 2024 13:27
Request request = prepareGet()
.setUri(uriBuilder.build())
.build();
JsonResponse<Credentials> response = httpClient.execute(request, createFullJsonResponseHandler(jsonCodec));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the endpoint returns a 404 I imagine it would not return a properly formatted JSON that we could decode.

Should we wrap this in a try / catch and return Optional.empty() if we get status 404 or otherwise propagate the exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the possible scenarios:
Server returns 404 -> Credentials provider returns Optional.empty()
Server returns 200, but incorrect response -> The parsing fails and the response value is null (No exception is thrown). I've added a condition to check the null status. Credentials provider returns Optional.empty()
Server return 200, with correct response: Parsing succeeds and the correct value is returned

Copy link
Member

@vagaerg vagaerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vagaerg vagaerg merged commit a0ff43f into trinodb:main Aug 7, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants