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

Adding proxy support #154

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 3.0.0 (unreleased)
## 3.1.0 (unreleased)

ENHANCEMENTS:

* provider: Added optional HTTP `proxy` configuration [#154](https://github.com/hashicorp/terraform-provider-tls/pull/154)).

## 3.0.0-rc.1 (unreleased)

NOTES:

Expand Down
51 changes: 49 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,52 @@ description: |-
The HTTP provider is a utility provider for interacting with generic HTTP
servers as part of a Terraform configuration.

This provider requires no configuration. For information on the resources
it provides, see the navigation bar.
### Configuring Proxy

```terraform
# This example makes an HTTP request to `website.com`
# via an HTTP proxy at `corporate.proxy.service`.

provider "http" {
proxy {
url = "https://corporate.proxy.service"
}
}

data "http" "test" {
url = "https://website.com"
}
```

```terraform
# This example makes an HTTP request to `website.com`
# via an HTTP proxy defined through environment variables (see
# https://pkg.go.dev/net/http#ProxyFromEnvironment for details).

provider "http" {
proxy {
from_env = true
}
}

data "http" "test" {
url = "https://website.com"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `proxy` (Attributes) Proxy used by resources and data sources that connect to external endpoints. (see [below for nested schema](#nestedatt--proxy))

<a id="nestedatt--proxy"></a>
### Nested Schema for `proxy`

Optional:

- `from_env` (Boolean) When `true` the provider will discover the proxy configuration from environment variables. This is based upon [`http.ProxyFromEnvironment`](https://pkg.go.dev/net/http#ProxyFromEnvironment) and it supports the same environment variables (default: `true`).
- `password` (String, Sensitive) Password used for Basic authentication against the Proxy.
- `url` (String) URL used to connect to the Proxy. Accepted schemes are: `http`, `https`, `socks5`.
- `username` (String) Username (or Token) used for Basic authentication against the Proxy.
12 changes: 12 additions & 0 deletions examples/provider/provider_with_proxy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This example makes an HTTP request to `website.com`
# via an HTTP proxy at `corporate.proxy.service`.

provider "http" {
proxy {
url = "https://corporate.proxy.service"
}
}

data "http" "test" {
url = "https://website.com"
}
13 changes: 13 additions & 0 deletions examples/provider/provider_with_proxy_from_env.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This example makes an HTTP request to `website.com`
# via an HTTP proxy defined through environment variables (see
# https://pkg.go.dev/net/http#ProxyFromEnvironment for details).

provider "http" {
proxy {
from_env = true
}
}

data "http" "test" {
url = "https://website.com"
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module github.com/terraform-providers/terraform-provider-http
go 1.17

require (
github.com/elazarl/goproxy v0.0.0-20220529153421-8ea89ba92021
github.com/hashicorp/terraform-plugin-docs v0.12.0
github.com/hashicorp/terraform-plugin-framework v0.9.0
github.com/hashicorp/terraform-plugin-go v0.10.0
github.com/hashicorp/terraform-plugin-log v0.4.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.18.0
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
)

require (
Expand Down Expand Up @@ -36,7 +39,6 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.17.2 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.4.1 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
Expand All @@ -60,7 +62,6 @@ require (
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.6 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/goproxy v0.0.0-20220529153421-8ea89ba92021 h1:EbF0UihnxWRcIMOwoVtqnAylsqcjzqpSvMdjF2Ud4rA=
github.com/elazarl/goproxy v0.0.0-20220529153421-8ea89ba92021/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -237,6 +241,7 @@ github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXq
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
Expand Down
16 changes: 12 additions & 4 deletions internal/provider/data_source_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ your control should be treated as untrustworthy.`,
}, nil
}

func (d *httpDataSourceType) NewDataSource(context.Context, tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return &httpDataSource{}, nil
func (d *httpDataSourceType) NewDataSource(_ context.Context, p tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return &httpDataSource{
p: p.(*provider),
}, nil
}

var _ tfsdk.DataSource = (*httpDataSource)(nil)

type httpDataSource struct{}
type httpDataSource struct {
p *provider
}

func (d *httpDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var model modelV0
Expand All @@ -98,7 +102,11 @@ func (d *httpDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceReque
url := model.URL.Value
headers := model.RequestHeaders

client := &http.Client{}
client := &http.Client{
Transport: &http.Transport{
Proxy: d.p.proxyForRequestFunc(ctx),
},
}

request, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
Expand Down
Loading