Merge ~barryprice/charm-telegraf:master into charm-telegraf:master

Proposed by Barry Price
Status: Merged
Approved by: Barry Price
Approved revision: e127023015de01c0434d53a936118a75bf2e4d96
Merged at revision: a310706c90982b25be4b20c363cc0c99590637a4
Proposed branch: ~barryprice/charm-telegraf:master
Merge into: charm-telegraf:master
Diff against target: 97 lines (+47/-3)
3 files modified
src/config.yaml (+3/-2)
src/reactive/telegraf.py (+23/-1)
src/tests/unit/test_telegraf.py (+21/-0)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Barry Price Needs Resubmitting
BootStack Reviewers Pending
Review via email: mp+398433@code.launchpad.net

Commit message

Make our prometheus_ip_range config option a little more resilient

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Barry Price (barryprice) wrote :

Addressed feedback, pushed changes

review: Needs Resubmitting
Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision a310706c90982b25be4b20c363cc0c99590637a4

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/config.yaml b/src/config.yaml
index ecdc9a7..f2bf6f6 100644
--- a/src/config.yaml
+++ b/src/config.yaml
@@ -210,8 +210,9 @@ options:
210 default: ""210 default: ""
211 type: string211 type: string
212 description: >212 description: >
213 If set, the list of IP Ranges which are allowed to access metrics.213 If set, the comma-separated list of IP Ranges which are allowed to access
214 ex: prometheus_ip_range = "[192.168.0.0/24, 192.168.1.0/30]"214 metrics.
215 ex: prometheus_ip_range = "192.168.0.0/24,192.168.1.0/30"
215 collect_iptables_metrics:216 collect_iptables_metrics:
216 default: false217 default: false
217 type: boolean218 type: boolean
diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py
index ea2af96..e3fcad3 100644
--- a/src/reactive/telegraf.py
+++ b/src/reactive/telegraf.py
@@ -17,6 +17,7 @@
17import base6417import base64
18import binascii18import binascii
19import io19import io
20import ipaddress
20import json21import json
21import os22import os
22import re23import re
@@ -84,6 +85,10 @@ class InvalidInstallMethod(Exception):
84 pass85 pass
8586
8687
88class InvalidPrometheusIPRange(Exception):
89 pass
90
91
87def write_telegraf_file(path, content):92def write_telegraf_file(path, content):
88 return host.write_file(93 return host.write_file(
89 path,94 path,
@@ -383,7 +388,24 @@ def get_prometheus_ip_range():
383 config = hookenv.config()388 config = hookenv.config()
384 if config.get("prometheus_ip_range") == "":389 if config.get("prometheus_ip_range") == "":
385 return []390 return []
386 return config.get("prometheus_ip_range").split(",")391 # we should have a list of IPs, confirm that's the case
392 ips = []
393 for ip in config.get("prometheus_ip_range").split(","):
394 # strip any spaces
395 ip = ip.strip()
396 try:
397 ipaddress.ip_network(ip)
398 except ValueError:
399 hookenv.log(
400 "Invalid prometheus_ip_range provided: {}".format(
401 config.get("prometheus_ip_range")
402 ),
403 level=hookenv.ERROR,
404 )
405 raise InvalidPrometheusIPRange()
406 else:
407 ips.append(ip)
408 return ips
387409
388410
389def get_socket_listener_port():411def get_socket_listener_port():
diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py
index 9771618..56da00f 100644
--- a/src/tests/unit/test_telegraf.py
+++ b/src/tests/unit/test_telegraf.py
@@ -1181,6 +1181,27 @@ def test_prometheus_client_output(mocker, monkeypatch, config):
1181 )1181 )
11821182
11831183
1184def test_get_prometheus_ip_range_with_bad_data(mocker, monkeypatch, config):
1185 # if prometheus_ip_range is populated with an invalid IP, confirm we block
1186 config["prometheus_ip_range"] = "1.2.3.4, 5.6.7.8, banana"
1187 # we expect to raise this specific exception (and the hook to therefore fail)
1188 with pytest.raises(telegraf.InvalidPrometheusIPRange):
1189 telegraf.get_prometheus_ip_range()
1190
1191
1192def test_get_prometheus_ip_range_with_good_data(mocker, monkeypatch, config):
1193 # if prometheus_ip_range is populated with valid IPs, confirm we don't block
1194 status_set = mocker.patch("reactive.telegraf.hookenv.status_set")
1195 config["prometheus_ip_range"] = "192.168.0.0/24, 10.0.0.0/8"
1196
1197 our_range = telegraf.get_prometheus_ip_range()
1198 # confirm we didn't block
1199 assert not status_set.called
1200 # and confirm we got the expected range back
1201 expected_range = ["192.168.0.0/24", "10.0.0.0/8"]
1202 assert our_range == expected_range
1203
1204
1184def test_prometheus_client_output_departed(mocker, monkeypatch, config):1205def test_prometheus_client_output_departed(mocker, monkeypatch, config):
1185 configs_dir().join("prometheus_client.conf").write("empty")1206 configs_dir().join("prometheus_client.conf").write("empty")
1186 relations = [1]1207 relations = [1]

Subscribers

People subscribed via source and target branches

to all changes: