n8n-nodes-gcloud-sa-impersonate
Version:
n8n community node for Google Cloud Service Account Token Impersonation
110 lines (109 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GCloudSaImpersonate = void 0;
const utils_1 = require("./utils");
class GCloudSaImpersonate {
constructor() {
this.description = {
displayName: 'Google Cloud SA Impersonate',
name: 'gCloudSaImpersonate',
icon: 'file:icon.svg',
group: ['output'],
version: 1,
subtitle: 'Impersonate Service Account',
description: 'Impersonate Google Cloud Service Account to get access token',
defaults: {
name: 'Google Cloud SA Impersonate',
},
inputs: [
{
displayName: '',
type: "main" /* NodeConnectionType.Main */,
},
],
outputs: [
{
displayName: '',
type: "main" /* NodeConnectionType.Main */,
},
],
properties: [
{
displayName: 'GCE Metadata Server URL',
name: 'metadataServerUrl',
type: 'string',
default: 'https://metadata.google.internal/',
description: 'The GCE metadata server URL (default: https://metadata.google.internal/)',
},
{
displayName: 'Target Service Account Email',
name: 'targetServiceAccount',
type: 'string',
default: '',
placeholder: 'service-account@project-id.iam.gserviceaccount.com',
required: true,
description: 'The email address of the service account to impersonate',
},
{
displayName: 'Scopes',
name: 'scopes',
type: 'string',
default: 'https://www.googleapis.com/auth/cloud-platform',
required: true,
description: 'Comma-separated list of OAuth 2.0 scopes for the access token',
},
{
displayName: 'Delegates',
name: 'delegates',
type: 'string',
default: '',
required: false,
description: 'Optional comma-separated list of service account emails in the delegation chain',
},
{
displayName: 'Token Lifetime',
name: 'lifetime',
type: 'string',
default: '300s',
description: 'The desired lifetime duration of the access token (e.g., "300s", "5m")',
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const metadataServerUrl = this.getNodeParameter('metadataServerUrl', i);
const targetServiceAccount = this.getNodeParameter('targetServiceAccount', i);
const scopesString = this.getNodeParameter('scopes', i);
const delegatesString = this.getNodeParameter('delegates', i);
const lifetime = this.getNodeParameter('lifetime', i);
const sourceToken = await (0, utils_1.getGceMetadataToken)(metadataServerUrl);
const scopes = (0, utils_1.parseScopesString)(scopesString);
const delegates = (0, utils_1.parseDelegatesString)(delegatesString);
const impersonationResponse = await (0, utils_1.impersonateServiceAccount)(sourceToken, targetServiceAccount, scopes, delegates, lifetime);
const responseData = {
accessToken: impersonationResponse.accessToken,
expireTime: impersonationResponse.expireTime,
targetServiceAccount: targetServiceAccount,
scopes,
lifetime,
};
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item: i } });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray({ error: error.message }), { itemData: { item: i } });
returnData.push(...executionData);
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.GCloudSaImpersonate = GCloudSaImpersonate;