|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 5 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 6 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 7 | + } |
| 8 | + Object.defineProperty(o, k2, desc); |
| 9 | +}) : (function(o, m, k, k2) { |
| 10 | + if (k2 === undefined) k2 = k; |
| 11 | + o[k2] = m[k]; |
| 12 | +})); |
| 13 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 14 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 15 | +}) : function(o, v) { |
| 16 | + o["default"] = v; |
| 17 | +}); |
| 18 | +var __importStar = (this && this.__importStar) || (function () { |
| 19 | + var ownKeys = function(o) { |
| 20 | + ownKeys = Object.getOwnPropertyNames || function (o) { |
| 21 | + var ar = []; |
| 22 | + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; |
| 23 | + return ar; |
| 24 | + }; |
| 25 | + return ownKeys(o); |
| 26 | + }; |
| 27 | + return function (mod) { |
| 28 | + if (mod && mod.__esModule) return mod; |
| 29 | + var result = {}; |
| 30 | + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); |
| 31 | + __setModuleDefault(result, mod); |
| 32 | + return result; |
| 33 | + }; |
| 34 | +})(); |
| 35 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 36 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 37 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 38 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 39 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 40 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 41 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 42 | + }); |
| 43 | +}; |
| 44 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 45 | +exports.AuthenticationUtils = void 0; |
| 46 | +const util_1 = require("util"); |
| 47 | +const core = __importStar(require("@actions/core")); |
| 48 | +const child_process_1 = require("child_process"); |
| 49 | +const UtilModels_1 = require("./UtilModels"); |
| 50 | +const jwt_decode_1 = require("jwt-decode"); |
| 51 | +class AuthenticationUtils { |
| 52 | + constructor() { |
| 53 | + this.dataPlanetoken = ''; |
| 54 | + this.controlPlaneToken = ''; |
| 55 | + this.subscriptionId = ''; |
| 56 | + this.env = 'AzureCloud'; |
| 57 | + this.armTokenScope = 'https://management.core.windows.net'; |
| 58 | + this.dataPlaneTokenScope = 'https://loadtest.azure-dev.com'; |
| 59 | + this.armEndpoint = 'https://management.azure.com'; |
| 60 | + this.resourceId = ''; |
| 61 | + } |
| 62 | + authorize() { |
| 63 | + return __awaiter(this, void 0, void 0, function* () { |
| 64 | + // NOTE: This will set the subscription id |
| 65 | + yield this.getTokenAPI(UtilModels_1.TokenScope.ControlPlane); |
| 66 | + const rg = core.getInput('resourceGroup'); |
| 67 | + const ltres = core.getInput('loadTestResource'); |
| 68 | + if ((0, util_1.isNullOrUndefined)(rg) || rg == '') { |
| 69 | + throw new Error(`The input field "resourceGroup" is empty. Provide an existing resource group name.`); |
| 70 | + } |
| 71 | + if ((0, util_1.isNullOrUndefined)(ltres) || ltres == '') { |
| 72 | + throw new Error(`The input field "loadTestResource" is empty. Provide an existing load test resource name.`); |
| 73 | + } |
| 74 | + this.resourceId = "/subscriptions/" + this.subscriptionId + "/resourcegroups/" + rg + "/providers/microsoft.loadtestservice/loadtests/" + ltres; |
| 75 | + yield this.setEndpointAndScope(); |
| 76 | + }); |
| 77 | + } |
| 78 | + setEndpointAndScope() { |
| 79 | + return __awaiter(this, void 0, void 0, function* () { |
| 80 | + try { |
| 81 | + const cmdArguments = ["cloud", "show"]; |
| 82 | + var result = yield this.execAz(cmdArguments); |
| 83 | + let env = result ? result.name : null; |
| 84 | + this.env = env ? env : this.env; |
| 85 | + let endpointUrl = (result && result.endpoints) ? result.endpoints.resourceManager : null; |
| 86 | + this.armEndpoint = endpointUrl ? endpointUrl : this.armEndpoint; |
| 87 | + if (this.env == 'AzureUSGovernment') { |
| 88 | + this.dataPlaneTokenScope = 'https://cnt-prod.loadtesting.azure.us'; |
| 89 | + this.armTokenScope = 'https://management.usgovcloudapi.net'; |
| 90 | + } |
| 91 | + } |
| 92 | + catch (err) { |
| 93 | + const message = `An error occurred while getting credentials from ` + |
| 94 | + `Azure CLI for setting endPoint and scope: ${err.message}`; |
| 95 | + throw new Error(message); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + getTokenAPI(scope) { |
| 100 | + return __awaiter(this, void 0, void 0, function* () { |
| 101 | + let tokenScopeDecoded = scope == UtilModels_1.TokenScope.Dataplane ? this.dataPlaneTokenScope : this.armTokenScope; |
| 102 | + try { |
| 103 | + const cmdArguments = ["account", "get-access-token", "--resource"]; |
| 104 | + cmdArguments.push(tokenScopeDecoded); |
| 105 | + var result = yield this.execAz(cmdArguments); |
| 106 | + let token = result.accessToken; |
| 107 | + // NOTE: Setting the subscription id |
| 108 | + this.subscriptionId = result.subscription; |
| 109 | + scope == UtilModels_1.TokenScope.ControlPlane ? this.controlPlaneToken = token : this.dataPlanetoken = token; |
| 110 | + return token; |
| 111 | + } |
| 112 | + catch (err) { |
| 113 | + const message = `An error occurred while getting credentials from ` + `Azure CLI: ${err.message}`; |
| 114 | + throw new Error(message); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + execAz(cmdArguments) { |
| 119 | + return __awaiter(this, void 0, void 0, function* () { |
| 120 | + const azCmd = process.platform === "win32" ? "az.cmd" : "az"; |
| 121 | + return new Promise((resolve, reject) => { |
| 122 | + (0, child_process_1.execFile)(azCmd, [...cmdArguments, "--out", "json"], { encoding: "utf8", shell: true }, (error, stdout) => { |
| 123 | + if (error) { |
| 124 | + return reject(error); |
| 125 | + } |
| 126 | + try { |
| 127 | + return resolve(JSON.parse(stdout)); |
| 128 | + } |
| 129 | + catch (err) { |
| 130 | + const msg = `An error occurred while parsing the output "${stdout}", of ` + |
| 131 | + `the cmd az "${cmdArguments}": ${err.message}.`; |
| 132 | + return reject(new Error(msg)); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + } |
| 138 | + isValid(scope) { |
| 139 | + let token = scope == UtilModels_1.TokenScope.Dataplane ? this.dataPlanetoken : this.controlPlaneToken; |
| 140 | + try { |
| 141 | + let header = token && (0, jwt_decode_1.jwtDecode)(token); |
| 142 | + const now = Math.floor(Date.now() / 1000); |
| 143 | + return (header && (header === null || header === void 0 ? void 0 : header.exp) && header.exp + 2 > now); |
| 144 | + } |
| 145 | + catch (error) { |
| 146 | + console.log("Error in getting the token"); |
| 147 | + } |
| 148 | + } |
| 149 | + getDataPlaneHeader(apicallType) { |
| 150 | + return __awaiter(this, void 0, void 0, function* () { |
| 151 | + var _a; |
| 152 | + if (!this.isValid(UtilModels_1.TokenScope.Dataplane)) { |
| 153 | + let tokenRes = yield this.getTokenAPI(UtilModels_1.TokenScope.Dataplane); |
| 154 | + this.dataPlanetoken = tokenRes; |
| 155 | + } |
| 156 | + let headers = { |
| 157 | + 'content-type': (_a = UtilModels_1.ContentTypeMap[apicallType]) !== null && _a !== void 0 ? _a : 'application/json', |
| 158 | + 'Authorization': 'Bearer ' + this.dataPlanetoken |
| 159 | + }; |
| 160 | + return headers; |
| 161 | + }); |
| 162 | + } |
| 163 | + armTokenHeader() { |
| 164 | + return __awaiter(this, void 0, void 0, function* () { |
| 165 | + // right now only get calls from the GH, so no need of content type for now for the get calls. |
| 166 | + var tokenRes = yield this.getTokenAPI(UtilModels_1.TokenScope.ControlPlane); |
| 167 | + this.controlPlaneToken = tokenRes; |
| 168 | + let headers = { |
| 169 | + 'Authorization': 'Bearer ' + this.controlPlaneToken, |
| 170 | + }; |
| 171 | + return headers; |
| 172 | + }); |
| 173 | + } |
| 174 | +} |
| 175 | +exports.AuthenticationUtils = AuthenticationUtils; |
0 commit comments