diff --git a/splunklib/client.py b/splunklib/client.py index de2f53a16..0831812c4 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=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()