Skip to content

Commit 4052783

Browse files
committed
Add option to ignore untrusted SSL certificates
1 parent f404908 commit 4052783

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

CHANGES.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ grafana-wtf changelog
66
in progress
77
===========
88

9-
2022-02-03 0.13.2
9+
2022-03-25 0.13.3
10+
=================
11+
- Add option to ignore untrusted SSL certificates. Thanks, @billabongrob!
12+
13+
2022-03-25 0.13.2
1014
=================
1115
- Use ``grafana-client-2.1.0``, remove monkeypatch
1216
- Tests: Improve fixture ``create_datasource`` to clean up afterwards

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ grafana-wtf - grep through all Grafana entities in the spirit of `git-wtf`_.
4343
********
4444
Synopsis
4545
********
46+
4647
Search Grafana API for string "weatherbase".
4748
::
4849

@@ -110,6 +111,11 @@ Before running ``grafana-wtf``, define URL and access token of your Grafana inst
110111
export GRAFANA_URL=https://daq.example.org/grafana/
111112
export GRAFANA_TOKEN=eyJrIjoiWHg...dGJpZCI6MX0=
112113

114+
In order to ignore untrusted SSL certificates, append the ``?verify=no`` query string
115+
to the ``GRAFANA_URL``::
116+
117+
export GRAFANA_URL=https://daq.example.org/grafana/?verify=no
118+
113119

114120
General information
115121
===================

doc/backlog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Prio 1.25
1616
*********
1717
- [o] Statistics reports for data sources and panels: https://github.com/panodata/grafana-wtf/issues/18
1818
- [o] Finding invalid data sources: https://github.com/panodata/grafana-wtf/issues/19
19+
- [o] Add subcommand ``dump`` for dumping whole documents from the API, unmodified
1920

2021

2122
********

grafana_wtf/core.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import dataclasses
66
import json
77
import logging
8+
import warnings
89
from collections import OrderedDict
910
from concurrent.futures.thread import ThreadPoolExecutor
10-
from urllib.parse import urljoin, urlparse
11+
from urllib.parse import parse_qs, urljoin, urlparse
1112

1213
import colored
1314
import requests
@@ -16,6 +17,7 @@
1617
from grafana_client.client import GrafanaClientError, GrafanaUnauthorizedError
1718
from munch import Munch, munchify
1819
from tqdm import tqdm
20+
from urllib3.exceptions import InsecureRequestWarning
1921

2022
from grafana_wtf.model import (
2123
DashboardDetails,
@@ -24,7 +26,7 @@
2426
DatasourceItem,
2527
GrafanaDataModel,
2628
)
27-
from grafana_wtf.util import JsonPathFinder
29+
from grafana_wtf.util import JsonPathFinder, as_bool
2830

2931
log = logging.getLogger(__name__)
3032

@@ -77,8 +79,16 @@ def grafana_client_factory(grafana_url, grafana_token=None):
7779
password = url.password or "admin"
7880
auth = (username, password)
7981

82+
verify = as_bool(parse_qs(url.query).get("verify", [True])[0])
83+
if verify is False:
84+
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
8085
grafana = GrafanaApi(
81-
auth, protocol=url.scheme, host=url.hostname, port=url.port, url_path_prefix=url.path.lstrip("/")
86+
auth,
87+
protocol=url.scheme,
88+
host=url.hostname,
89+
port=url.port,
90+
url_path_prefix=url.path.lstrip("/"),
91+
verify=verify,
8292
)
8393

8494
return grafana

grafana_wtf/util.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,37 @@ def _dict_representer(dumper, data):
124124

125125
OrderedDumper.add_representer(OrderedDict, _dict_representer)
126126
return yaml.dump(data, stream, OrderedDumper, **kwds)
127+
128+
129+
def as_bool(value: str) -> bool:
130+
"""
131+
Given a string value that represents True or False, returns the Boolean equivalent.
132+
Heavily inspired from distutils strtobool.
133+
134+
From `isort`: https://github.com/PyCQA/isort/blob/5.10.1/isort/settings.py#L915-L922
135+
"""
136+
137+
if value is None:
138+
return False
139+
140+
if isinstance(value, bool):
141+
return value
142+
143+
_STR_BOOLEAN_MAPPING = {
144+
"y": True,
145+
"yes": True,
146+
"t": True,
147+
"on": True,
148+
"1": True,
149+
"true": True,
150+
"n": False,
151+
"no": False,
152+
"f": False,
153+
"off": False,
154+
"0": False,
155+
"false": False,
156+
}
157+
try:
158+
return _STR_BOOLEAN_MAPPING[value.lower()]
159+
except KeyError:
160+
raise ValueError(f"invalid truth value {value}")

0 commit comments

Comments
 (0)