Island: Fix credentials formatting to use simplified credentials object

This commit is contained in:
Ilija Lazoroski 2022-07-18 21:48:47 +02:00
parent c56b38f695
commit 57f2c7e058
2 changed files with 29 additions and 13 deletions

View File

@ -17,19 +17,22 @@ def format_creds_for_reporting(credentials: Sequence[Credentials]) -> Sequence[M
CredentialComponentType.SSH_KEYPAIR: "Clear SSH private key",
}
for cred in credentials:
for secret_type in cred.secrets:
if secret_type.credential_type not in cred_type_dict:
continue
username = _get_username(cred)
cred_row = {
"username": username,
"_type": secret_type.credential_type.name,
"type": cred_type_dict[secret_type.credential_type],
}
if cred_row not in formatted_creds:
formatted_creds.append(cred_row)
secret = cred.secret
if secret is None:
continue
if secret.credential_type not in cred_type_dict:
continue
username = _get_username(cred)
cred_row = {
"username": username,
"_type": secret.credential_type.name,
"type": cred_type_dict[secret.credential_type],
}
if cred_row not in formatted_creds:
formatted_creds.append(cred_row)
return formatted_creds
def _get_username(credentials: Credentials) -> str:
return credentials.identities[0].username if credentials.identities else ""
return credentials.identity.username if credentials.identity else ""

View File

@ -23,7 +23,14 @@ fake_ssh_key = SSHKeypair(fake_ssh_private_key, fake_ssh_public_key)
identities = (fake_username,)
secrets = (fake_nt_hash, fake_lm_hash, fake_password, fake_ssh_key)
fake_credentials = [Credentials(identities, secrets)]
fake_credentials = [
Credentials(fake_username, fake_nt_hash),
Credentials(fake_username, fake_lm_hash),
Credentials(fake_username, fake_password),
Credentials(fake_username, fake_ssh_key),
Credentials(None, fake_ssh_key),
Credentials(fake_username, None),
]
def test_formatting_credentials_for_report():
@ -50,7 +57,13 @@ def test_formatting_credentials_for_report():
"type": "Clear SSH private key",
"username": fake_username.username,
}
result5 = {
"_type": CredentialComponentType.SSH_KEYPAIR.name,
"type": "Clear SSH private key",
"username": "",
}
assert result1 in credentials
assert result2 in credentials
assert result3 in credentials
assert result4 in credentials
assert result5 in credentials