def test_get_secret_bad_retry(mocked_getpass):
    """Should ask for secret twice and raise WmflibError if they don't match."""
    mocked_getpass.getpass.side_effect = ['interactive_password', 'foobar']
    with pytest.raises(WmflibError, match='secret: Passwords did not match'):
        interactive.get_secret('secret', confirm=True)
    mocked_getpass.getpass.assert_has_calls(
        [mock.call(prompt='secret: '), mock.call(prompt='Again, just to be sure: ')])
Beispiel #2
0
def run(args, spicerack):
    """Required by Spicerack API."""
    ensure_shell_is_durable()
    session = Session()
    session.verify = False
    return_code = 0
    current_password = get_secret('Current password')
    new_password = get_secret("New password", confirm=True)

    session.auth = (args.username, current_password)

    _pdus = pdus.get_pdu_ips(spicerack.netbox(), args.query)

    for pdu in _pdus:
        try:
            if not spicerack.dry_run:
                change_password(pdu, session, new_password)
            else:
                logger.info('%s: Dry run, not trying.', pdu)
            if args.check_default:
                if pdus.check_default(pdu, session):
                    # TODO: delete default user
                    return_code = 1
        except (pdus.VersionError, PasswordResetError) as error:
            logger.error(error)
            return_code = 1
    return return_code
def get_management_password() -> str:
    """Get the management password either from the environment or asking for it.

    Returns:
        str: the password.

    Raises:
        spicerack.exceptions.SpicerackError: if the password is empty.

    """
    password = os.getenv("MGMT_PASSWORD")

    if password is None:
        logger.debug("MGMT_PASSWORD environment variable not found")
        # Ask for a password, raise exception if not a tty
        password = get_secret("Management Password")
    else:
        logger.info(
            "Using Management Password from the MGMT_PASSWORD environment variable"
        )

    if not password:
        raise SpicerackError("Empty Management Password")

    return password
def test_get_secret_too_small(mocked_getpass):
    """Should ask for secret until the minimum length is met."""
    mocked_getpass.getpass.side_effect = ['5char', 'interactive_password']
    assert interactive.get_secret('secret') == 'interactive_password'
    mocked_getpass.getpass.assert_has_calls(
        [mock.call(prompt='secret: '),
         mock.call(prompt='Secret must be at least 6 characters. try again: ')])
def run(args, spicerack):
    """Required by Spicerack API."""
    ensure_shell_is_durable()
    return_code = 0
    session = Session()
    session.verify = False
    password = get_secret('Enter login password')
    snmp_ro = get_secret('New SNMP RO String', confirm=True)

    session.auth = (args.username, password)

    _pdus = pdus.get_pdu_ips(spicerack.netbox(), args.query)

    for pdu in _pdus:
        snmp_rw = random_string() if args.reset_rw else None
        try:
            if not spicerack.dry_run:
                version = pdus.get_version(pdu, session)
                if change_snmp(pdu, version, session, snmp_ro, snmp_rw,
                               args.force):
                    reboot_time = datetime.utcnow()
                    pdus.reboot(pdu, version, session)
                    # Reboots from experience take at least 60 seconds
                    logger.info('%s: sleep while reboot', pdu)
                    sleep(60)
                    pdus.wait_reboot_since(pdu, reboot_time, session)
            else:
                logger.info('%s: Dry run, not trying.', pdu)
            if args.check_default:
                if pdus.check_default(pdu, session):
                    # TODO: delete default user
                    pass
        except (pdus.VersionError, SnmpResetError, pdus.RebootError) as error:
            logger.error(error)
            return_code = 1
    return return_code
Beispiel #6
0
def run(args, spicerack):
    """Required by Spicerack API."""
    session = Session()
    session.verify = False
    return_code = 0
    current_password = get_secret('Current password')
    session.auth = (args.username, current_password)

    _pdus = pdus.get_pdu_ips(spicerack.netbox(), args.query)

    for pdu in _pdus:
        uptime = None
        try:
            uptime = pdus.get_uptime(pdu, session)
            logger.info('%s: uptime %s', pdu, uptime)
        except pdus.UptimeError as error:
            logger.error(error)
            return_code = 1
        if args.check_default:
            if pdus.check_default(pdu, session):
                # TODO: delete default user
                return_code = 1
    return return_code
def run(args, spicerack):
    """Required by Spicerack API."""
    if spicerack.dry_run:
        logger.info('this cookbook does nothing with with --dry-run')
        return 0
    ensure_shell_is_durable()
    session = Session()
    session.verify = False
    return_code = 0
    current_password = get_secret('Current password')
    session.auth = (args.username, current_password)

    _pdus = pdus.get_pdu_ips(spicerack.netbox(), args.query)

    for pdu in _pdus:
        try:
            if args.since:
                uptime = pdus.parse_uptime(pdus.get_uptime(pdu, session))
                if uptime < args.since:
                    logger.info('%s: Not rebooting uptime is %d', pdu, uptime)
                    continue
            reboot_time = datetime.utcnow()
            version = pdus.get_version(pdu, session)
            pdus.reboot(pdu, version, session)
            # Reboots from expereince take at least 60 seconds
            logger.info('%s: sleep while reboot', pdu)
            sleep(60)
            pdus.wait_reboot_since(pdu, reboot_time, session)
        except (pdus.VersionError, pdus.RebootError,
                pdus.UptimeError) as error:
            logger.error(error)
            return_code = 1
        if args.check_default:
            if pdus.check_default(pdu, session):
                # TODO: delete default user
                return_code = 1
    return return_code
def test_get_secret_correct(mocked_getpass):
    """Should ask for secret twice and return the secret."""
    mocked_getpass.getpass.side_effect = ['interactive_password', 'interactive_password']
    assert interactive.get_secret('secret', confirm=True) == 'interactive_password'
    mocked_getpass.getpass.assert_has_calls(
        [mock.call(prompt='secret: '), mock.call(prompt='Again, just to be sure: ')])
def test_get_secret_correct_noconfirm(mocked_getpass):
    """Should ask for secret once and return the secret."""
    mocked_getpass.getpass.return_value = 'interactive_password'
    assert interactive.get_secret('secret') == 'interactive_password'
    mocked_getpass.getpass.assert_called_once_with(prompt='secret: ')