Exemple #1
0
def get_couchdb_info(sys_type:str) -> dict:

    vals = ('local_couchdb_url', 'couchdb_username', 'couchdb_password_b64_cipher')
    prompts = {'local_couchdb_url':'Enter the URL of your local couchdb server:',
               'couchdb_username':'******',
               'couchdb_password_b64_cipher':'Enter the couchdb password:'******'couchdb_username':{'default':lambda s: encrypt(bytes(s, 'utf-8'))},
                  'couchdb_password_b64_cipher':{'default':lambda s: encrypt(bytes(s, 'utf-8'))}}
    return prompt(vals, prompts, generators)
Exemple #2
0
def get_mqtt_info(sys_type:str) -> dict:
    vals = ('mqtt_username', 'mqtt_password_b64_cipher', 'mqtt_url', 'mqtt_port')
    prompts = {'mqtt_username':'******', 
               'mqtt_password_b64_cipher':'Enter your MQTT account password.',
               'mqtt_url':'Enter your MQTT broker url.',
               'mqtt_port':'Enter your MQTT broker port number.'}
    generators = {'mqtt_password_b64_cipher':{'default':lambda s: encrypt(bytes(s, 'utf-8'))},
                  'mqtt_port':{'default':lambda s: int(s)}}
    return prompt(vals, prompts, generators)
Exemple #3
0
def get_jwt_info(sys_type: str) -> dict:

    vals = ('fop_jose_id', 'hmac_secret_key_b64_cipher', 'fws_url')
    prompts = {'fop_jose_id':'Enter the JWT id of the fop that you wish to connect to\n'+\
                             '(e.g. c0e94b7e-2ab9-45c7-9dfb-468f349c67a2)',
               'hmac_secret_key_b64_cipher':'Enter the value of your jose fop hmac secret key.\n'+\
                                            'This is a 32 character URL safe random token provided by your fop provider.',
               'fws_url':'Enter the URL of your fopd web services server.'
              } 
    generators = {'hmac_secret_key_b64_cipher': {'default':lambda s: encrypt(bytes(s, 'utf-8'))}}

    #- if sys_type == 'fc1':
    #-     vals = vals + ('fws_url',)
    #-    prompts['fws_url'] = 'Enter the URL of your Farm Web Services server.\n'
        
    return prompt(vals, prompts, generators)
Exemple #4
0
def encrypt_util(pt):
    print(encrypt(pt))
    return 'OK'
Exemple #5
0
def reset_couchdb_passwords(args):

    try:

        # write the password to the couchdb configuration file
        logger.info(
            'changing couchdb password in local.ini. Reset the couchdb service to take up the new password.'
        )
        # Note: The couchdb service updates the local.ini file to contain the hashed password instead of the plaintext
        #       password. So the admin password is essentially unknown.
        couchdb_pwd = generate_password(16)
        pwd_changed = change_file_line(
            path.join(couchdb_local_config_file_directory, 'local.ini'),
            r'^admin[ |\t]*=', 'admin = {}\n'.format(couchdb_pwd))

        if not pwd_changed:
            logger.error('Unable to reset the couchdb admin password.')
            raise Exception('ERROR: unable to reset couchdb admin password.')
        else:
            logger.info(
                'Writing the couchdb admin password to the config file.')
            change_file_line(
                path.join(configuration_directory_location, 'config.py'),
                r'^couchdb_admin_password_b64_cipher[ |\t]*=',
                'couchdb_admin_password_b64_cipher = {}\n'.format(
                    encrypt(couchdb_pwd.encode('utf-8'))))

            logger.info(
                'Restarting couchdb so that the admin password change is taken up.'
            )
            run('sudo systemctl restart couchdb', shell=True)

        # get a random value for the fopd couchdb user password
        fopd_password = generate_password(16)
        #- logger.info('password: {}'.format(fopd_password))

        logger.info(
            'waiting 5 seconds for couchdb to start, so that I can query it')
        sleep(5)

        # Get the document for the fopd user.
        # Note: Use the admin credentials created above; you can't use the new value in the config.py
        #       file because it is not loaded into the Python interpretter.
        logger.info('retrieving document for fopd user from couchdb')
        r = requests.get("http://127.0.0.1:5984/_users/org.couchdb.user:fopd",
                         auth=('admin', couchdb_pwd))
        if r.status_code == 200:
            logger.info('resetting fopd couchdb password')
            new_fopd_pwd = generate_password(16)

            #+ DEBUG
            # print('admin password {}'.format(couchdb_pwd))
            # print('fopd password {}'.format(new_fopd_pwd))
            #+ """

            # The couchdb API is a JSON driven website. POST to this API a request
            # to change the password for the fopd user.
            r = requests.put(
                'http://localhost:5984/_users/org.couchdb.user:fopd',
                data=dumps({
                    'name': 'fopd',
                    'roles': [],
                    'type': 'user',
                    'password': '******'.format(new_fopd_pwd)
                }),
                auth=('admin', couchdb_pwd),
                headers={
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'If-Match': '{}'.format(r.json()['_rev'])
                })
            if r.status_code == 201:
                logger.info(
                    'updating the configuration file with the new fopd couchdb password'
                )
                change_file_line(
                    path.join(configuration_directory_location, 'config.py'),
                    r'^couchdb_password_b64_cipher[ |\t]*=',
                    'couchdb_password_b64_cipher = {}\n'.format(
                        encrypt(new_fopd_pwd.encode('utf-8'))))
            else:
                logger.error(
                    'Cannot reset the database. Couch reply {}:{}'.format(
                        r.status_code, r.json()))
        else:
            logger.error(
                'Unable to retrieve user document for fopd from the couchdb database'
            )
            raise Exception(
                'ERROR Unable to retrieve user document for fopd from the couchdb database'
            )

        return True if args['silent'] else 'OK'

    except:
        logger.error('Exception in reset_couchdb_passwords: {}, {}'.format(
            exc_info()[0],
            exc_info()[1]))
        return False if args['silent'] else 'ERROR'