Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const axios = require('axios');
const {google} = require('googleapis');
const compute = google.compute('v1');
// This example can be run from a GCE VM in your project. The example fetches
// your project ID from the VM's metadata server, and then uses the Compute API
// to fetch the list of GCE zones in your project.
//
// See the defaultauth.js sample for an alternate way of fetching compute credentials.
//
google.options({auth: new google.auth.Compute()});
async function getZones() {
// Get the projectId from the GCE metadata server
const url = 'http://metadata/computeMetadata/v1/project/project-id';
const headers = {'Metadata-Flavor': 'Google'};
const res = await axios.get({url, headers});
const project = res.data;
const zonesResponse = await compute.zones.list({project});
console.log(zonesResponse.data);
}
getZones().catch(console.error);
const _setAuthCredential = async () => {
const res = await auth.getApplicationDefault();
let client = res.credential;
if (client.hasScopes && !client.hasScopes()) {
client = client.createScoped([
"https://www.googleapis.com/auth/cloud-billing",
"https://www.googleapis.com/auth/cloud-platform",
]);
}
// Set credential globally for all requests
google.options({
auth: client,
});
};
function setAuth(serviceAccount) {
const jwtAccess = new google.auth.JWT();
jwtAccess.fromJSON(serviceAccount);
// Note that if you require additional scopes, they should be specified as a
// string, separated by spaces.
jwtAccess.scopes = 'https://www.googleapis.com/auth/cloud-platform';
// Set the default authentication to the above JWT access.
google.options({ auth: jwtAccess });
}
const { omit, uniq } = this.lodash
const scopes = uniq([
...DEFAULT_SCOPES,
...(this.settings.scopes || []),
...(options.scopes || []),
])
const auth = await g.auth.getClient({ ...omit(options, 'fresh', 'cache', 'default'), scopes })
if (options.cache) {
this.hide('auth', auth)
}
if (options.default) {
g.options({ auth })
}
return auth
}
}
if (
typeof options.accessToken === 'string' &&
this.runtime.fsx.existsSync(this.runtime.resolve(options.accessToken))
) {
const accessToken = this.runtime.fsx.readJsonSync(this.runtime.resolve(options.accessToken))
oauthClient.setCredentials(accessToken)
} else if (typeof options.accessToken === 'object') {
oauthClient.setCredentials(options.accessToken)
}
if (options.cache !== false) {
this.hide('oauthClient', oauthClient)
}
if (options.default) {
g.options({ auth: oauthClient })
}
return oauthClient
}
function setGlobalGoogleAuthentication(OAuthClient) {
google.options({
auth: OAuthClient
})
}
return google.auth.getApplicationDefault().then( ( { credential } ) => {
google.options( {
auth : credential
} )
return google.discoverAPI( DISCOVERY_URL )
} )
}
export function updateGlobalParams(globalParams: GlobalParams, paramName: string, value: any): void {
tl.debug('Updating Global Parameters');
tl.debug('SETTING ' + paramName + ' TO ' + JSON.stringify(value));
globalParams.params[paramName] = value;
google.options(globalParams);
tl.debug('Global Params set to ' + JSON.stringify(globalParams));
}