- Notifications
You must be signed in to change notification settings - Fork 20
feat(VpcInstanceAuthenticator): add support for new VPC authentication flow #153
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
Changes from 4 commits
aa24a0c 8388f0d 18e858b f91e184 629564b 7911eb1 4ce3416 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -235,7 +235,25 @@ private OkHttpClient setProxyAuthenticator(OkHttpClient client, Authenticator pr | |
| * @return the new {@link OkHttpClient} instance with the logging configured | ||
| */ | ||
| private OkHttpClient setLoggingLevel(OkHttpClient client, LoggingLevel loggingLevel) { | ||
| HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | ||
| // First check to see if the client already has the http logging interceptor registered. | ||
| // If not, then we'll register a new instance of one. | ||
| HttpLoggingInterceptor loggingInterceptor = null; | ||
| for (Interceptor i : client.networkInterceptors()) { | ||
| if (i instanceof HttpLoggingInterceptor) { | ||
| loggingInterceptor = (HttpLoggingInterceptor) i; | ||
| } | ||
| } | ||
| | ||
| OkHttpClient updatedClient = client; | ||
| if (loggingInterceptor == null) { | ||
| loggingInterceptor = new HttpLoggingInterceptor(); | ||
| loggingInterceptor.redactHeader(HttpHeaders.AUTHORIZATION); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this so that we don't end up logging Authorization header values when debug logging is turned on :) | ||
| loggingInterceptor.redactHeader(HttpHeaders.WWW_AUTHENTICATE); | ||
| loggingInterceptor.redactHeader("Proxy-Authenticate"); | ||
| loggingInterceptor.redactHeader("Proxy-Authorization"); | ||
| OkHttpClient.Builder builder = client.newBuilder().addNetworkInterceptor(loggingInterceptor); | ||
| updatedClient = builder.build(); | ||
| } | ||
| | ||
| switch (loggingLevel) { | ||
| case BODY: | ||
| | @@ -250,9 +268,8 @@ private OkHttpClient setLoggingLevel(OkHttpClient client, LoggingLevel loggingLe | |
| default: | ||
| loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE); | ||
| } | ||
| OkHttpClient.Builder builder = client.newBuilder().addNetworkInterceptor(loggingInterceptor); | ||
| | ||
| return builder.build(); | ||
| return updatedClient; | ||
| } | ||
| | ||
| /** | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /** | ||
| * (C) Copyright IBM Corp. 2015, 2019. | ||
| * (C) Copyright IBM Corp. 2015, 2021. | ||
| * | ||
| * 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 | ||
| | @@ -21,15 +21,23 @@ | |
| * Represents response from IAM API. | ||
| */ | ||
| public class IamToken extends AbstractToken implements ObjectModel, TokenServerResponse { | ||
| | ||
| // These fields are obtained from the IAM token service "getToken" operation response body. | ||
| @SerializedName("access_token") | ||
| private String accessToken; | ||
| | ||
| @SerializedName("refresh_token") | ||
| private String refreshToken; | ||
| | ||
| @SerializedName("token_type") | ||
| private String tokenType; | ||
| | ||
| @SerializedName("expires_in") | ||
| private Long expiresIn; | ||
| | ||
| private Long expiration; | ||
| | ||
| // The remaining fields are computed rather than obtained from operation response. | ||
| private Long refreshTime; | ||
| | ||
| public IamToken() { | ||
| | @@ -40,6 +48,16 @@ public IamToken(Throwable t) { | |
| super(t); | ||
| } | ||
| | ||
| /** | ||
| * Converts a VpcTokenResponse instance to an IamToken instance. | ||
| * @param vpcResponse the VpcTokenResponse instance to be converted. | ||
| */ | ||
| public IamToken(VpcTokenResponse vpcResponse) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this constructor to make it easy to convert a VpcTokenResponse instance (the response received from the VPC Instance Metadata Service) into an IamToken instance (the class we used to cache the iam access token and related info). | ||
| accessToken = vpcResponse.getAccessToken(); | ||
| expiresIn = vpcResponse.getExpiresIn(); | ||
| expiration = vpcResponse.getExpiresAt().getTime() / 1000; | ||
| } | ||
| | ||
| @Override | ||
| public String getAccessToken() { | ||
| return accessToken; | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While testing the VpcInstanceAuthenticator, I noticed that we had a nasty bug here where we would register a new instance of the logging interceptor EACH TIME the setLoggingLevel method is called, even if the
clientalready had the logging interceptor registered. This would result in multiple copies of the debug output for each request/response.