Skip to content

Commit ad983e9

Browse files
authored
Add insecure flag to Prometheus sink to require HTTPS by default (#6688)
Signed-off-by: ps48 <pshenoy36@gmail.com>
1 parent a5020bb commit ad983e9

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

data-prepper-plugins/prometheus-sink/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The Prometheus sink should be configured as part of a Data Prepper pipeline YAML
88

99
### Open Source Prometheus (No Auth)
1010

11-
To use with a vanilla Prometheus instance, provide an `http://` or `https://` URL. No `aws` block is needed.
11+
To use with a vanilla Prometheus instance, provide an `https://` URL. If using `http://`, set `insecure: true`. No `aws` block is needed.
1212

1313
Prometheus must be started with the `--web.enable-remote-write-receiver` flag.
1414

@@ -18,8 +18,9 @@ pipeline:
1818
sink:
1919
- prometheus:
2020
url: "http://localhost:9090/api/v1/write"
21+
insecure: true
2122
threshold:
22-
max_events: 500
23+
max_events: 1000
2324
flush_interval: 5s
2425
```
2526
@@ -32,7 +33,7 @@ pipeline:
3233
...
3334
sink:
3435
- prometheus:
35-
url: "http://localhost:9090/api/v1/write"
36+
url: "https://localhost:9090/api/v1/write"
3637
authentication:
3738
http_basic:
3839
username: "promuser"
@@ -53,7 +54,7 @@ pipeline:
5354
region: "us-east-2"
5455
sts_role_arn: "arn:aws:iam::123456789012:role/data-prepper-prometheus-role"
5556
threshold:
56-
max_events: 500
57+
max_events: 1000
5758
flush_interval: 5s
5859
```
5960

@@ -72,12 +73,13 @@ pipeline:
7273

7374
| Option | Description |
7475
|--------|-------------|
75-
| `url` | The Prometheus Remote Write endpoint URL. Supports `http://` and `https://` schemes. When `aws` is configured, `https://` is required. |
76+
| `url` | The Prometheus Remote Write endpoint URL. Supports `https://` by default. To use `http://`, set `insecure` to `true`. When `aws` is configured, `https://` is required. |
7677

7778
### Optional
7879

7980
| Option | Default | Description |
8081
|--------|---------|-------------|
82+
| `insecure` | `false` | When `true`, allows `http://` URLs. By default, only `https://` URLs are permitted. |
8183
| `aws` | `null` | AWS configuration for SigV4 signing. When present, requests are signed with AWS credentials. See [AWS Configuration](#aws-configuration). |
8284
| `authentication` | `null` | HTTP Basic authentication credentials. See [Authentication](#authentication). Cannot be used with `aws`. |
8385
| `encoding` | `snappy` | Compression encoding. Currently only `snappy` is supported. |
@@ -95,7 +97,7 @@ pipeline:
9597

9698
| Option | Default | Description |
9799
|--------|---------|-------------|
98-
| `max_events` | `500` | Maximum number of events to buffer before flushing. |
100+
| `max_events` | `1000` | Maximum number of events to buffer before flushing. |
99101
| `max_request_size` | `1048576` (1 MB) | Maximum request size in bytes before flushing. |
100102
| `flush_interval` | `10000` (ms) | Maximum time in milliseconds to wait before flushing the buffer. |
101103

data-prepper-plugins/prometheus-sink/src/main/java/org/opensearch/dataprepper/plugins/sink/prometheus/configuration/PrometheusSinkConfiguration.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ public class PrometheusSinkConfiguration {
8181
@DurationMax(seconds = 600)
8282
private Duration idleTimeout = DEFAULT_IDLE_TIMEOUT;
8383

84+
@JsonProperty("insecure")
85+
private boolean insecure = false;
86+
8487
@JsonProperty("sanitize_names")
8588
private boolean sanitizeNames = true;
8689

90+
public boolean isInsecure() {
91+
return insecure;
92+
}
93+
8794
public boolean getSanitizeNames() {
8895
return sanitizeNames;
8996
}
@@ -139,6 +146,17 @@ public Duration getIdleTimeout() {
139146
return idleTimeout;
140147
}
141148

149+
@AssertTrue(message = "url must be https when insecure is not set to true.")
150+
boolean isHttpsOrInsecure() {
151+
if (url == null) {
152+
return true;
153+
}
154+
if (insecure) {
155+
return true;
156+
}
157+
return url.startsWith("https://");
158+
}
159+
142160
@AssertTrue(message = "Cannot use both AWS SigV4 and authentication options. Choose one.")
143161
boolean isValidAuthConfig() {
144162
return !(awsConfig != null && authentication != null);

data-prepper-plugins/prometheus-sink/src/test/java/org/opensearch/dataprepper/plugins/sink/prometheus/configuration/PrometheusSinkConfigurationTest.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ void prometheus_sink_config_test_with_invalid_url() throws JsonProcessingExcepti
123123
}
124124

125125
@Test
126-
void prometheus_sink_config_test_with_http_url_is_valid() throws JsonProcessingException {
126+
void prometheus_sink_config_test_with_http_url_and_insecure_is_valid() throws JsonProcessingException {
127127
final String HTTP_SINK_YAML =
128128
" url: \"http://localhost:8080/test\"\n" +
129+
" insecure: true\n" +
129130
" encoding: \"snappy\" \n" +
130131
" remote_write_version: \"0.1.0\" \n" +
131132
" content_type: \"application/x-protobuf\" \n";
132133
final PrometheusSinkConfiguration prometheusSinkConfiguration = objectMapper.readValue(HTTP_SINK_YAML, PrometheusSinkConfiguration.class);
133134
assertTrue(prometheusSinkConfiguration.isValidConfig());
135+
assertTrue(prometheusSinkConfiguration.isHttpsOrInsecure());
134136
}
135137

136138
@Test
@@ -148,6 +150,7 @@ void prometheus_sink_config_test_without_aws_config_returns_null() throws JsonPr
148150
void prometheus_sink_config_test_with_basic_auth() throws JsonProcessingException {
149151
final String AUTH_SINK_YAML =
150152
" url: \"http://localhost:9090/api/v1/write\"\n" +
153+
" insecure: true\n" +
151154
" encoding: \"snappy\" \n" +
152155
" remote_write_version: \"0.1.0\" \n" +
153156
" content_type: \"application/x-protobuf\" \n" +
@@ -164,6 +167,7 @@ void prometheus_sink_config_test_with_basic_auth() throws JsonProcessingExceptio
164167
void prometheus_sink_config_test_without_auth_returns_null() throws JsonProcessingException {
165168
final String NO_AUTH_YAML =
166169
" url: \"http://localhost:9090/api/v1/write\"\n" +
170+
" insecure: true\n" +
167171
" encoding: \"snappy\" \n" +
168172
" remote_write_version: \"0.1.0\" \n" +
169173
" content_type: \"application/x-protobuf\" \n";
@@ -192,6 +196,7 @@ void prometheus_sink_config_test_aws_and_auth_is_invalid() throws JsonProcessing
192196
void prometheus_sink_config_test_basic_auth_without_aws_is_valid() throws JsonProcessingException {
193197
final String AUTH_ONLY_YAML =
194198
" url: \"http://localhost:9090/api/v1/write\"\n" +
199+
" insecure: true\n" +
195200
" encoding: \"snappy\" \n" +
196201
" remote_write_version: \"0.1.0\" \n" +
197202
" content_type: \"application/x-protobuf\" \n" +
@@ -207,6 +212,7 @@ void prometheus_sink_config_test_basic_auth_without_aws_is_valid() throws JsonPr
207212
void prometheus_sink_config_test_bearer_token_is_rejected() throws JsonProcessingException {
208213
final String BEARER_TOKEN_YAML =
209214
" url: \"http://localhost:9090/api/v1/write\"\n" +
215+
" insecure: true\n" +
210216
" encoding: \"snappy\" \n" +
211217
" remote_write_version: \"0.1.0\" \n" +
212218
" content_type: \"application/x-protobuf\" \n" +
@@ -221,13 +227,66 @@ void prometheus_sink_config_test_bearer_token_is_rejected() throws JsonProcessin
221227
void prometheus_sink_config_test_without_bearer_token_is_valid() throws JsonProcessingException {
222228
final String NO_BEARER_YAML =
223229
" url: \"http://localhost:9090/api/v1/write\"\n" +
230+
" insecure: true\n" +
224231
" encoding: \"snappy\" \n" +
225232
" remote_write_version: \"0.1.0\" \n" +
226233
" content_type: \"application/x-protobuf\" \n";
227234
final PrometheusSinkConfiguration config = objectMapper.readValue(NO_BEARER_YAML, PrometheusSinkConfiguration.class);
228235
assertTrue(config.isValidBearerTokenConfig());
229236
}
230237

238+
@Test
239+
void prometheus_sink_config_insecure_defaults_to_false() {
240+
final PrometheusSinkConfiguration config = new PrometheusSinkConfiguration();
241+
assertFalse(config.isInsecure());
242+
}
243+
244+
@Test
245+
void prometheus_sink_config_http_url_without_insecure_is_invalid() throws JsonProcessingException {
246+
final String HTTP_YAML =
247+
" url: \"http://localhost:9090/api/v1/write\"\n" +
248+
" encoding: \"snappy\" \n" +
249+
" remote_write_version: \"0.1.0\" \n" +
250+
" content_type: \"application/x-protobuf\" \n";
251+
final PrometheusSinkConfiguration config = objectMapper.readValue(HTTP_YAML, PrometheusSinkConfiguration.class);
252+
assertFalse(config.isHttpsOrInsecure());
253+
}
254+
255+
@Test
256+
void prometheus_sink_config_http_url_with_insecure_true_is_valid() throws JsonProcessingException {
257+
final String HTTP_YAML =
258+
" url: \"http://localhost:9090/api/v1/write\"\n" +
259+
" insecure: true\n" +
260+
" encoding: \"snappy\" \n" +
261+
" remote_write_version: \"0.1.0\" \n" +
262+
" content_type: \"application/x-protobuf\" \n";
263+
final PrometheusSinkConfiguration config = objectMapper.readValue(HTTP_YAML, PrometheusSinkConfiguration.class);
264+
assertTrue(config.isHttpsOrInsecure());
265+
}
266+
267+
@Test
268+
void prometheus_sink_config_https_url_without_insecure_is_valid() throws JsonProcessingException {
269+
final String HTTPS_YAML =
270+
" url: \"https://localhost:9090/api/v1/write\"\n" +
271+
" encoding: \"snappy\" \n" +
272+
" remote_write_version: \"0.1.0\" \n" +
273+
" content_type: \"application/x-protobuf\" \n";
274+
final PrometheusSinkConfiguration config = objectMapper.readValue(HTTPS_YAML, PrometheusSinkConfiguration.class);
275+
assertTrue(config.isHttpsOrInsecure());
276+
}
277+
278+
@Test
279+
void prometheus_sink_config_https_url_with_insecure_true_is_valid() throws JsonProcessingException {
280+
final String HTTPS_YAML =
281+
" url: \"https://localhost:9090/api/v1/write\"\n" +
282+
" insecure: true\n" +
283+
" encoding: \"snappy\" \n" +
284+
" remote_write_version: \"0.1.0\" \n" +
285+
" content_type: \"application/x-protobuf\" \n";
286+
final PrometheusSinkConfiguration config = objectMapper.readValue(HTTPS_YAML, PrometheusSinkConfiguration.class);
287+
assertTrue(config.isHttpsOrInsecure());
288+
}
289+
231290
@Test
232291
void prometheus_sink_config_test_aws_with_http_url_is_invalid() throws JsonProcessingException {
233292
final String AWS_HTTP_SINK_YAML =

0 commit comments

Comments
 (0)