Skip to content

Commit 09a48c1

Browse files
authored
add CA certificate verification and insecure option (hashicorp#125)
Signed-off-by: Jan-Otto Kröpke <[email protected]> Signed-off-by: Jan-Otto Kröpke <[email protected]>
1 parent 90b5183 commit 09a48c1

File tree

4 files changed

+261
-56
lines changed

4 files changed

+261
-56
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 3.2.0 (unreleased)
2+
3+
ENHANCEMENTS:
4+
5+
* data-source/http: Added `ca_cert_pem` attribute which allows PEM encoded certificate(s) to be included in the set of root certificate authorities used when verifying server certificates ([#125](https://github.com/hashicorp/terraform-provider-http/pull/125)).
6+
* data-source/http: Added `insecure` attribute to allow disabling the verification of a server's certificate chain and host name. Defaults to `false` ([#125](https://github.com/hashicorp/terraform-provider-http/pull/125)).
7+
18
## 3.1.0 (August 30, 2022)
29

310
ENHANCEMENTS:

docs/data-sources/http.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ resource "null_resource" "example" {
143143

144144
### Optional
145145

146+
- `ca_cert_pem` (String) Certificate data of the Certificate Authority (CA) in [PEM (RFC 1421)](https://datatracker.ietf.org/doc/html/rfc1421) format.
147+
- `insecure` (Boolean) Disables verification of the server's certificate chain and hostname. Defaults to `false`
146148
- `method` (String) The HTTP Method for the request. Allowed methods are a subset of methods defined in [RFC7231](https://datatracker.ietf.org/doc/html/rfc7231#section-4.3) namely, `GET`, `HEAD`, and `POST`. `POST` support is only intended for read-only URLs, such as submitting a search.
147149
- `request_body` (String) The request body as a string.
148150
- `request_headers` (Map of String) A map of request header field names and values.

internal/provider/data_source_http.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package provider
22

33
import (
44
"context"
5+
"crypto/tls"
6+
"crypto/x509"
57
"fmt"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator"
9+
"github.com/hashicorp/terraform-plugin-framework/path"
610
"io/ioutil"
711
"mime"
812
"net/http"
@@ -104,6 +108,22 @@ your control should be treated as untrustworthy.`,
104108
DeprecationMessage: "Use response_body instead",
105109
},
106110

111+
"ca_cert_pem": {
112+
Description: "Certificate data of the Certificate Authority (CA) " +
113+
"in [PEM (RFC 1421)](https://datatracker.ietf.org/doc/html/rfc1421) format.",
114+
Type: types.StringType,
115+
Optional: true,
116+
Validators: []tfsdk.AttributeValidator{
117+
schemavalidator.ConflictsWith(path.MatchRoot("insecure")),
118+
},
119+
},
120+
121+
"insecure": {
122+
Description: "Disables verification of the server's certificate chain and hostname. Defaults to `false`",
123+
Type: types.BoolType,
124+
Optional: true,
125+
},
126+
107127
"response_headers": {
108128
Description: `A map of response header field names and values.` +
109129
` Duplicate headers are concatenated according to [RFC2616](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).`,
@@ -139,7 +159,33 @@ func (d *httpDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
139159
method = "GET"
140160
}
141161

142-
client := &http.Client{}
162+
caCertificate := model.CaCertificate
163+
164+
tr := &http.Transport{
165+
TLSClientConfig: &tls.Config{},
166+
}
167+
168+
if !model.Insecure.IsNull() {
169+
tr.TLSClientConfig.InsecureSkipVerify = model.Insecure.Value
170+
}
171+
172+
// Use `ca_cert_pem` cert pool
173+
if !caCertificate.IsNull() {
174+
caCertPool := x509.NewCertPool()
175+
if ok := caCertPool.AppendCertsFromPEM([]byte(caCertificate.Value)); !ok {
176+
resp.Diagnostics.AddError(
177+
"Error configuring TLS client",
178+
"Error tls: Can't add the CA certificate to certificate pool. Only PEM encoded certificates are supported.",
179+
)
180+
return
181+
}
182+
183+
tr.TLSClientConfig.RootCAs = caCertPool
184+
}
185+
186+
client := &http.Client{
187+
Transport: tr,
188+
}
143189

144190
request, err := http.NewRequestWithContext(ctx, method, url, requestBody)
145191
if err != nil {
@@ -249,6 +295,8 @@ type modelV0 struct {
249295
RequestHeaders types.Map `tfsdk:"request_headers"`
250296
RequestBody types.String `tfsdk:"request_body"`
251297
ResponseHeaders types.Map `tfsdk:"response_headers"`
298+
CaCertificate types.String `tfsdk:"ca_cert_pem"`
299+
Insecure types.Bool `tfsdk:"insecure"`
252300
ResponseBody types.String `tfsdk:"response_body"`
253301
Body types.String `tfsdk:"body"`
254302
StatusCode types.Int64 `tfsdk:"status_code"`

0 commit comments

Comments
 (0)