Esempio n. 1
0
def auth(email: str, password: str, mfa: Optional[str], tokenfile):
    status, rsp = epmanage.lib.auth.auth(email, password)
    if not status:
        exit_fail(rsp)
    token = epmanage.lib.auth.check_token(rsp)
    if not token:
        exit_fail('Invalid token')

    if epmanage.lib.auth.require_mfa(token):
        mfa_status = False
        exp = arrow.get(token.get('exp'))
        if mfa:
            mfa_status, rsp = epmanage.lib.auth.login_mfa(mfa)
            if not mfa_status:
                exit_fail(rsp)
        while not mfa_status and exp > arrow.utcnow():
            mfa = click.prompt("Enter MFA token")
            mfa_status, rsp = epmanage.lib.auth.login_mfa(mfa)
            if not mfa_status:
                echo_fail(rsp)

        if not mfa_status:
            exit_fail('Invalid MFA token')
        token = epmanage.lib.auth.check_token(rsp)
        if not token:
            exit_fail('Invalid token')

    click.secho('Auth success', fg='green')
    exp = arrow.get(token.get('exp'))
    click.echo('  token expires {} ({})'.format(exp.humanize(), exp))
    click.echo('  privileges: {}'.format(','.join(epmanage.lib.auth.get_privileges(token))))

    tokenfile.write(rsp)
Esempio n. 2
0
def check_token():
    token = click.get_current_context().obj['token']
    if not token:
        exit_fail('Invalid token')
    click.secho('Valid token', fg='green')
    exp = arrow.get(token.get('exp'))
    click.echo('  token expires {} ({})'.format(exp.humanize(), exp))
    click.echo('  privileges: {}'.format(','.join(epmanage.lib.auth.get_privileges(token))))
Esempio n. 3
0
def delete(uuid):
    agent_api = AgentAPI()
    agent = agent_api.get(uuid)
    if not agent:
        exit_fail('Agent not found')

    if click.confirm('Do you really want to delete this agent?'):
        ret, error = agent_api.delete(agent)
        if ret:
            click.secho('Agent deleted', fg='green')
        else:
            click.secho('Deletion error', fg='red')
Esempio n. 4
0
def delete(email):
    user_api = UserAPI()
    user = user_api.from_email(email)
    if not user:
        exit_fail('User not found')

    if click.confirm('Do you really want to delete this user?'):
        ret, error = user_api.delete(user)
        if ret:
            click.secho('User deleted', fg='green')
        else:
            click.secho('Deletion error', fg='red')
Esempio n. 5
0
def profile(param, value):
    token = click.get_current_context().obj['token']
    user_id = token['sub']
    user_api = UserAPI()
    user, error = user_api.get(user_id)
    if not user:
        exit_fail('Cannot get profile, {}'.format(error))
    attrs = user.attributes()

    if param and value:
        # EDIT
        if not param in attrs:
            exit_fail('Invalid parameter')
        elif user.is_prop_read_only(param):
            exit_fail('The specified parameter is read-only')

        prop_type = user.get_prop_type(param).get('type')
        value, error = convert(prop_type, value)
        if not value:
            exit_fail('Invalid value: {}'.format(error))

        ret, error = user_api.patch(user, {param: value})
        if ret:
            click.secho('Update successful', fg='green')
        else:
            click.secho('Update error: {}'.format(error), fg='red')
    else:
        # Display
        for name, value in attrs.items():
            click.secho('{}'.format(name), nl=False)
            click.echo(' = {}'.format(value))
Esempio n. 6
0
def print(email):
    user_api = UserAPI()
    if not email:
        data = user_api.list()  # type: List[User]
        if not data:
            exit_warning('No data')
        emails = dict()
        for i, user in enumerate(data):
            click.echo('[{}] '.format(i), nl=False)
            print_user(user)
            click.echo()
            emails[i] = user.attributes()['email']
        email = emails[click.prompt('Select an user', type=click.IntRange(0, len(data)))]

    user, error = user_api.from_email(email)
    if not user:
        exit_fail('User not found')

    print_user(user, expand=True)
Esempio n. 7
0
def print(uuid):
    agent_api = AgentAPI()
    if not uuid:
        data = agent_api.list()  # type: list[Agent]
        if not data:
            exit_warning('No data')
        uuids = dict()
        for i, agent in enumerate(data):
            click.echo('[{}] '.format(i), nl=False)
            print_agent(agent)
            click.echo()
            uuids[i] = agent.attributes()['uuid']
        uuid = uuids[click.prompt('Select an agent',
                                  type=click.IntRange(0, len(data)))]

    agent, error = agent_api.get(uuid)
    if not agent:
        exit_fail('Agent not found')

    print_agent(agent, expand=True)
Esempio n. 8
0
def download(os, osversion, arch):
    def choose_callback(pkgs):
        for i in range(len(pkgs)):
            click.echo('[{}] '.format(i), nl=False)
            print_pkg(pkgs[i])
        return click.prompt('Select a package',
                            type=click.IntRange(0, len(pkgs)))

    try:
        fname, content = PackageAPI().download(os, osversion, arch,
                                               choose_callback)
    except ValueError as exc:
        exit_fail(exc)

    try:
        with open(fname, 'wb') as ofile:
            ofile.write(content)
        click.secho('Download successful : {}'.format(fname), fg='green')
    except OSError as exc:
        exit_fail('Cannot save file to disk: {}'.format(exc))
Esempio n. 9
0
def set(uuid, param, value):
    agent_api = AgentAPI()
    agent = agent_api.get(uuid)  # type: Agent
    if not agent:
        exit_fail('Agent not found')
    attrs = agent.attributes()

    if not param in attrs:
        exit_fail('Invalid parameter')
    elif agent.is_prop_read_only(param):
        exit_fail('The specified parameter is read-only')

    prop_type = agent.get_prop_type(param).get('type')
    value, error = convert(prop_type, value)
    if not value:
        exit_fail('Invalid value: {}'.format(error))

    ret, error = agent_api.patch(agent, {param: value})
    if ret:
        click.secho('Update successful', fg='green')
    else:
        click.secho('Update error: {}'.format(error), fg='red')
Esempio n. 10
0
def set(email, param, value):
    user_api = UserAPI()
    user = user_api.from_email(email)  # type: User
    if not user:
        exit_fail('User not found')
    attrs = user.attributes()

    if not param in attrs:
        exit_fail('Invalid parameter')
    elif user.is_prop_read_only(param):
        exit_fail('The specified parameter is read-only')

    prop_type = user.get_prop_type(param).get('type')
    value, error = convert(prop_type, value)
    if not value:
        exit_fail('Invalid value: {}'.format(error))

    ret, error = user_api.patch(user, {param: value})
    if ret:
        click.secho('Update successful', fg='green')
    else:
        click.secho('Update error: {}'.format(error), fg='red')
Esempio n. 11
0
def manage(action: str, name: str):
    result, error = AppAPI().manage(name, action)
    if not result:
        exit_fail(error.get('_error', {}).get('message', 'Unknown error'))
    else:
        click.secho('Success', fg='green')