Island: small improvements code style in credential parsing code

This commit is contained in:
vakarisz 2022-02-23 17:38:15 +02:00
parent 9d23c3dd62
commit e17d95bf18
3 changed files with 8 additions and 8 deletions

View File

@ -1,17 +1,17 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Sequence
from typing import Sequence, Mapping, Any
@dataclass(frozen=True)
class Credentials:
identities: Sequence[dict]
secrets: Sequence[dict]
identities: Sequence[Mapping]
secrets: Sequence[Mapping]
monkey_guid: str
@staticmethod
def from_dict(cred_dict: dict, monkey_guid: str) -> Credentials:
def from_mapping(cred_dict: Mapping[str, Any], monkey_guid: str) -> Credentials:
return Credentials(
identities=cred_dict["identities"],
secrets=cred_dict["secrets"],

View File

@ -24,7 +24,7 @@ CREDENTIAL_COMPONENT_PROCESSORS = {
def parse_credentials(telemetry_dict: Mapping):
credentials = [
Credentials.from_dict(credential, telemetry_dict["monkey_guid"])
Credentials.from_mapping(credential, telemetry_dict["monkey_guid"])
for credential in telemetry_dict["data"]
]

View File

@ -15,13 +15,13 @@ class SSHKeyProcessingError(ValueError):
def process_ssh_key(keypair: Mapping, credentials: Credentials):
if len(credentials.identities) != 1:
raise SSHKeyProcessingError(
f"SSH credentials have {len(credentials.identities)}" f" users associated with " f"it!"
f"SSH credentials have {len(credentials.identities)} users associated with it!"
)
if not _contains_both_keys(keypair):
raise SSHKeyProcessingError("Private or public key missing!")
raise SSHKeyProcessingError("Private or public key missing")
# TODO SSH key should be associated with IP that monkey exploited
# TODO investigate if IP is needed at all
ip = Monkey.get_single_monkey_by_guid(credentials.monkey_guid).ip_addresses[0]
username = credentials.identities[0]["username"]