Skip to content

Commit 936dbb4

Browse files
feat: tls_config add cert & key parameters (#295)
Co-authored-by: Cristian Ciutea <[email protected]>
1 parent 663fd88 commit 936dbb4

File tree

3 files changed

+77
-44
lines changed

3 files changed

+77
-44
lines changed

Diff for: docs/apis/url.md

+55-43
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# url API
22

3-
The `url` API allows you to retrieve information from an HTTP endpoint.
3+
The `url` API allows you to retrieve information from an HTTP endpoint.
44

5-
* [Basic usage](#Basicusage)
6-
* [Use POST/PUT methods with a body](#UsePOSTPUTmethodswithabody)
7-
* [Configure your HTTPS connections](#ConfigureyourHTTPSconnections)
8-
* [Specify a common base URL](#SpecifyacommonbaseURL)
9-
* [URL with cache for later processing](#URLwithcacheforlaterprocessing)
10-
* [Include response headers on sample](#ReturnResponseHeaders)
5+
- [Basic usage](#Basicusage)
6+
- [Use POST/PUT methods with a body](#UsePOSTPUTmethodswithabody)
7+
- [Configure your HTTPS connections](#ConfigureyourHTTPSconnections)
8+
- [Specify a common base URL](#SpecifyacommonbaseURL)
9+
- [URL with cache for later processing](#URLwithcacheforlaterprocessing)
10+
- [Include response headers on sample](#ReturnResponseHeaders)
1111

12-
## <a name='Basicusage'></a>Basic usage
12+
## <a name='Basicusage'></a>Basic usage
1313

14-
```yaml
14+
```yaml
1515
name: example
1616
apis:
1717
- event_type: ExampleSample
@@ -22,29 +22,31 @@ apis:
2222
2323
The above Flex configuration retrieves a JSON file containing a set of metrics from the provided URL. Note that the `url` key can be followed by a `headers` section, which allows specifying HTTP headers.
2424

25-
## <a name='UsePOSTPUTmethodswithabody'></a>Use POST/PUT methods with a body
25+
## <a name='UsePOSTPUTmethodswithabody'></a>Use POST/PUT methods with a body
2626

2727
To specify a `POST` or `PUT` request with a body, use the `method` and `payload` properties.
2828

2929
```yaml
30-
name: httpPostExample
31-
apis:
30+
name: httpPostExample
31+
apis:
3232
- name: httpPost
3333
url: https://jsonplaceholder.typicode.com/posts
3434
method: POST
35-
payload: >
35+
payload: >
3636
{"title": "foo","body": "bar","userId": 1}
3737
```
3838

39-
## <a name='ConfigureyourHTTPSconnections'></a>Configure your HTTPS connections
39+
## <a name='ConfigureyourHTTPSconnections'></a>Configure your HTTPS connections
4040

4141
When using TLS endpoints with self-signed certificates, define a `tls_config` section with any of the following items:
4242

43-
| Name | Type | Default | Description |
44-
|---:|:---:|:---:|---|
45-
| `enable` | bool | `false` | Set to `true` to enable custom TLS configuration. Requires `ca` to be defined if enabled. |
46-
| `insecure_skip_verify` | bool | `false` | Set to `true` to skip the verification of TLS certificates. |
47-
| `ca` | string | _Empty_ | The Certificate Authority PEM certificate, in case your HTTPS endpoint has self-signed certificates. |
43+
| Name | Type | Default | Description |
44+
| ---------------------: | :----: | :-----: | ------------------------------------------------------------------------------------------------------------ |
45+
| `enable` | bool | `false` | Set to `true` to enable custom TLS configuration. Requires `ca` to be defined if enabled. |
46+
| `insecure_skip_verify` | bool | `false` | Set to `true` to skip the verification of TLS certificates. |
47+
| `ca` | string | _Empty_ | The Certificate Authority PEM certificate, in case your HTTPS endpoint has self-signed certificates. |
48+
| `cert` | string | _Empty_ | PEM encoded certificate (must be used with `key`), in case your HTTPS endpoint has self-signed certificates. |
49+
| `key` | string | _Empty_ | PEM encoded key (must be used with `cert`), in case your HTTPS endpoint has self-signed certificates. |
4850

4951
### TLS configuration example:
5052

@@ -60,18 +62,18 @@ apis:
6062
ca: /etc/bundles/my-ca-cert.pem
6163
```
6264

63-
## <a name='SpecifyacommonbaseURL'></a>Specify a common base URL
65+
## <a name='SpecifyacommonbaseURL'></a>Specify a common base URL
6466

6567
When you have to query several different URLs, specifying a `base_url` under `global` can be quite helpful, as it allows you to provide URL path segment in `url` fields instead of full URLs.
6668

67-
### Base URL example
69+
### Base URL example
6870

6971
```yaml
7072
name: consulFlex
7173
global:
72-
base_url: http://consul-host/v1/
73-
headers:
74-
X-Consul-Token: my-root-consul-token
74+
base_url: http://consul-host/v1/
75+
headers:
76+
X-Consul-Token: my-root-consul-token
7577
apis:
7678
- event_type: ConsulHealthSample
7779
url: health/service/consul
@@ -81,7 +83,7 @@ apis:
8183
url: agent/members
8284
```
8385

84-
## <a name='URLwithcacheforlaterprocessing'></a>URL with cache for later processing
86+
## <a name='URLwithcacheforlaterprocessing'></a>URL with cache for later processing
8587

8688
URL invocations are cached to avoid having to query them repeatedly. Use `cache` under `command` to read cached data.
8789

@@ -101,42 +103,52 @@ apis:
101103
- expression: Active connections:\s(\S+)
102104
keys: [net.connectionsActive]
103105
- expression: \s?(\d+)\s(\d+)\s(\d+)
104-
keys: [net.connectionsAcceptedPerSecond, net.handledPerSecond, net.requestsPerSecond]
106+
keys:
107+
[
108+
net.connectionsAcceptedPerSecond,
109+
net.handledPerSecond,
110+
net.requestsPerSecond,
111+
]
105112
- expression: Reading:\s(\d+)\s\S+\s(\d+)\s\S+\s(\d+)
106-
keys: [net.connectionsReading, net.connectionsWriting, net.connectionsWaiting]
113+
keys:
114+
[
115+
net.connectionsReading,
116+
net.connectionsWriting,
117+
net.connectionsWaiting,
118+
]
107119
math:
108120
net.connectionsDroppedPerSecond: ${net.connectionsAcceptedPerSecond} - ${net.handledPerSecond}
109121
```
110122

111-
## <a name='ReturnResponseHeaders'></a>Include response headers on sample
123+
## <a name='ReturnResponseHeaders'></a>Include response headers on sample
112124

113125
To include response headers on the metric sample set `return_headers` attribute to true.
114126

115-
### Return headers example
127+
### Return headers example
116128

117129
```yaml
118130
name: example
119131
apis:
120132
- name: ExampleSample
121133
url: https://my-host:8443/admin/metrics/1
122-
return_headers: true
134+
return_headers: true
123135
```
124136

125137
Given the following output for each metric:
126138

127139
```json
128140
{
129-
"event_type": "ExampleSample",
130-
"integration_name": "com.newrelic.nri-flex",
131-
"integration_version": "version-number",
132-
"id": 1,
133-
"completed": "true",
134-
"api.StatusCode": 200,
135-
"api.header.Access-Control-Allow-Credentials": "[true]",
136-
"api.header.Age": "[4459]",
137-
"api.header.Content-Type": "[application/json; charset=utf-8]",
138-
"api.header.Date": "[Mon, 25 May 2020 16:23:53 GMT]",
139-
"api.header.Expires": "[-1]",
140-
"api.header.Retry-Count": "[0]"
141+
"event_type": "ExampleSample",
142+
"integration_name": "com.newrelic.nri-flex",
143+
"integration_version": "version-number",
144+
"id": 1,
145+
"completed": "true",
146+
"api.StatusCode": 200,
147+
"api.header.Access-Control-Allow-Credentials": "[true]",
148+
"api.header.Age": "[4459]",
149+
"api.header.Content-Type": "[application/json; charset=utf-8]",
150+
"api.header.Date": "[Mon, 25 May 2020 16:23:53 GMT]",
151+
"api.header.Expires": "[-1]",
152+
"api.header.Retry-Count": "[0]"
141153
}
142-
```
154+
```

Diff for: internal/inputs/http.go

+19
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ func setRequestOptions(request *gorequest.SuperAgent, yml load.Config, api load.
242242
}
243243
}
244244

245+
if yml.Global.TLSConfig.Key != "" && yml.Global.TLSConfig.Cert != "" {
246+
cert, err := tls.LoadX509KeyPair(yml.Global.TLSConfig.Cert, yml.Global.TLSConfig.Key)
247+
if err != nil {
248+
load.Logrus.WithError(err).Error("http: failed to load x509 keypair")
249+
} else {
250+
tmpGlobalTLSConfig.Certificates = []tls.Certificate{cert}
251+
}
252+
}
253+
245254
request = request.TLSClientConfig(&tmpGlobalTLSConfig)
246255

247256
if api.TLSConfig.Enable {
@@ -260,6 +269,16 @@ func setRequestOptions(request *gorequest.SuperAgent, yml load.Config, api load.
260269
tmpAPITLSConfig.RootCAs = rootCAs
261270
}
262271
}
272+
273+
if api.TLSConfig.Key != "" && api.TLSConfig.Cert != "" {
274+
cert, err := tls.LoadX509KeyPair(api.TLSConfig.Cert, api.TLSConfig.Key)
275+
if err != nil {
276+
load.Logrus.WithError(err).Error("http: failed to load x509 keypair")
277+
} else {
278+
tmpAPITLSConfig.Certificates = []tls.Certificate{cert}
279+
}
280+
}
281+
263282
request = request.TLSClientConfig(&tmpAPITLSConfig)
264283
}
265284

Diff for: internal/load/load.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ type TLSConfig struct {
238238
InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
239239
MinVersion uint16 `yaml:"min_version"`
240240
MaxVersion uint16 `yaml:"max_version"`
241-
Ca string `yaml:"ca"` // path to ca to read
241+
Ca string `yaml:"ca"` // path to ca to read
242+
Key string `yaml:"key"` // path to key to read
243+
Cert string `yaml:"cert"` // path to cert to read
242244
}
243245

244246
// SampleMerge merge multiple samples into one (will remove previous samples)

0 commit comments

Comments
 (0)