Skip to content

Commit bc14948

Browse files
committed
Merge pull request #37 from rakyll/credentialsFile
Creating a Service Accounts generates .json not .P12
2 parents aee1b96 + 8eb8389 commit bc14948

8 files changed

Lines changed: 24 additions & 45 deletions

File tree

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ If you are not running this client on Google Compute Engine, you need a Google D
3434
* Google Cloud Storage JSON API
3535
* Google Cloud Pub/Sub
3636
* Once API access is enabled, switch back to "APIs & auth" section on the navigation panel and switch to "Credentials" page.
37-
* Click on "Create new client ID" to create a new **service account**. Once the account is created, a p12 file will be auto downloaded. You need to run the following command to convert this file to a pem file.
37+
* Click on "Create new client ID" to create a new **service account**. Once the account is created, click on "Generate new JSON key" to download
38+
your private key.
3839

39-
~~~~ sh
40-
openssl pkcs12 -in <key.p12> -nocerts -passin pass:notasecret -nodes -out <key.pem>
41-
~~~~
42-
43-
The pem file is the private key you'll need for authorization.
40+
The downloaded file contains credentials you'll need for authorization.
4441
* You'll the following for auth configuration:
4542
* Developers Console project's ID (e.g. bamboo-shift-455)
46-
* Service account's email address (e.g. xxx@developer.gserviceaccount.com)
47-
* The path to the pem file.
43+
* The path to the JSON key file.
4844

4945
## Developer's Guide
5046

@@ -92,8 +88,7 @@ Elsewhere, initiate with project ID, service account's email and private key dow
9288
var gcloud = require('gcloud'),
9389
ds = new gcloud.datastore.Dataset({
9490
projectId: YOUR_PROJECT_ID,
95-
email: 'xxx@developer.gserviceaccount.com',
96-
pemFilePath: '/path/to/the/pem/private/key.pem'
91+
keyFilename: '/path/to/the/key.json'
9792
});
9893
~~~~
9994

@@ -295,9 +290,8 @@ Elsewhere, initiate with bucket's name, service account's email and private key
295290
~~~~ js
296291
var gcloud = require('gcloud'),
297292
bucket = new gcloud.storage.Bucket({
298-
bucketName: YOUR_BUCKET_NAME,
299-
email: 'xxx@developer.gserviceaccount.com',
300-
pemFilePath: '/path/to/the/pem/private/key.pem'
293+
projectId: YOUR_PROJECT_ID,
294+
keyFilename: '/path/to/the/key.json'
301295
});
302296
~~~~
303297

lib/common/connection.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ Token.prototype.isExpired = function() {
4444
};
4545

4646
/**
47-
* @param {Object} opts Options. { email, privateKey, scopes }
47+
* @param {Object} opts Options.
4848
*/
4949
function Connection(opts) {
50-
// TODO: If no email and key is provided, use metaserver to retrieve a new token.
51-
this.email = opts.email; // client email for the service account
52-
this.privateKey = opts.privateKey; // contains the contents of a pem file
50+
var credentials = opts.keyFilename && require(opts.keyFilename) || {};
51+
this.email = credentials['client_email']; // client email for the service account
52+
this.privateKey = credentials['private_key']; // contains the contents of a pem file
53+
5354
this.scopes = opts.scopes || [];
5455
this.token = null; // existing access token, if exists
5556

@@ -106,7 +107,7 @@ Connection.prototype.fetchToken = function(callback) {
106107
}
107108
var gapi = new GAPIToken({
108109
iss: this.email,
109-
keyFile: this.privateKey,
110+
key: this.privateKey,
110111
scope: this.scopes.join(' ')
111112
}, function(err) {
112113
if (err) {

lib/datastore/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ Transaction.prototype.makeReq = function(method, req, callback) {
313313
* Creates a new dataset with the provided options.
314314
* @param {object} opts Dataset identifier options.
315315
* @param {string} opts.id Dataset ID, this is your project ID
316-
* from Google Developer Console.
317-
* @param {string} opts.email Client email of the service account.
318-
* @param {string} opts.pemFilepath The path to the pem file.
316+
* from Google Developers Console.
317+
* @param {string} opts.keyFilename Path to the JSON key file downloaded from
318+
* Google Developers Console.
319319
*/
320320
function Dataset(opts) {
321321
opts = opts || {};
@@ -327,8 +327,7 @@ function Dataset(opts) {
327327

328328
this.id = id;
329329
this.transaction = new Transaction(new conn.Connection({
330-
email: opts.email,
331-
privateKey: opts.pemFilePath,
330+
keyFilename: opts.keyFilename,
332331
scopes: SCOPES
333332
}), this.id);
334333
}

lib/pubsub/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ function Connection(opts) {
219219

220220
this.id = id;
221221
this.conn = new conn.Connection({
222-
email: opts.email,
223-
privateKey: opts.pemFilePath,
222+
keyFilename: opts.keyFilename,
224223
scopes: SCOPES
225224
});
226225
}

lib/storage/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ ReadStream.prototype.pipe = function(dest, opts) {
148148
function Bucket(opts) {
149149
this.bucketName = opts.bucketName;
150150
this.conn = new conn.Connection({
151-
email: opts.email,
152-
privateKey: opts.pemFilePath,
151+
keyFilename: opts.keyFilename,
153152
scopes: SCOPES
154153
});
155154
}

regression/datastore.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
var env = require('./env.js'),
18-
projectId = env.projectId,
19-
email = env.serviceAccount,
20-
pemFilePath = env.pemKey;
17+
var env = require('./env.js');
2118

2219
var assert = require('assert'),
2320
datastore = require('../lib/datastore'),
24-
ds = new datastore.Dataset({
25-
projectId: projectId,
26-
email: email,
27-
pemFilePath: pemFilePath
28-
});
21+
ds = new datastore.Dataset(env);
2922

3023
describe('datastore', function() {
3124

regression/env.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616

1717
if (!process.env.GCLOUD_TESTS_PROJECT_ID &&
18-
!process.env.GCLOUD_TESTS_SERVICE_ACCOUNT &&
19-
!process.env.GCLOUD_TESTS_PEM_KEY) {
18+
!process.env.GCLOUD_TESTS_KEY) {
2019
var error = ['To run the regression tests, you need to set the value of some environment variables.',
2120
'Please check the README for instructions.'
2221
].join('\n');
@@ -25,6 +24,5 @@ if (!process.env.GCLOUD_TESTS_PROJECT_ID &&
2524

2625
module.exports = {
2726
projectId: process.env.GCLOUD_TESTS_PROJECT_ID,
28-
serviceAccount: process.env.GCLOUD_TESTS_SERVICE_ACCOUNT,
29-
pemKey: process.env.GCLOUD_TESTS_PEM_KEY
27+
keyFilename: process.env.GCLOUD_TESTS_KEY
3028
};

regression/pubsub.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ var subscriptions = [{
2929
ackDeadlineSeconds: 60
3030
}];
3131

32-
var conn = new gcloud.pubsub.Connection({
33-
projectId: env.projectId,
34-
email: env.serviceAccount,
35-
pemFilePath: env.pemKey,
36-
});
32+
var conn = new gcloud.pubsub.Connection(env);
3733

3834
before(function(done) {
3935
// TODO: Handle pagination.

0 commit comments

Comments
 (0)