Skip to content
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

Replace powershell with pwsh in Powershell Login #2558

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ jobs:

steps:
- task: PowerShell@2
condition: eq(variables.type, 'windows' )
inputs:
targetType: 'inline'
script: 'Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -AllowClobber -Force'
pwsh: 'true'
displayName: 'Install Powershell Az Module'
- task: GoTool@0
inputs:
Expand Down
19 changes: 10 additions & 9 deletions common/azure_ps_context_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,15 @@ var defaultAzdTokenProvider PSTokenProvider = func(ctx context.Context, _ string
defer cancel()
}

r := regexp.MustCompile(`\{"token".*"expiresOn".*\}`)
r := regexp.MustCompile("(?s){.*Token.*ExpiresOn.*}")

if tenantID != "" {
tenantID += " -TenantId " + tenantID
tenantID += " -TenantId" + tenantID
}
cmd := `$token = Get-AzAccessToken -ResourceUrl https://storage.azure.com` + tenantID + ";"
cmd += `$output = -join('{"token":','"',$token.Token,'"', ',"expiresOn":', '"',$token.ExpiresOn.ToString("yyyy-MM-ddTHH:mm:ss.fffK"),'"',"}");`
cmd += "echo $output"
cmd := "Get-AzAccessToken -ResourceUrl https://storage.azure.com" + tenantID + " | ConvertTo-Json"


cliCmd := exec.CommandContext(ctx, "powershell", cmd)
cliCmd := exec.CommandContext(ctx, "pwsh", "-Command", cmd)
cliCmd.Env = os.Environ()
var stderr bytes.Buffer
cliCmd.Stderr = &stderr
Expand All @@ -142,15 +141,17 @@ var defaultAzdTokenProvider PSTokenProvider = func(ctx context.Context, _ string

output = []byte(r.FindString(string(output)))
if string(output) == "" {
return nil, errors.New(credNamePSContext + "Invalid output while retrving token")
invalidTokenMsg := " Invalid output received while retrieving token with Powershell. Run command \"" + cmd + "\"" +
" on powershell and verify that the output is indeed a valid token."
return nil, errors.New(credNamePSContext + invalidTokenMsg)
}
return output, nil
}

func (c *PowershellContextCredential) createAccessToken(tk []byte) (azcore.AccessToken, error) {
t := struct {
AccessToken string `json:"token"`
ExpiresOn string `json:"expiresOn"`
AccessToken string `json:"Token"`
ExpiresOn string `json:"ExpiresOn"`
}{}

err := json.Unmarshal(tk, &t)
Expand Down
11 changes: 2 additions & 9 deletions e2etest/zt_basic_cli_ps_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"os"
"os/exec"
"runtime"
"testing"

"github.com/Azure/azure-storage-azcopy/v10/common"
Expand Down Expand Up @@ -111,9 +110,6 @@ func TestBasic_PSAuth(t *testing.T) {
recursive: true,
}, &hooks{
beforeTestRun: func(h hookHelper) {
if runtime.GOOS != "windows" {
h.SkipTest()
}
tenId, appId, clientSecret := GlobalInputManager{}.GetServicePrincipalAuth()
cmd := `$secret = ConvertTo-SecureString -String %s -AsPlainText -Force;
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList %s, $secret;
Expand All @@ -123,7 +119,7 @@ func TestBasic_PSAuth(t *testing.T) {
}

script := fmt.Sprintf(cmd, clientSecret, appId)
out, err := exec.Command("powershell", script).Output()
out, err := exec.Command("pwsh", "-Command", script).Output()
if err != nil {
e := err.(*exec.ExitError)
t.Logf(string(e.Stderr))
Expand All @@ -149,9 +145,6 @@ func TestBasic_PSAuthCamelCase(t *testing.T) {
recursive: true,
}, &hooks{
beforeTestRun: func(h hookHelper) {
if runtime.GOOS != "windows" {
h.SkipTest()
}
tenId, appId, clientSecret := GlobalInputManager{}.GetServicePrincipalAuth()
cmd := `$secret = ConvertTo-SecureString -String %s -AsPlainText -Force;
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList %s, $secret;
Expand All @@ -161,7 +154,7 @@ func TestBasic_PSAuthCamelCase(t *testing.T) {
}

script := fmt.Sprintf(cmd, clientSecret, appId)
out, err := exec.Command("powershell", script).Output()
out, err := exec.Command("pwsh", "-Command", script).Output()
if err != nil {
e := err.(*exec.ExitError)
t.Logf(string(e.Stderr))
Expand Down
Loading