Use IMDS to get subscription for Azure MSI

This commit is contained in:
Joel Lopes 2019-02-21 09:35:14 -08:00
parent 5cc442872e
commit 1c030cb54b
2 changed files with 49 additions and 2 deletions

View File

@ -59,8 +59,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
b.ctxCancel = cancel
defer cancel()
if err := newConfigRetriever().FillParameters(b.config); err != nil {
return nil, err
if !b.config.useMSI() {
if err := newConfigRetriever().FillParameters(b.config); err != nil {
return nil, err
}
}
log.Print(":: Configuration")
@ -74,6 +76,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
return nil, err
}
if b.config.useMSI() {
if err := newConfigRetriever().FillParameters(b.config); err != nil {
return nil, err
}
}
ui.Message("Creating Azure Resource Manager (ARM) client ...")
azureClient, err := NewAzureClient(
b.config.SubscriptionID,

View File

@ -8,6 +8,10 @@ package arm
// 1. TenantID
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/packer/builder/azure/common"
)
@ -24,6 +28,14 @@ func newConfigRetriever() configRetriever {
}
func (cr configRetriever) FillParameters(c *Config) error {
if c.SubscriptionID == "" {
subscriptionID, err := cr.getSubscriptionFromIMDS()
if err != nil {
return err
}
c.SubscriptionID = subscriptionID
}
if c.TenantID == "" {
tenantID, err := cr.findTenantID(*c.cloudEnvironment, c.SubscriptionID)
if err != nil {
@ -34,3 +46,30 @@ func (cr configRetriever) FillParameters(c *Config) error {
return nil
}
func (cr configRetriever) getSubscriptionFromIMDS() (string, error) {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://169.254.169.254/metadata/instance/compute", nil)
req.Header.Add("Metadata", "True")
q := req.URL.Query()
q.Add("format", "json")
q.Add("api-version", "2017-08-01")
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)
result := map[string]string{}
err = json.Unmarshal(resp_body, &result)
if err != nil {
return "", err
}
return result["subscriptionId"], nil
}