-
Notifications
You must be signed in to change notification settings - Fork 435
Merge pull request #93 from Azure/azuresovereigncloud #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
594ec8f
66f5d6e
7539594
55dbf29
45f157e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,14 +8,22 @@ export default class ScriptBuilder { | |
| getAzPSLoginScript(scheme: string, tenantId: string, args: any): string { | ||
| let command = `Clear-AzContext -Scope Process; | ||
| Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue;`; | ||
|
|
||
| if (scheme === Constants.ServicePrincipal) { | ||
|
|
||
| if (args.environment.toLowerCase() == "azurestack") { | ||
| command += `Add-AzEnvironment -Name ${args.environment} -ARMEndpoint ${args.resourceManagerEndpointUrl} | out-null;`; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: leave a line
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
|
||
| 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.allowNoSubscriptionsLogin) { | ||
| command += `Set-AzContext -SubscriptionId '${args.subscriptionId}' -TenantId '${tenantId}' | out-null;`; | ||
| } | ||
| } | ||
|
|
||
| this.script += `try { | ||
| $ErrorActionPreference = "Stop" | ||
| $WarningPreference = "SilentlyContinue" | ||
|
|
@@ -27,6 +35,7 @@ export default class ScriptBuilder { | |
| $output['${Constants.Error}'] = $_.exception.Message | ||
| } | ||
| return ConvertTo-Json $output`; | ||
|
|
||
| core.debug(`Azure PowerShell Login Script: ${this.script}`); | ||
| return this.script; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import * as core from '@actions/core'; | ||
| import * as exec from '@actions/exec'; | ||
| import * as io from '@actions/io'; | ||
|
|
||
| import { FormatType, SecretParser } from 'actions-secret-parser'; | ||
| import { ServicePrincipalLogin } from './PowerShell/ServicePrincipalLogin'; | ||
|
|
||
|
|
@@ -21,6 +20,14 @@ async function main() { | |
| core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azurePSHostEnv); | ||
|
|
||
| azPath = await io.which("az", true); | ||
|
|
||
| let azureSupportedCloudName = new Set([ | ||
| "azureusgovernment", | ||
| "azurechinacloud", | ||
| "azuregermancloud", | ||
| "azurecloud", | ||
| "azurestack"]); | ||
|
|
||
| let output: string = ""; | ||
| const execOptions: any = { | ||
| listeners: { | ||
|
|
@@ -38,15 +45,60 @@ async function main() { | |
| let servicePrincipalKey = secrets.getSecret("$.clientSecret", true); | ||
| let tenantId = secrets.getSecret("$.tenantId", false); | ||
| let subscriptionId = secrets.getSecret("$.subscriptionId", false); | ||
| let resourceManagerEndpointUrl = secrets.getSecret("$.resourceManagerEndpointUrl", false); | ||
| let environment = core.getInput("environment").toLowerCase(); | ||
| 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."); | ||
| } | ||
|
|
||
| if (!azureSupportedCloudName.has(environment)){ | ||
| throw new Error("Unsupported value for environment is passed.The list of supported values for environment are ‘azureusgovernment', ‘azurechinacloud’, ‘azuregermancloud’, ‘azurecloud’ or ’azurestack’"); | ||
| } | ||
|
|
||
|
|
||
| // TODO: refactor this into an Azure stack specific utility. | ||
| // Attempting Az cli login | ||
| if (environment == "azurestack") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could have a utility for azure stack
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot do anything for this now. these changes are already in master. I'll add a todo and leave it for later. |
||
| if (!resourceManagerEndpointUrl) { | ||
| throw new Error("resourceManagerEndpointUrl is a required parameter when environment is defined."); | ||
| } | ||
|
|
||
| console.log(`Unregistering cloud: "${environment}" first if it exists`); | ||
| try { | ||
| await executeAzCliCommand(`cloud set -n AzureCloud`, true); | ||
| await executeAzCliCommand(`cloud unregister -n "${environment}"`, false); | ||
| } | ||
| catch (error) { | ||
| console.log(`Ignore cloud not registered error: "${error}"`); | ||
| } | ||
|
|
||
| console.log(`Registering cloud: "${environment}" with ARM endpoint: "${resourceManagerEndpointUrl}"`); | ||
| try { | ||
| let baseUri = resourceManagerEndpointUrl; | ||
| if (baseUri.endsWith('/')) { | ||
| baseUri = baseUri.substring(0, baseUri.length-1); // need to remove trailing / from resourceManagerEndpointUrl to correctly derive suffixes below | ||
| } | ||
| let suffixKeyvault = ".vault" + baseUri.substring(baseUri.indexOf('.')); // keyvault suffix starts with . | ||
| let suffixStorage = baseUri.substring(baseUri.indexOf('.')+1); // storage suffix starts without . | ||
| let profileVersion = "2019-03-01-hybrid"; | ||
| await executeAzCliCommand(`cloud register -n "${environment}" --endpoint-resource-manager "${resourceManagerEndpointUrl}" --suffix-keyvault-dns "${suffixKeyvault}" --suffix-storage-endpoint "${suffixStorage}" --profile "${profileVersion}"`, false); | ||
| } | ||
| catch (error) { | ||
| core.error(`Error while trying to register cloud "${environment}": "${error}"`); | ||
| } | ||
|
|
||
| console.log(`Done registering cloud: "${environment}"`) | ||
| } | ||
|
|
||
| await executeAzCliCommand(`cloud set -n "${environment}"`, false); | ||
| console.log(`Done setting cloud: "${environment}"`); | ||
|
|
||
| // Attempting Az cli login | ||
| if (allowNoSubscriptionsLogin) { | ||
|
|
@@ -73,35 +125,52 @@ async function main() { | |
| ]; | ||
| await executeAzCliCommand(`account set`, true, {}, args); | ||
| } | ||
|
|
||
| isAzCLISuccess = true; | ||
| if (enableAzPSSession) { | ||
| // Attempting Az PS login | ||
| console.log(`Running Azure PS Login`); | ||
| const spnlogin: ServicePrincipalLogin = new ServicePrincipalLogin(servicePrincipalId, servicePrincipalKey, tenantId, subscriptionId, allowNoSubscriptionsLogin); | ||
| const spnlogin: ServicePrincipalLogin = new ServicePrincipalLogin( | ||
| servicePrincipalId, | ||
| servicePrincipalKey, | ||
| tenantId, | ||
| subscriptionId, | ||
| allowNoSubscriptionsLogin, | ||
| environment, | ||
| resourceManagerEndpointUrl); | ||
| await spnlogin.initialize(); | ||
| await spnlogin.login(); | ||
| } | ||
|
|
||
| console.log("Login successful."); | ||
| } catch (error) { | ||
| } | ||
| catch (error) { | ||
| if (!isAzCLISuccess) { | ||
| core.error("Az CLI Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"); | ||
| } else { | ||
| } | ||
| else { | ||
| core.error(`Azure PowerShell Login failed. Please check the credentials. For more information refer https://aka.ms/create-secrets-for-GitHub-workflows"`); | ||
| } | ||
| core.setFailed(error); | ||
| } finally { | ||
| } | ||
| finally { | ||
| // Reset AZURE_HTTP_USER_AGENT | ||
| core.exportVariable('AZURE_HTTP_USER_AGENT', prefix); | ||
| core.exportVariable('AZUREPS_HOST_ENVIRONMENT', azPSHostEnv); | ||
| } | ||
| } | ||
|
|
||
| async function executeAzCliCommand(command: string, silent?: boolean, execOptions: any = {}, args: any = []) { | ||
| async function executeAzCliCommand( | ||
| command: string, | ||
| silent?: boolean, | ||
| execOptions: any = {}, | ||
| args: any = []) { | ||
|
|
||
| execOptions.silent = !!silent; | ||
| try { | ||
| await exec.exec(`"${azPath}" ${command}`, args, execOptions); | ||
| } | ||
| catch(error) { | ||
| catch (error) { | ||
| throw new Error(error); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
casing in description doesnt match the defaultValue's casing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.