-
Notifications
You must be signed in to change notification settings - Fork 0
Add get_tls_connection_error_elaboration to allow clients to get more descriptive errors #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexdubois-ni
merged 1 commit into
main
from
users/adubois/addElaborationExtractionMechanismForClients
Aug 25, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| """Marks gRPC channels this package created. | ||
|
|
||
| A caller holding only a channel cannot tell whether NI-TLS had any part in | ||
| building it, so the channel factory tags what it creates. Two features read the | ||
| tag: audit records name the address, and connection-error elaboration speaks only | ||
| for channels we built. | ||
|
|
||
| The tag is advisory, not a security control. It says where a channel came from, | ||
| never that a connection is trustworthy. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| _TARGET_ATTR = "_nitls_channel_target" | ||
|
|
||
|
|
||
| def tag_channel_target(channel: object, target: str) -> None: | ||
| """Record on a channel the ``host:port`` it was created for. Never raises.""" | ||
| try: | ||
| setattr(channel, _TARGET_ATTR, target) | ||
| except Exception: | ||
| logging.getLogger(__name__).debug( | ||
| "Unable to tag channel; it will go unaudited and will not elaborate on " | ||
| "connection errors.", | ||
| exc_info=True, | ||
| ) | ||
|
|
||
|
|
||
| def get_channel_target(channel: object) -> str: | ||
| """Return the ``host:port`` a channel was created for, or empty if we did not create it.""" | ||
| target = getattr(channel, _TARGET_ATTR, "") | ||
| return target if isinstance(target, str) else "" | ||
|
|
||
|
|
||
| def is_nitls_channel(channel: object) -> bool: | ||
| """Return whether this package created the channel.""" | ||
| return bool(get_channel_target(channel)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| "Pytests for nitlsconfig.channel_tag." | ||
|
|
||
| from nitlsconfig.channel_tag import get_channel_target, is_nitls_channel, tag_channel_target | ||
|
|
||
|
|
||
| class FakeChannel: | ||
| """Stands in for a grpc.Channel, which is only an attribute holder here.""" | ||
|
|
||
|
|
||
| def test___tagged_channel___reports_its_target() -> None: | ||
| channel = FakeChannel() | ||
| tag_channel_target(channel, "localhost:31763") | ||
|
|
||
| assert get_channel_target(channel) == "localhost:31763" | ||
| assert is_nitls_channel(channel) | ||
|
|
||
|
|
||
| def test___untagged_channel___is_not_recognized_as_ours() -> None: | ||
| assert get_channel_target(FakeChannel()) == "" | ||
| assert not is_nitls_channel(FakeChannel()) | ||
|
|
||
|
|
||
| def test___channel_carrying_a_foreign_attribute___is_not_recognized_as_ours() -> None: | ||
| channel = FakeChannel() | ||
| setattr(channel, "_nitls_channel_target", object()) | ||
|
|
||
| assert not is_nitls_channel(channel) | ||
|
|
||
|
|
||
| def test___channel_rejects_attributes___tagging_does_not_raise() -> None: | ||
| class Slotted: | ||
| __slots__ = () | ||
|
|
||
| tag_channel_target(Slotted(), "localhost:31763") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """Pytests for nitlsconfig.errors.""" | ||
|
|
||
| from nitlsconfig.channel_tag import tag_channel_target | ||
| from nitlsconfig.errors import get_tls_connection_error_elaboration | ||
|
|
||
|
|
||
| class FakeChannel: | ||
| """Stands in for a grpc.Channel, which the elaboration never calls into.""" | ||
|
|
||
|
|
||
| def test___channel_we_created___elaborates_on_connection_errors() -> None: | ||
| channel = FakeChannel() | ||
| tag_channel_target(channel, "localhost:31763") | ||
|
|
||
| elaboration = get_tls_connection_error_elaboration(channel) | ||
|
|
||
| assert elaboration is not None | ||
| assert "NI Hardware Manager" in elaboration | ||
|
|
||
|
|
||
| def test___channel_we_did_not_create___does_not_elaborate() -> None: | ||
| assert get_tls_connection_error_elaboration(object()) is None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.