2020-07-30 01:30:01 -07:00
|
|
|
"""Validate credential configuration variables."""
|
|
|
|
|
2020-10-11 14:39:17 -07:00
|
|
|
# Standard Library
|
|
|
|
from typing import Optional
|
|
|
|
|
2020-07-30 01:30:01 -07:00
|
|
|
# Third Party
|
2020-10-11 14:39:17 -07:00
|
|
|
from pydantic import FilePath, SecretStr, StrictStr, constr, root_validator
|
2020-07-30 01:30:01 -07:00
|
|
|
|
2020-10-11 13:14:07 -07:00
|
|
|
# Local
|
2020-10-11 14:39:17 -07:00
|
|
|
from ..main import HyperglassModelExtra
|
|
|
|
|
|
|
|
Methods = constr(regex=r"(password|unencrypted_key|encrypted_key)")
|
2020-07-30 01:30:01 -07:00
|
|
|
|
|
|
|
|
2020-10-11 14:39:17 -07:00
|
|
|
class Credential(HyperglassModelExtra):
|
2020-07-30 01:30:01 -07:00
|
|
|
"""Model for per-credential config in devices.yaml."""
|
|
|
|
|
|
|
|
username: StrictStr
|
2020-10-11 14:39:17 -07:00
|
|
|
password: Optional[SecretStr]
|
|
|
|
key: Optional[FilePath]
|
|
|
|
|
|
|
|
@root_validator
|
|
|
|
def validate_credential(cls, values):
|
|
|
|
"""Ensure either a password or an SSH key is set."""
|
|
|
|
if values["key"] is None and values["password"] is None:
|
|
|
|
raise ValueError(
|
|
|
|
"Either a password or an SSH key must be specified for user '{}'".format(
|
|
|
|
values["username"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return values
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
"""Set private attribute _method based on validated model."""
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self._method = None
|
|
|
|
if self.password is not None and self.key is not None:
|
|
|
|
self._method = "encrypted_key"
|
|
|
|
elif self.password is None:
|
|
|
|
self._method = "unencrypted_key"
|
|
|
|
elif self.key is None:
|
|
|
|
self._method = "password"
|