Skip to content

Commit fe32b83

Browse files
committed
connection: add sr_get_module_ds_access API
Add a new function to get owner, group and permissions of a module based on its name. Signed-off-by: Bagg Tobias (ETAS-DAP/XPC-Fe3) <[email protected]>
1 parent 59f456b commit fe32b83

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

README.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ Supported Features
158158
__ https://pypi.org/project/libyang/
159159
.. _async: https://docs.python.org/3/library/asyncio-task.html#coroutine
160160

161-
Not Yet Supported Features
162-
--------------------------
161+
Partially Supported Features
162+
----------------------------
163163

164-
All other features are not yet supported by sysrepo-python. The most notable
164+
All other features are not yet or only partially supported by sysrepo-python. The most notable
165165
are:
166166

167167
- Module management (``sr_*_module_*``)

cffi/cdefs.h

+3
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,6 @@ int sr_notif_subscribe_tree(sr_session_ctx_t *, const char *module_name, const c
292292
void *priv, sr_subscr_options_t, sr_subscription_ctx_t **);
293293

294294
int sr_notif_send_tree(sr_session_ctx_t *, struct lyd_node *notif, uint32_t timeout_ms, int wait);
295+
296+
typedef int... mode_t;
297+
int sr_get_module_ds_access(sr_conn_ctx_t *conn, const char *module_name, int mod_ds, char **owner, char **group, mode_t *perm);

sysrepo/connection.py

+30-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from contextlib import contextmanager
55
import logging
66
import signal
7-
from typing import Dict, Optional, Sequence
7+
from typing import Dict, Optional, Sequence, Tuple
88

99
import libyang
1010

1111
from _sysrepo import ffi, lib
1212
from .errors import SysrepoInternalError, check_call
1313
from .session import SysrepoSession, datastore_value
14-
from .util import str2c
14+
from .util import c2str, str2c
1515

1616

1717
LOG = logging.getLogger(__name__)
@@ -279,3 +279,31 @@ def enable_module_feature(self, name: str, feature_name: str) -> None:
279279
check_call(
280280
lib.sr_enable_module_feature, self.cdata, str2c(name), str2c(feature_name)
281281
)
282+
283+
def get_module_ds_access(
284+
self, module_name: str, datastore: str = "running"
285+
) -> Tuple[str, str, int]:
286+
"""
287+
Learn about module permissions.
288+
289+
:arg str module_name:
290+
Name of the module.
291+
:arg str datastore:
292+
Name of the datastore that will be operated on.
293+
:returns:
294+
The owner, group and permissions of the given module name.
295+
Owner and group are names, not numeric ids.
296+
"""
297+
owner = ffi.new("char **owner")
298+
group = ffi.new("char **group")
299+
perm = ffi.new("mode_t *perm")
300+
check_call(
301+
lib.sr_get_module_ds_access,
302+
self.cdata,
303+
str2c(module_name),
304+
datastore_value(datastore),
305+
owner,
306+
group,
307+
perm,
308+
)
309+
return (c2str(owner[0]), c2str(group[0]), perm[0])

tests/test_connection.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Copyright (c) 2020 6WIND S.A.
22
# SPDX-License-Identifier: BSD-3-Clause
33

4+
import grp
45
import logging
56
import os
7+
import pwd
68
import unittest
79

810
import libyang
@@ -77,3 +79,11 @@ def test_conn_enable_module_feature(self):
7779
data = [x for x in data if x["name"] == "sysrepo-example"][0]
7880
self.assertIn("feature", data)
7981
conn.remove_module("sysrepo-example")
82+
83+
def test_conn_get_module_infos(self):
84+
with sysrepo.SysrepoConnection() as conn:
85+
conn.install_module(YANG_FILE)
86+
owner, group, perm = conn.get_module_ds_access("sysrepo-example")
87+
self.assertEqual(pwd.getpwnam(owner).pw_uid, os.geteuid())
88+
self.assertEqual(grp.getgrnam(group).gr_gid, os.getegid())
89+
self.assertEqual(perm, 0o600)

0 commit comments

Comments
 (0)