Skip to content

authCredentials set on DatastoreOptions.builder() is not used so can't access Datastore over http proxy #1210

@successhawk

Description

@successhawk

Can't use Datastore over an http proxy. I first reported this under issue 1138
This issue was resolved, however I still can't use Datastore over http proxy due to auth calls not using proxy. I attempted to configure the AuthCredentials to use my http proxy with the following code:

        return DatastoreOptions.builder()
                            .authCredentials(
                                    new MyApplicationDefaultAuthCredentials(
                                            GoogleCredentials.getApplicationDefault(GcpClientUtil.getHttpTransport())))
                            .httpTransportFactory(new CustomHttpTransportFactoryWithProxy())
                            .build()
                            .service());

Where MyApplicationDefaultAuthCredentials

  /**
   * This was copied and modified so that we could provide an instance of GoogleCredentials
   * 
   * Represents Application Default Credentials, which are credentials that are inferred from the
   * runtime environment.
   *
   * @see <a
   *     href="https://developers.google.com/identity/protocols/application-default-credentials">
   *     Google Application Default Credentials</a>
   */
  static class MyApplicationDefaultAuthCredentials extends AuthCredentials {

    private GoogleCredentials googleCredentials;

    private static final ApplicationDefaultAuthCredentialsState STATE =
        new ApplicationDefaultAuthCredentialsState();

    private static class ApplicationDefaultAuthCredentialsState
        implements RestorableState<AuthCredentials>, Serializable {

      private static final long serialVersionUID = -8839085552021212257L;

      @Override
      public AuthCredentials restore() {
        try {
          return new MyApplicationDefaultAuthCredentials(
                  GoogleCredentials.getApplicationDefault(GcpClientUtil.getHttpTransport()));
        } catch (IOException e) {
          throw new IllegalStateException(
              "Could not restore " + ApplicationDefaultAuthCredentials.class.getSimpleName(), e);
        }
      }

      @Override
      public int hashCode() {
        return getClass().getName().hashCode();
      }

      @Override
      public boolean equals(Object obj) {
        return obj instanceof ApplicationDefaultAuthCredentialsState;
      }
    }

    public MyApplicationDefaultAuthCredentials(GoogleCredentials credentials) {
      googleCredentials = credentials;
    }

    @Override
    public GoogleCredentials credentials() {
      return googleCredentials;
    }

    @Override
    public RestorableState<AuthCredentials> capture() {
      return STATE;
    }
  }

Where getHttpTransport is something like follows:

public static HttpTransport getHttpTransport() {
    URI uri = getGcpHttpProxyURI();
    if ( uri !=null ) {
        return new NetHttpTransport.Builder().setProxy(new Proxy(Proxy.Type.HTTP,
                new InetSocketAddress(parseHostFromUri(uri),parsePortFromUri(uri)))).build();
    } else {
        return Utils.getDefaultTransport();
    }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions