GeezerVison is showing github.com — core: read projectId from authClient by stephenplusplus · Pull Request #1656 · googleapis/google-cloud-node
core: read projectId from authClient
#1656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.html
(1)
.js
(9)
.json
(1)
.md
(1)
All 4 file types selected
Only manifest files
Deleted files
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -11,18 +11,21 @@ var config = {
### The `config` object
A `config` object requires the following properties:
A `config` object is not required if you are in an environment which supports [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials). This could be your own development machine when using the [gcloud SDK](https://cloud.google.com/sdk) or within Google App Engine and Compute Engine.
1. `projectId`
If this doesn't describe your environment, the `config` object expects the following properties:
If you wish, you can set an environment variable (`GCLOUD_PROJECT`) in place of specifying this inline.
2. One of the following:
1. `config.credentials` object containing `client_email` and `private_key` properties.
2. `config.keyFilename` path to a .json, .pem, or .p12 key file.
1. One of the following:
1. `credentials` object containing `client_email` and `private_key` properties.
2. `keyFilename` path to a .json, .pem, or .p12 key file.
3. `GOOGLE_APPLICATION_CREDENTIALS` environment variable with a full path to your key file.
2. `projectId`
If you wish, you can set an environment variable (`GCLOUD_PROJECT`) in place of specifying this inline. Or, if you have provided a service account JSON key file as the `config.keyFilename` property explained above, your project ID will be detected automatically.
**Note**: When using a .pem or .p12 key file, `config.email` is also required.
[dev-console]: https://console.developers.google.com/project
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using
Original file line number
Diff line number
Diff line change
Expand Up
@@ -91,9 +91,7 @@ <h4>Example: Upload a file to Cloud Storage</h4>
<div hljs hlsj-language="js" ng-show="selected == 'compute engine'">
// Authentication is automatic
// inside Google Compute Engine.
var gcloud = require('google-cloud')({
projectId: 'grape-spaceship-123'
});
var gcloud = require('google-cloud')();
var gcs = gcloud.storage();
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -241,10 +241,6 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
return;
}
// Clean up gcloud-specific options.
delete reqOpts.autoPaginate;
delete reqOpts.autoPaginateVal;
var service = this.getService_(protoOpts);
var metadata = this.grpcMetadata;
Expand All
@@ -254,6 +250,13 @@ GrpcService.prototype.request = function(protoOpts, reqOpts, callback) {
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
}
try {
reqOpts = util.decorateRequest(reqOpts, { projectId: self.projectId });
} catch(e) {
callback(e);
return;
}
// Retains a reference to an error from the response. If the final callback is
// executed with this as the "response", we return it to the user as an error.
var respError;
Expand Down
Expand Up
@@ -336,7 +339,6 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
}
var objectMode = !!reqOpts.objectMode;
delete reqOpts.objectMode;
var service = this.getService_(protoOpts);
var grpcOpts = {};
Expand All
@@ -345,6 +347,15 @@ GrpcService.prototype.requestStream = function(protoOpts, reqOpts) {
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
}
try {
reqOpts = util.decorateRequest(reqOpts, { projectId: this.projectId });
} catch(e) {
setImmediate(function() {
stream.destroy(e);
});
return stream;
}
var retryOpts = {
retries: this.maxRetries,
objectMode: objectMode,
Expand Down
Expand Up
@@ -416,6 +427,15 @@ GrpcService.prototype.requestWritableStream = function(protoOpts, reqOpts) {
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
}
try {
reqOpts = util.decorateRequest(reqOpts, { projectId: this.projectId });
} catch (e) {
setImmediate(function() {
stream.destroy(e);
});
return stream;
}
var grpcStream = service[protoOpts.method](reqOpts, grpcOpts)
.on('status', function(status) {
var grcpStatus = GrpcService.decorateStatus_(status);
Expand Down
Expand Up
@@ -724,6 +744,8 @@ GrpcService.structToObj_ = function(struct) {
* @param {?error} callback.err - An error getting an auth client.
*/
GrpcService.prototype.getGrpcCredentials_ = function(callback) {
var self = this;
this.authClient.getAuthClient(function(err, authClient) {
if (err) {
callback(err);
Expand All
@@ -735,6 +757,8 @@ GrpcService.prototype.getGrpcCredentials_ = function(callback) {
grpc.credentials.createFromGoogleCredential(authClient)
);
self.projectId = authClient.projectId;
callback(null, credentials);
});
};
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -51,14 +51,13 @@ function Service(config, options) {
});
this.makeAuthenticatedRequest = util.makeAuthenticatedRequestFactory(reqCfg);
this.authClient = this.makeAuthenticatedRequest.authClient;
this.baseUrl = config.baseUrl;
this.getCredentials = this.makeAuthenticatedRequest.getCredentials;
this.globalInterceptors = arrify(options.interceptors_);
this.interceptors = [];
this.packageJson = config.packageJson;
this.projectId = options.projectId;
this.projectId = options.projectId || '{{projectId}}' ;
Comment thread
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
this.projectIdRequired = config.projectIdRequired !== false;
this.Promise = options.promise || Promise;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -303,6 +303,8 @@ util.shouldRetryRequest = shouldRetryRequest;
/**
* Get a function for making authenticated requests.
*
* @throws {Error} If a projectId is requested, but not able to be detected.
*
* @param {object} config - Configuration object.
* @param {boolean=} config.autoRetry - Automatically retry requests if the
* response is related to rate limits or certain intermittent server errors.
Expand Down
Expand Up
@@ -344,6 +346,17 @@ function makeAuthenticatedRequestFactory(config) {
}
function onAuthenticated(err, authenticatedReqOpts) {
if (!err) {
try {
authenticatedReqOpts = util.decorateRequest(
authenticatedReqOpts,
extend({ projectId: authClient.projectId }, config)
);
} catch(e) {
err = util.missingProjectIdError;
}
}
if (err) {
if (stream) {
stream.destroy(err);
Expand All
@@ -354,8 +367,6 @@ function makeAuthenticatedRequestFactory(config) {
return;
}
authenticatedReqOpts = util.decorateRequest(authenticatedReqOpts, config);
if (options && options.onAuthenticated) {
options.onAuthenticated(null, authenticatedReqOpts);
} else {
Expand Down
Expand Up
@@ -468,6 +479,10 @@ util.makeRequest = makeRequest;
function decorateRequest(reqOpts, config) {
config = config || {};
delete reqOpts.autoPaginate;
delete reqOpts.autoPaginateVal;
delete reqOpts.objectMode;
if (is.object(reqOpts.qs)) {
delete reqOpts.qs.autoPaginate;
delete reqOpts.qs.autoPaginateVal;
Expand All
@@ -478,6 +493,19 @@ function decorateRequest(reqOpts, config) {
delete reqOpts.json.autoPaginateVal;
}
for (var opt in reqOpts) {
if (is.string(reqOpts[opt])) {
if (reqOpts[opt].indexOf('{{projectId}}') > -1) {
if (!config.projectId) {
throw util.missingProjectIdError;
}
reqOpts[opt] = reqOpts[opt].replace(/{{projectId}}/g, config.projectId);
}
} else if (is.object(reqOpts[opt])) {
decorateRequest(reqOpts[opt], config);
}
}
return reqOpts;
}
Expand Down
Expand Up
@@ -530,8 +558,6 @@ util.extendGlobalConfig = extendGlobalConfig;
/**
* Merge and validate API configurations.
*
* @throws {Error} If a projectId is not specified.
*
* @param {object} globalContext - gcloud-level context.
* @param {object} globalContext.config_ - gcloud-level configuration.
* @param {object} localConfig - Service-level configurations.
Expand All
@@ -545,13 +571,7 @@ function normalizeArguments(globalContext, localConfig, options) {
var globalConfig = globalContext && globalContext.config_;
var config = util.extendGlobalConfig(globalConfig, localConfig);
if (options.projectIdRequired !== false && !config.projectId) {
throw util.missingProjectIdError;
}
return config;
return util.extendGlobalConfig(globalConfig, localConfig);
}
util.normalizeArguments = normalizeArguments;
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.