Skip to content

Commit e7ab2e7

Browse files
Replace powershell with pwsh in Powershell Login (#2558)
* Replace powershell with pwsh * Fix testcase * Fix testcase * Remove debug
1 parent 7aa3141 commit e7ab2e7

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ jobs:
154154

155155
steps:
156156
- task: PowerShell@2
157-
condition: eq(variables.type, 'windows' )
158157
inputs:
159158
targetType: 'inline'
160159
script: 'Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -AllowClobber -Force'
160+
pwsh: 'true'
161161
displayName: 'Install Powershell Az Module'
162162
- task: GoTool@0
163163
inputs:

common/azure_ps_context_credential.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,15 @@ var defaultAzdTokenProvider PSTokenProvider = func(ctx context.Context, _ string
117117
defer cancel()
118118
}
119119

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

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

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

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

150151
func (c *PowershellContextCredential) createAccessToken(tk []byte) (azcore.AccessToken, error) {
151152
t := struct {
152-
AccessToken string `json:"token"`
153-
ExpiresOn string `json:"expiresOn"`
153+
AccessToken string `json:"Token"`
154+
ExpiresOn string `json:"ExpiresOn"`
154155
}{}
155156

156157
err := json.Unmarshal(tk, &t)

e2etest/zt_basic_cli_ps_auth_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"fmt"
2525
"os"
2626
"os/exec"
27-
"runtime"
2827
"testing"
2928

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

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

163156
script := fmt.Sprintf(cmd, clientSecret, appId)
164-
out, err := exec.Command("powershell", script).Output()
157+
out, err := exec.Command("pwsh", "-Command", script).Output()
165158
if err != nil {
166159
e := err.(*exec.ExitError)
167160
t.Logf(string(e.Stderr))

0 commit comments

Comments
 (0)