Skip to content

Commit

Permalink
added metadata header when msi is used (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
weinong authored and jhendrixMSFT committed Aug 29, 2017
1 parent 10cfe58 commit 5432abe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions autorest/adal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const (

// managedIdentitySettingsPath is the path to the MSI Extension settings file (to discover the endpoint)
managedIdentitySettingsPath = "/var/lib/waagent/ManagedIdentity-Settings"

// metadataHeader is the header required by MSI extension
metadataHeader = "Metadata"
)

var expirationBase time.Time
Expand Down Expand Up @@ -364,6 +367,9 @@ func (spt *ServicePrincipalToken) refreshInternal(resource string) error {

req.ContentLength = int64(len(s))
req.Header.Set(contentType, mimeTypeFormPost)
if _, ok := spt.secret.(*ServicePrincipalMSISecret); ok {
req.Header.Set(metadataHeader, "true")
}
resp, err := spt.sender.Do(req)
if err != nil {
return fmt.Errorf("adal: Failed to execute the refresh request. Error = '%v'", err)
Expand Down
61 changes: 61 additions & 0 deletions autorest/adal/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,67 @@ func TestServicePrincipalTokenRefreshUsesPOST(t *testing.T) {
}
}

func TestServicePrincipalTokenFromMSIRefreshUsesPOST(t *testing.T) {
resource := "https://resource"

cb := func(token Token) error { return nil }
tempSettingsFile, err := ioutil.TempFile("", "ManagedIdentity-Settings")
if err != nil {
t.Fatal("Couldn't write temp settings file")
}
defer os.Remove(tempSettingsFile.Name())

settingsContents := []byte(`{
"url": "http://msiendpoint/"
}`)

if _, err := tempSettingsFile.Write(settingsContents); err != nil {
t.Fatal("Couldn't fill temp settings file")
}

oauthConfig, err := NewOAuthConfig("http://adendpoint", "1-2-3-4")
if err != nil {
t.Fatal("Failed to construct oauthconfig")
}

spt, err := newServicePrincipalTokenFromMSI(
*oauthConfig,
resource,
tempSettingsFile.Name(),
cb)
if err != nil {
t.Fatalf("Failed to get MSI SPT: %v", err)
}

body := mocks.NewBody(newTokenJSON("test", "test"))
resp := mocks.NewResponseWithBodyAndStatus(body, http.StatusOK, "OK")

c := mocks.NewSender()
s := DecorateSender(c,
(func() SendDecorator {
return func(s Sender) Sender {
return SenderFunc(func(r *http.Request) (*http.Response, error) {
if r.Method != "POST" {
t.Fatalf("adal: ServicePrincipalToken#Refresh did not correctly set HTTP method -- expected %v, received %v", "POST", r.Method)
}
if h := r.Header.Get("Metadata"); h != "true" {
t.Fatalf("adal: ServicePrincipalToken#Refresh did not correctly set Metadata header for MSI")
}
return resp, nil
})
}
})())
spt.SetSender(s)
err = spt.Refresh()
if err != nil {
t.Fatalf("adal: ServicePrincipalToken#Refresh returned an unexpected error (%v)", err)
}

if body.IsOpen() {
t.Fatalf("the response was not closed!")
}
}

func TestServicePrincipalTokenRefreshSetsMimeType(t *testing.T) {
spt := newServicePrincipalToken()

Expand Down

0 comments on commit 5432abe

Please sign in to comment.