Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pip install netboxlabs-diode-sdk

### Example

* `target` should be the address of the Diode service, e.g. `grpc://localhost:8080/diode` for insecure connection
or `grpcs://example.com` for secure connection.
* `target` should be the address of the Diode service.
* Insecure connections: `grpc://localhost:8080/diode` or `http://localhost:8080/diode`
* Secure connections: `grpcs://example.com` or `https://example.com`

```python
from netboxlabs.diode.sdk import DiodeClient
Expand Down
11 changes: 7 additions & 4 deletions netboxlabs/diode/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ def parse_target(target: str) -> tuple[str, str, bool]:
"""Parse the target into authority, path and tls_verify."""
parsed_target = urlparse(target)

if parsed_target.scheme not in ["grpc", "grpcs"]:
raise ValueError("target should start with grpc:// or grpcs://")
if parsed_target.scheme not in ["grpc", "grpcs", "http", "https"]:
raise ValueError("target should start with grpc:// or grpcs:// or http:// or https://")

tls_verify = parsed_target.scheme == "grpcs"
tls_verify = parsed_target.scheme in ["grpcs", "https"]

authority = parsed_target.netloc

if ":" not in authority:
authority += ":443"
if parsed_target.scheme in ["grpc", "http"]:
authority += ":80"
elif parsed_target.scheme in ["grpcs", "https"]:
authority += ":443"

return authority, parsed_target.path, tls_verify

Expand Down
25 changes: 15 additions & 10 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,10 @@ def test_load_certs_returns_bytes():
assert isinstance(_load_certs(), bytes)


def test_parse_target_handles_http_prefix():
"""Check that parse_target raises an error when the target contains http://."""
def test_parse_target_handles_ftp_prefix():
"""Check that parse_target raises an error when the target contains ftp://."""
with pytest.raises(ValueError):
parse_target("http://localhost:8081")


def test_parse_target_handles_https_prefix():
"""Check that parse_target raises an error when the target contains https://."""
with pytest.raises(ValueError):
parse_target("https://localhost:8081")

parse_target("ftp://localhost:8081")

def test_parse_target_parses_authority_correctly():
"""Check that parse_target parses the authority correctly."""
Expand All @@ -127,6 +120,12 @@ def test_parse_target_parses_authority_correctly():
def test_parse_target_adds_default_port_if_missing():
"""Check that parse_target adds the default port if missing."""
authority, _, _ = parse_target("grpc://localhost")
assert authority == "localhost:80"
authority, _, _ = parse_target("http://localhost")
assert authority == "localhost:80"
authority, _, _ = parse_target("grpcs://localhost")
assert authority == "localhost:443"
authority, _, _ = parse_target("https://localhost")
assert authority == "localhost:443"


Expand All @@ -144,8 +143,14 @@ def test_parse_target_handles_no_path():

def test_parse_target_parses_tls_verify_correctly():
"""Check that parse_target parses tls_verify correctly."""
_, _, tls_verify = parse_target("grpc://localhost:8081")
assert tls_verify is False
_, _, tls_verify = parse_target("http://localhost:8081")
assert tls_verify is False
_, _, tls_verify = parse_target("grpcs://localhost:8081")
assert tls_verify is True
_, _, tls_verify = parse_target("https://localhost:8081")
assert tls_verify is True


def test_get_sentry_dsn_returns_env_var_when_no_input():
Expand Down