Exemple #1
0
def _login_file(opts, logins):
    '''
    Load the login credentials from a plaintext YAML file.

    Do not overwrite the global credentials if they are set.

    @param opts   The command line options.
    @param logins The login credentials.
    @returns the updated login credentials.
    '''
    if opts.login is not None:
        for item in opts.login:
            ifile = item[0]
            ifp = open(ifile, 'r')
            data = ifp.read()
            ifp.close()

            # Heuristic guess to determine whether this is an
            # encrypted file.
            password = None
            if data is not None:
                if data.find('{') < 0:
                    # This isn't a YAML file.
                    # assume that is it encrypted.
                    password = getpass('%s password: '******'decryption failed for file: %s (%s)' %
                                     (ifile, str(exp)))
                        sys.exit(1)

            try:
                logins = _load_login_data(opts, logins, ifile, data)
            except yaml.reader.ReaderError as exp:
                if password is not None:
                    LOGGER.error('load failed for file (invalid password?): %s (%s)' %
                                 (ifile, str(exp)))
                else:
                    LOGGER.error('load failed for file: %s (%s)' %
                                 (ifile, str(exp)))
                sys.exit(1)

    return logins
Exemple #2
0
def _login_file_encrypted(opts, logins):
    '''
    Load the login credentials from an encrypted YAML file.

    Encryption is done using openssl enc -e -a -aes-256-cbc -salt.

    Do not overwrite the global credentials if they are set.

    @param opts   The command line options.
    @param logins The login credentials.
    @returns the updated login credentials.
    '''
    if opts.login_secure is not None:
        for item in opts.login_secure:
            ifile = item[0]
            password = item[1]
            ifp = open(ifile, 'r')
            ciphertext = ifp.read()
            ifp.close()

            if password == '':
                password = getpass('%s password: '******'decrypt failed for file: %s (%s)' %
                             (ifile, str(exp)))
                sys.exit(1)

            try:
                logins = _load_login_data(opts, logins, ifile, plaintext)
            except yaml.reader.ReaderError as exp:
                LOGGER.error('load failed for file (invalid password?): %s (%s)' %
                             (ifile, str(exp)))
                sys.exit(1)

    return logins