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 NoAuthCredentials #719

Merged
merged 3 commits into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ public RestorableState<AuthCredentials> capture() {
}
}

/**
* Represents service account credentials.
*
* @see <a href="https://cloud.google.com/docs/authentication#user_accounts_and_service_accounts">
* User accounts and service accounts</a>
*/
public static class ServiceAccountAuthCredentials extends AuthCredentials {

private final String account;
Expand Down Expand Up @@ -195,6 +201,14 @@ public RestorableState<AuthCredentials> capture() {
}
}

/**
* 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>
*/
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {

private GoogleCredentials googleCredentials;
Expand Down Expand Up @@ -243,6 +257,38 @@ public RestorableState<AuthCredentials> capture() {
}
}

/**
* A placeholder for credentials to signify that requests sent to the server should not be
* authenticated. This is typically useful when using the local service emulators, such as
* {@code LocalGcdHelper} and {@code LocalResourceManagerHelper}.
*/
public static class NoAuthCredentials extends AuthCredentials {

This comment was marked as spam.


private static final AuthCredentials INSTANCE = new NoAuthCredentials();
private static final NoAuthCredentialsState STATE = new NoAuthCredentialsState();

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

private static final long serialVersionUID = -4022100563954640465L;

@Override
public AuthCredentials restore() {
return INSTANCE;
}
}

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

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

public abstract GoogleCredentials credentials();

public static AuthCredentials createForAppEngine() {
Expand Down Expand Up @@ -281,6 +327,15 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
return new ServiceAccountAuthCredentials(account, privateKey);
}

/**
* Creates a placeholder denoting that no credentials should be used. This is typically useful
* when using the local service emulators, such as {@code LocalGcdHelper} and
* {@code LocalResourceManagerHelper}.
*/
public static AuthCredentials noAuth() {
return NoAuthCredentials.INSTANCE;
}

/**
* Creates Service Account Credentials given a stream for credentials in JSON format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,10 @@ public RetryParams retryParams() {
* options.
*/
public HttpRequestInitializer httpRequestInitializer() {
final HttpRequestInitializer delegate = authCredentials() != null
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
: null;
final HttpRequestInitializer delegate =
authCredentials() != null && authCredentials.credentials() != null
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
: null;
return new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.api.services.datastore.DatastoreV1.RunQueryResponse;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.RetryParams;
import com.google.gcloud.datastore.Query.ResultType;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
Expand Down Expand Up @@ -128,6 +129,7 @@ public void setUp() {
options = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.host("http://localhost:" + PORT)
.authCredentials(AuthCredentials.noAuth())
.retryParams(RetryParams.noRetries())
.build();
datastore = options.service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.gcloud.AuthCredentials;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;

import com.sun.net.httpserver.Headers;
Expand Down Expand Up @@ -550,17 +551,21 @@ private LocalResourceManagerHelper() {
}

/**
* Creates a LocalResourceManagerHelper object that listens to requests on the local machine.
* Creates a {@code LocalResourceManagerHelper} object that listens to requests on the local

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* machine.
*/
public static LocalResourceManagerHelper create() {
return new LocalResourceManagerHelper();
}

/**
* Returns a ResourceManagerOptions instance that sets the host to use the mock server.
* Returns a {@link ResourceManagerOptions} instance that sets the host to use the mock server.

This comment was marked as spam.

This comment was marked as spam.

*/
public ResourceManagerOptions options() {
return ResourceManagerOptions.builder().host("http://localhost:" + port).build();
return ResourceManagerOptions.builder()
.host("http://localhost:" + port)
.authCredentials(AuthCredentials.noAuth())
.build();
}

/**
Expand Down