From 2cd1bbfddf51e54507f4e92bbd9361cbb4718cc7 Mon Sep 17 00:00:00 2001 From: Ganeshrockz Date: Fri, 30 Oct 2020 13:48:25 +0530 Subject: [PATCH] added no subcriptions login support --- action.yml | 4 ++++ src/PowerShell/ServicePrincipalLogin.ts | 11 +++++++++-- src/PowerShell/Utilities/ScriptBuilder.ts | 4 +--- src/main.ts | 10 ++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index c166d10e..e7bd4f63 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,10 @@ inputs: description: 'Set this value to true to enable Azure PowerShell Login in addition to Az CLI login' required: false default: false + allow-no-subscriptions: + description: 'Set this value to true to enable support for accessing tenants without subscriptions' + required: false + default: false branding: icon: 'login.svg' color: 'blue' diff --git a/src/PowerShell/ServicePrincipalLogin.ts b/src/PowerShell/ServicePrincipalLogin.ts index 8aa293a4..580bff56 100644 --- a/src/PowerShell/ServicePrincipalLogin.ts +++ b/src/PowerShell/ServicePrincipalLogin.ts @@ -13,12 +13,18 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession { servicePrincipalKey: string; tenantId: string; subscriptionId: string; + allowNoSubscriptionsLogin: boolean; - constructor(servicePrincipalId: string, servicePrincipalKey: string, tenantId: string, subscriptionId: string) { + constructor(servicePrincipalId: string, + servicePrincipalKey: string, + tenantId: string, + subscriptionId: string, + allowNoSubscriptionsLogin: boolean) { this.servicePrincipalId = servicePrincipalId; this.servicePrincipalKey = servicePrincipalKey; this.tenantId = tenantId; this.subscriptionId = subscriptionId; + this.allowNoSubscriptionsLogin = allowNoSubscriptionsLogin; } async initialize() { @@ -42,7 +48,8 @@ export class ServicePrincipalLogin implements IAzurePowerShellSession { servicePrincipalKey: this.servicePrincipalKey, subscriptionId: this.subscriptionId, environment: ServicePrincipalLogin.environment, - scopeLevel: ServicePrincipalLogin.scopeLevel + scopeLevel: ServicePrincipalLogin.scopeLevel, + allowNoSubscriptionsLogin: this.allowNoSubscriptionsLogin } const script: string = new ScriptBuilder().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args); await PowerShellToolRunner.init(); diff --git a/src/PowerShell/Utilities/ScriptBuilder.ts b/src/PowerShell/Utilities/ScriptBuilder.ts index fb4fbfcb..9f383891 100644 --- a/src/PowerShell/Utilities/ScriptBuilder.ts +++ b/src/PowerShell/Utilities/ScriptBuilder.ts @@ -12,9 +12,7 @@ export default class ScriptBuilder { 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.subscriptionId && - args.subscriptionId.length > 0) { + if (args.scopeLevel === Constants.Subscription && !args.allowNoSubscriptionsLogin) { command += `Set-AzContext -SubscriptionId '${args.subscriptionId}' -TenantId '${tenantId}' | out-null;`; } } diff --git a/src/main.ts b/src/main.ts index 87e9f4cb..755dcbfe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,11 +31,17 @@ async function main() { let tenantId = secrets.getSecret("$.tenantId", false); let subscriptionId = secrets.getSecret("$.subscriptionId", false); 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."); } + + if (!subscriptionId && !allowNoSubscriptionsLogin) { + throw new Error("Not all values are present in the creds object. Ensure subscriptionId is supplied."); + } + // Attempting Az cli login - if (!subscriptionId) { + if (allowNoSubscriptionsLogin) { await executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); } else { @@ -46,7 +52,7 @@ async function main() { if (enableAzPSSession) { // Attempting Az PS login console.log(`Running Azure PS Login`); - const spnlogin: ServicePrincipalLogin = new ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId); + const spnlogin: ServicePrincipalLogin = new ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, allowNoSubscriptionsLogin); await spnlogin.initialize(); await spnlogin.login(); }