From 7c0f79b351cd29f3b74430467fe91b1d16283467 Mon Sep 17 00:00:00 2001 From: Shakeel Date: Tue, 12 Nov 2019 13:37:48 -0800 Subject: [PATCH 1/2] add a get() function for storage passwords --- splunklib/client.py | 31 +++++++++++++++++++++++++++++++ tests/test_storage_passwords.py | 22 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/splunklib/client.py b/splunklib/client.py index de2f53a16..81ad317d1 100644 --- a/splunklib/client.py +++ b/splunklib/client.py @@ -1848,6 +1848,37 @@ def create(self, password, username, realm=None): return storage_password + def get(self, username, realm=None): + """ Retrieves a storage password. + + A `StoragePassword` can be identified by , or by : if the + optional realm parameter is also provided. + + :param username: The username for the credentials. + :type name: ``string`` + :param realm: The credential realm. (optional) + :type name: ``string`` + + :return: The :class:`StoragePassword` object. + """ + if realm is None: + # This case makes the username optional, so + # the full name can be passed in as realm. + # Assume it's already encoded. + name = username + else: + # Encode each component separately + name = UrlEncoded(realm, encode_slash=True) + ":" + UrlEncoded(username, encode_slash=True) + + # Append the : expected at the end of the name + if name[-1] is not ":": + name = name + ":" + + response = Collection.get(self, name) + entries = _load_atom_entries(response) + state = _parse_atom_entry(entries[0]) + return StoragePassword(self.service, self._entity_path(state), state=state, skip_refresh=True) + def delete(self, username, realm=None): """Delete a storage password by username and/or realm. diff --git a/tests/test_storage_passwords.py b/tests/test_storage_passwords.py index c6d83a90a..6f4772152 100644 --- a/tests/test_storage_passwords.py +++ b/tests/test_storage_passwords.py @@ -47,6 +47,28 @@ def test_create(self): p.delete() self.assertEqual(start_count, len(self.storage_passwords)) + def test_get(self): + start_count = len(self.storage_passwords) + realm = testlib.tmpname() + username = testlib.tmpname() + + p = self.storage_passwords.create("changeme", username, realm) + self.assertEqual(start_count + 1, len(self.storage_passwords)) + self.assertEqual(p.realm, realm) + self.assertEqual(p.username, username) + self.assertEqual(p.clear_password, "changeme") + self.assertEqual(p.name, realm + ":" + username + ":") + + p2 = self.storage_passwords.get(username, realm) + self.assertEqual(start_count + 1, len(self.storage_passwords)) + self.assertEqual(p2.realm, realm) + self.assertEqual(p2.username, username) + self.assertEqual(p2.clear_password, "changeme") + self.assertEqual(p2.name, realm + ":" + username + ":") + + p.delete() + self.assertEqual(start_count, len(self.storage_passwords)) + def test_create_with_backslashes(self): start_count = len(self.storage_passwords) realm = "\\" + testlib.tmpname() From e6ab69e6bb81a9997899da8db20b8110ce49e0ff Mon Sep 17 00:00:00 2001 From: Shakeel Date: Tue, 12 Nov 2019 14:01:19 -0800 Subject: [PATCH 2/2] pass name as named arg --- splunklib/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunklib/client.py b/splunklib/client.py index 81ad317d1..0831812c4 100644 --- a/splunklib/client.py +++ b/splunklib/client.py @@ -1874,7 +1874,7 @@ def get(self, username, realm=None): if name[-1] is not ":": name = name + ":" - response = Collection.get(self, name) + response = Collection.get(self, name=name) entries = _load_atom_entries(response) state = _parse_atom_entry(entries[0]) return StoragePassword(self.service, self._entity_path(state), state=state, skip_refresh=True)