Skip to content

harborapi.auth

harborapi.auth defines utility functions for use with harborapi.

HarborAuthFile

Bases: Robot

Represents a Harbor robot account auth file.

Supports arbitrary extra fields to allow for future compatibility.

Source code in harborapi/auth.py
class HarborAuthFile(Robot):
    """Represents a Harbor robot account auth file.

    Supports arbitrary extra fields to allow for future compatibility.
    """

    name: str = Field(None, description="The name of the robot account")
    secret: str = Field(None, description="The secret for the robot account")
    model_config = ConfigDict(populate_by_name=True, extra="allow")

load_harbor_auth_file(path)

Load a HarborAuthFile from a file path. Ensure that the file contains a name and secret.

Parameters:

Name Type Description Default
path Union[str, Path]

The path to the file to load.

required

Returns:

Type Description
HarborAuthFile

The HarborAuthFile loaded from the file.

Raises:

Type Description
ValueError

The auth file does not contain a username and/or secret.

Source code in harborapi/auth.py
def load_harbor_auth_file(path: Union[str, Path]) -> "HarborAuthFile":
    """Load a HarborAuthFile from a file path. Ensure that the file contains
    a name and secret.

    Parameters
    ----------
    path : Union[str, Path]
        The path to the file to load.

    Returns
    -------
    HarborAuthFile
        The HarborAuthFile loaded from the file.

    Raises
    ------
    ValueError
        The auth file does not contain a username and/or secret.
    """
    authfile = _load_harbor_auth_file(path)
    if not authfile.name:
        raise ValueError("Field 'name' is required")
    if not authfile.secret:
        raise ValueError("Field 'secret' is required")
    return authfile

save_authfile(path, authfile, overwrite)

Save the authfile to the given path.

Parameters:

Name Type Description Default
path Union[str, Path]

Path to save the file to.

required
authfile HarborAuthFile

Auth file definition to save.

required
overwrite bool

Overwrite file if it exists.

required

Raises:

Type Description
FileExistsError

A file with the given path already exists, and overwrite is False.

Source code in harborapi/auth.py
def save_authfile(
    path: Union[str, Path], authfile: "HarborAuthFile", overwrite: bool
) -> None:
    """Save the authfile to the given path.

    Parameters
    ----------
    path : Union[str, Path]
        Path to save the file to.
    authfile : HarborAuthFile
        Auth file definition to save.
    overwrite : bool
        Overwrite file if it exists.

    Raises
    ------
    FileExistsError
        A file with the given path already exists, and `overwrite` is `False`.
    """
    p = Path(path)
    if p.exists() and not overwrite:
        raise FileExistsError(f"File {p} already exists")
    with open(path, "w") as f:
        f.write(authfile.model_dump_json(indent=4))

new_authfile_from_robotcreate(path, robotcreate, robotcreated, overwrite=False)

Create a new authfile from the result of a create robot call.

Parameters:

Name Type Description Default
path Union[str, Path]

Path to save the file to.

required
robotcreate RobotCreate

The arguments used to create the robot.

required
robotcreated RobotCreated

The result of the create robot call.

required
overwrite bool

Overwrite file if it exists.

False
See Also

harborapi.auth.save_authfile

Source code in harborapi/auth.py
def new_authfile_from_robotcreate(
    path: Union[str, Path],
    robotcreate: RobotCreate,
    robotcreated: RobotCreated,
    overwrite: bool = False,
) -> None:
    """Create a new authfile from the result of a create robot call.

    Parameters
    ----------
    path : Union[str, Path]
        Path to save the file to.
    robotcreate : RobotCreate
        The arguments used to create the robot.
    robotcreated : RobotCreated
        The result of the create robot call.
    overwrite : bool, optional
        Overwrite file if it exists.

    See Also
    --------
    [harborapi.auth.save_authfile][]
    """
    # Specify robotcreated last, since that is the object that should
    # contain the secret (which we definitely don't want to overwrite)
    authfile = HarborAuthFile.model_validate(
        {**(robotcreate.model_dump()), **(robotcreated.model_dump())}
    )
    save_authfile(path, authfile, overwrite=overwrite)

new_authfile_from_robot(path, robot, secret, overwrite=False)

Create a new authfile from a Robot definition and a secret.

Parameters:

Name Type Description Default
path Union[str, Path]

Path to save the file to.

required
robot Robot

Robot definition.

required
secret str

Secret to use for the robot.

required
overwrite bool

Overwrite file if it exists.

False
See Also

harborapi.auth.save_authfile

Source code in harborapi/auth.py
def new_authfile_from_robot(
    path: Union[str, Path],
    robot: Robot,
    secret: str,
    overwrite: bool = False,
) -> None:
    """Create a new authfile from a Robot definition and a secret.

    Parameters
    ----------
    path : Union[str, Path]
        Path to save the file to.
    robot : Robot
        Robot definition.
    secret : str
        Secret to use for the robot.
    overwrite : bool
        Overwrite file if it exists.

    See Also
    --------
    [harborapi.auth.save_authfile][]
    """
    authfile = HarborAuthFile.model_validate(robot.model_dump())
    authfile.secret = secret
    save_authfile(path, authfile, overwrite=overwrite)