diff --git a/lib/PowerShell/Constants.js b/lib/PowerShell/Constants.js index 98f75ccf..8ba2db80 100644 --- a/lib/PowerShell/Constants.js +++ b/lib/PowerShell/Constants.js @@ -12,3 +12,4 @@ Constants.ServicePrincipal = "ServicePrincipal"; Constants.Success = "Success"; Constants.Error = "Error"; Constants.AzVersion = "AzVersion"; +Constants.AzureSupportedCloudName = ["azureusgovernment", "azurechinacloud", "azuregermancloud", "azurecloud", "azurestack"]; diff --git a/lib/main.js b/lib/main.js index b07b4960..509ce102 100644 --- a/lib/main.js +++ b/lib/main.js @@ -27,6 +27,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); const crypto = __importStar(require("crypto")); @@ -34,7 +37,7 @@ const exec = __importStar(require("@actions/exec")); const io = __importStar(require("@actions/io")); const actions_secret_parser_1 = require("actions-secret-parser"); const ServicePrincipalLogin_1 = require("./PowerShell/ServicePrincipalLogin"); -const Enums_1 = require("./PowerShell/Enums"); +const Constants_1 = __importDefault(require("./PowerShell/Constants")); var azPath; var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; var azPSHostEnv = !!process.env.AZUREPS_HOST_ENVIRONMENT ? `${process.env.AZUREPS_HOST_ENVIRONMENT}` : ""; @@ -59,12 +62,16 @@ function main() { let subscriptionId = secrets.getSecret("$.subscriptionId", false); let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); let environment = core.getInput("environment"); + environment = environment.toLowerCase(); const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) { throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied."); } + if (!Constants_1.default.AzureSupportedCloudName.includes(environment)) { + throw new Error("Unsuppoted value is passed.The list of supported values for environment are ‘azureusgovernment', ‘azurechinacloud’, ‘azuregermancloud’, ‘azurecloud’ or ’azurestack’"); + } // Attempting Az cli login - if (environment.toLowerCase() == "azurestack") { + if (environment == "azurestack") { if (!resourceManagerEndpointUrl) { throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined."); } @@ -90,13 +97,10 @@ function main() { catch (error) { core.error(`Error while trying to register cloud "${environment}": "${error}"`); } - yield executeAzCliCommand(`cloud set -n "${environment}"`, false); console.log(`Done registering cloud: "${environment}"`); } - //setting context to Azure Gov Cloud - if (environment.toLowerCase() == Enums_1.AzureCloudName.AzureUSGovernment || environment.toLowerCase() == Enums_1.AzureCloudName.AzureChinaCloud || environment.toLowerCase() == Enums_1.AzureCloudName.AzureGermanCloud || environment.toLowerCase() == Enums_1.AzureCloudName.AzureCloud) { - yield executeAzCliCommand(`cloud set --name "${environment}"`, true); - } + yield executeAzCliCommand(`cloud set -n "${environment}"`, false); + console.log(`Done setting cloud: "${environment}"`); isAzCLISuccess = true; if (enableAzPSSession) { // Attempting Az PS login diff --git a/src/PowerShell/Constants.ts b/src/PowerShell/Constants.ts index 9613db1d..572b7b70 100644 --- a/src/PowerShell/Constants.ts +++ b/src/PowerShell/Constants.ts @@ -10,4 +10,5 @@ export default class Constants { static readonly Success: string = "Success"; static readonly Error: string = "Error"; static readonly AzVersion: string = "AzVersion"; + static readonly AzureSupportedCloudName:string[] = ["azureusgovernment", "azurechinacloud", "azuregermancloud","azurecloud","azurestack"]; } \ No newline at end of file diff --git a/src/PowerShell/Enums.ts b/src/PowerShell/Enums.ts deleted file mode 100644 index 9169b9f6..00000000 --- a/src/PowerShell/Enums.ts +++ /dev/null @@ -1,7 +0,0 @@ -export enum AzureCloudName { - AzureUSGovernment = "azureusgovernment", - AzureChinaCloud = "azurechinacloud", - AzureGermanCloud = "azuregermancloud", - AzureCloud = "azurecloud", - AzureStack="azurestack" -} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 4e475c9a..cc08ee20 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import * as exec from '@actions/exec'; import * as io from '@actions/io'; import { FormatType, SecretParser } from 'actions-secret-parser'; import { ServicePrincipalLogin } from './PowerShell/ServicePrincipalLogin'; -import { AzureCloudName} from './PowerShell/Enums'; +import Constants from './PowerShell/Constants'; var azPath: string; var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; @@ -32,23 +32,31 @@ async function main() { let subscriptionId = secrets.getSecret("$.subscriptionId", false); let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); let environment = core.getInput("environment"); + environment = environment.toLowerCase(); const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) { throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied."); } - + + if(!Constants.AzureSupportedCloudName.includes(environment)){ + throw new Error("Unsuppoted value is passed.The list of supported values for environment are ‘azureusgovernment', ‘azurechinacloud’, ‘azuregermancloud’, ‘azurecloud’ or ’azurestack’"); + } + // Attempting Az cli login - if (environment.toLowerCase() == "azurestack") { + if (environment == "azurestack") { if (!resourceManagerEndpointUrl) { throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined."); } + console.log(`Unregistering cloud: "${environment}" first if it exists`); try { await executeAzCliCommand(`cloud set -n AzureCloud`, true); await executeAzCliCommand(`cloud unregister -n "${environment}"`, false); - } catch (error) { + } + catch (error) { console.log(`Ignore cloud not registered error: "${error}"`); } + console.log(`Registering cloud: "${environment}" with ARM endpoint: "${resourceManagerEndpointUrl}"`); try { let baseUri = resourceManagerEndpointUrl; @@ -59,17 +67,16 @@ async function main() { let suffixStorage = baseUri.substring(baseUri.indexOf('.')+1); // storage suffix starts without . let profileVersion = "2019-03-01-hybrid"; await executeAzCliCommand(`cloud register -n "${environment}" --endpoint-resource-manager "${resourceManagerEndpointUrl}" --suffix-keyvault-dns "${suffixKeyvault}" --suffix-storage-endpoint "${suffixStorage}" --profile "${profileVersion}"`, false); - } catch (error) { + } + catch (error) { core.error(`Error while trying to register cloud "${environment}": "${error}"`); } - await executeAzCliCommand(`cloud set -n "${environment}"`, false); - console.log(`Done registering cloud: "${environment}"`); + + console.log(`Done registering cloud: "${environment}"`) } - - //setting context to Azure Gov Cloud - if (environment.toLowerCase() == AzureCloudName.AzureUSGovernment|| environment.toLowerCase() ==AzureCloudName.AzureChinaCloud || environment.toLowerCase() ==AzureCloudName.AzureGermanCloud || environment.toLowerCase() ==AzureCloudName.AzureCloud ) { - await executeAzCliCommand(`cloud set --name "${environment}"`, true); - } + + await executeAzCliCommand(`cloud set -n "${environment}"`, false); + console.log(`Done setting cloud: "${environment}"`); isAzCLISuccess = true; if (enableAzPSSession) { @@ -79,20 +86,23 @@ async function main() { await spnlogin.initialize(); await spnlogin.login(); } - else{ + else { // login using az cli await executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); await executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); } console.log("Login successful."); - } catch (error) { + } + catch (error) { if (!isAzCLISuccess) { core.error("Az CLI Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"); - } else { + } + else { core.error(`Azure PowerShell Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"`); } core.setFailed(error); - } finally { + } + finally { // Reset AZURE_HTTP_USER_AGENT core.exportVariable('AZURE_HTTP_USER_AGENT', prefix); core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv);