|  | 
|  | 1 | +/** | 
|  | 2 | + * Copyright 2013 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 | + | 
|  | 17 | +import * as util from 'util'; | 
|  | 18 | +import Auth2Client from './oauth2client'; | 
|  | 19 | + | 
|  | 20 | +export default class Compute extends Auth2Client { | 
|  | 21 | + | 
|  | 22 | + /** | 
|  | 23 | + * Google Compute Engine metadata server token endpoint. | 
|  | 24 | + */ | 
|  | 25 | + protected static readonly _GOOGLE_OAUTH2_TOKEN_URL = | 
|  | 26 | + 'http://metadata.google.internal/computeMetadata/v1beta1/instance/service-accounts/default/token'; | 
|  | 27 | + | 
|  | 28 | + /** | 
|  | 29 | + * Google Compute Engine service account credentials. | 
|  | 30 | + * | 
|  | 31 | + * Retrieve access token from the metadata server. | 
|  | 32 | + * See: https://developers.google.com/compute/docs/authentication | 
|  | 33 | + */ | 
|  | 34 | + constructor() { | 
|  | 35 | + super(); | 
|  | 36 | + // Start with an expired refresh token, which will automatically be refreshed | 
|  | 37 | + // before the first API call is made. | 
|  | 38 | + this.credentials = { | 
|  | 39 | + expiry_date: 1, | 
|  | 40 | + refresh_token: 'compute-placeholder' | 
|  | 41 | + }; | 
|  | 42 | + } | 
|  | 43 | + | 
|  | 44 | + /** | 
|  | 45 | + * Indicates whether the credential requires scopes to be created by calling createdScoped before | 
|  | 46 | + * use. | 
|  | 47 | + * @return {object} The cloned instance. | 
|  | 48 | + */ | 
|  | 49 | + public createScopedRequired() { | 
|  | 50 | + // On compute engine, scopes are specified at the compute instance's creation time, | 
|  | 51 | + // and cannot be changed. For this reason, always return false. | 
|  | 52 | + return false; | 
|  | 53 | + } | 
|  | 54 | + | 
|  | 55 | + /** | 
|  | 56 | + * Refreshes the access token. | 
|  | 57 | + * @param {object=} ignored_ | 
|  | 58 | + * @param {function=} opt_callback Optional callback. | 
|  | 59 | + */ | 
|  | 60 | + protected refreshToken(ignored, callback?) { | 
|  | 61 | + const uri = this._opts.tokenUrl || Compute._GOOGLE_OAUTH2_TOKEN_URL; | 
|  | 62 | + // request for new token | 
|  | 63 | + return this.transporter.request({ | 
|  | 64 | + method: 'GET', | 
|  | 65 | + uri: uri, | 
|  | 66 | + json: true | 
|  | 67 | + }, (err, tokens, response) => { | 
|  | 68 | + if (!err && tokens && tokens.expires_in) { | 
|  | 69 | + tokens.expiry_date = ((new Date()).getTime() + (tokens.expires_in * 1000)); | 
|  | 70 | + delete tokens.expires_in; | 
|  | 71 | + } | 
|  | 72 | + if (callback) { | 
|  | 73 | + callback(err, tokens, response); | 
|  | 74 | + } | 
|  | 75 | + }); | 
|  | 76 | + } | 
|  | 77 | + | 
|  | 78 | + /** | 
|  | 79 | + * Inserts a helpful error message guiding the user toward fixing common auth issues. | 
|  | 80 | + * @param {object} err Error result. | 
|  | 81 | + * @param {object} result The result. | 
|  | 82 | + * @param {object} response The HTTP response. | 
|  | 83 | + * @param {Function} callback The callback. | 
|  | 84 | + */ | 
|  | 85 | + protected postRequest(err, result, response, callback) { | 
|  | 86 | + if (response && response.statusCode) { | 
|  | 87 | + let helpfulMessage = null; | 
|  | 88 | + if (response.statusCode === 403) { | 
|  | 89 | + helpfulMessage = 'A Forbidden error was returned while attempting to retrieve an access ' + | 
|  | 90 | + 'token for the Compute Engine built-in service account. This may be because the Compute ' + | 
|  | 91 | + 'Engine instance does not have the correct permission scopes specified.'; | 
|  | 92 | + } else if (response.statusCode === 404) { | 
|  | 93 | + helpfulMessage = 'A Not Found error was returned while attempting to retrieve an access' + | 
|  | 94 | + 'token for the Compute Engine built-in service account. This may be because the Compute ' + | 
|  | 95 | + 'Engine instance does not have any permission scopes specified.'; | 
|  | 96 | + } | 
|  | 97 | + if (helpfulMessage) { | 
|  | 98 | + if (err && err.message) { | 
|  | 99 | + helpfulMessage += ' ' + err.message; | 
|  | 100 | + } | 
|  | 101 | + | 
|  | 102 | + if (err) { | 
|  | 103 | + err.message = helpfulMessage; | 
|  | 104 | + } else { | 
|  | 105 | + err = new Error(helpfulMessage); | 
|  | 106 | + err.code = response.statusCode; | 
|  | 107 | + } | 
|  | 108 | + } | 
|  | 109 | + } | 
|  | 110 | + callback(err, result, response); | 
|  | 111 | + } | 
|  | 112 | +} | 
0 commit comments