Skip to content

Commit

Permalink
Merge pull request #37 from rakyll/credentialsFile
Browse files Browse the repository at this point in the history
Creating a Service Accounts generates .json not .P12
  • Loading branch information
silvolu committed Jul 29, 2014
2 parents aee1b96 + 8eb8389 commit bc14948
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 45 deletions.
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ If you are not running this client on Google Compute Engine, you need a Google D
* Google Cloud Storage JSON API
* Google Cloud Pub/Sub
* Once API access is enabled, switch back to "APIs & auth" section on the navigation panel and switch to "Credentials" page.
* 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.
* 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
your private key.

~~~~ sh
openssl pkcs12 -in <key.p12> -nocerts -passin pass:notasecret -nodes -out <key.pem>
~~~~

The pem file is the private key you'll need for authorization.
The downloaded file contains credentials you'll need for authorization.
* You'll the following for auth configuration:
* Developers Console project's ID (e.g. bamboo-shift-455)
* Service account's email address (e.g. xxx@developer.gserviceaccount.com)
* The path to the pem file.
* The path to the JSON key file.

## Developer's Guide

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

Expand Down Expand Up @@ -295,9 +290,8 @@ Elsewhere, initiate with bucket's name, service account's email and private key
~~~~ js
var gcloud = require('gcloud'),
bucket = new gcloud.storage.Bucket({
bucketName: YOUR_BUCKET_NAME,
email: 'xxx@developer.gserviceaccount.com',
pemFilePath: '/path/to/the/pem/private/key.pem'
projectId: YOUR_PROJECT_ID,
keyFilename: '/path/to/the/key.json'
});
~~~~

Expand Down
11 changes: 6 additions & 5 deletions lib/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Token.prototype.isExpired = function() {
};

/**
* @param {Object} opts Options. { email, privateKey, scopes }
* @param {Object} opts Options.
*/
function Connection(opts) {
// TODO: If no email and key is provided, use metaserver to retrieve a new token.
this.email = opts.email; // client email for the service account
this.privateKey = opts.privateKey; // contains the contents of a pem file
var credentials = opts.keyFilename && require(opts.keyFilename) || {};
this.email = credentials['client_email']; // client email for the service account
this.privateKey = credentials['private_key']; // contains the contents of a pem file

this.scopes = opts.scopes || [];
this.token = null; // existing access token, if exists

Expand Down Expand Up @@ -106,7 +107,7 @@ Connection.prototype.fetchToken = function(callback) {
}
var gapi = new GAPIToken({
iss: this.email,
keyFile: this.privateKey,
key: this.privateKey,
scope: this.scopes.join(' ')
}, function(err) {
if (err) {
Expand Down
9 changes: 4 additions & 5 deletions lib/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ Transaction.prototype.makeReq = function(method, req, callback) {
* Creates a new dataset with the provided options.
* @param {object} opts Dataset identifier options.
* @param {string} opts.id Dataset ID, this is your project ID
* from Google Developer Console.
* @param {string} opts.email Client email of the service account.
* @param {string} opts.pemFilepath The path to the pem file.
* from Google Developers Console.
* @param {string} opts.keyFilename Path to the JSON key file downloaded from
* Google Developers Console.
*/
function Dataset(opts) {
opts = opts || {};
Expand All @@ -327,8 +327,7 @@ function Dataset(opts) {

this.id = id;
this.transaction = new Transaction(new conn.Connection({
email: opts.email,
privateKey: opts.pemFilePath,
keyFilename: opts.keyFilename,
scopes: SCOPES
}), this.id);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ function Connection(opts) {

this.id = id;
this.conn = new conn.Connection({
email: opts.email,
privateKey: opts.pemFilePath,
keyFilename: opts.keyFilename,
scopes: SCOPES
});
}
Expand Down
3 changes: 1 addition & 2 deletions lib/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ ReadStream.prototype.pipe = function(dest, opts) {
function Bucket(opts) {
this.bucketName = opts.bucketName;
this.conn = new conn.Connection({
email: opts.email,
privateKey: opts.pemFilePath,
keyFilename: opts.keyFilename,
scopes: SCOPES
});
}
Expand Down
11 changes: 2 additions & 9 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@
* limitations under the License.
*/

var env = require('./env.js'),
projectId = env.projectId,
email = env.serviceAccount,
pemFilePath = env.pemKey;
var env = require('./env.js');

var assert = require('assert'),
datastore = require('../lib/datastore'),
ds = new datastore.Dataset({
projectId: projectId,
email: email,
pemFilePath: pemFilePath
});
ds = new datastore.Dataset(env);

describe('datastore', function() {

Expand Down
6 changes: 2 additions & 4 deletions regression/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/

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

module.exports = {
projectId: process.env.GCLOUD_TESTS_PROJECT_ID,
serviceAccount: process.env.GCLOUD_TESTS_SERVICE_ACCOUNT,
pemKey: process.env.GCLOUD_TESTS_PEM_KEY
keyFilename: process.env.GCLOUD_TESTS_KEY
};
6 changes: 1 addition & 5 deletions regression/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ var subscriptions = [{
ackDeadlineSeconds: 60
}];

var conn = new gcloud.pubsub.Connection({
projectId: env.projectId,
email: env.serviceAccount,
pemFilePath: env.pemKey,
});
var conn = new gcloud.pubsub.Connection(env);

before(function(done) {
// TODO: Handle pagination.
Expand Down

0 comments on commit bc14948

Please sign in to comment.