From e2ec7a0d584dd8044a3ecef3e857b51f9d527838 Mon Sep 17 00:00:00 2001 From: aksm-ms <58936966+aksm-ms@users.noreply.github.com> Date: Tue, 3 Nov 2020 16:15:14 +0530 Subject: [PATCH] Added no subscription support (#73) (#77) * 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> * adding lib Co-authored-by: Ganeshrockz Co-authored-by: Ganesh S --- .../PowerShell/ServicePrinicipalLogin.test.ts | 2 +- .../Utilities/ScriptBuilder.test.ts | 25 +++++++++++++++++++ action.yml | 4 +++ lib/PowerShell/ServicePrincipalLogin.js | 6 +++-- lib/PowerShell/Utilities/ScriptBuilder.js | 2 +- lib/main.js | 19 ++++++++++---- src/PowerShell/ServicePrincipalLogin.ts | 11 ++++++-- src/PowerShell/Utilities/ScriptBuilder.ts | 2 +- src/main.ts | 21 ++++++++++++---- 9 files changed, 75 insertions(+), 17 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/lib/PowerShell/ServicePrincipalLogin.js b/lib/PowerShell/ServicePrincipalLogin.js index 7a4be40b..519b0d2c 100644 --- a/lib/PowerShell/ServicePrincipalLogin.js +++ b/lib/PowerShell/ServicePrincipalLogin.js @@ -25,11 +25,12 @@ const PowerShellToolRunner_1 = __importDefault(require("./Utilities/PowerShellTo const ScriptBuilder_1 = __importDefault(require("./Utilities/ScriptBuilder")); const Constants_1 = __importDefault(require("./Constants")); class ServicePrincipalLogin { - constructor(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId) { + constructor(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, allowNoSubscriptionsLogin) { this.servicePrincipalId = servicePrincipalId; this.servicePrincipalKey = servicePrincipalKey; this.tenantId = tenantId; this.subscriptionId = subscriptionId; + this.allowNoSubscriptionsLogin = allowNoSubscriptionsLogin; } initialize() { return __awaiter(this, void 0, void 0, function* () { @@ -54,7 +55,8 @@ class ServicePrincipalLogin { servicePrincipalKey: this.servicePrincipalKey, subscriptionId: this.subscriptionId, environment: ServicePrincipalLogin.environment, - scopeLevel: ServicePrincipalLogin.scopeLevel + scopeLevel: ServicePrincipalLogin.scopeLevel, + allowNoSubscriptionsLogin: this.allowNoSubscriptionsLogin }; const script = new ScriptBuilder_1.default().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args); yield PowerShellToolRunner_1.default.init(); diff --git a/lib/PowerShell/Utilities/ScriptBuilder.js b/lib/PowerShell/Utilities/ScriptBuilder.js index 113f8f35..30cb4421 100644 --- a/lib/PowerShell/Utilities/ScriptBuilder.js +++ b/lib/PowerShell/Utilities/ScriptBuilder.js @@ -23,7 +23,7 @@ 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_1.default.Subscription) { + if (args.scopeLevel === Constants_1.default.Subscription && !args.allowNoSubscriptionsLogin) { command += `Set-AzContext -SubscriptionId '${args.subscriptionId}' -TenantId '${tenantId}' | out-null;`; } } diff --git a/lib/main.js b/lib/main.js index 7c33c8fe..eb708357 100644 --- a/lib/main.js +++ b/lib/main.js @@ -44,17 +44,26 @@ 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 - yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); - yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); + if (allowNoSubscriptionsLogin) { + yield executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); + } + else { + yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true); + yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`, true); + } isAzCLISuccess = true; if (enableAzPSSession) { // Attempting Az PS login console.log(`Running Azure PS Login`); - const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId); + const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, allowNoSubscriptionsLogin); yield spnlogin.initialize(); yield spnlogin.login(); } 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 4fc752b2..9d606f1b 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(); }