Skip to content

Commit a2be320

Browse files
committed
added review comments
1 parent 7e72509 commit a2be320

12 files changed

Lines changed: 471 additions & 109 deletions

File tree

lib/PowerShell/Constants.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ exports.default = Constants;
66
Constants.prefix = "az_";
77
Constants.moduleName = "Az.Accounts";
88
Constants.versionPattern = /[0-9]\.[0-9]\.[0-9]/;
9-
Constants.environment = "AzureCloud";
10-
Constants.scopeLevel = "Subscription";
11-
Constants.scheme = "ServicePrincipal";
9+
Constants.AzureCloud = "AzureCloud";
10+
Constants.Subscription = "Subscription";
11+
Constants.ServicePrincipal = "ServicePrincipal";
12+
Constants.Success = "Success";
13+
Constants.Error = "Error";
14+
Constants.AzVersion = "AzVersion";

lib/PowerShell/ServicePrincipalLogin.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,59 @@ class ServicePrincipalLogin {
3434
initialize() {
3535
return __awaiter(this, void 0, void 0, function* () {
3636
Utils_1.default.setPSModulePath();
37-
const azLatestVersion = yield Utils_1.default.getLatestModule(Constants_1.default.moduleName);
37+
const script = new ScriptBuilder_1.default().getLatestModuleScript(Constants_1.default.moduleName);
38+
const outputJson = yield this.getLatestModule(script);
39+
const azLatestVersion = outputJson.Constants.AzPSVersion;
40+
if (!(Constants_1.default.Success in outputJson) || !Utils_1.default.isValidVersion(azLatestVersion)) {
41+
throw new Error(`Invalid AzPSVersion: ${azLatestVersion}`);
42+
}
3843
core.debug(`Az Module version used: ${azLatestVersion}`);
3944
Utils_1.default.setPSModulePath(`${Constants_1.default.prefix}${azLatestVersion}`);
4045
});
4146
}
47+
getLatestModule(script) {
48+
return __awaiter(this, void 0, void 0, function* () {
49+
let output = "";
50+
const options = {
51+
listeners: {
52+
stdout: (data) => {
53+
output += data.toString();
54+
}
55+
}
56+
};
57+
yield PowerShellToolRunner_1.default.init();
58+
yield PowerShellToolRunner_1.default.executePowerShellCommand(script, options);
59+
return JSON.parse(output.trim());
60+
});
61+
}
4262
login() {
4363
return __awaiter(this, void 0, void 0, function* () {
64+
let output = "";
65+
const options = {
66+
listeners: {
67+
stdout: (data) => {
68+
output += data.toString();
69+
}
70+
}
71+
};
72+
const args = {
73+
servicePrincipalId: this.servicePrincipalId,
74+
servicePrincipalKey: this.servicePrincipalKey,
75+
subscriptionId: this.subscriptionId,
76+
environment: ServicePrincipalLogin.environment,
77+
scopeLevel: ServicePrincipalLogin.scopeLevel
78+
};
79+
const script = new ScriptBuilder_1.default().getAzPSLoginScript(ServicePrincipalLogin.scheme, this.tenantId, args);
4480
yield PowerShellToolRunner_1.default.init();
45-
const scriptBuilder = new ScriptBuilder_1.default();
46-
const script = scriptBuilder.getScript(ServicePrincipalLogin.scheme, this.tenantId, this.servicePrincipalId, this.servicePrincipalKey, this.subscriptionId, ServicePrincipalLogin.environment, ServicePrincipalLogin.scopeLevel);
47-
PowerShellToolRunner_1.default.executePowerShellCommand(script);
81+
yield PowerShellToolRunner_1.default.executePowerShellCommand(script, options);
82+
const outputJson = JSON.parse(output.trim());
83+
if (!(Constants_1.default.Success in outputJson)) {
84+
throw new Error(`Azure PowerShell login failed with error: ${outputJson.Constants.Error}`);
85+
}
4886
});
4987
}
5088
}
5189
exports.ServicePrincipalLogin = ServicePrincipalLogin;
52-
ServicePrincipalLogin.environment = Constants_1.default.environment;
53-
ServicePrincipalLogin.scopeLevel = Constants_1.default.scopeLevel;
54-
ServicePrincipalLogin.scheme = Constants_1.default.scheme;
90+
ServicePrincipalLogin.environment = Constants_1.default.AzureCloud;
91+
ServicePrincipalLogin.scopeLevel = Constants_1.default.Subscription;
92+
ServicePrincipalLogin.scheme = Constants_1.default.ServicePrincipal;

lib/PowerShell/Utilities/ScriptBuilder.js

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,60 @@
11
"use strict";
2+
var __importStar = (this && this.__importStar) || function (mod) {
3+
if (mod && mod.__esModule) return mod;
4+
var result = {};
5+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6+
result["default"] = mod;
7+
return result;
8+
};
9+
var __importDefault = (this && this.__importDefault) || function (mod) {
10+
return (mod && mod.__esModule) ? mod : { "default": mod };
11+
};
212
Object.defineProperty(exports, "__esModule", { value: true });
13+
const core = __importStar(require("@actions/core"));
14+
const Constants_1 = __importDefault(require("../Constants"));
315
class ScriptBuilder {
416
constructor() {
517
this.script = "";
618
}
7-
getScript(scheme, tenantId, servicePrincipalId, servicePrincipalKey, subscriptionId, environment, scopeLevel) {
8-
this.script += `Clear-AzContext -Scope Process; Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`;
9-
if (scheme === "ServicePrincipal") {
10-
this.script += `Connect-AzAccount -ServicePrincipal -Tenant ${tenantId} -Credential \
11-
(New-Object System.Management.Automation.PSCredential('${servicePrincipalId}',(ConvertTo-SecureString ${servicePrincipalKey} -AsPlainText -Force))) \
12-
-Environment ${environment};`;
13-
if (scopeLevel === "Subscription") {
14-
this.script += `Set-AzContext -SubscriptionId ${subscriptionId} -TenantId ${tenantId};`;
19+
getAzPSLoginScript(scheme, tenantId, args) {
20+
let command = `Clear-AzContext -Scope Process;
21+
Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`;
22+
if (scheme === Constants_1.default.ServicePrincipal) {
23+
command += `Connect-AzAccount -ServicePrincipal -Tenant ${tenantId} -Credential \
24+
(New-Object System.Management.Automation.PSCredential('${args.servicePrincipalId}',(ConvertTo-SecureString ${args.servicePrincipalKey} -AsPlainText -Force))) \
25+
-Environment ${args.environment};`;
26+
if (args.scopeLevel === Constants_1.default.Subscription) {
27+
command += `Set-AzContext -SubscriptionId ${args.subscriptionId} -TenantId ${tenantId};`;
1528
}
1629
}
17-
this.script += `Get-AzContext`;
30+
command += `Get-AzContext`;
31+
this.script += `try {
32+
$$ErrorActionPreference = "Stop";
33+
$$output = @{};
34+
${command}
35+
$$output['${Constants_1.default.Success}'] = "true";
36+
}
37+
catch {
38+
$$output['${Constants_1.default.Error}'] = $$_.exception.Message;
39+
}
40+
return ConvertTo-Json $$a`;
41+
core.debug(`Azure PowerShell Login Script: ${this.script}`);
42+
return this.script;
43+
}
44+
getLatestModuleScript(moduleName) {
45+
const command = `Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1`;
46+
this.script += `try {
47+
$$ErrorActionPreference = "Stop";
48+
$$output = @{};
49+
$$data = ${command};
50+
$$output['${Constants_1.default.AzVersion}'] = $$data.Version.ToString();
51+
$$output['${Constants_1.default.Success}'] = "true";
52+
}
53+
catch {
54+
$$output['${Constants_1.default.Error}'] = $$_.exception.Message;
55+
}
56+
return ConvertTo-Json $$a`;
57+
core.debug(`GetLatestModuleScript: ${this.script}`);
1858
return this.script;
1959
}
2060
}

lib/PowerShell/Utilities/Utils.js

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
"use strict";
2-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4-
return new (P || (P = Promise))(function (resolve, reject) {
5-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8-
step((generator = generator.apply(thisArg, _arguments || [])).next());
9-
});
10-
};
112
var __importStar = (this && this.__importStar) || function (mod) {
123
if (mod && mod.__esModule) return mod;
134
var result = {};
@@ -21,33 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
2112
Object.defineProperty(exports, "__esModule", { value: true });
2213
const os = __importStar(require("os"));
2314
const Constants_1 = __importDefault(require("../Constants"));
24-
const PowerShellToolRunner_1 = __importDefault(require("./PowerShellToolRunner"));
2515
class Utils {
26-
static getLatestModule(moduleName) {
27-
return __awaiter(this, void 0, void 0, function* () {
28-
let output = "";
29-
let error = "";
30-
const options = {
31-
listeners: {
32-
stdout: (data) => {
33-
output += data.toString();
34-
},
35-
stderr: (data) => {
36-
error += data.toString();
37-
}
38-
}
39-
};
40-
yield PowerShellToolRunner_1.default.init();
41-
yield PowerShellToolRunner_1.default.executePowerShellCommand(`(Get-Module -Name ${moduleName} -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1).Version.ToString()`, options);
42-
if (!Utils.isValidVersion(output.trim())) {
43-
return "";
44-
}
45-
return output.trim();
46-
});
47-
}
48-
static isValidVersion(version) {
49-
return !!version.match(Constants_1.default.versionPattern);
50-
}
5116
static setPSModulePath(azPSVersion = "") {
5217
let modulePath = "";
5318
const runner = process.env.RUNNER_OS || os.type();
@@ -64,9 +29,12 @@ class Utils {
6429
// TODO: add modulepath
6530
break;
6631
default:
67-
throw new Error("Unknown os");
32+
throw new Error(`Unknown os: ${runner.toLowerCase()}`);
6833
}
6934
process.env.PSModulePath = `${modulePath}${process.env.PSModulePath}`;
7035
}
36+
static isValidVersion(version) {
37+
return !!version.match(Constants_1.default.versionPattern);
38+
}
7139
}
7240
exports.default = Utils;

lib/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ function main() {
4444
if (!servicePrincipalId || !servicePrincipalKey || !tenantId || !subscriptionId) {
4545
throw new Error("Not all values are present in the creds object. Ensure clientId, clientSecret, tenantId and subscriptionId are supplied.");
4646
}
47+
// Attempting Az cli login
4748
yield executeAzCliCommand(`login --service-principal -u "${servicePrincipalId}" -p "${servicePrincipalKey}" --tenant "${tenantId}"`);
4849
yield executeAzCliCommand(`account set --subscription "${subscriptionId}"`);
4950
if (enablePSSession) {
51+
// Attempting Az PS login
5052
console.log(`Running Azure PS Login`);
5153
const spnlogin = new ServicePrincipalLogin_1.ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId);
52-
spnlogin.initialize();
53-
spnlogin.login();
54+
yield spnlogin.initialize();
55+
yield spnlogin.login();
5456
}
5557
console.log("Login successful.");
5658
}

0 commit comments

Comments
 (0)