|
| 1 | +""" |
| 2 | +Example of Duo Accounts API set child account edition |
| 3 | +""" |
| 4 | + |
| 5 | +import duo_client |
| 6 | +import getpass |
| 7 | + |
| 8 | +ALLOWED_DUO_EDITIONS = ("PERSONAL", "ENTERPRISE", "PLATFORM", "BEYOND") |
| 9 | + |
| 10 | +def _get_user_input(prompt, secure=False): |
| 11 | + """Read information from STDIN, using getpass when sensitive information should not be echoed to tty""" |
| 12 | + if secure is True: |
| 13 | + return getpass.getpass(prompt) |
| 14 | + else: |
| 15 | + return input(prompt) |
| 16 | + |
| 17 | + |
| 18 | +def prompt_for_credentials() -> dict: |
| 19 | + """Collect required API credentials from command line prompts""" |
| 20 | + |
| 21 | + ikey = _get_user_input('Duo Accounts API integration key ("DI..."): ') |
| 22 | + skey = _get_user_input('Duo Accounts API integration secret key: ', secure=True) |
| 23 | + host = _get_user_input('Duo Accounts API hostname ("api-....duosecurity.com"): ') |
| 24 | + account_id = _get_user_input('Child account ID: ') |
| 25 | + account_apihost = _get_user_input('Child account api_hostname: ') |
| 26 | + account_edition = _get_user_input('Child account edition: ') |
| 27 | + while account_edition.upper() not in ALLOWED_DUO_EDITIONS: |
| 28 | + print(f"Invalid account edition. Please select one of {ALLOWED_DUO_EDITIONS}") |
| 29 | + account_edition = _get_user_input('Child account edition: ') |
| 30 | + |
| 31 | + return { |
| 32 | + "ikey": ikey, |
| 33 | + "skey": skey, |
| 34 | + "host": host, |
| 35 | + "account_id": account_id, |
| 36 | + "child_api_host": account_apihost, |
| 37 | + "account_edition": account_edition, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + """Main program entry point""" |
| 43 | + |
| 44 | + inputs = prompt_for_credentials() |
| 45 | + edition = inputs.pop('account_edition') |
| 46 | + edition = edition.upper() |
| 47 | + |
| 48 | + account_admin_api = duo_client.admin.AccountAdmin(**inputs) |
| 49 | + |
| 50 | + print(f"Setting edition for account ID {inputs['account_id']} to {edition}") |
| 51 | + result = account_admin_api.set_edition(edition) |
| 52 | + if result != "": |
| 53 | + print(f"An error occurred while setting edition for account {inputs['account_id']}") |
| 54 | + print(f"Error message: {result}") |
| 55 | + else: |
| 56 | + print(f"Edition [{edition}] successfully set for account ID {inputs['account_id']}") |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + main() |
0 commit comments