mirror of
https://github.com/azure/login.git
synced 2026-03-15 09:20:56 -04:00
Compare commits
3 Commits
ganeshrock
...
users/aksm
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f001fbb0b9 | ||
|
|
b4fcf5d07d | ||
|
|
e2ec7a0d58 |
@@ -5,7 +5,7 @@ jest.mock('../../src/PowerShell/Utilities/PowerShellToolRunner');
|
|||||||
let spnlogin: ServicePrincipalLogin;
|
let spnlogin: ServicePrincipalLogin;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
spnlogin = new ServicePrincipalLogin("servicePrincipalID", "servicePrinicipalkey", "tenantId", "subscriptionId");
|
spnlogin = new ServicePrincipalLogin("servicePrincipalID", "servicePrinicipalkey", "tenantId", "subscriptionId", false);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|||||||
25
__tests__/PowerShell/Utilities/ScriptBuilder.test.ts
Normal file
25
__tests__/PowerShell/Utilities/ScriptBuilder.test.ts
Normal file
@@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
21
lib/main.js
21
lib/main.js
@@ -36,7 +36,17 @@ function main() {
|
|||||||
core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString);
|
core.exportVariable('AZURE_HTTP_USER_AGENT', userAgentString);
|
||||||
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv);
|
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv);
|
||||||
azPath = yield io.which("az", true);
|
azPath = yield io.which("az", true);
|
||||||
yield executeAzCliCommand("--version");
|
let output = "";
|
||||||
|
const options = {
|
||||||
|
listeners: {
|
||||||
|
stdout: (data) => {
|
||||||
|
output += data.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
yield executeAzCliCommand("--version", true, options);
|
||||||
|
core.debug(`az cli version used:\n${output}`);
|
||||||
|
|
||||||
let creds = core.getInput('creds', { required: true });
|
let creds = core.getInput('creds', { required: true });
|
||||||
let secrets = new actions_secret_parser_1.SecretParser(creds, actions_secret_parser_1.FormatType.JSON);
|
let secrets = new actions_secret_parser_1.SecretParser(creds, actions_secret_parser_1.FormatType.JSON);
|
||||||
let servicePrincipalId = secrets.getSecret("$.clientId", false);
|
let servicePrincipalId = secrets.getSecret("$.clientId", false);
|
||||||
@@ -45,7 +55,7 @@ function main() {
|
|||||||
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
|
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
|
||||||
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
|
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
|
||||||
const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true";
|
const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true";
|
||||||
console.log(allowNoSubscriptionsLogin);
|
|
||||||
if (!servicePrincipalId || !servicePrincipalKey || !tenantId) {
|
if (!servicePrincipalId || !servicePrincipalKey || !tenantId) {
|
||||||
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied.");
|
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied.");
|
||||||
}
|
}
|
||||||
@@ -53,6 +63,7 @@ function main() {
|
|||||||
throw new Error("Not all values are present in the creds object. Ensure subscriptionId is supplied.");
|
throw new Error("Not all values are present in the creds object. Ensure subscriptionId is supplied.");
|
||||||
}
|
}
|
||||||
// Attempting Az cli login
|
// Attempting Az cli login
|
||||||
|
console.log(`ak - clientId: ${servicePrincipalId}`);
|
||||||
if (allowNoSubscriptionsLogin) {
|
if (allowNoSubscriptionsLogin) {
|
||||||
yield executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true);
|
yield executeAzCliCommand(`login --allow-no-subscriptions --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`, true);
|
||||||
}
|
}
|
||||||
@@ -86,10 +97,12 @@ function main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function executeAzCliCommand(command, silent) {
|
|
||||||
|
function executeAzCliCommand(command, silent, options = {}) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
options.silent = !!silent;
|
||||||
try {
|
try {
|
||||||
yield exec.exec(`"${azPath}" ${command}`, [], { silent: !!silent });
|
yield exec.exec(`"${azPath}" ${command}`, [], options);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
|
|||||||
21
src/main.ts
21
src/main.ts
@@ -21,8 +21,17 @@ async function main() {
|
|||||||
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv);
|
core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv);
|
||||||
|
|
||||||
azPath = await io.which("az", true);
|
azPath = await io.which("az", true);
|
||||||
await executeAzCliCommand("--version");
|
let output: string = "";
|
||||||
|
const options: any = {
|
||||||
|
listeners: {
|
||||||
|
stdout: (data: Buffer) => {
|
||||||
|
output += data.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
await executeAzCliCommand("--version", true, options);
|
||||||
|
core.debug(`az cli version used:\n${output}`);
|
||||||
|
|
||||||
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,7 +40,6 @@ async function main() {
|
|||||||
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
|
let subscriptionId = secrets.getSecret("$.subscriptionId", false);
|
||||||
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
|
const enableAzPSSession = core.getInput('enable-AzPSSession').toLowerCase() === "true";
|
||||||
const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true";
|
const allowNoSubscriptionsLogin = core.getInput('allow-no-subscriptions').toLowerCase() === "true";
|
||||||
console.log(allowNoSubscriptionsLogin);
|
|
||||||
if (!servicePrincipalId || !servicePrincipalKey || !tenantId) {
|
if (!servicePrincipalId || !servicePrincipalKey || !tenantId) {
|
||||||
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied.");
|
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret and tenantId are supplied.");
|
||||||
}
|
}
|
||||||
@@ -71,13 +79,14 @@ async function main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeAzCliCommand(command: string, silent?: boolean) {
|
async function executeAzCliCommand(command: string, silent?: boolean, options: any = {}) {
|
||||||
|
options.silent = !!silent;
|
||||||
try {
|
try {
|
||||||
await exec.exec(`"${azPath}" ${command}`, [], {silent: !!silent});
|
await exec.exec(`"${azPath}" ${command}`, [], options);
|
||||||
}
|
}
|
||||||
catch(error) {
|
catch(error) {
|
||||||
throw new Error(error);
|
throw new Error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
Reference in New Issue
Block a user