Skip to content

Commit e5f9046

Browse files
authored
Remove AuthCredentials and related classes, use google-auth-library-java instead (#1375)
* Remove AuthCredentials and related classes, use google-auth-library-java instead * Throw NPE when setCredentials is called with null * Fix wording in main README's Authentication section * Add NoCredentials class and remove setNoCredentials method
1 parent e4fb76b commit e5f9046

File tree

50 files changed

+298
-1109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+298
-1109
lines changed

README.md

+39-16
Original file line numberDiff line numberDiff line change
@@ -115,40 +115,63 @@ Most `google-cloud` libraries require a project ID. There are multiple ways to
115115
Authentication
116116
--------------
117117

118-
First, ensure that the necessary Google Cloud APIs are enabled for your project. To do this, follow the instructions on the [authentication document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/authentication/readme.md#authentication) shared by all the gcloud language libraries.
118+
`google-cloud-java` uses
119+
[https://github.com/google/google-auth-library-java](https://github.com/google/google-auth-library-java)
120+
to authenticate requests. `google-auth-library-java` supports a wide range of authentication types;
121+
see the project's [README](https://github.com/google/google-auth-library-java/blob/master/README.md)
122+
and [javadoc](http://google.github.io/google-auth-library-java/releases/0.6.0/apidocs/) for more
123+
details.
124+
125+
To access Google Cloud services, you first need to ensure that the necessary Google Cloud APIs are
126+
enabled for your project. To do this, follow the instructions on the
127+
[authentication document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/authentication/readme.md#authentication)
128+
shared by all the Google Cloud language libraries.
119129

120130
Next, choose a method for authenticating API requests from within your project:
121131

122-
1. When using `google-cloud` libraries from within Compute/App Engine, no additional authentication steps are necessary.
123-
2. When using `google-cloud` libraries elsewhere, there are three options:
124-
* [Generate a JSON service account key](https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts). After downloading that key, you must do one of the following:
125-
* Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:
132+
1. When using `google-cloud` libraries from within Compute/App Engine, no additional authentication
133+
steps are necessary. For example:
134+
```java
135+
Storage storage = StorageOptions.getDefaultInstance().getService();
136+
```
137+
2. When using `google-cloud` libraries elsewhere, there are several options:
138+
* [Generate a JSON service account key](https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts).
139+
After downloading that key, you must do one of the following:
140+
* Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key.
141+
For example:
126142
```bash
127143
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json
128144
```
129-
* Supply the JSON credentials file when building the service options. For example, this Storage object has the necessary permissions to interact with your Google Cloud Storage data:
145+
* Supply the JSON credentials file when building the service options. For example, this Storage
146+
object has the necessary permissions to interact with your Google Cloud Storage data:
130147
```java
131148
Storage storage = StorageOptions.newBuilder()
132-
.setAuthCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
149+
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/my/key.json"))
133150
.build()
134151
.getService();
135152
```
136-
* If running locally for development/testing, you can use Google Cloud SDK. Download the SDK if you haven't already, then login using the SDK (`gcloud auth login` in command line). Be sure to set your project ID as described above.
137-
* If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case the access token will not be automatically refreshed):
153+
* If running locally for development/testing, you can use the
154+
[Google Cloud SDK](https://cloud.google.com/sdk/). Create Application Default Credentials with
155+
`gcloud auth application-default login`, and then `google-cloud` will automatically detect such
156+
credentials.
157+
* If you already have an OAuth2 access token, you can use it to authenticate (notice that in this
158+
case, the access token will not be automatically refreshed):
138159
```java
139160
Storage storage = StorageOptions.newBuilder()
140-
.setAuthCredentials(AuthCredentials.createFor("your_access_token"))
161+
.setCredentials(new GoogleCredentials(new AccessToken(accessToken, expirationTime)))
141162
.build()
142163
.getService();
143164
```
144165
145-
`google-cloud` looks for credentials in the following order, stopping once it finds credentials:
166+
If no credentials are provided, `google-cloud` will attempt to detect them from the environment
167+
using `GoogleCredentials.getApplicationDefault()` which will search for Default Application
168+
Credentials in the following locations (in order):
146169
147-
1. Credentials supplied when building the service options
148-
2. App Engine credentials
149-
3. Key file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
150-
4. Google Cloud SDK credentials
151-
5. Compute Engine credentials
170+
1. The credentials file pointed to by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
171+
2. Credentials provided by the Google Cloud SDK `gcloud auth application-default login` command
172+
3. Google App Engine built-in credentials
173+
4. Google Cloud Shell built-in credentials
174+
5. Google Compute Engine built-in credentials
152175
153176
Google Cloud BigQuery (Alpha)
154177
----------------------

TESTING.md

+2-2
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-
.setAuthCredentials(AuthCredentials.noAuth())
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-
.setAuthCredentials(AuthCredentials.noAuth())
212+
.setCredentials(NoCredentials.getInstance())
213213
.build();
214214
PubSub localPubsub = options.getService();
215215
```

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class BigQueryOptions extends HttpServiceOptions<BigQuery, BigQueryRpc, B
2828

2929
private static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery";
3030
private static final Set<String> SCOPES = ImmutableSet.of(BIGQUERY_SCOPE);
31-
private static final long serialVersionUID = -8592198255032667206L;
31+
private static final long serialVersionUID = -2437598817433266049L;
3232

3333
public static class DefaultBigqueryFactory implements BigQueryFactory {
3434

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.cloud.bigquery.testing;
1818

19-
import com.google.cloud.AuthCredentials;
19+
import com.google.auth.oauth2.ServiceAccountCredentials;
2020
import com.google.cloud.RetryParams;
2121
import com.google.cloud.bigquery.BigQuery;
2222
import com.google.cloud.bigquery.BigQueryException;
@@ -96,7 +96,7 @@ public static RemoteBigQueryHelper create(String projectId, InputStream keyStrea
9696
throws BigQueryHelperException {
9797
try {
9898
BigQueryOptions bigqueryOptions = BigQueryOptions.newBuilder()
99-
.setAuthCredentials(AuthCredentials.createForJson(keyStream))
99+
.setCredentials(ServiceAccountCredentials.fromStream(keyStream))
100100
.setProjectId(projectId)
101101
.setRetryParams(retryParams())
102102
.setConnectTimeout(60000)

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.google.cloud.bigquery;
1818

19-
import com.google.cloud.AuthCredentials;
2019
import com.google.cloud.BaseSerializationTest;
20+
import com.google.cloud.NoCredentials;
2121
import com.google.cloud.Restorable;
2222
import com.google.cloud.bigquery.StandardTableDefinition.StreamingBuffer;
2323
import com.google.common.collect.ImmutableList;
@@ -229,12 +229,9 @@ public class SerializationTest extends BaseSerializationTest {
229229
protected Serializable[] serializableObjects() {
230230
BigQueryOptions options = BigQueryOptions.newBuilder()
231231
.setProjectId("p1")
232-
.setAuthCredentials(AuthCredentials.createForAppEngine())
233-
.build();
234-
BigQueryOptions otherOptions = options.toBuilder()
235-
.setProjectId("p2")
236-
.setAuthCredentials(null)
232+
.setCredentials(NoCredentials.getInstance())
237233
.build();
234+
BigQueryOptions otherOptions = options.toBuilder().setProjectId("p2").build();
238235
return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID,
239236
DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION,
240237
EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO,

google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeOptions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ComputeOptions extends HttpServiceOptions<Compute, ComputeRpc, Comp
2828

2929
private static final String COMPUTE_SCOPE = "https://www.googleapis.com/auth/compute";
3030
private static final Set<String> SCOPES = ImmutableSet.of(COMPUTE_SCOPE);
31-
private static final long serialVersionUID = 5074781985597996770L;
31+
private static final long serialVersionUID = 6983703596543425691L;
3232

3333
public static class DefaultComputeFactory implements ComputeFactory {
3434

google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.cloud.compute.testing;
1818

19-
import com.google.cloud.AuthCredentials;
19+
import com.google.auth.oauth2.ServiceAccountCredentials;
2020
import com.google.cloud.RetryParams;
2121
import com.google.cloud.compute.ComputeOptions;
2222

@@ -83,7 +83,7 @@ public static String baseResourceName() {
8383
public static RemoteComputeHelper create(String projectId, InputStream keyStream) {
8484
try {
8585
ComputeOptions computeOptions = ComputeOptions.newBuilder()
86-
.setAuthCredentials(AuthCredentials.createForJson(keyStream))
86+
.setCredentials(ServiceAccountCredentials.fromStream(keyStream))
8787
.setProjectId(projectId)
8888
.setRetryParams(retryParams())
8989
.setConnectTimeout(60000)

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.google.cloud.compute;
1818

19-
import com.google.cloud.AuthCredentials;
2019
import com.google.cloud.BaseSerializationTest;
20+
import com.google.cloud.NoCredentials;
2121
import com.google.cloud.Restorable;
2222
import com.google.cloud.RetryParams;
2323
import com.google.cloud.compute.AttachedDisk.CreateDiskConfiguration;
@@ -265,12 +265,11 @@ public class SerializationTest extends BaseSerializationTest {
265265
protected Serializable[] serializableObjects() {
266266
ComputeOptions options = ComputeOptions.newBuilder()
267267
.setProjectId("p1")
268-
.setAuthCredentials(AuthCredentials.createForAppEngine())
268+
.setCredentials(NoCredentials.getInstance())
269269
.build();
270270
ComputeOptions otherOptions = options.toBuilder()
271271
.setProjectId("p2")
272272
.setRetryParams(RetryParams.getDefaultInstance())
273-
.setAuthCredentials(null)
274273
.build();
275274
return new Serializable[]{DISK_TYPE_ID, DISK_TYPE, MACHINE_TYPE_ID, MACHINE_TYPE, REGION_ID,
276275
REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE, DEPRECATION_STATUS, GLOBAL_OPERATION_ID,

google-cloud-core/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
<dependency>
2121
<groupId>com.google.auth</groupId>
2222
<artifactId>google-auth-library-credentials</artifactId>
23-
<version>0.3.1</version>
23+
<version>${google.auth.version}</version>
2424
</dependency>
2525
<dependency>
2626
<groupId>com.google.auth</groupId>
2727
<artifactId>google-auth-library-oauth2-http</artifactId>
28-
<version>0.3.1</version>
28+
<version>${google.auth.version}</version>
2929
<exclusions>
3030
<exclusion>
3131
<groupId>com.google.guava</groupId>

0 commit comments

Comments
 (0)