Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion anchorecli/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from anchorecli import version
import anchorecli.clients # noqa
from .utils import ContextObject


@click.group(context_settings=dict(help_option_names=["-h", "--help", "help"]))
Expand Down Expand Up @@ -73,7 +74,7 @@ def main_entry(
if config["debug"]:
logging.basicConfig(level=logging.DEBUG)

ctx.obj = config
ctx.obj = ContextObject(config, None)


class Help(click.Command):
Expand Down
184 changes: 121 additions & 63 deletions anchorecli/cli/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,58 @@


@click.group(name="account", short_help="Account operations")
@click.pass_obj
def account(ctx_config):
global config, whoami
config = ctx_config
@click.pass_context
def account(ctx):
def execute():
global config, whoami
config = ctx.parent.obj.config

try:
anchorecli.cli.utils.check_access(config)
except Exception as err:
print(anchorecli.cli.utils.format_error_output(config, "account", {}, err))
sys.exit(2)
try:
anchorecli.cli.utils.check_access(config)
except Exception as err:
print(anchorecli.cli.utils.format_error_output(config, "account", {}, err))
sys.exit(2)

try:
ret = anchorecli.clients.apiexternal.get_account(config)
if ret["success"]:
whoami["account"] = ret["payload"]
else:
raise Exception(json.dumps(ret["error"], indent=4))
except Exception as err:
print(anchorecli.cli.utils.format_error_output(config, "account", {}, err))
sys.exit(2)
try:
ret = anchorecli.clients.apiexternal.get_account(config)
if ret["success"]:
whoami["account"] = ret["payload"]
else:
raise Exception(json.dumps(ret["error"], indent=4))
except Exception as err:
print(anchorecli.cli.utils.format_error_output(config, "account", {}, err))
sys.exit(2)

try:
ret = anchorecli.clients.apiexternal.get_user(config)
if ret["success"]:
whoami["user"] = ret["payload"]
else:
raise Exception(json.dumps(ret["error"], indent=4))
except Exception as err:
print(anchorecli.cli.utils.format_error_output(config, "account", {}, err))
sys.exit(2)
try:
ret = anchorecli.clients.apiexternal.get_user(config)
if ret["success"]:
whoami["user"] = ret["payload"]
else:
raise Exception(json.dumps(ret["error"], indent=4))
except Exception as err:
print(anchorecli.cli.utils.format_error_output(config, "account", {}, err))
sys.exit(2)

ctx.obj = anchorecli.cli.utils.ContextObject(ctx.parent.obj.config, execute)


@account.command(name="whoami", short_help="Get current account/user information")
def get_current_user():
@click.pass_context
def get_current_user(ctx):
global whoami
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)
except RuntimeError as err:
print(
anchorecli.cli.utils.format_error_output(
config, "get_current_user", {}, err
)
)
ecode = 2
anchorecli.cli.utils.doexit(ecode)

print(anchorecli.cli.utils.format_output(config, "account_whoami", {}, whoami))
anchorecli.cli.utils.doexit(ecode)

Expand All @@ -55,7 +71,8 @@ def get_current_user():
)
@click.argument("account_name", nargs=1, required=True)
@click.option("--email", help="Optional email address to associate with account")
def add(account_name, email):
@click.pass_context
def add(ctx, account_name, email):
"""
ACCOUNT_NAME: name of new account to create

Expand All @@ -65,6 +82,8 @@ def add(account_name, email):
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

ret = anchorecli.clients.apiexternal.add_account(
config, account_name=account_name, email=email
)
Expand All @@ -88,14 +107,17 @@ def add(account_name, email):

@account.command(name="get", short_help="Get account information")
@click.argument("account_name", nargs=1, required=True)
def get(account_name):
@click.pass_context
def get(ctx, account_name):
"""
ACCOUNT_NAME: name of new account to create

"""
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

ret = anchorecli.clients.apiexternal.get_account(
config, account_name=account_name
)
Expand All @@ -120,10 +142,14 @@ def get(account_name):
@account.command(
name="list", short_help="List information about all accounts (admin only)"
)
def list_accounts():
""" """
@click.pass_context
def list_accounts(ctx):
""""""
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

ret = anchorecli.clients.apiexternal.list_accounts(config)
ecode = anchorecli.cli.utils.get_ecode(ret)
if ret["success"]:
Expand All @@ -148,14 +174,24 @@ def list_accounts():
@click.option(
"--dontask", is_flag=True, help="Do not prompt for confirmation of account deletion"
)
def delete(account_name, dontask):
@click.pass_context
def delete(ctx, account_name, dontask):
global input
"""
ACCOUNT_NAME: name of account to delete (must be disabled first)

"""
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)
except RuntimeError as err:
print(
anchorecli.cli.utils.format_error_output(config, "account_delete", {}, err)
)
ecode = 2
anchorecli.cli.utils.doexit(ecode)

answer = "n"
if dontask:
answer = "y"
Expand Down Expand Up @@ -202,14 +238,17 @@ def delete(account_name, dontask):

@account.command(name="enable", short_help="Enable a disabled account")
@click.argument("account_name", nargs=1, required=True)
def enable(account_name):
@click.pass_context
def enable(ctx, account_name):
"""
ACCOUNT_NAME: name of account to enable

"""
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

ret = anchorecli.clients.apiexternal.enable_account(
config, account_name=account_name
)
Expand All @@ -235,14 +274,17 @@ def enable(account_name):

@account.command(name="disable", short_help="Disable an enabled account")
@click.argument("account_name", nargs=1, required=True)
def disable(account_name):
@click.pass_context
def disable(ctx, account_name):
"""
ACCOUNT_NAME: name of account to disable

"""
ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

ret = anchorecli.clients.apiexternal.disable_account(
config, account_name=account_name
)
Expand Down Expand Up @@ -270,27 +312,35 @@ def disable(account_name):


@account.group(name="user", short_help="Account user operations")
def user():
@click.pass_context
def user(ctx):
global config, whoami

# since there's nothing to execute here, just pass the parent config and callback down
ctx.obj = anchorecli.cli.utils.ContextObject(
ctx.parent.obj.config, ctx.parent.obj.execute_callback
)


@user.command(name="add", short_help="Add a new user")
@click.argument("user_name", nargs=1, required=True)
@click.argument("user_password", nargs=1, required=True)
@click.option("--account", help="Optional account name")
def user_add(user_name, user_password, account):
@click.pass_context
def user_add(ctx, user_name, user_password, account):
global whoami
"""
ACCOUNT: optional name of the account to act as

"""

if not account:
account = whoami.get("account", {}).get("name", None)

ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

if not account:
account = whoami.get("account", {}).get("name", None)

# do some input validation
if not re.match(".{6,128}$", user_password):
raise Exception(
Expand Down Expand Up @@ -324,19 +374,21 @@ def user_add(user_name, user_password, account):
@user.command(name="del", short_help="Delete a user")
@click.argument("user_name", nargs=1, required=True)
@click.option("--account", help="Optional account name")
def user_delete(user_name, account):
@click.pass_context
def user_delete(ctx, user_name, account):
global whoami
"""
ACCOUNT: optional name of the account to act as

"""

if not account:
account = whoami.get("account", {}).get("name", None)

ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

if not account:
account = whoami.get("account", {}).get("name", None)

ret = anchorecli.clients.apiexternal.del_user(
config, account_name=account, user_name=user_name
)
Expand All @@ -361,19 +413,21 @@ def user_delete(user_name, account):
@user.command(name="get", short_help="Get information about a user")
@click.argument("user_name", nargs=1, required=True)
@click.option("--account", help="Optional account name")
def user_get(user_name, account):
@click.pass_context
def user_get(ctx, user_name, account):
global whoami
"""
ACCOUNT: optional name of the account to act as

"""

if not account:
account = whoami.get("account", {}).get("name", None)

ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

if not account:
account = whoami.get("account", {}).get("name", None)

ret = anchorecli.clients.apiexternal.get_user(
config, account_name=account, user_name=user_name
)
Expand All @@ -397,19 +451,21 @@ def user_get(user_name, account):

@user.command(name="list", short_help="Get a list of account users")
@click.option("--account", help="Optional account name")
def user_list(account):
@click.pass_context
def user_list(ctx, account):
global whoami
"""
ACCOUNT: optional name of the account to act as

"""

if not account:
account = whoami.get("account", {}).get("name", None)

ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

if not account:
account = whoami.get("account", {}).get("name", None)

ret = anchorecli.clients.apiexternal.list_users(config, account_name=account)
ecode = anchorecli.cli.utils.get_ecode(ret)
if ret["success"]:
Expand All @@ -433,21 +489,23 @@ def user_list(account):
@click.argument("user_password", nargs=1, required=True)
@click.option("--username", help="Optional user name")
@click.option("--account", help="Optional account name")
def user_setpassword(user_password, username, account):
@click.pass_context
def user_setpassword(ctx, user_password, username, account):
global whoami
"""
ACCOUNT: optional name of the account to act as

"""

if not account:
account = whoami.get("account", {}).get("name", None)
if not username:
username = whoami.get("user", {}).get("username", None)

ecode = 0

try:
anchorecli.cli.utils.handle_parent_callback(ctx)

if not account:
account = whoami.get("account", {}).get("name", None)
if not username:
username = whoami.get("user", {}).get("username", None)

ret = anchorecli.clients.apiexternal.update_user_password(
config,
account_name=account,
Expand Down
Loading