Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 5b35412

Browse files
authored
Added keyFilename/credentials to config object (#169)
PR-URL: #169
1 parent 7166534 commit 5b35412

File tree

6 files changed

+165
-11
lines changed

6 files changed

+165
-11
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,28 @@ If your application is running outside of Google Cloud Platform, such as locally
5353

5454
export GCLOUD_PROJECT=<project name>
5555

56-
2. You need to provide service account credentials to your application. The recommended way is via [Application Default Credentials][app-default-credentials].
57-
58-
1. [Create a new JSON service account key][service-account].
59-
2. Copy the key somewhere your application can access it. Be sure not to expose the key publicly.
60-
3. Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the full path to the key. The debug agent will automatically look for this environment variable.
61-
62-
3. Alternatively, if you are running your application on a machine where your are using the [`gcloud` command line tools][gcloud-sdk], and are logged using `gcloud auth login`, you already have sufficient credentials, and a service account key is not required.
63-
64-
4. Generate a `source-context.json` file which contains information about the version of the source code used to build the application. This file should be located in the root directory of your application. When you open the Stackdriver Debugger in the Cloud Platform Console, it uses the information in this file to display the correct version of the source.
56+
1. You need to provide service account credentials to your application.
57+
* The recommended way is via [Application Default Credentials][app-default-credentials].
58+
1. [Create a new JSON service account key][service-account].
59+
1. Copy the key somewhere your application can access it. Be sure not to expose the key publicly.
60+
1. Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the full path to the key. The debug agent will automatically look for this environment variable.
61+
* If you are running your application on a machine where your are using the [`gcloud` command line tools][gcloud-sdk], and are logged using `gcloud auth login`, you already have sufficient credentials, and a service account key is not required.
62+
* Alternatively, you may set the keyFilename or credentials configuration field to the full path or contents to the key file, respectively. Setting either of these fields will override either setting GOOGLE_APPLICATION_CREDENTIALS or logging in using gcloud. For example:
63+
64+
```js
65+
// Require and start the agent with configuration options
66+
require('@google/cloud-debug').start({
67+
// The path to your key file:
68+
keyFilename: '/path/to/keyfile.json',
69+
70+
// Or the contents of the key file:
71+
credentials: require('./path/to/keyfile.json')
72+
});
73+
```
74+
75+
See the [default configuration](https://github.com/GoogleCloudPlatform/cloud-debug-nodejs/blob/master/config.js) for more details.
76+
77+
1. Generate a `source-context.json` file which contains information about the version of the source code used to build the application. This file should be located in the root directory of your application. When you open the Stackdriver Debugger in the Cloud Platform Console, it uses the information in this file to display the correct version of the source.
6578

6679
gcloud app gen-repo-info-file
6780

config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ module.exports = {
8484
logDelaySeconds: 1
8585
},
8686

87+
// A path to a key file relative to the current working directory. If this
88+
// field is set, the contents of the pointed file will be used for
89+
// authentication instead of your application default credentials.
90+
keyFilename: null,
91+
92+
// The contents of a key file. If this field is set, its contents will be
93+
// used for authentication instead of your application default credentials.
94+
// If keyFilename is also set, the value of credentials will be ignored.
95+
credentials: null,
96+
8797
// These configuration options are for internal experimentation only.
8898
internal: {
8999
registerDelayOnFetcherErrorSec: 300, // 5 minutes.

lib/debugletapi.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ function DebugletApi(config) {
4545
var config_ = config || {};
4646

4747
/** @private {Object} request style request object */
48-
this.request_ = utils.authorizedRequestFactory(SCOPES);
48+
this.request_ = utils.authorizedRequestFactory(SCOPES, {
49+
keyFile: config_.keyFilename,
50+
credentials: config_.credentials
51+
});
4952

5053
/** @private {string} numeric project id */
5154
this.project_ = null;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"proxyquire": "^1.4.0"
6060
},
6161
"dependencies": {
62-
"@google/cloud-diagnostics-common": "0.2.5",
62+
"@google/cloud-diagnostics-common": "0.3.0",
6363
"acorn": "^3.3.0",
6464
"async": "^2.1.2",
6565
"coffee-script": "^1.9.3",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"client_id": "x",
3+
"client_secret": "y",
4+
"refresh_token": "z",
5+
"type": "authorized_user"
6+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
'use strict';
17+
18+
var path = require('path');
19+
var assert = require('assert');
20+
var nock = require('nock');
21+
22+
process.env.GCLOUD_PROJECT = 0;
23+
24+
describe('test-config-credentials', function() {
25+
it('should use the keyFilename field of the config object', function(done) {
26+
var credentials = require('../fixtures/gcloud-credentials.json');
27+
var config = {
28+
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json')
29+
};
30+
var agent = require('../..');
31+
nock.disableNetConnect();
32+
var scope = nock('https://accounts.google.com')
33+
.post('/o/oauth2/token', function(body) {
34+
assert.equal(body.client_id, credentials.client_id);
35+
assert.equal(body.client_secret, credentials.client_secret);
36+
assert.equal(body.refresh_token, credentials.refresh_token);
37+
return true;
38+
}).reply(200, {
39+
refresh_token: 'hello',
40+
access_token: 'goodbye',
41+
expiry_date: new Date(9999, 1, 1)
42+
});
43+
// Since we have to get an auth token, this always gets intercepted second
44+
nock('https://clouddebugger.googleapis.com')
45+
.post('/v2/controller/debuggees/register', function() {
46+
scope.done();
47+
agent.start.wasSuccessful_ = false;
48+
setImmediate(done);
49+
return true;
50+
}).reply(200);
51+
agent.start(config);
52+
});
53+
54+
it('should use the credentials field of the config object', function(done) {
55+
var config = {
56+
credentials: require('../fixtures/gcloud-credentials.json')
57+
};
58+
var agent = require('../..');
59+
nock.disableNetConnect();
60+
var scope = nock('https://accounts.google.com')
61+
.post('/o/oauth2/token', function(body) {
62+
assert.equal(body.client_id, config.credentials.client_id);
63+
assert.equal(body.client_secret, config.credentials.client_secret);
64+
assert.equal(body.refresh_token, config.credentials.refresh_token);
65+
return true;
66+
}).reply(200, {
67+
refresh_token: 'hello',
68+
access_token: 'goodbye',
69+
expiry_date: new Date(9999, 1, 1)
70+
});
71+
// Since we have to get an auth token, this always gets intercepted second
72+
nock('https://clouddebugger.googleapis.com')
73+
.post('/v2/controller/debuggees/register', function() {
74+
scope.done();
75+
agent.start.wasSuccessful_ = false;
76+
setImmediate(done);
77+
return true;
78+
}).reply(200);
79+
agent.start(config);
80+
});
81+
82+
it('should ignore credentials if keyFilename is provided', function(done) {
83+
var correctCredentials = require('../fixtures/gcloud-credentials.json');
84+
var config = {
85+
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
86+
credentials: {
87+
client_id: 'a',
88+
client_secret: 'b',
89+
refresh_token: 'c',
90+
type: 'authorized_user'
91+
}
92+
};
93+
['client_id', 'client_secret', 'refresh_token'].forEach(function (field) {
94+
assert(correctCredentials.hasOwnProperty(field));
95+
assert(config.credentials.hasOwnProperty(field));
96+
assert.notEqual(config.credentials[field],
97+
correctCredentials[field]);
98+
});
99+
var agent = require('../..');
100+
nock.disableNetConnect();
101+
var scope = nock('https://accounts.google.com')
102+
.post('/o/oauth2/token', function(body) {
103+
assert.equal(body.client_id, correctCredentials.client_id);
104+
assert.equal(body.client_secret, correctCredentials.client_secret);
105+
assert.equal(body.refresh_token, correctCredentials.refresh_token);
106+
return true;
107+
}).reply(200, {
108+
refresh_token: 'hello',
109+
access_token: 'goodbye',
110+
expiry_date: new Date(9999, 1, 1)
111+
});
112+
// Since we have to get an auth token, this always gets intercepted second
113+
nock('https://clouddebugger.googleapis.com')
114+
.post('/v2/controller/debuggees/register', function() {
115+
scope.done();
116+
agent.start.wasSuccessful_ = false;
117+
setImmediate(done);
118+
return true;
119+
}).reply(200);
120+
agent.start(config);
121+
});
122+
});

0 commit comments

Comments
 (0)