Skip to content
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

Fix TLS support after tracing feature #1370

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions pynipap/pynipap.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
import sys
import logging
import xmlrpc.client as xmlrpclib
import urllib.parse

from tracing import create_span

__version__ = "0.32.3"
__author__ = "Kristian Larsson, Lukas Garberg"
Expand Down Expand Up @@ -253,13 +256,6 @@ def __init__(self, options = None):
self.options = options


try:
from tracing import create_span, TracingXMLTransport
xml_transport = TracingXMLTransport
except ImportError:
xml_transport = xmlrpclib.Transport


class XMLRPCConnection:
""" Handles a shared XML-RPC connection.
"""
Expand All @@ -281,6 +277,20 @@ def __init__(self):
if xmlrpc_uri is None:
raise NipapError('XML-RPC URI not specified')

p = urllib.parse.urlsplit(xmlrpc_uri)

try:
from tracing import TracingXMLTransport, TracingXMLSafeTransport
if p.scheme == "http":
xml_transport = TracingXMLTransport
elif p.scheme == "https":
xml_transport = TracingXMLSafeTransport
except ImportError:
if p.scheme == "http":
xml_transport = xmlrpclib.Transport
elif p.scheme == "https":
xml_transport = xmlrpclib.SafeTransport

# creating new instance
self.connection = xmlrpclib.ServerProxy(xmlrpc_uri,
transport=xml_transport(),
Expand Down
35 changes: 34 additions & 1 deletion pynipap/tracing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inspect
import sys
from functools import wraps
from pynipap import AuthOptions

if sys.version_info[0] < 3:
import xmlrpclib
Expand Down Expand Up @@ -45,6 +44,7 @@ def decorated(*args, **kwargs):
"""
"""

from pynipap import AuthOptions
signature = inspect.signature(f)

if args[0].__class__ == type:
Expand Down Expand Up @@ -103,6 +103,39 @@ def send_content(self, connection, request_body):
current_span.get_span_context().span_id)[2:].zfill(16) + "-01")

super().send_content(connection, request_body)


class TracingXMLSafeTransport(xmlrpclib.SafeTransport):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too many blank lines (2)


def __init__(self, use_datetime=False, use_builtin_types=False,
*, headers=(), context=None):
super().__init__(use_datetime=use_datetime,
use_builtin_types=use_builtin_types,
headers=headers, context=context)

def request(self, host, handler, request_body, verbose=False):
with tracer.start_as_current_span("POST XML request", context.get_current(), SpanKind.CLIENT):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (106 > 79 characters)

current_span = trace.get_current_span()
try:
result = super().request(host, handler, request_body, verbose)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (82 > 79 characters)

current_span.set_attribute("http.status_code", 200)
except xmlrpclib.ProtocolError as protocolError:
current_span.set_attribute("http.status_code", protocolError.errcode)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (89 > 79 characters)

current_span.record_exception(protocolError)
raise protocolError
return result

def send_content(self, connection, request_body):
current_span = trace.get_current_span()
if current_span != INVALID_SPAN:
current_span.set_attribute("net.peer.ip", connection.host)
current_span.set_attribute("net.peer.port", connection.port)
current_span.set_attribute("net.peer.transport", "ip_tcp")
connection.putheader("traceparent", "00-" + hex(current_span.get_span_context().trace_id)[2:].zfill(32) + "-" + hex(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (132 > 79 characters)

current_span.get_span_context().span_id)[2:].zfill(16) + "-01")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (83 > 79 characters)


super().send_content(connection, request_body)

except ImportError:
def create_span(f):
@wraps(f)
Expand Down
Loading