opentofu/backend/remote-state/azure/sender.go
Tom Harvey 0ec109bdc0
backend/azurerm: upgrading the SDK / support for proxies (#19414)
* vendor updates

- updating to v21.3.0 of github.com/Azure/azure-sdk-for-go
- updating to v10.15.4 of github.com/Azure/go-autorest
- vendoring github.com/hashicorp/go-azure-helpers @ 0.1.1

* backend/azurerm: refactoring to use the new auth package

- refactoring the backend to use a shared client via the new auth package
- adding tests covering both Service Principal and Access Key auth
- support for authenticating using a proxy
- rewriting the backend documentation to include examples of both authentication types

* switching to use the build-in logging function

* documenting it's also possible to retrieve the access key from an env var
2018-11-21 22:06:03 +01:00

65 lines
1.7 KiB
Go

package azure
import (
"log"
"net/http"
"net/http/httputil"
"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/terraform/helper/logging"
)
func buildSender() autorest.Sender {
return autorest.DecorateSender(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}, withRequestLogging())
}
func withRequestLogging() autorest.SendDecorator {
return func(s autorest.Sender) autorest.Sender {
return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
// only log if logging's enabled
logLevel := logging.LogLevel()
if logLevel == "" {
return s.Do(r)
}
// strip the authorization header prior to printing
authHeaderName := "Authorization"
auth := r.Header.Get(authHeaderName)
if auth != "" {
r.Header.Del(authHeaderName)
}
// dump request to wire format
if dump, err := httputil.DumpRequestOut(r, true); err == nil {
log.Printf("[DEBUG] Azure Backend Request: \n%s\n", dump)
} else {
// fallback to basic message
log.Printf("[DEBUG] Azure Backend Request: %s to %s\n", r.Method, r.URL)
}
// add the auth header back
if auth != "" {
r.Header.Add(authHeaderName, auth)
}
resp, err := s.Do(r)
if resp != nil {
// dump response to wire format
if dump, err2 := httputil.DumpResponse(resp, true); err2 == nil {
log.Printf("[DEBUG] Azure Backend Response for %s: \n%s\n", r.URL, dump)
} else {
// fallback to basic message
log.Printf("[DEBUG] Azure Backend Response: %s for %s\n", resp.Status, r.URL)
}
} else {
log.Printf("[DEBUG] Request to %s completed with no response", r.URL)
}
return resp, err
})
}
}