Skip to content

Commit 85e06c2

Browse files
committed
Add NoCredentials class and remove setNoCredentials method
1 parent 96c2b60 commit 85e06c2

20 files changed

Lines changed: 88 additions & 49 deletions

File tree

TESTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ You can test against a remote Datastore emulator as well. To do this, set the `
102102
DatastoreOptions options = DatastoreOptions.newBuilder()
103103
.setProjectId("my-project-id") // must match project ID specified on remote machine
104104
.setHost("http://<hostname of machine>:<port>")
105-
.setNoCredentials()
105+
.setCredentials(NoCredentials.getInstance())
106106
.build();
107107
Datastore localDatastore = options.getService();
108108
```
@@ -209,7 +209,7 @@ endpoint to the hostname of the remote machine, like the example below.
209209
PubSubOptions options = PubSubOptions.newBuilder()
210210
.setProjectId("my-project-id") // must match project ID specified on remote machine
211211
.setHost("<hostname of machine>:<port>")
212-
.setNoCredentials()
212+
.setCredentials(NoCredentials.getInstance())
213213
.build();
214214
PubSub localPubsub = options.getService();
215215
```

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/SerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.bigquery;
1818

1919
import com.google.cloud.BaseSerializationTest;
20+
import com.google.cloud.NoCredentials;
2021
import com.google.cloud.Restorable;
2122
import com.google.cloud.bigquery.StandardTableDefinition.StreamingBuffer;
2223
import com.google.common.collect.ImmutableList;
@@ -228,7 +229,7 @@ public class SerializationTest extends BaseSerializationTest {
228229
protected Serializable[] serializableObjects() {
229230
BigQueryOptions options = BigQueryOptions.newBuilder()
230231
.setProjectId("p1")
231-
.setNoCredentials()
232+
.setCredentials(NoCredentials.getInstance())
232233
.build();
233234
BigQueryOptions otherOptions = options.toBuilder().setProjectId("p2").build();
234235
return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID,

google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.compute;
1818

1919
import com.google.cloud.BaseSerializationTest;
20+
import com.google.cloud.NoCredentials;
2021
import com.google.cloud.Restorable;
2122
import com.google.cloud.RetryParams;
2223
import com.google.cloud.compute.AttachedDisk.CreateDiskConfiguration;
@@ -264,7 +265,7 @@ public class SerializationTest extends BaseSerializationTest {
264265
protected Serializable[] serializableObjects() {
265266
ComputeOptions options = ComputeOptions.newBuilder()
266267
.setProjectId("p1")
267-
.setNoCredentials()
268+
.setCredentials(NoCredentials.getInstance())
268269
.build();
269270
ComputeOptions otherOptions = options.toBuilder()
270271
.setProjectId("p2")

google-cloud-core/src/main/java/com/google/cloud/GrpcServiceOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected ConnectionSettings.Builder getConnectionSettings() {
320320
.setServiceAddress(hostAndPort.getHostText())
321321
.setPort(hostAndPort.getPort());
322322
Credentials scopedCredentials = getScopedCredentials();
323-
if (scopedCredentials != null) {
323+
if (scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()) {
324324
builder.provideCredentialsWith(scopedCredentials);
325325
}
326326
return builder;

google-cloud-core/src/main/java/com/google/cloud/HttpServiceOptions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ public HttpRequestInitializer httpRequestInitializer() {
215215
public HttpRequestInitializer getHttpRequestInitializer() {
216216
Credentials scopedCredentials = getScopedCredentials();
217217
final HttpRequestInitializer delegate =
218-
scopedCredentials != null ? new HttpCredentialsAdapter(scopedCredentials) : null;
218+
scopedCredentials != null && scopedCredentials != NoCredentials.getInstance()
219+
? new HttpCredentialsAdapter(scopedCredentials) : null;
219220
return new HttpRequestInitializer() {
220221
@Override
221222
public void initialize(HttpRequest httpRequest) throws IOException {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import com.google.auth.oauth2.OAuth2Credentials;
20+
21+
import java.io.ObjectStreamException;
22+
23+
/**
24+
* A placeholder for credentials to signify that requests sent to the server should not be
25+
* authenticated. This is typically useful when using local service emulators.
26+
*/
27+
public class NoCredentials extends OAuth2Credentials {
28+
29+
private static final long serialVersionUID = -6263971603971044288L;
30+
private static final NoCredentials INSTANCE = new NoCredentials();
31+
32+
private NoCredentials() {}
33+
34+
private Object readResolve() throws ObjectStreamException {
35+
return INSTANCE;
36+
}
37+
38+
public static NoCredentials getInstance() {
39+
return INSTANCE;
40+
}
41+
}

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

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, Service
8484
private final String serviceRpcFactoryClassName;
8585
private final String serviceFactoryClassName;
8686
private final Clock clock;
87-
private final boolean noCredentials;
8887
private final Credentials credentials;
8988

9089
private transient ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFactory;
@@ -106,7 +105,6 @@ protected abstract static class Builder<ServiceT extends Service<OptionsT>, Serv
106105

107106
private String projectId;
108107
private String host;
109-
private boolean noCredentials;
110108
private Credentials credentials;
111109
private RetryParams retryParams;
112110
private ServiceFactory<ServiceT, OptionsT> serviceFactory;
@@ -118,7 +116,6 @@ protected Builder() {}
118116
protected Builder(ServiceOptions<ServiceT, ServiceRpcT, OptionsT> options) {
119117
projectId = options.projectId;
120118
host = options.host;
121-
noCredentials = options.noCredentials;
122119
credentials = options.credentials;
123120
retryParams = options.retryParams;
124121
serviceFactory = options.serviceFactory;
@@ -214,29 +211,18 @@ public B setHost(String host) {
214211
}
215212

216213
/**
217-
* Sets the service authentication credentials. If this method or {@link #setNoCredentials() are
218-
* not used on the builder, {@link GoogleCredentials#getApplicationDefault()} will be used to
219-
* attempt getting credentials from the environment.
214+
* Sets the service authentication credentials. If no credentials are set,
215+
* {@link GoogleCredentials#getApplicationDefault()} will be used to attempt getting credentials
216+
* from the environment. Use {@link NoCredentials#getInstance()} to skip authentication, this is
217+
* typically useful when using local service emulators.
220218
*
221219
* @param credentials authentication credentials, should not be {@code null}
222220
* @return the builder
223221
* @throws NullPointerException if {@code credentials} is {@code null}. To disable
224-
* authentication use {@link Builder#setNoCredentials()}
222+
* authentication use {@link NoCredentials#getInstance()}
225223
*/
226224
public B setCredentials(Credentials credentials) {
227225
this.credentials = checkNotNull(credentials);
228-
this.noCredentials = false;
229-
return self();
230-
}
231-
232-
/**
233-
* Sets that no credentials should be used. This is typically useful when using the local
234-
* service emulators, such as {@code LocalDatastoreHelper}, {@code LocalPubsubHelper} and
235-
* {@code LocalResourceManagerHelper}.
236-
*/
237-
public B setNoCredentials() {
238-
this.noCredentials = true;
239-
this.credentials = null;
240226
return self();
241227
}
242228

@@ -296,9 +282,7 @@ protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> ser
296282
+ "or the environment. Please set a project ID using the builder.");
297283
}
298284
host = firstNonNull(builder.host, getDefaultHost());
299-
noCredentials = builder.noCredentials;
300-
credentials = builder.credentials != null || noCredentials
301-
? builder.credentials : defaultCredentials();
285+
credentials = builder.credentials != null ? builder.credentials : defaultCredentials();
302286
retryParams = firstNonNull(builder.retryParams, defaultRetryParams());
303287
serviceFactory = firstNonNull(builder.serviceFactory,
304288
getFromServiceLoader(serviceFactoryClass, getDefaultServiceFactory()));
@@ -540,8 +524,8 @@ public Credentials getCredentials() {
540524
*/
541525
public Credentials getScopedCredentials() {
542526
Credentials credentialsToReturn = credentials;
543-
if (credentials instanceof GoogleCredentials &&
544-
((GoogleCredentials) credentials).createScopedRequired()) {
527+
if (credentials instanceof GoogleCredentials
528+
&& ((GoogleCredentials) credentials).createScopedRequired()) {
545529
credentialsToReturn = ((GoogleCredentials) credentials).createScoped(getScopes());
546530
}
547531
return credentialsToReturn;
@@ -626,13 +610,12 @@ public String getLibraryVersion() {
626610
}
627611

628612
protected int baseHashCode() {
629-
return Objects.hash(projectId, host, noCredentials, credentials, retryParams,
630-
serviceFactoryClassName, serviceRpcFactoryClassName, clock);
613+
return Objects.hash(projectId, host, credentials, retryParams, serviceFactoryClassName,
614+
serviceRpcFactoryClassName, clock);
631615
}
632616

633617
protected boolean baseEquals(ServiceOptions<?, ?, ?> other) {
634-
return noCredentials == other.noCredentials
635-
&& Objects.equals(projectId, other.projectId)
618+
return Objects.equals(projectId, other.projectId)
636619
&& Objects.equals(host, other.host)
637620
&& Objects.equals(credentials, other.credentials)
638621
&& Objects.equals(retryParams, other.retryParams)

google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class ServiceOptionsTest {
8484
.build();
8585
private static final TestServiceOptions OPTIONS_NO_CREDENTIALS =
8686
TestServiceOptions.newBuilder()
87-
.setNoCredentials()
87+
.setCredentials(NoCredentials.getInstance())
8888
.setClock(TEST_CLOCK)
8989
.setHost("host")
9090
.setProjectId("project-id")
@@ -224,7 +224,7 @@ public void testBuilder() {
224224

225225
@Test
226226
public void testBuilderNoCredentials() {
227-
assertNull(OPTIONS_NO_CREDENTIALS.getCredentials());
227+
assertEquals(NoCredentials.getInstance(), OPTIONS_NO_CREDENTIALS.getCredentials());
228228
assertSame(TEST_CLOCK, OPTIONS_NO_CREDENTIALS.getClock());
229229
assertEquals("host", OPTIONS_NO_CREDENTIALS.getHost());
230230
assertEquals("project-id", OPTIONS_NO_CREDENTIALS.getProjectId());

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.MoreObjects.firstNonNull;
2020
import static com.google.common.base.Preconditions.checkArgument;
2121

22+
import com.google.cloud.NoCredentials;
2223
import com.google.cloud.RetryParams;
2324
import com.google.cloud.datastore.DatastoreOptions;
2425
import com.google.common.base.Strings;
@@ -639,7 +640,7 @@ private DatastoreOptions.Builder optionsBuilder() {
639640
return DatastoreOptions.newBuilder()
640641
.setProjectId(projectId)
641642
.setHost("localhost:" + Integer.toString(port))
642-
.setNoCredentials()
643+
.setCredentials(NoCredentials.getInstance())
643644
.setRetryParams(RetryParams.noRetries());
644645
}
645646

google-cloud-datastore/src/test/java/com/google/cloud/datastore/SerializationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020

2121
import com.google.cloud.BaseSerializationTest;
22+
import com.google.cloud.NoCredentials;
2223
import com.google.cloud.Restorable;
2324
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
2425
import com.google.cloud.datastore.StructuredQuery.OrderBy;
@@ -110,7 +111,7 @@ public class SerializationTest extends BaseSerializationTest {
110111
@Override
111112
protected java.io.Serializable[] serializableObjects() {
112113
DatastoreOptions options = DatastoreOptions.newBuilder()
113-
.setNoCredentials()
114+
.setCredentials(NoCredentials.getInstance())
114115
.setProjectId("ds1")
115116
.build();
116117
DatastoreOptions otherOptions = options.toBuilder().setNamespace("ns1").build();

0 commit comments

Comments
 (0)