Compare commits

..

12 Commits

Author SHA1 Message Date
Zainuden Veetikadam
4610f2b768 changes to environment input 2020-12-08 14:56:31 +05:30
Zainuden Veetikadam
ab062435f9 changes from arary to set 2020-12-08 14:49:48 +05:30
Zainuden Veetikadam
e22df6b974 added library of Consts.js 2020-12-08 14:40:55 +05:30
Zainuden Veetikadam
c23434512d changes to constant file 2020-12-08 14:38:39 +05:30
Zainuden Veetikadam
78d65b04b4 deleted Enums.js 2020-12-08 10:52:18 +05:30
Zainuden Veetikadam
b7f16dbb80 changes 2020-12-07 17:18:58 +05:30
Zainuden Veetikadam
d71ab3132d changes to include validation 2020-12-07 17:07:39 +05:30
Zainuden Veetikadam
dbd3c8ef8d chnages to include enum and valdiation 2020-12-05 15:03:22 +05:30
Zainuden Veetikadam
61f3da0eb1 changes as per PR comment 2020-12-05 14:21:52 +05:30
Zainuden Veetikadam
b5bd602263 az gov cloud changes 2020-11-30 15:47:39 +05:30
Zainuden Veetikadam
8476f79486 changes to Azure cloud 2020-11-30 15:19:18 +05:30
Zainuden Veetikadam
ebd3ce082b added support for Azure Gov cloud 2020-11-30 15:15:50 +05:30
2 changed files with 174 additions and 134 deletions

View File

@@ -1,121 +1,141 @@
"use strict"; "use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } if (k2 === undefined) k2 = k;
return new (P || (P = Promise))(function (resolve, reject) { Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } }) : (function(o, m, k, k2) {
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } if (k2 === undefined) k2 = k;
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } o[k2] = m[k];
step((generator = generator.apply(thisArg, _arguments || [])).next()); }));
}); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
}; Object.defineProperty(o, "default", { enumerable: true, value: v });
var __importStar = (this && this.__importStar) || function (mod) { }) : function(o, v) {
if (mod && mod.__esModule) return mod; o["default"] = v;
var result = {}; });
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; var __importStar = (this && this.__importStar) || function (mod) {
result["default"] = mod; if (mod && mod.__esModule) return mod;
return result; var result = {};
}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
Object.defineProperty(exports, "__esModule", { value: true }); __setModuleDefault(result, mod);
const core = __importStar(require("@actions/core")); return result;
const crypto = __importStar(require("crypto")); };
const exec = __importStar(require("@actions/exec")); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
const io = __importStar(require("@actions/io")); function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
const actions_secret_parser_1 = require("actions-secret-parser"); return new (P || (P = Promise))(function (resolve, reject) {
const ServicePrincipalLogin_1 = require("./PowerShell/ServicePrincipalLogin"); function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
var azPath; function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
var azPSHostEnv = !!process.env.AZUREPS_HOST_ENVIRONMENT ? `${process.env.AZUREPS_HOST_ENVIRONMENT}` : ""; step((generator = generator.apply(thisArg, _arguments || [])).next());
function main() { });
return __awaiter(this, void 0, void 0, function* () { };
try { Object.defineProperty(exports, "__esModule", { value: true });
// Set user agent variable const core = __importStar(require("@actions/core"));
var isAzCLISuccess = false; const crypto = __importStar(require("crypto"));
let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex'); const exec = __importStar(require("@actions/exec"));
let actionName = 'AzureLogin'; const io = __importStar(require("@actions/io"));
let userAgentString = (!!prefix ? `${prefix}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`; const actions_secret_parser_1 = require("actions-secret-parser");
let azurePSHostEnv = (!!azPSHostEnv ? `${azPSHostEnv}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`; const ServicePrincipalLogin_1 = require("./PowerShell/ServicePrincipalLogin");
core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString); var azPath;
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv); var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : "";
azPath = yield io.which("az", true); var azPSHostEnv = !!process.env.AZUREPS_HOST_ENVIRONMENT ? `${process.env.AZUREPS_HOST_ENVIRONMENT}` : "";
yield executeAzCliCommand("--version"); function main() {
let creds = core.getInput('creds', { required: true }); return __awaiter(this, void 0, void 0, function* () {
let secrets = new actions_secret_parser_1.SecretParser(creds, actions_secret_parser_1.FormatType.JSON); try {
let servicePrincipalId = secrets.getSecret("$.clientId", false); // Set user agent variable
let servicePrincipalKey = secrets.getSecret("$.clientSecret", true); var isAzCLISuccess = false;
let tenantId = secrets.getSecret("$.tenantId", false); let usrAgentRepo = crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex');
let subscriptionId = secrets.getSecret("$.subscriptionId", false); let actionName = 'AzureLogin';
let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); let userAgentString = (!!prefix ? `${prefix}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`;
let environment = core.getInput("environment"); let azurePSHostEnv = (!!azPSHostEnv ? `${azPSHostEnv}+` : '') + `GITHUBACTIONS/${actionName}@v1_${usrAgentRepo}`;
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString);
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) { core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv);
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied."); azPath = yield io.which("az", true);
} yield executeAzCliCommand("--version");
// Attempting Az cli login let azureSupportedCloudName = new Set(["azureusgovernment", "azurechinacloud", "azuregermancloud", "azurecloud", "azurestack"]);
if (environment.toLowerCase() == "azurestack") { let creds = core.getInput('creds', { required: true });
if (!resourceManagerEndpointUrl) { let secrets = new actions_secret_parser_1.SecretParser(creds, actions_secret_parser_1.FormatType.JSON);
throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined."); let servicePrincipalId = secrets.getSecret("$.clientId", false);
} let servicePrincipalKey = secrets.getSecret("$.clientSecret", true);
console.log(`Unregistering cloud: "${environment}" first if it exists`); let tenantId = secrets.getSecret("$.tenantId", false);
try { let subscriptionId = secrets.getSecret("$.subscriptionId", false);
yield executeAzCliCommand(`cloud set -n AzureCloud`, true); let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false);
yield executeAzCliCommand(`cloud unregister -n "${environment}"`, false); let environment = core.getInput("environment").toLowerCase();
} const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
catch (error) { if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) {
console.log(`Ignore cloud not registered error: "${error}"`); throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
} }
console.log(`Registering cloud: "${environment}" with ARM endpoint: "${resourceManagerEndpointUrl}"`); if (!azureSupportedCloudName.has(environment)) {
try { throw new Error("Unsupported value for environment is passed.The list of supported values for environment are azureusgovernment', azurechinacloud, azuregermancloud, azurecloud or azurestack");
let baseUri = resourceManagerEndpointUrl; }
if (baseUri.endsWith('/')) { // Attempting Az cli login
baseUri = baseUri.substring(0, baseUri.length - 1); // need to remove trailing / from resourceManagerEndpointUrl to correctly derive suffixes below if (environment == "azurestack") {
} if (!resourceManagerEndpointUrl) {
let suffixKeyvault = ".vault" + baseUri.substring(baseUri.indexOf('.')); // keyvault suffix starts with . throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined.");
let suffixStorage = baseUri.substring(baseUri.indexOf('.') + 1); // storage suffix starts without . }
let profileVersion = "2019-03-01-hybrid"; console.log(`Unregistering cloud: "${environment}" first if it exists`);
yield executeAzCliCommand(`cloud register -n "${environment}" --endpoint-resource-manager "${resourceManagerEndpointUrl}" --suffix-keyvault-dns "${suffixKeyvault}" --suffix-storage-endpoint "${suffixStorage}" --profile "${profileVersion}"`, false); try {
} yield executeAzCliCommand(`cloud set -n AzureCloud`, true);
catch (error) { yield executeAzCliCommand(`cloud unregister -n "${environment}"`, false);
core.error(`Error while trying to register cloud "${environment}": "${error}"`); }
} catch (error) {
yield executeAzCliCommand(`cloud set -n "${environment}"`, false); console.log(`Ignore cloud not registered error: "${error}"`);
console.log(`Done registering cloud: "${environment}"`); }
} console.log(`Registering cloud: "${environment}" with ARM endpoint: "${resourceManagerEndpointUrl}"`);
yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); try {
yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); let baseUri = resourceManagerEndpointUrl;
isAzCLISuccess = true; if (baseUri.endsWith('/')) {
if (enableAzPSSession) { baseUri = baseUri.substring(0, baseUri.length - 1); // need to remove trailing / from resourceManagerEndpointUrl to correctly derive suffixes below
// Attempting Az PS login }
console.log(`Running Azure PS Login`); let suffixKeyvault = ".vault" + baseUri.substring(baseUri.indexOf('.')); // keyvault suffix starts with .
const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, environment, resourceManagerEndpointUrl); let suffixStorage = baseUri.substring(baseUri.indexOf('.') + 1); // storage suffix starts without .
yield spnlogin.initialize(); let profileVersion = "2019-03-01-hybrid";
yield spnlogin.login(); yield executeAzCliCommand(`cloud register -n "${environment}" --endpoint-resource-manager "${resourceManagerEndpointUrl}" --suffix-keyvault-dns "${suffixKeyvault}" --suffix-storage-endpoint "${suffixStorage}" --profile "${profileVersion}"`, false);
} }
console.log("Login successful."); catch (error) {
} core.error(`Error while trying to register cloud "${environment}": "${error}"`);
catch (error) { }
if (!isAzCLISuccess) { console.log(`Done registering cloud: "${environment}"`);
core.error("Az CLI Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"); }
} yield executeAzCliCommand(`cloud set -n "${environment}"`, false);
else { console.log(`Done setting cloud: "${environment}"`);
core.error(`Azure PowerShell Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"`); isAzCLISuccess = true;
} if (enableAzPSSession) {
core.setFailed(error); // Attempting Az PS login
} console.log(`Running Azure PS Login`);
finally { const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, environment, resourceManagerEndpointUrl);
// Reset AZURE_HTTP_USER_AGENT yield spnlogin.initialize();
core.exportVariable('AZURE_HTTP_USER_AGENT', prefix); yield spnlogin.login();
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv); }
} else {
}); // login using az cli
} yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true);
function executeAzCliCommand(command, silent) { yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true);
return __awaiter(this, void 0, void 0, function* () { }
try { console.log("Login successful.");
yield exec.exec(`"${azPath}" ${command}`, [], { silent: !!silent }); }
} catch (error) {
catch (error) { if (!isAzCLISuccess) {
throw new Error(error); core.error("Az CLI Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows");
} }
}); else {
} core.error(`Azure PowerShell Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"`);
main(); }
core.setFailed(error);
}
finally {
// Reset AZURE_HTTP_USER_AGENT
core.exportVariable('AZURE_HTTP_USER_AGENT', prefix);
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv);
}
});
}
function executeAzCliCommand(command, silent) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield exec.exec(`"${azPath}" ${command}`, [], { silent: !!silent });
}
catch (error) {
throw new Error(error);
}
});
}
main();

View File

@@ -2,7 +2,6 @@ import * as core from '@actions/core';
import * as crypto from "crypto"; import * as crypto from "crypto";
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
import * as io from '@actions/io'; import * as io from '@actions/io';
import { FormatType, SecretParser } from 'actions-secret-parser'; import { FormatType, SecretParser } from 'actions-secret-parser';
import { ServicePrincipalLogin } from './PowerShell/ServicePrincipalLogin'; import { ServicePrincipalLogin } from './PowerShell/ServicePrincipalLogin';
@@ -23,7 +22,8 @@ async function main() {
azPath = await io.which("az", true); azPath = await io.which("az", true);
await executeAzCliCommand("--version"); await executeAzCliCommand("--version");
let azureSupportedCloudName = new Set(["azureusgovernment", "azurechinacloud", "azuregermancloud","azurecloud","azurestack"]);
let creds = core.getInput('creds', { required: true }); let creds = core.getInput('creds', { required: true });
let secrets = new SecretParser(creds, FormatType.JSON); let secrets = new SecretParser(creds, FormatType.JSON);
let servicePrincipalId = secrets.getSecret("$.clientId", false); let servicePrincipalId = secrets.getSecret("$.clientId", false);
@@ -31,23 +31,32 @@ async function main() {
let tenantId = secrets.getSecret("$.tenantId", false); let tenantId = secrets.getSecret("$.tenantId", false);
let subscriptionId = secrets.getSecret("$.subscriptionId", false); let subscriptionId = secrets.getSecret("$.subscriptionId", false);
let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false);
let environment = core.getInput("environment"); let environment = core.getInput("environment").toLowerCase();
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) { 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."); throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
} }
if(!azureSupportedCloudName.has(environment)){
throw new Error("Unsupported value for environment is passed.The list of supported values for environment are azureusgovernment', azurechinacloud, azuregermancloud, azurecloud or azurestack");
}
// Attempting Az cli login // Attempting Az cli login
if (environment.toLowerCase() == "azurestack") { if (environment == "azurestack") {
if (!resourceManagerEndpointUrl) { if (!resourceManagerEndpointUrl) {
throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined."); throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined.");
} }
console.log(`Unregistering cloud: "${environment}" first if it exists`); console.log(`Unregistering cloud: "${environment}" first if it exists`);
try { try {
await executeAzCliCommand(`cloud set -n AzureCloud`, true); await executeAzCliCommand(`cloud set -n AzureCloud`, true);
await executeAzCliCommand(`cloud unregister -n "${environment}"`, false); await executeAzCliCommand(`cloud unregister -n "${environment}"`, false);
} catch (error) { }
catch (error) {
console.log(`Ignore cloud not registered error: "${error}"`); console.log(`Ignore cloud not registered error: "${error}"`);
} }
console.log(`Registering cloud: "${environment}" with ARM endpoint: "${resourceManagerEndpointUrl}"`); console.log(`Registering cloud: "${environment}" with ARM endpoint: "${resourceManagerEndpointUrl}"`);
try { try {
let baseUri = resourceManagerEndpointUrl; let baseUri = resourceManagerEndpointUrl;
@@ -58,14 +67,17 @@ async function main() {
let suffixStorage = baseUri.substring(baseUri.indexOf('.')+1); // storage suffix starts without . let suffixStorage = baseUri.substring(baseUri.indexOf('.')+1); // storage suffix starts without .
let profileVersion = "2019-03-01-hybrid"; 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); 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}"`); 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}"`)
} }
await executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true);
await executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); await executeAzCliCommand(`cloud set -n "${environment}"`, false);
console.log(`Done setting cloud: "${environment}"`);
isAzCLISuccess = true; isAzCLISuccess = true;
if (enableAzPSSession) { if (enableAzPSSession) {
// Attempting Az PS login // Attempting Az PS login
@@ -74,15 +86,23 @@ async function main() {
await spnlogin.initialize(); await spnlogin.initialize();
await spnlogin.login(); await spnlogin.login();
} }
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."); console.log("Login successful.");
} catch (error) { }
catch (error) {
if (!isAzCLISuccess) { if (!isAzCLISuccess) {
core.error("Az CLI Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"); 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.error(`Azure PowerShell Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"`);
} }
core.setFailed(error); core.setFailed(error);
} finally { }
finally {
// Reset AZURE_HTTP_USER_AGENT // Reset AZURE_HTTP_USER_AGENT
core.exportVariable('AZURE_HTTP_USER_AGENT', prefix); core.exportVariable('AZURE_HTTP_USER_AGENT', prefix);
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv); core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv);