Coverage for harbor_cli/commands/api/ldap.py: 52%
29 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
1from __future__ import annotations
3from typing import Optional
5import typer
6from harborapi.models.models import LdapConf
8from ...output.render import render_result
9from ...state import state
10from ...utils.args import model_params_from_ctx
11from ...utils.commands import inject_help
13# Create a command group
14app = typer.Typer(
15 name="ldap",
16 help="LDAP configuration",
17 no_args_is_help=True,
18)
19search_cmd = typer.Typer(
20 name="search",
21 help="Search for users and groups in LDAP",
22 no_args_is_help=True,
23)
24app.add_typer(search_cmd)
27@app.command("ping")
28@inject_help(LdapConf)
29def ping(
30 ctx: typer.Context,
31 ldap_url: str = typer.Option(None, "--url"),
32 ldap_search_dn: str = typer.Option(None, "--search-dn"),
33 ldap_search_password: str = typer.Option(None, "--search-password"),
34 ldap_base_dn: str = typer.Option(None, "--base-dn"),
35 ldap_filter: str = typer.Option(None, "--filter"),
36 ldap_uid: str = typer.Option(None, "--uid"),
37 ldap_scope: int = typer.Option(None, "--scope"),
38 ldap_connection_timeout: int = typer.Option(None, "--timeout"),
39 ldap_verify_cert: bool = typer.Option(None, "--verify-cert"),
40) -> None:
41 """Ping LDAP service. Uses default configuration if none is provided."""
42 params = model_params_from_ctx(ctx, LdapConf)
43 if params:
44 conf = LdapConf(**params)
45 else:
46 conf = None
47 ldap_info = state.run(state.client.ping_ldap(conf))
48 render_result(ldap_info, ctx)
51# FIXME: this will never return more than one user...
52# Should we rename to `user` and return an LdapUser instead
53# of a list (len=1) of LdapUser?
54@search_cmd.command("users")
55def search_users(
56 ctx: typer.Context,
57 username: str = typer.Argument(..., help="Username to search for"),
58) -> None:
59 """Search for users in LDAP."""
60 result = state.run(state.client.search_ldap_users(username))
61 render_result(result, ctx)
64@search_cmd.command("groups")
65def search_groups(
66 ctx: typer.Context,
67 group_name: Optional[str] = typer.Option(None, help="Group name to search for"),
68 group_dn: Optional[str] = typer.Option(None, help="Group DN to search for"),
69) -> None:
70 """Search for groups in LDAP."""
71 if not group_name and not group_dn:
72 raise typer.BadParameter("One of --group-name or --group-dn is required")
73 result = state.run(state.client.search_ldap_groups(group_name, group_dn))
74 render_result(result, ctx)