From 481142a71d63ab15a2263c5d704006027370877b Mon Sep 17 00:00:00 2001 From: Ganeshrockz Date: Tue, 3 Nov 2020 13:49:13 +0530 Subject: [PATCH] Added no subscription support (#73) * Added no subscription support * Added L0s * added no subcriptions login support * test changes Co-authored-by: Ganesh S Co-authored-by: aksm-ms <58936966+aksm-ms@users.noreply.github.com> --- .../PowerShell/ServicePrinicipalLogin.test.ts | 2 +- .../Utilities/ScriptBuilder.test.ts | 25 +++++++++++++++++++ action.yml | 4 +++ src/PowerShell/ServicePrincipalLogin.ts | 11 ++++++-- src/PowerShell/Utilities/ScriptBuilder.ts | 2 +- src/main.ts | 21 ++++++++++++---- 6 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 __tests__/PowerShell/Utilities/ScriptBuilder.test.ts diff --git a/__tests__/PowerShell/ServicePrinicipalLogin.test.ts b/__tests__/PowerShell/ServicePrinicipalLogin.test.ts index f8fdbb24..ffd59881 100644 --- a/__tests__/PowerShell/ServicePrinicipalLogin.test.ts +++ b/__tests__/PowerShell/ServicePrinicipalLogin.test.ts @@ -5,7 +5,7 @@ jest.mock('../../src/PowerShell/Utilities/PowerShellToolRunner'); let spnlogin: ServicePrincipalLogin; beforeAll(() => { - spnlogin = new ServicePrincipalLogin("servicePrincipalID", "servicePrinicipalkey", "tenantId", "subscriptionId"); + spnlogin = new ServicePrincipalLogin("servicePrincipalID", "servicePrinicipalkey", "tenantId", "subscriptionId", false); }); afterEach(() => { diff --git a/__tests__/PowerShell/Utilities/ScriptBuilder.test.ts b/__tests__/PowerShell/Utilities/ScriptBuilder.test.ts new file mode 100644 index 00000000..291cbd04 --- /dev/null +++ b/__tests__/PowerShell/Utilities/ScriptBuilder.test.ts @@ -0,0 +1,25 @@ +import ScriptBuilder from "../../../src/PowerShell/Utilities/ScriptBuilder"; +import Constants from "../../../src/PowerShell/Constants"; + +describe("Getting AzLogin PS script" , () => { + const scheme = Constants.ServicePrincipal; + let args: any = { + servicePrincipalId: "service-principal-id", + servicePrincipalKey: "service-principal-key", + environment: "environment", + scopeLevel: Constants.Subscription, + subscriptionId: "subId", + allowNoSubscriptionsLogin: true + } + + test("PS script should not set context while passing allowNoSubscriptionsLogin as true", () => { + const loginScript = new ScriptBuilder().getAzPSLoginScript(scheme, "tenant-id", args); + expect(loginScript.includes("Set-AzContext -SubscriptionId")).toBeFalsy(); + }); + + test("PS script should set context while passing allowNoSubscriptionsLogin as false", () => { + args["allowNoSubscriptionsLogin"] = false; + const loginScript = new ScriptBuilder().getAzPSLoginScript(scheme, "tenant-id", args); + expect(loginScript.includes("Set-AzContext -SubscriptionId")).toBeTruthy(); + }); +}); \ No newline at end of file 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 d43060e8..9f383891 100644 --- a/src/PowerShell/Utilities/ScriptBuilder.ts +++ b/src/PowerShell/Utilities/ScriptBuilder.ts @@ -12,7 +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) { + 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 cd355f0e..572c352c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,17 +30,28 @@ async function main() { let tenantId = secrets.getSecret("$.tenantId", false); let subscriptionId = secrets.getSecret("$.subscriptionId", false); 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."); + 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 - await executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); - await executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); + if (allowNoSubscriptionsLogin) { + await executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); + } + else { + await executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); + await executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); + } isAzCLISuccess = true; 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(); }