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

WIP: add storage passwords get() #292

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions splunklib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <username>, or by <realm>:<username> 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.

Expand Down
22 changes: 22 additions & 0 deletions tests/test_storage_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down