|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 12 | + if (mod && mod.__esModule) return mod; |
| 13 | + var result = {}; |
| 14 | + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; |
| 15 | + result["default"] = mod; |
| 16 | + return result; |
| 17 | +}; |
| 18 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 19 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 20 | +}; |
| 21 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 22 | +const core = __importStar(require("@actions/core")); |
| 23 | +const Utils_1 = __importDefault(require("./Utilities/Utils")); |
| 24 | +const PowerShellToolRunner_1 = __importDefault(require("./Utilities/PowerShellToolRunner")); |
| 25 | +const ScriptBuilder_1 = __importDefault(require("./Utilities/ScriptBuilder")); |
| 26 | +const Constants_1 = __importDefault(require("./Constants")); |
| 27 | +class ServicePrincipalLogin { |
| 28 | + constructor(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId) { |
| 29 | + this.servicePrincipalId = servicePrincipalId; |
| 30 | + this.servicePrincipalKey = servicePrincipalKey; |
| 31 | + this.tenantId = tenantId; |
| 32 | + this.subscriptionId = subscriptionId; |
| 33 | + } |
| 34 | + initialize() { |
| 35 | + return __awaiter(this, void 0, void 0, function* () { |
| 36 | + Utils_1.default.setPSModulePath(); |
| 37 | + const azLatestVersion = yield Utils_1.default.getLatestModule(Constants_1.default.moduleName); |
| 38 | + core.debug(`Az Module version used: ${azLatestVersion}`); |
| 39 | + Utils_1.default.setPSModulePath(`${Constants_1.default.prefix}${azLatestVersion}`); |
| 40 | + }); |
| 41 | + } |
| 42 | + login() { |
| 43 | + return __awaiter(this, void 0, void 0, function* () { |
| 44 | + let output = ""; |
| 45 | + const options = { |
| 46 | + listeners: { |
| 47 | + stdout: (data) => { |
| 48 | + output += data.toString(); |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + const args = { |
| 53 | + servicePrincipalId: this.servicePrincipalId, |
| 54 | + servicePrincipalKey: this.servicePrincipalKey, |
| 55 | + subscriptionId: this.subscriptionId, |
| 56 | + environment: ServicePrincipalLogin.environment, |
| 57 | + scopeLevel: ServicePrincipalLogin.scopeLevel |
| 58 | + }; |
| 59 | + const script = new ScriptBuilder_1.default().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args); |
| 60 | + yield PowerShellToolRunner_1.default.init(); |
| 61 | + yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(script, options); |
| 62 | + const result = JSON.parse(output.trim()); |
| 63 | + if (!(Constants_1.default.Success in result)) { |
| 64 | + throw new Error(`Azure PowerShell login failed with error: ${result[Constants_1.default.Error]}`); |
| 65 | + } |
| 66 | + console.log(`Azure PowerShell session successfully initialized`); |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
| 70 | +exports.ServicePrincipalLogin = ServicePrincipalLogin; |
| 71 | +ServicePrincipalLogin.environment = Constants_1.default.AzureCloud; |
| 72 | +ServicePrincipalLogin.scopeLevel = Constants_1.default.Subscription; |
| 73 | +ServicePrincipalLogin.scheme = Constants_1.default.ServicePrincipal; |
0 commit comments