Skip to content

Commit 7a4dafb

Browse files
committed
Add wrapper _delete_secret() that accepts a Secret object.
This keeps the code cleaner. Signed-off-by: Kurt Garloff <[email protected]>
1 parent e565cc2 commit 7a4dafb

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

Tests/iaas/key-manager/check-for-key-manager.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def check_presence_of_key_manager(conn: openstack.connection.Connection) -> None
5757
return True
5858

5959

60-
def _find_secrets(conn: openstack.connection.Connection, secret_name_or_id: str):
60+
def _find_secrets(conn: openstack.connection.Connection, secret_name_or_id: str) -> list:
6161
"""Replacement method for finding secrets.
6262
6363
Mimicks the behavior of Connection.key_manager.find_secret()
@@ -69,6 +69,32 @@ def _find_secrets(conn: openstack.connection.Connection, secret_name_or_id: str)
6969
return [s for s in secrets if s.name == secret_name_or_id or s.id == secret_name_or_id]
7070

7171

72+
def _delete_secret(conn: openstack.connection.Connection, secret: openstack.key_manager.v1.secret.Secret):
73+
"""Replacement method for deleting secrets
74+
_delete_secret(connection, secret object)
75+
76+
Workaround for SDK bugs:
77+
- The id field in reality is a href (containg the UUID at the end)
78+
- The delete_secret() function contrary to the documentation does
79+
not accept openstack.key_manager.v1.secret.Secret objects nor the
80+
hrefs, just plain UUIDs.
81+
- It does not return an error when I try to delete a secret passing
82+
an object or href, just silently does nothing.
83+
The code here assumes that the SDK (when fixed) will continue to
84+
accept UUIDs as argument for delete_secret() in the future.
85+
Code is robust against those being passed directly in the .id attr
86+
of the objects. (It would be even more robust to try to pass the
87+
object first, then the href, then the UUID extracted from the href,
88+
each time checking whether it was effective. But that's three delete
89+
plus list calls and very ugly.)
90+
"""
91+
uuid_part = secret.id.rfind('/') + 1
92+
if uuid_part != 0:
93+
conn.key_manager.delete_secret(secret.id[uuid_part:])
94+
else:
95+
conn.key_manager.delete_secret(secret.id)
96+
97+
7298
def check_key_manager_permissions(conn: openstack.connection.Connection) -> None:
7399
"""
74100
After checking that the current user only has the member and maybe the
@@ -79,25 +105,10 @@ def check_key_manager_permissions(conn: openstack.connection.Connection) -> None
79105
try:
80106
existing_secrets = _find_secrets(conn, secret_name)
81107
for secret in existing_secrets:
82-
# Workaround for SDK bugs:
83-
# - The id field in reality is a href (containg the UUID at the end)
84-
# - The delete_secret() function contrary to the documentation does
85-
# not accept openstack.key_managerv1.secret.Secret objects nor the
86-
# hrefs, just plain UUIDs.
87-
# - It does not return an error when I try to delete a secret passing
88-
# an object or href, just silently does nothing.
89-
# The code here assumes that the SDK (when fixed) will continue to
90-
# accept UUIDs as argument for delete_secret() in the future.
91-
# Code is robust against those being passed directly in the .id attr
92-
# of the objects. (It would be even more robust to try to pass the
93-
# object first, then the href, then the UUID extracted from the href,
94-
# each time checking whether it was effective. But that's three delete
95-
# plus list calls and very ugly.)
96-
uuid_part = secret.id.rfind('/') + 1
97-
if uuid_part != 0:
98-
conn.key_manager.delete_secret(secret.id[uuid_part:])
99-
else:
100-
conn.key_manager.delete_secret(secret.id)
108+
_delete_secret(conn, secret)
109+
110+
if existing_secrets:
111+
logger.debug(f'Deleted {len(existing_secrets)} secrets')
101112

102113
conn.key_manager.create_secret(
103114
name=secret_name,
@@ -106,11 +117,11 @@ def check_key_manager_permissions(conn: openstack.connection.Connection) -> None
106117
payload="foo",
107118
)
108119
try:
109-
new_secret = _find_secret(conn, secret_name)
120+
new_secret = _find_secrets(conn, secret_name)
110121
if not new_secret:
111122
raise ValueError(f"Secret '{secret_name}' was not discoverable by the user")
112123
finally:
113-
conn.key_manager.delete_secret(new_secret)
124+
_delete_secret(conn, new_secret[0])
114125
except openstack.exceptions.ForbiddenException:
115126
logger.debug('exception details', exc_info=True)
116127
logger.error(

0 commit comments

Comments
 (0)