Skip to content

Commit

Permalink
Merge pull request #359 from aiven/jclarysse/handle-wrong-service-use…
Browse files Browse the repository at this point in the history
…r-in-user-kafka-java-creds

client: handle wrong service user in user-kafka-java-creds [HH-2459]
  • Loading branch information
tvainika authored Sep 6, 2023
2 parents 5c35a83 + 0af192c commit f35c3cf
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions aiven/client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,40 +2062,43 @@ def service__user_creds_download(self) -> None:

missing_user_items = []
service = self.client.get_service(project=self.get_project(), service=self.args.service_name)
for user in service["users"]:
if user["username"] == self.args.username:
cert = user.get("access_cert")
if cert is None:
missing_user_items.append("certificate")
else:
with open(os.path.join(self.args.target_directory, "service.cert"), "w", encoding="utf-8") as fp:
fp.write(cert)
downloaded_items.append("certificate")

key = user.get("access_key")
if key is None:
missing_user_items.append("key")
else:
with open(os.path.join(self.args.target_directory, "service.key"), "w", encoding="utf-8") as fp:
fp.write(key)
downloaded_items.append("key")
matched_service_users = [s_user for s_user in service["users"] if s_user["username"] == self.args.username]

if not matched_service_users:
error_messages.append(
"The value passed as argument --username does not match any service user,\n"
+ "therefore the service certificate key pair cannot be obtained.\n\n"
+ "To get the service users and their passwords, type:\n"
+ " avn service user-list --format '{{username}} {{password}}' --project {} {}".format(
project_name, self.args.service_name
)
)

break
else:
user = matched_service_users[0]
cert = user.get("access_cert")
if cert is None:
missing_user_items.append("certificate")
else:
with open(os.path.join(self.args.target_directory, "service.cert"), "w", encoding="utf-8") as fp:
fp.write(cert)
downloaded_items.append("certificate")

if downloaded_items:
print("Downloaded to directory '{}': {}".format(self.args.target_directory, ", ".join(downloaded_items)))
print()
key = user.get("access_key")
if key is None:
missing_user_items.append("key")
else:
with open(os.path.join(self.args.target_directory, "service.key"), "w", encoding="utf-8") as fp:
fp.write(key)
downloaded_items.append("key")

print("To get the user passwords type:")
print(
"avn service user-list --format '{{username}} {{password}}' --project {} {}".format(
project_name, self.args.service_name
)
)
if downloaded_items:
print("Downloaded to directory '{}': {}".format(self.args.target_directory, ", ".join(downloaded_items)))
print()

if missing_user_items:
missing_items_str = " and ".join(missing_user_items)
error_messages.append("The user '{}' does not have {}".format(self.args.username, missing_items_str))
if missing_user_items:
missing_items_str = " and ".join(missing_user_items)
error_messages.append("The user '{}' does not have {}".format(self.args.username, missing_items_str))

if error_messages:
print()
Expand Down

0 comments on commit f35c3cf

Please sign in to comment.