|
| 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 (mod) { |
| 19 | + if (mod && mod.__esModule) return mod; |
| 20 | + var result = {}; |
| 21 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 22 | + __setModuleDefault(result, mod); |
| 23 | + return result; |
| 24 | +}; |
| 25 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 26 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 27 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 28 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 29 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 30 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 31 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 32 | + }); |
| 33 | +}; |
| 34 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 35 | +exports.AzureCliLogin = void 0; |
| 36 | +const exec = __importStar(require("@actions/exec")); |
| 37 | +const core = __importStar(require("@actions/core")); |
| 38 | +const io = __importStar(require("@actions/io")); |
| 39 | +class AzureCliLogin { |
| 40 | + constructor(loginConfig) { |
| 41 | + this.loginConfig = loginConfig; |
| 42 | + } |
| 43 | + login() { |
| 44 | + return __awaiter(this, void 0, void 0, function* () { |
| 45 | + this.azPath = yield io.which("az", true); |
| 46 | + core.debug(`az cli path: ${this.azPath}`); |
| 47 | + let output = ""; |
| 48 | + const execOptions = { |
| 49 | + listeners: { |
| 50 | + stdout: (data) => { |
| 51 | + output += data.toString(); |
| 52 | + } |
| 53 | + } |
| 54 | + }; |
| 55 | + yield this.executeAzCliCommand("--version", true, execOptions); |
| 56 | + core.debug(`az cli version used:\n${output}`); |
| 57 | + this.setAzurestackEnvIfNecessary(); |
| 58 | + yield this.executeAzCliCommand(`cloud set -n "${this.loginConfig.environment}"`, false); |
| 59 | + console.log(`Done setting cloud: "${this.loginConfig.environment}"`); |
| 60 | + // Attempting Az cli login |
| 61 | + var commonArgs = ["--service-principal", |
| 62 | + "-u", this.loginConfig.servicePrincipalId, |
| 63 | + "--tenant", this.loginConfig.tenantId |
| 64 | + ]; |
| 65 | + if (this.loginConfig.allowNoSubscriptionsLogin) { |
| 66 | + commonArgs = commonArgs.concat("--allow-no-subscriptions"); |
| 67 | + } |
| 68 | + if (this.loginConfig.enableOIDC) { |
| 69 | + commonArgs = commonArgs.concat("--federated-token", this.loginConfig.federatedToken); |
| 70 | + } |
| 71 | + else { |
| 72 | + console.log("Note: Azure/login action also supports OIDC login mechanism. Refer https://github.com/azure/login#configure-a-service-principal-with-a-federated-credential-to-use-oidc-based-authentication for more details."); |
| 73 | + commonArgs = commonArgs.concat(`--password=${this.loginConfig.servicePrincipalKey}`); |
| 74 | + } |
| 75 | + const loginOptions = defaultExecOptions(); |
| 76 | + yield this.executeAzCliCommand(`login`, true, loginOptions, commonArgs); |
| 77 | + if (!this.loginConfig.allowNoSubscriptionsLogin) { |
| 78 | + var args = [ |
| 79 | + "--subscription", |
| 80 | + this.loginConfig.subscriptionId |
| 81 | + ]; |
| 82 | + yield this.executeAzCliCommand(`account set`, true, loginOptions, args); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + setAzurestackEnvIfNecessary() { |
| 87 | + return __awaiter(this, void 0, void 0, function* () { |
| 88 | + if (this.loginConfig.environment != "azurestack") { |
| 89 | + return; |
| 90 | + } |
| 91 | + if (!this.loginConfig.resourceManagerEndpointUrl) { |
| 92 | + throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined."); |
| 93 | + } |
| 94 | + console.log(`Unregistering cloud: "${this.loginConfig.environment}" first if it exists`); |
| 95 | + try { |
| 96 | + yield this.executeAzCliCommand(`cloud set -n AzureCloud`, true); |
| 97 | + yield this.executeAzCliCommand(`cloud unregister -n "${this.loginConfig.environment}"`, false); |
| 98 | + } |
| 99 | + catch (error) { |
| 100 | + console.log(`Ignore cloud not registered error: "${error}"`); |
| 101 | + } |
| 102 | + console.log(`Registering cloud: "${this.loginConfig.environment}" with ARM endpoint: "${this.loginConfig.resourceManagerEndpointUrl}"`); |
| 103 | + try { |
| 104 | + let baseUri = this.loginConfig.resourceManagerEndpointUrl; |
| 105 | + if (baseUri.endsWith('/')) { |
| 106 | + baseUri = baseUri.substring(0, baseUri.length - 1); // need to remove trailing / from resourceManagerEndpointUrl to correctly derive suffixes below |
| 107 | + } |
| 108 | + let suffixKeyvault = ".vault" + baseUri.substring(baseUri.indexOf('.')); // keyvault suffix starts with . |
| 109 | + let suffixStorage = baseUri.substring(baseUri.indexOf('.') + 1); // storage suffix starts without . |
| 110 | + let profileVersion = "2019-03-01-hybrid"; |
| 111 | + yield this.executeAzCliCommand(`cloud register -n "${this.loginConfig.environment}" --endpoint-resource-manager "${this.loginConfig.resourceManagerEndpointUrl}" --suffix-keyvault-dns "${suffixKeyvault}" --suffix-storage-endpoint "${suffixStorage}" --profile "${profileVersion}"`, false); |
| 112 | + } |
| 113 | + catch (error) { |
| 114 | + core.error(`Error while trying to register cloud "${this.loginConfig.environment}": "${error}"`); |
| 115 | + } |
| 116 | + console.log(`Done registering cloud: "${this.loginConfig.environment}"`); |
| 117 | + }); |
| 118 | + } |
| 119 | + executeAzCliCommand(command, silent, execOptions = {}, args = []) { |
| 120 | + return __awaiter(this, void 0, void 0, function* () { |
| 121 | + execOptions.silent = !!silent; |
| 122 | + yield exec.exec(`"${this.azPath}" ${command}`, args, execOptions); |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
| 126 | +exports.AzureCliLogin = AzureCliLogin; |
| 127 | +function defaultExecOptions() { |
| 128 | + return { |
| 129 | + silent: true, |
| 130 | + listeners: { |
| 131 | + stderr: (data) => { |
| 132 | + let error = data.toString(); |
| 133 | + let startsWithWarning = error.toLowerCase().startsWith('warning'); |
| 134 | + let startsWithError = error.toLowerCase().startsWith('error'); |
| 135 | + // printing ERROR |
| 136 | + if (error && error.trim().length !== 0 && !startsWithWarning) { |
| 137 | + if (startsWithError) { |
| 138 | + //removing the keyword 'ERROR' to avoid duplicates while throwing error |
| 139 | + error = error.slice(5); |
| 140 | + } |
| 141 | + core.setFailed(error); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + }; |
| 146 | +} |
0 commit comments