Skip to content

Commit d3224b3

Browse files
committed
Merge pull request #719 from ajkannan/add-no-auth
Add NoAuthCredentials
2 parents 7eb957d + 0c4af89 commit d3224b3

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ public RestorableState<AuthCredentials> capture() {
132132
}
133133
}
134134

135+
/**
136+
* Represents service account credentials.
137+
*
138+
* @see <a href="https://cloud.google.com/docs/authentication#user_accounts_and_service_accounts">
139+
* User accounts and service accounts</a>
140+
*/
135141
public static class ServiceAccountAuthCredentials extends AuthCredentials {
136142

137143
private final String account;
@@ -195,6 +201,14 @@ public RestorableState<AuthCredentials> capture() {
195201
}
196202
}
197203

204+
/**
205+
* Represents Application Default Credentials, which are credentials that are inferred from the
206+
* runtime environment.
207+
*
208+
* @see <a
209+
* href="https://developers.google.com/identity/protocols/application-default-credentials">
210+
* Google Application Default Credentials</a>
211+
*/
198212
public static class ApplicationDefaultAuthCredentials extends AuthCredentials {
199213

200214
private GoogleCredentials googleCredentials;
@@ -243,6 +257,38 @@ public RestorableState<AuthCredentials> capture() {
243257
}
244258
}
245259

260+
/**
261+
* A placeholder for credentials to signify that requests sent to the server should not be
262+
* authenticated. This is typically useful when using the local service emulators, such as
263+
* {@code LocalGcdHelper} and {@code LocalResourceManagerHelper}.
264+
*/
265+
public static class NoAuthCredentials extends AuthCredentials {
266+
267+
private static final AuthCredentials INSTANCE = new NoAuthCredentials();
268+
private static final NoAuthCredentialsState STATE = new NoAuthCredentialsState();
269+
270+
private static class NoAuthCredentialsState
271+
implements RestorableState<AuthCredentials>, Serializable {
272+
273+
private static final long serialVersionUID = -4022100563954640465L;
274+
275+
@Override
276+
public AuthCredentials restore() {
277+
return INSTANCE;
278+
}
279+
}
280+
281+
@Override
282+
public GoogleCredentials credentials() {
283+
return null;
284+
}
285+
286+
@Override
287+
public RestorableState<AuthCredentials> capture() {
288+
return STATE;
289+
}
290+
}
291+
246292
public abstract GoogleCredentials credentials();
247293

248294
public static AuthCredentials createForAppEngine() {
@@ -281,6 +327,15 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey
281327
return new ServiceAccountAuthCredentials(account, privateKey);
282328
}
283329

330+
/**
331+
* Creates a placeholder denoting that no credentials should be used. This is typically useful
332+
* when using the local service emulators, such as {@code LocalGcdHelper} and
333+
* {@code LocalResourceManagerHelper}.
334+
*/
335+
public static AuthCredentials noAuth() {
336+
return NoAuthCredentials.INSTANCE;
337+
}
338+
284339
/**
285340
* Creates Service Account Credentials given a stream for credentials in JSON format.
286341
*

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,10 @@ public RetryParams retryParams() {
523523
* options.
524524
*/
525525
public HttpRequestInitializer httpRequestInitializer() {
526-
final HttpRequestInitializer delegate = authCredentials() != null
527-
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
528-
: null;
526+
final HttpRequestInitializer delegate =
527+
authCredentials() != null && authCredentials.credentials() != null
528+
? new HttpCredentialsAdapter(authCredentials().credentials().createScoped(scopes()))
529+
: null;
529530
return new HttpRequestInitializer() {
530531
@Override
531532
public void initialize(HttpRequest httpRequest) throws IOException {

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.api.services.datastore.DatastoreV1.RunQueryResponse;
3333
import com.google.common.collect.Iterators;
3434
import com.google.common.collect.Lists;
35+
import com.google.gcloud.AuthCredentials;
3536
import com.google.gcloud.RetryParams;
3637
import com.google.gcloud.datastore.Query.ResultType;
3738
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
@@ -128,6 +129,7 @@ public void setUp() {
128129
options = DatastoreOptions.builder()
129130
.projectId(PROJECT_ID)
130131
.host("http://localhost:" + PORT)
132+
.authCredentials(AuthCredentials.noAuth())
131133
.retryParams(RetryParams.noRetries())
132134
.build();
133135
datastore = options.service();

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.google.common.collect.ImmutableMap;
1313
import com.google.common.collect.ImmutableSet;
1414
import com.google.common.io.ByteStreams;
15+
import com.google.gcloud.AuthCredentials;
1516
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
1617

1718
import com.sun.net.httpserver.Headers;
@@ -550,17 +551,21 @@ private LocalResourceManagerHelper() {
550551
}
551552

552553
/**
553-
* Creates a LocalResourceManagerHelper object that listens to requests on the local machine.
554+
* Creates a {@code LocalResourceManagerHelper} object that listens to requests on the local
555+
* machine.
554556
*/
555557
public static LocalResourceManagerHelper create() {
556558
return new LocalResourceManagerHelper();
557559
}
558560

559561
/**
560-
* Returns a ResourceManagerOptions instance that sets the host to use the mock server.
562+
* Returns a {@link ResourceManagerOptions} instance that sets the host to use the mock server.
561563
*/
562564
public ResourceManagerOptions options() {
563-
return ResourceManagerOptions.builder().host("http://localhost:" + port).build();
565+
return ResourceManagerOptions.builder()
566+
.host("http://localhost:" + port)
567+
.authCredentials(AuthCredentials.noAuth())
568+
.build();
564569
}
565570

566571
/**

0 commit comments

Comments
 (0)