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

implemented authentication #16

Merged
merged 3 commits into from
Sep 6, 2023
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
23 changes: 20 additions & 3 deletions src/confd_gnmi_api_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class GnmiConfDApiServerAdapter(GnmiServerAdapter):
monitor_external_changes: bool = ApiAdapterDefaults.MONITOR_EXTERNAL_CHANGES
external_port: int = ApiAdapterDefaults.EXTERNAL_PORT

class AuthenticationException(Exception):
def __init__(self, user, reason="N/A"):
self.user = user
self.reason = reason
def __str__(self):
return f"Authentication failed for user '{self.user}': {self.reason}."

def __init__(self):
self.addr: str = GnmiConfDApiServerAdapter.addr
self.port: int = GnmiConfDApiServerAdapter.port
Expand Down Expand Up @@ -406,10 +413,21 @@ def get_subscription_handler(self,
log.debug("<== handler=%s", handler)
return handler

def set_credentials(self, username="admin", password="admin"):
def authenticate(self, username="admin", password="admin"):
log.info("==> username=%s password=:-)", username)
self.username = username
self.password = password
auth_status = maapi.Maapi().authenticate(self.username, self.password, 1)
reason = "N/A"
if not isinstance(auth_status, int):
reason = auth_status[1]
auth_status = auth_status[0]
if auth_status == 1:
log.info(f"Authenticated {self.username}.")
else:
e = self.AuthenticationException(self.username, str(reason))
log.warning(e)
raise e
log.info("<== self.username=%s self.password=:-)", self.username)

# https://tools.ietf.org/html/rfc6022#page-8
Expand Down Expand Up @@ -538,7 +556,7 @@ def make_updates_with_maagic_rec(self, tr, node):
elif isinstance(node, maagic.Leaf):
yield from self.append_update(tr, node._path, node._cs_node)
else:
if hasattr(node, "_children"): # skip nodes w/o children, e.g. Action
if hasattr(node, "_children"): # skip nodes w/o children, e.g. Action
children = node._children.get_children(node._backend, node)
if len(children) == 0 and isinstance(node,
maagic.PresenceContainer):
Expand All @@ -554,7 +572,6 @@ def make_updates_with_maagic(self, tr, path_str):
else:
yield from self.make_updates_with_maagic_rec(tr, node)


def get_updates(self, trans, path_str, confd_path, save_flags, allow_aggregation=False):
log.debug("==> path_str=%s", path_str)
tagpath = '/' + '/'.join(tag for tag, _ in parse_instance_path(path_str))
Expand Down
2 changes: 1 addition & 1 deletion src/confd_gnmi_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_and_connect_adapter(self, username, password):
elif self.adapter_type == AdapterType.API:
from confd_gnmi_api_adapter import GnmiConfDApiServerAdapter
adapter = GnmiConfDApiServerAdapter.get_adapter()
adapter.set_credentials(username=username, password=password)
adapter.authenticate(username=username, password=password)
log.debug("<== adapter=%s", adapter)
return adapter

Expand Down
16 changes: 16 additions & 0 deletions tests/test_client_server_confd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import pytest

import grpc
import gnmi_pb2
from client_server_test_base import GrpcBase
from confd_gnmi_common import make_gnmi_path, make_xpath_path
from confd_gnmi_server import AdapterType
from confd_gnmi_client import ConfDgNMIClient
from route_status import RouteData, RouteProvider, ChangeOp
from utils.utils import log
sys.path.append(os.getenv('CONFD_DIR')+"/src/confd/pyapi/confd")
Expand Down Expand Up @@ -168,3 +170,17 @@ def test_subscribe_stream_on_change_api_state(self, request):
RouteProvider.stop_confd_loop()
confd_thread.join()
RouteProvider.close_dp()

def _assert_auth(self, err_string, username="admin", password="admin"):
client = ConfDgNMIClient(username=username, password=password, insecure=True)
with pytest.raises(grpc.RpcError) as err:
capabilities = client.get_capabilities()
client.close()
assert err_string in str(err)

@pytest.mark.confd
def test_authentication(self, request):
log.info("testing authentication")
self._assert_auth("Bad password", password="bad")
self._assert_auth("No such local user", username="bad", password="bad")
self._assert_auth("No such local user", username="bad")
Loading