Skip to content

Commit 68b370e

Browse files
committed
refactor diode client target with grpc:// and grpcs://, removed redundant tls verify param/env variable, add tests and refactor existing ones, more code coverage
Signed-off-by: Michal Fiedorowicz <mfiedorowicz@netboxlabs.com>
1 parent bef8de9 commit 68b370e

File tree

7 files changed

+874
-363
lines changed

7 files changed

+874
-363
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pip install netboxlabs-diode-sdk
2121
### Environment variables
2222

2323
* `DIODE_API_KEY` - API key for the Diode service
24-
* `DIODE_TLS_VERIFY` - Verify TLS certificate
2524
* `DIODE_SDK_LOG_LEVEL` - Log level for the SDK (default: `INFO`)
2625
* `DIODE_SENTRY_DSN` - Optional Sentry DSN for error reporting
2726

@@ -39,10 +38,9 @@ from netboxlabs.diode.sdk.ingester import (
3938

4039
def main():
4140
with DiodeClient(
42-
target="localhost:8081",
41+
target="grpc://localhost:8081",
4342
app_name="my-test-app",
4443
app_version="0.0.1",
45-
tls_verify=False,
4644
) as client:
4745
entities = []
4846

docs/entities.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ from netboxlabs.diode.sdk.ingester import (
3333

3434
def main():
3535
with DiodeClient(
36-
target="localhost:8081",
36+
target="grpc://localhost:8081",
3737
app_name="my-test-app",
3838
app_version="0.0.1",
39-
tls_verify=False,
4039
) as client:
4140
entities = []
4241

@@ -144,10 +143,9 @@ from netboxlabs.diode.sdk.ingester import (
144143

145144
def main():
146145
with DiodeClient(
147-
target="localhost:8081",
146+
target="grpc://localhost:8081",
148147
app_name="my-test-app",
149148
app_version="0.0.1",
150-
tls_verify=False,
151149
) as client:
152150
entities = []
153151

@@ -245,10 +243,9 @@ from netboxlabs.diode.sdk.ingester import (
245243

246244
def main():
247245
with DiodeClient(
248-
target="localhost:8081",
246+
target="grpc://localhost:8081",
249247
app_name="my-test-app",
250248
app_version="0.0.1",
251-
tls_verify=False,
252249
) as client:
253250
entities = []
254251

@@ -329,10 +326,9 @@ from netboxlabs.diode.sdk.ingester import (
329326

330327
def main():
331328
with DiodeClient(
332-
target="localhost:8081",
329+
target="grpc://localhost:8081",
333330
app_name="my-test-app",
334331
app_version="0.0.1",
335-
tls_verify=False,
336332
) as client:
337333
entities = []
338334

@@ -405,10 +401,9 @@ from netboxlabs.diode.sdk.ingester import (
405401

406402
def main():
407403
with DiodeClient(
408-
target="localhost:8081",
404+
target="grpc://localhost:8081",
409405
app_name="my-test-app",
410406
app_version="0.0.1",
411-
tls_verify=False,
412407
) as client:
413408
entities = []
414409

@@ -459,10 +454,9 @@ from netboxlabs.diode.sdk.ingester import (
459454

460455
def main():
461456
with DiodeClient(
462-
target="localhost:8081",
457+
target="grpc://localhost:8081",
463458
app_name="my-test-app",
464459
app_version="0.0.1",
465-
tls_verify=False,
466460
) as client:
467461
entities = []
468462

@@ -514,10 +508,9 @@ from netboxlabs.diode.sdk.ingester import (
514508

515509
def main():
516510
with DiodeClient(
517-
target="localhost:8081",
511+
target="grpc://localhost:8081",
518512
app_name="my-test-app",
519513
app_version="0.0.1",
520-
tls_verify=False,
521514
) as client:
522515
entities = []
523516

@@ -583,10 +576,9 @@ from netboxlabs.diode.sdk.ingester import (
583576

584577
def main():
585578
with DiodeClient(
586-
target="localhost:8081",
579+
target="grpc://localhost:8081",
587580
app_name="my-test-app",
588581
app_version="0.0.1",
589-
tls_verify=False,
590582
) as client:
591583
entities = []
592584

@@ -689,10 +681,9 @@ from netboxlabs.diode.sdk.ingester import (
689681

690682
def main():
691683
with DiodeClient(
692-
target="localhost:8081",
684+
target="grpc://localhost:8081",
693685
app_name="my-test-app",
694686
app_version="0.0.1",
695-
tls_verify=False,
696687
) as client:
697688
entities = []
698689

netboxlabs/diode/sdk/client.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
import platform
8+
import urllib
89
import uuid
910
from collections.abc import Iterable
1011

@@ -17,7 +18,6 @@
1718
from netboxlabs.diode.sdk.ingester import Entity
1819

1920
_DIODE_API_KEY_ENVVAR_NAME = "DIODE_API_KEY"
20-
_DIODE_TLS_VERIFY_ENVVAR_NAME = "DIODE_TLS_VERIFY"
2121
_DIODE_SDK_LOG_LEVEL_ENVVAR_NAME = "DIODE_SDK_LOG_LEVEL"
2222
_DIODE_SENTRY_DSN_ENVVAR_NAME = "DIODE_SENTRY_DSN"
2323
_DEFAULT_STREAM = "latest"
@@ -41,34 +41,21 @@ def _get_api_key(api_key: str | None = None) -> str:
4141
return api_key
4242

4343

44-
def _get_tls_verify(tls_verify: bool | None) -> bool:
45-
"""Get TLS Verify either from provided value or environment variable."""
46-
if tls_verify is None:
47-
tls_verify_env_var = os.getenv(_DIODE_TLS_VERIFY_ENVVAR_NAME, "false")
48-
return tls_verify_env_var.lower() in ["true", "1", "yes"]
49-
if not isinstance(tls_verify, bool):
50-
raise DiodeConfigError("tls_verify must be a boolean")
44+
def parse_target(target: str) -> tuple[str, str, bool]:
45+
"""Parse the target into authority, path and tls_verify."""
46+
parsed_target = urllib.parse.urlparse(target)
5147

52-
return tls_verify
48+
if parsed_target.scheme not in ["grpc", "grpcs"]:
49+
raise ValueError("target should start with grpc:// or grpcs://")
5350

51+
tls_verify = parsed_target.scheme == "grpcs"
5452

55-
def parse_target(target: str) -> tuple[str, str]:
56-
"""Parse the target into authority and path."""
57-
if target.startswith(("http://", "https://")):
58-
raise ValueError("target should not contain http:// or https://")
59-
60-
parts = [str(part) for part in target.split("/") if part != ""]
61-
62-
authority = ":".join([str(part) for part in parts[0].split(":") if part != ""])
53+
authority = parsed_target.netloc
6354

6455
if ":" not in authority:
6556
authority += ":443"
6657

67-
path = ""
68-
if len(parts) > 1:
69-
path = "/" + "/".join(parts[1:])
70-
71-
return authority, path
58+
return authority, parsed_target.path, tls_verify
7259

7360

7461
def _get_sentry_dsn(sentry_dsn: str | None = None) -> str | None:
@@ -94,7 +81,6 @@ def __init__(
9481
app_name: str,
9582
app_version: str,
9683
api_key: str | None = None,
97-
tls_verify: bool = None,
9884
sentry_dsn: str = None,
9985
sentry_traces_sample_rate: float = 1.0,
10086
sentry_profiles_sample_rate: float = 1.0,
@@ -103,7 +89,7 @@ def __init__(
10389
log_level = os.getenv(_DIODE_SDK_LOG_LEVEL_ENVVAR_NAME, "INFO").upper()
10490
logging.basicConfig(level=log_level)
10591

106-
self._target, self._path = parse_target(target)
92+
self._target, self._path, self._tls_verify = parse_target(target)
10793
self._app_name = app_name
10894
self._app_version = app_version
10995
self._platform = platform.platform()
@@ -116,8 +102,6 @@ def __init__(
116102
("python-version", self._python_version),
117103
)
118104

119-
self._tls_verify = _get_tls_verify(tls_verify)
120-
121105
if self._tls_verify:
122106
_LOGGER.debug("Setting up gRPC secure channel")
123107
self._channel = grpc.secure_channel(

netboxlabs/diode/sdk/ingester.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,22 @@
11
#!/usr/bin/env python
22
# Copyright 2024 NetBox Labs Inc
33
"""NetBox Labs, Diode - SDK - ingester protobuf message wrappers."""
4-
54
from typing import Any
65

76
from google.protobuf import timestamp_pb2 as _timestamp_pb2
87

8+
# ruff: noqa: I001
99
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
1010
Device as DevicePb,
11-
)
12-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
1311
DeviceType as DeviceTypePb,
14-
)
15-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
1612
Entity as EntityPb,
17-
)
18-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
19-
Interface as InterfacePb,
20-
)
21-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
2213
IPAddress as IPAddressPb,
23-
)
24-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
14+
Interface as InterfacePb,
2515
Manufacturer as ManufacturerPb,
26-
)
27-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
2816
Platform as PlatformPb,
29-
)
30-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
3117
Prefix as PrefixPb,
32-
)
33-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
3418
Role as RolePb,
35-
)
36-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
3719
Site as SitePb,
38-
)
39-
from netboxlabs.diode.sdk.diode.v1.ingester_pb2 import (
4020
Tag as TagPb,
4121
)
4222

0 commit comments

Comments
 (0)