From 04f4eba6207ef01ca5a182653c6fe2666f0905b0 Mon Sep 17 00:00:00 2001 From: Atharva Mulmuley Date: Wed, 28 Apr 2021 16:12:56 +0530 Subject: [PATCH] removed comments and removed powershell folder --- .github/workflows/main.yml | 2 +- lib/PowerShell/Constants.js | 14 ---- lib/PowerShell/IAzurePowerShellSession.js | 0 lib/PowerShell/ServicePrincipalLogin.js | 67 ----------------- .../Utilities/PowerShellToolRunner.js | 28 -------- lib/PowerShell/Utilities/ScriptBuilder.js | 55 -------------- lib/PowerShell/Utilities/Utils.js | 70 ------------------ lib/main.js | 33 --------- src/PowerShell/Constants.ts | 13 ---- src/PowerShell/IAzurePowerShellSession.ts | 4 -- src/PowerShell/ServicePrincipalLogin.ts | 71 ------------------- .../Utilities/PowerShellToolRunner.ts | 16 ----- src/PowerShell/Utilities/ScriptBuilder.ts | 54 -------------- src/PowerShell/Utilities/Utils.ts | 61 ---------------- src/main.ts | 38 +--------- 15 files changed, 4 insertions(+), 522 deletions(-) delete mode 100644 lib/PowerShell/Constants.js delete mode 100644 lib/PowerShell/IAzurePowerShellSession.js delete mode 100644 lib/PowerShell/ServicePrincipalLogin.js delete mode 100644 lib/PowerShell/Utilities/PowerShellToolRunner.js delete mode 100644 lib/PowerShell/Utilities/ScriptBuilder.js delete mode 100644 lib/PowerShell/Utilities/Utils.js delete mode 100644 src/PowerShell/Constants.ts delete mode 100644 src/PowerShell/IAzurePowerShellSession.ts delete mode 100644 src/PowerShell/ServicePrincipalLogin.ts delete mode 100644 src/PowerShell/Utilities/PowerShellToolRunner.ts delete mode 100644 src/PowerShell/Utilities/ScriptBuilder.ts delete mode 100644 src/PowerShell/Utilities/Utils.ts diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1a6ed74c..c1f064c9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,7 +25,7 @@ jobs: - uses: atharvamulmuley/k8s-set-context@cluster-connect-2 with: - method: spn + method: service-account cluster-type: 'arc' creds: '${{ secrets.AZURE_CREDS }}' cluster-name: arcaction diff --git a/lib/PowerShell/Constants.js b/lib/PowerShell/Constants.js deleted file mode 100644 index 98f75ccf..00000000 --- a/lib/PowerShell/Constants.js +++ /dev/null @@ -1,14 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -class Constants { -} -exports.default = Constants; -Constants.prefix = "az_"; -Constants.moduleName = "Az.Accounts"; -Constants.versionPattern = /[0-9]\.[0-9]\.[0-9]/; -Constants.AzureCloud = "AzureCloud"; -Constants.Subscription = "Subscription"; -Constants.ServicePrincipal = "ServicePrincipal"; -Constants.Success = "Success"; -Constants.Error = "Error"; -Constants.AzVersion = "AzVersion"; diff --git a/lib/PowerShell/IAzurePowerShellSession.js b/lib/PowerShell/IAzurePowerShellSession.js deleted file mode 100644 index e69de29b..00000000 diff --git a/lib/PowerShell/ServicePrincipalLogin.js b/lib/PowerShell/ServicePrincipalLogin.js deleted file mode 100644 index 807cd75a..00000000 --- a/lib/PowerShell/ServicePrincipalLogin.js +++ /dev/null @@ -1,67 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const core = require("@actions/core"); -const Utils_1 = require("./Utilities/Utils"); -const PowerShellToolRunner_1 = require("./Utilities/PowerShellToolRunner"); -const ScriptBuilder_1 = require("./Utilities/ScriptBuilder"); -const Constants_1 = require("./Constants"); -class ServicePrincipalLogin { - constructor(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, allowNoSubscriptionsLogin, environment, resourceManagerEndpointUrl) { - this.servicePrincipalId = servicePrincipalId; - this.servicePrincipalKey = servicePrincipalKey; - this.tenantId = tenantId; - this.subscriptionId = subscriptionId; - this.environment = environment; - this.resourceManagerEndpointUrl = resourceManagerEndpointUrl; - this.allowNoSubscriptionsLogin = allowNoSubscriptionsLogin; - } - initialize() { - return __awaiter(this, void 0, void 0, function* () { - Utils_1.default.setPSModulePath(); - const azLatestVersion = yield Utils_1.default.getLatestModule(Constants_1.default.moduleName); - core.debug(`Az Module version used: ${azLatestVersion}`); - Utils_1.default.setPSModulePath(`${Constants_1.default.prefix}${azLatestVersion}`); - }); - } - login() { - return __awaiter(this, void 0, void 0, function* () { - let output = ""; - const options = { - listeners: { - stdout: (data) => { - output += data.toString(); - } - } - }; - const args = { - servicePrincipalId: this.servicePrincipalId, - servicePrincipalKey: this.servicePrincipalKey, - subscriptionId: this.subscriptionId, - environment: this.environment, - scopeLevel: ServicePrincipalLogin.scopeLevel, - allowNoSubscriptionsLogin: this.allowNoSubscriptionsLogin, - resourceManagerEndpointUrl: this.resourceManagerEndpointUrl - }; - const script = new ScriptBuilder_1.default().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args); - yield PowerShellToolRunner_1.default.init(); - yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(script, options); - const result = JSON.parse(output.trim()); - if (!(Constants_1.default.Success in result)) { - throw new Error(`Azure PowerShell login failed with error: ${result[Constants_1.default.Error]}`); - } - console.log(`Azure PowerShell session successfully initialized`); - }); - } -} -exports.ServicePrincipalLogin = ServicePrincipalLogin; -ServicePrincipalLogin.scopeLevel = Constants_1.default.Subscription; -ServicePrincipalLogin.scheme = Constants_1.default.ServicePrincipal; diff --git a/lib/PowerShell/Utilities/PowerShellToolRunner.js b/lib/PowerShell/Utilities/PowerShellToolRunner.js deleted file mode 100644 index edca71d9..00000000 --- a/lib/PowerShell/Utilities/PowerShellToolRunner.js +++ /dev/null @@ -1,28 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const io = require("@actions/io"); -const exec = require("@actions/exec"); -class PowerShellToolRunner { - static init() { - return __awaiter(this, void 0, void 0, function* () { - if (!PowerShellToolRunner.psPath) { - PowerShellToolRunner.psPath = yield io.which("pwsh", true); - } - }); - } - static executePowerShellScriptBlock(scriptBlock, options = {}) { - return __awaiter(this, void 0, void 0, function* () { - yield exec.exec(`"${PowerShellToolRunner.psPath}" -Command`, [scriptBlock], options); - }); - } -} -exports.default = PowerShellToolRunner; diff --git a/lib/PowerShell/Utilities/ScriptBuilder.js b/lib/PowerShell/Utilities/ScriptBuilder.js deleted file mode 100644 index 62b6276e..00000000 --- a/lib/PowerShell/Utilities/ScriptBuilder.js +++ /dev/null @@ -1,55 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const core = require("@actions/core"); -const Constants_1 = require("../Constants"); -class ScriptBuilder { - constructor() { - this.script = ""; - } - getAzPSLoginScript(scheme, tenantId, args) { - let command = `Clear-AzContext -Scope Process; - Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`; - if (scheme === Constants_1.default.ServicePrincipal) { - if (args.environment.toLowerCase() == "azurestack") { - command += `Add-AzEnvironment -Name ${args.environment} -ARMEndpoint ${args.resourceManagerEndpointUrl} | out-null;`; - } - command += `Connect-AzAccount -ServicePrincipal -Tenant '${tenantId}' -Credential \ - (New-Object System.Management.Automation.PSCredential('${args.servicePrincipalId}',(ConvertTo-SecureString '${args.servicePrincipalKey.replace("'", "''")}' -AsPlainText -Force))) \ - -Environment '${args.environment}' | out-null;`; - if (args.scopeLevel === Constants_1.default.Subscription && !args.allowNoSubscriptionsLogin) { - command += `Set-AzContext -SubscriptionId '${args.subscriptionId}' -TenantId '${tenantId}' | out-null;`; - } - } - this.script += `try { - $ErrorActionPreference = "Stop" - $WarningPreference = "SilentlyContinue" - $output = @{} - ${command} - $output['${Constants_1.default.Success}'] = "true" - } - catch { - $output['${Constants_1.default.Error}'] = $_.exception.Message - } - return ConvertTo-Json $output`; - core.debug(`Azure PowerShell Login Script: ${this.script}`); - return this.script; - } - getLatestModuleScript(moduleName) { - const command = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`; - this.script += `try { - $ErrorActionPreference = "Stop" - $WarningPreference = "SilentlyContinue" - $output = @{} - $data = ${command} - $output['${Constants_1.default.AzVersion}'] = $data.Version.ToString() - $output['${Constants_1.default.Success}'] = "true" - } - catch { - $output['${Constants_1.default.Error}'] = $_.exception.Message - } - return ConvertTo-Json $output`; - core.debug(`GetLatestModuleScript: ${this.script}`); - return this.script; - } -} -exports.default = ScriptBuilder; diff --git a/lib/PowerShell/Utilities/Utils.js b/lib/PowerShell/Utilities/Utils.js deleted file mode 100644 index f0ccd81e..00000000 --- a/lib/PowerShell/Utilities/Utils.js +++ /dev/null @@ -1,70 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const os = require("os"); -const Constants_1 = require("../Constants"); -const ScriptBuilder_1 = require("./ScriptBuilder"); -const PowerShellToolRunner_1 = require("./PowerShellToolRunner"); -class Utils { - /** - * Add the folder path where Az modules are present to PSModulePath based on runner - * @param azPSVersion - * If azPSVersion is empty, folder path in which all Az modules are present are set - * If azPSVersion is not empty, folder path of exact Az module version is set - */ - static setPSModulePath(azPSVersion = "") { - let modulePath = ""; - const runner = process.env.RUNNER_OS || os.type(); - switch (runner.toLowerCase()) { - case "linux": - modulePath = `/usr/share/${azPSVersion}:`; - break; - case "windows": - case "windows_nt": - modulePath = `C:\\Modules\\${azPSVersion};`; - break; - case "macos": - case "darwin": - throw new Error(`OS not supported`); - default: - throw new Error(`Unknown os: ${runner.toLowerCase()}`); - } - process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; - } - static getLatestModule(moduleName) { - return __awaiter(this, void 0, void 0, function* () { - let output = ""; - const options = { - listeners: { - stdout: (data) => { - output += data.toString(); - } - } - }; - yield PowerShellToolRunner_1.default.init(); - yield PowerShellToolRunner_1.default.executePowerShellScriptBlock(new ScriptBuilder_1.default() - .getLatestModuleScript(moduleName), options); - const result = JSON.parse(output.trim()); - if (!(Constants_1.default.Success in result)) { - throw new Error(result[Constants_1.default.Error]); - } - const azLatestVersion = result[Constants_1.default.AzVersion]; - if (!Utils.isValidVersion(azLatestVersion)) { - throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`); - } - return azLatestVersion; - }); - } - static isValidVersion(version) { - return !!version.match(Constants_1.default.versionPattern); - } -} -exports.default = Utils; diff --git a/lib/main.js b/lib/main.js index 3ac278fc..0005e04c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -53,8 +53,6 @@ function main() { let subscriptionId = secrets.getSecret("$.subscriptionId", false); let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); let environment = core.getInput("environment").toLowerCase(); - // const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; - // const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true"; if (!servicePrincipalId || !servicePrincipalKey || !tenantId) { throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied."); } @@ -95,19 +93,6 @@ function main() { } yield executeAzCliCommand(`cloud set -n "${environment}"`, false); console.log(`Done setting cloud: "${environment}"`); - // Attempting Az cli login - // if (allowNoSubscriptionsLogin) { - // let args = [ - // "--allow-no-subscriptions", - // "--service-principal", - // "-u", servicePrincipalId, - // "-p", servicePrincipalKey, - // "--tenant", tenantId - // ]; - // await executeAzCliCommand(`login`, true, {}, args); - // } - // else { - // } let args = [ "--service-principal", "-u", servicePrincipalId, @@ -121,29 +106,12 @@ function main() { ]; yield executeAzCliCommand(`account set`, true, {}, args); isAzCLISuccess = true; - // if (enableAzPSSession) { - // // Attempting Az PS login - // console.log(`Running Azure PS Login`); - // const spnlogin: ServicePrincipalLogin = new ServicePrincipalLogin( - // servicePrincipalId, - // servicePrincipalKey, - // tenantId, - // subscriptionId, - // allowNoSubscriptionsLogin, - // environment, - // resourceManagerEndpointUrl); - // await spnlogin.initialize(); - // await spnlogin.login(); - // } console.log("Login successful."); } 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 { - // 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 { @@ -166,4 +134,3 @@ function executeAzCliCommand(command, silent, execOptions = {}, args = []) { }); } exports.executeAzCliCommand = executeAzCliCommand; -//main(); diff --git a/src/PowerShell/Constants.ts b/src/PowerShell/Constants.ts deleted file mode 100644 index 9613db1d..00000000 --- a/src/PowerShell/Constants.ts +++ /dev/null @@ -1,13 +0,0 @@ -export default class Constants { - static readonly prefix: string = "az_"; - static readonly moduleName: string = "Az.Accounts"; - static readonly versionPattern = /[0-9]\.[0-9]\.[0-9]/; - - static readonly AzureCloud: string = "AzureCloud"; - static readonly Subscription: string = "Subscription"; - static readonly ServicePrincipal: string = "ServicePrincipal"; - - static readonly Success: string = "Success"; - static readonly Error: string = "Error"; - static readonly AzVersion: string = "AzVersion"; -} \ No newline at end of file diff --git a/src/PowerShell/IAzurePowerShellSession.ts b/src/PowerShell/IAzurePowerShellSession.ts deleted file mode 100644 index 544369d5..00000000 --- a/src/PowerShell/IAzurePowerShellSession.ts +++ /dev/null @@ -1,4 +0,0 @@ -interface IAzurePowerShellSession { - initialize(); - login(); -} \ No newline at end of file diff --git a/src/PowerShell/ServicePrincipalLogin.ts b/src/PowerShell/ServicePrincipalLogin.ts deleted file mode 100644 index cc8a2dde..00000000 --- a/src/PowerShell/ServicePrincipalLogin.ts +++ /dev/null @@ -1,71 +0,0 @@ -import * as core from '@actions/core'; - -import Utils from './Utilities/Utils'; -import PowerShellToolRunner from './Utilities/PowerShellToolRunner'; -import ScriptBuilder from './Utilities/ScriptBuilder'; -import Constants from './Constants'; - -export class ServicePrincipalLogin implements IAzurePowerShellSession { - static readonly scopeLevel: string = Constants.Subscription; - static readonly scheme: string = Constants.ServicePrincipal; - environment: string; - servicePrincipalId: string; - servicePrincipalKey: string; - tenantId: string; - subscriptionId: string; - resourceManagerEndpointUrl: string; - allowNoSubscriptionsLogin: boolean; - - constructor(servicePrincipalId: string, - servicePrincipalKey: string, - tenantId: string, - subscriptionId: string, - allowNoSubscriptionsLogin: boolean, - environment: string, - resourceManagerEndpointUrl: string) { - - this.servicePrincipalId = servicePrincipalId; - this.servicePrincipalKey = servicePrincipalKey; - this.tenantId = tenantId; - this.subscriptionId = subscriptionId; - this.environment = environment; - this.resourceManagerEndpointUrl = resourceManagerEndpointUrl; - this.allowNoSubscriptionsLogin = allowNoSubscriptionsLogin; - } - - async initialize() { - Utils.setPSModulePath(); - const azLatestVersion: string = await Utils.getLatestModule(Constants.moduleName); - core.debug(`Az Module version used: ${azLatestVersion}`); - Utils.setPSModulePath(`${Constants.prefix}${azLatestVersion}`); - } - - async login() { - let output: string = ""; - const options: any = { - listeners: { - stdout: (data: Buffer) => { - output += data.toString(); - } - } - }; - const args: any = { - servicePrincipalId: this.servicePrincipalId, - servicePrincipalKey: this.servicePrincipalKey, - subscriptionId: this.subscriptionId, - environment: this.environment, - scopeLevel: ServicePrincipalLogin.scopeLevel, - allowNoSubscriptionsLogin: this.allowNoSubscriptionsLogin, - resourceManagerEndpointUrl: this.resourceManagerEndpointUrl - } - const script: string = new ScriptBuilder().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args); - await PowerShellToolRunner.init(); - await PowerShellToolRunner.executePowerShellScriptBlock(script, options); - const result: any = JSON.parse(output.trim()); - if (!(Constants.Success in result)) { - throw new Error(`Azure PowerShell login failed with error: ${result[Constants.Error]}`); - } - console.log(`Azure PowerShell session successfully initialized`); - } - -} \ No newline at end of file diff --git a/src/PowerShell/Utilities/PowerShellToolRunner.ts b/src/PowerShell/Utilities/PowerShellToolRunner.ts deleted file mode 100644 index e8364f32..00000000 --- a/src/PowerShell/Utilities/PowerShellToolRunner.ts +++ /dev/null @@ -1,16 +0,0 @@ -import * as io from '@actions/io'; -import * as exec from '@actions/exec'; - -export default class PowerShellToolRunner { - static psPath: string; - - static async init() { - if(!PowerShellToolRunner.psPath) { - PowerShellToolRunner.psPath = await io.which("pwsh", true); - } - } - - static async executePowerShellScriptBlock(scriptBlock: string, options: any = {}) { - await exec.exec(`"${PowerShellToolRunner.psPath}" -Command`, [scriptBlock], options) - } -} \ No newline at end of file diff --git a/src/PowerShell/Utilities/ScriptBuilder.ts b/src/PowerShell/Utilities/ScriptBuilder.ts deleted file mode 100644 index 2da79a1f..00000000 --- a/src/PowerShell/Utilities/ScriptBuilder.ts +++ /dev/null @@ -1,54 +0,0 @@ -import * as core from '@actions/core'; - -import Constants from "../Constants"; - -export default class ScriptBuilder { - script: string = ""; - - getAzPSLoginScript(scheme: string, tenantId: string, args: any): string { - let command = `Clear-AzContext -Scope Process; - Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`; - if (scheme === Constants.ServicePrincipal) { - if (args.environment.toLowerCase() == "azurestack") { - command += `Add-AzEnvironment -Name ${args.environment} -ARMEndpoint ${args.resourceManagerEndpointUrl} | out-null;`; - } - command += `Connect-AzAccount -ServicePrincipal -Tenant '${tenantId}' -Credential \ - (New-Object System.Management.Automation.PSCredential('${args.servicePrincipalId}',(ConvertTo-SecureString '${args.servicePrincipalKey.replace("'", "''")}' -AsPlainText -Force))) \ - -Environment '${args.environment}' | out-null;`; - if (args.scopeLevel === Constants.Subscription && !args.allowNoSubscriptionsLogin) { - command += `Set-AzContext -SubscriptionId '${args.subscriptionId}' -TenantId '${tenantId}' | out-null;`; - } - } - this.script += `try { - $ErrorActionPreference = "Stop" - $WarningPreference = "SilentlyContinue" - $output = @{} - ${command} - $output['${Constants.Success}'] = "true" - } - catch { - $output['${Constants.Error}'] = $_.exception.Message - } - return ConvertTo-Json $output`; - core.debug(`Azure PowerShell Login Script: ${this.script}`); - return this.script; - } - - getLatestModuleScript(moduleName: string): string { - const command: string = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`; - this.script += `try { - $ErrorActionPreference = "Stop" - $WarningPreference = "SilentlyContinue" - $output = @{} - $data = ${command} - $output['${Constants.AzVersion}'] = $data.Version.ToString() - $output['${Constants.Success}'] = "true" - } - catch { - $output['${Constants.Error}'] = $_.exception.Message - } - return ConvertTo-Json $output`; - core.debug(`GetLatestModuleScript: ${this.script}`); - return this.script; - } -} diff --git a/src/PowerShell/Utilities/Utils.ts b/src/PowerShell/Utilities/Utils.ts deleted file mode 100644 index 8814ab1f..00000000 --- a/src/PowerShell/Utilities/Utils.ts +++ /dev/null @@ -1,61 +0,0 @@ -import * as os from 'os'; - -import Constants from '../Constants'; -import ScriptBuilder from './ScriptBuilder'; -import PowerShellToolRunner from './PowerShellToolRunner'; - -export default class Utils { - /** - * Add the folder path where Az modules are present to PSModulePath based on runner - * @param azPSVersion - * If azPSVersion is empty, folder path in which all Az modules are present are set - * If azPSVersion is not empty, folder path of exact Az module version is set - */ - static setPSModulePath(azPSVersion: string = "") { - let modulePath: string = ""; - const runner: string = process.env.RUNNER_OS || os.type(); - switch (runner.toLowerCase()) { - case "linux": - modulePath = `/usr/share/${azPSVersion}:`; - break; - case "windows": - case "windows_nt": - modulePath = `C:\\Modules\\${azPSVersion};`; - break; - case "macos": - case "darwin": - throw new Error(`OS not supported`); - default: - throw new Error(`Unknown os: ${runner.toLowerCase()}`); - } - process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`; - } - - static async getLatestModule(moduleName: string): Promise { - let output: string = ""; - const options: any = { - listeners: { - stdout: (data: Buffer) => { - output += data.toString(); - } - } - }; - await PowerShellToolRunner.init(); - await PowerShellToolRunner.executePowerShellScriptBlock(new ScriptBuilder() - .getLatestModuleScript(moduleName), options); - const result = JSON.parse(output.trim()); - if (!(Constants.Success in result)) { - throw new Error(result[Constants.Error]); - } - const azLatestVersion: string = result[Constants.AzVersion]; - if (!Utils.isValidVersion(azLatestVersion)) { - throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`); - } - return azLatestVersion; - } - - static isValidVersion(version: string): boolean { - return !!version.match(Constants.versionPattern); - } -} - diff --git a/src/main.ts b/src/main.ts index 19a66a49..e75c80db 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,6 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; import * as io from '@actions/io'; import { FormatType, SecretParser } from 'actions-secret-parser'; -import { ServicePrincipalLogin } from './PowerShell/ServicePrincipalLogin'; var azPath: string; var prefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : ""; @@ -47,8 +46,7 @@ export async function main() { let subscriptionId = secrets.getSecret("$.subscriptionId", false); let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); let environment = core.getInput("environment").toLowerCase(); - // const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true"; - // const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true"; + if (!servicePrincipalId || !servicePrincipalKey || !tenantId) { throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied."); @@ -98,20 +96,7 @@ export async function main() { await executeAzCliCommand(`cloud set -n "${environment}"`, false); console.log(`Done setting cloud: "${environment}"`); - // Attempting Az cli login - // if (allowNoSubscriptionsLogin) { - // let args = [ - // "--allow-no-subscriptions", - // "--service-principal", - // "-u", servicePrincipalId, - // "-p", servicePrincipalKey, - // "--tenant", tenantId - // ]; - // await executeAzCliCommand(`login`, true, {}, args); - // } - // else { - - // } + let args = [ "--service-principal", "-u", servicePrincipalId, @@ -126,20 +111,7 @@ export async function main() { await executeAzCliCommand(`account set`, true, {}, args); isAzCLISuccess = true; - // if (enableAzPSSession) { - // // Attempting Az PS login - // console.log(`Running Azure PS Login`); - // const spnlogin: ServicePrincipalLogin = new ServicePrincipalLogin( - // servicePrincipalId, - // servicePrincipalKey, - // tenantId, - // subscriptionId, - // allowNoSubscriptionsLogin, - // environment, - // resourceManagerEndpointUrl); - // await spnlogin.initialize(); - // await spnlogin.login(); - // } + console.log("Login successful."); } @@ -147,9 +119,6 @@ export async function main() { 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 { - // 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 { @@ -174,4 +143,3 @@ export async function executeAzCliCommand( } } -//main(); \ No newline at end of file