示例#1
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        auth_found = False
        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_("No auth credentials found"))
        else:
            cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)

            for cred in cred_list:
                if cred.get('name') == self.options.name:
                    auth_found = True
                    password = cred.get('password')
                    if password is not None:
                        cred['password'] = utilities.PASSWORD_MASKING
                    if cred.get('sudo_password') is not None:
                        cred['sudo_password'] = utilities.PASSWORD_MASKING

                    data = json.dumps(cred, sort_keys=True, indent=4,
                                      separators=(',', ': '))
                    print(data)
                    break

            if not auth_found:
                print(_('Auth "%s" does not exist' % self.options.name))
                sys.exit(1)
示例#2
0
    def _do_command(self):
        if self.options.name:
            auth_found = False
            vault = get_vault(self.options.vaultfile)
            if os.path.isfile(utilities.CREDENTIALS_PATH):
                cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
                for index, cred in enumerate(cred_list):
                    if cred.get('name') == self.options.name:
                        del cred_list[index]
                        print(_('Auth "%s" was removed' % self.options.name))
                        auth_found = True
                        break
                if not auth_found:
                    print(_('Auth "%s" was not found' % self.options.name))
                    sys.exit(1)
                vault.dump_as_json_to_file(cred_list,
                                           utilities.CREDENTIALS_PATH)
            else:
                print(_("All authorization credentials removed"))

        elif self.options.all:
            if os.path.isfile(utilities.CREDENTIALS_PATH):
                os.remove(utilities.CREDENTIALS_PATH)

            print(_("All authorization credentials removed"))
示例#3
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        auth_found = False

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_("No auth credentials found"))
            sys.exit(1)
        else:
            cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)

            for cred in cred_list:
                if cred.get('name') == self.options.name:
                    auth_found = True
                    if self.options.username:
                        cred['username'] = self.options.username
                    if self.options.password:
                        print(_('Provide connection password.'))
                        cred['password'] = getpass()
                    if self.options.sudo_password:
                        print(_('Provide password for sudo.'))
                        cred['sudo_password'] = getpass()
                    if self.options.filename:
                        cred['ssh_key_file'] = self.options.filename
                    break
            if not auth_found:
                print(_('Auth "%s" does not exist' % self.options.name))
                sys.exit(1)

            vault.dump_as_json_to_file(cred_list, utilities.CREDENTIALS_PATH)

        print(_("Auth '%s' updated") % self.options.name)
示例#4
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        auth_found = False
        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_("No auth credentials found"))
        else:
            cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)

            for cred in cred_list:
                if cred.get('name') == self.options.name:
                    auth_found = True
                    output = cred.get('id') + ','
                    output += cred.get('name') + ','
                    output += cred.get('username')
                    password = cred.get('password')
                    sshkeyfile = cred.get('ssh_key_file')
                    if not password == '':
                        output += ',******'
                    if not sshkeyfile == '':
                        output += ',' + sshkeyfile

                    print(output)
                    break

            if not auth_found:
                print(_('Auth "%s" does not exist' % self.options.name))
                sys.exit(1)
示例#5
0
    def _do_command(self):
        # pylint: disable=too-many-locals, too-many-branches
        # pylint: disable=too-many-statements, too-many-nested-blocks
        vault = get_vault(self.options.vaultfile)
        cred_list = []
        profiles_list = []
        range_list = []
        profile_found = False
        auth_found = False

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_('No credentials exist yet.'))
            sys.exit(1)

        if not os.path.isfile(utilities.PROFILES_PATH):
            print(_('No profiles exist yet.'))
            sys.exit(1)

        cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
        profiles_list = vault.load_as_json(utilities.PROFILES_PATH)

        if self.options.hosts:
            range_list = read_ranges(self.options.hosts)

        for curr_profile in profiles_list:
            if curr_profile.get('name') == self.options.name:
                profile_found = True
                if self.options.hosts:
                    curr_profile['hosts'] = range_list

                if self.options.sshport:
                    curr_profile['ssh_port'] = str(self.options.sshport)

                if self.options.auth:
                    new_auths = []
                    auth_list = self.options.auth
                    for auth in auth_list:
                        for cred in cred_list:
                            if auth == cred.get('name'):
                                auth_found = True
                                store_cred = {
                                    'id': cred.get('id'),
                                    'name': cred.get('name')
                                }
                                new_auths.append(store_cred)
                    if not auth_found:
                        print(_("Auths do not exist."))
                        sys.exit(1)

                    curr_profile['auth'] = new_auths
                break

        if not profile_found:
            print(_("Profile '%s' does not exist.") % self.options.name)
            sys.exit(1)

        vault.dump_as_json_to_file(profiles_list, utilities.PROFILES_PATH)
        print(_("Profile '%s' edited" % self.options.name))
示例#6
0
    def _do_command(self):
        profiles_list = []

        if self.options.name:
            vault = get_vault(self.options.vaultfile)
            profile = self.options.name
            profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
            profile_found = False

            for index, curr_profile in enumerate(profiles_list):
                if curr_profile.get('name') == profile:
                    del curr_profile[index]
                    profile_found = True
                    break

            if not profile_found:
                print(_("No such profile: '%s'") % profile)
                sys.exit(1)

            vault.dump_as_json_to_file(profiles_list, utilities.PROFILES_PATH)

            # removes inventory associated with the profile
            if os.path.isfile('data/' + profile + "_hosts"):
                os.remove('data/' + profile + "_hosts")

            profile_mapping = 'data/' + profile + '_host_auth_mapping'

            # when a profile is removed, it 'archives' the host auth mapping
            # by renaming it '(DELETED PROFILE)<profile_name>_host_auth_mapping
            # for identification by the user. The time stamps in mapping files
            # help in identifying the various forms and times in which the said
            # profile existed.
            if os.path.isfile(profile_mapping):
                os.rename(
                    profile_mapping,
                    'data/(DELETED PROFILE)' + profile + '_host_auth_mapping')

        # removes all inventories ever.
        elif self.options.all:
            if not os.path.isfile(utilities.PROFILES_PATH):
                print(_("All network profiles removed"))
            else:
                os.remove(utilities.PROFILES_PATH)
                for file_list in glob.glob("data/*_hosts"):
                    os.remove(file_list)
                    profile = file_list.strip('_hosts')
                    profile_mapping = 'data/' + profile + '_host_auth_mapping'
                    if os.path.isfile(profile_mapping):
                        os.rename(
                            profile_mapping, 'data/(DELETED PROFILE)' +
                            profile + '_host_auth_mapping')

                print(_("All network profiles removed"))
示例#7
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        hosts_list = self.options.hosts
        profiles_list = []
        ssh_port = 22

        if hasattr(self.options, 'sshport') \
           and self.options.sshport is not None:
            ssh_port = utilities.validate_port(self.options.sshport)

        if os.path.isfile(utilities.PROFILES_PATH):
            profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
            profile_found = profile_exists(profiles_list, self.options.name)
            if profile_found:
                print(_("Profile '%s' already exists.") % self.options.name)
                sys.exit(1)

        range_list = hosts_list

        # pylint: disable=len-as-condition
        if len(hosts_list) > 0 and os.path.isfile(hosts_list[0]):
            range_list = _read_in_file(hosts_list[0])

        _check_range_validity(range_list)

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_('No credentials exist yet.'))
            sys.exit(1)

        creds = []
        cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
        for auth in self.options.auth:
            for auth_item in auth.strip().split(","):
                valid = False
                for cred in cred_list:
                    if cred.get('name') == auth:
                        valid = True
                        # add the uuids of credentials
                        store_cred = {'id': cred.get('id'),
                                      'name': cred.get('name')}
                        creds.append(store_cred)

                if not valid:
                    print("Auth " + auth_item + " does not exist")
                    sys.exit(1)

        new_profile = OrderedDict([("name", self.options.name),
                                   ("hosts", range_list),
                                   ("ssh_port", str(ssh_port)),
                                   ("auth", creds)])

        _save_profile(vault, new_profile, profiles_list)
示例#8
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)

        if not os.path.isfile(utilities.PROFILES_PATH):
            print(_('No profiles exist yet.'))
            sys.exit(1)

        profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
        data = json.dumps(profiles_list,
                          sort_keys=True,
                          indent=4,
                          separators=(',', ': '))
        print(data)
示例#9
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        auth_name = self.options.name
        cred_list = []

        if os.path.isfile(utilities.CREDENTIALS_PATH):
            cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
            auth_found = auth_exists(cred_list, auth_name)
            if auth_found:
                print(_("Auth with name exists"))
                sys.exit(1)

        cred = make_auth_for_options(self.options)
        _save_cred(vault, cred, cred_list)
        print(_('Auth "%s" was added' % self.options.name))
示例#10
0
    def _do_command(self):
        if self.options.name:
            vault = get_vault(self.options.vaultfile)
            if os.path.isfile(utilities.CREDENTIALS_PATH):
                cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
                for index, cred in enumerate(cred_list):
                    if cred.get('name') == self.options.name:
                        del cred_list[index]
                        break
                vault.dump_as_json_to_file(cred_list,
                                           utilities.CREDENTIALS_PATH)

        elif self.options.all:
            if os.path.isfile(utilities.CREDENTIALS_PATH):
                os.remove(utilities.CREDENTIALS_PATH)

            print(_("All authorization credentials removed"))
示例#11
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        profiles_list = []
        ssh_port = self.options.sshport

        if os.path.isfile(utilities.PROFILES_PATH):
            profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
            profile_found = profile_exists(profiles_list, self.options.name)
            if profile_found:
                print(_("Profile '%s' already exists.") % self.options.name)
                sys.exit(1)

        range_list = read_ranges(self.options.hosts)

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_('No credentials exist yet.'))
            sys.exit(1)

        creds = []
        cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
        for auth in self.options.auth:
            for auth_item in auth.strip().split(","):
                valid = False
                for cred in cred_list:
                    if cred.get('name') == auth:
                        valid = True
                        # add the uuids of credentials
                        store_cred = {
                            'id': cred.get('id'),
                            'name': cred.get('name')
                        }
                        creds.append(store_cred)

                if not valid:
                    print("Auth " + auth_item + " does not exist")
                    sys.exit(1)

        new_profile = OrderedDict([("name", self.options.name),
                                   ("hosts", range_list),
                                   ("ssh_port", str(ssh_port)),
                                   ("auth", creds)])

        _save_profile(vault, new_profile, profiles_list)
        print(_('Profile "%s" was added' % self.options.name))
示例#12
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)

        if not os.path.isfile(utilities.PROFILES_PATH):
            print(_('No profiles exist yet.'))
            sys.exit(1)

        profile_found = False
        profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
        for profile in profiles_list:
            if self.options.name == profile.get('name'):
                profile_found = True
                data = json.dumps(profile, sort_keys=True, indent=4,
                                  separators=(',', ': '))
                print(data)
                break

        if not profile_found:
            print(_("Profile '%s' does not exist.") % self.options.name)
            sys.exit(1)
示例#13
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_('No credentials exist yet.'))
            sys.exit(1)

        cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)

        for cred in cred_list:
            output = cred.get('id') + ','
            output += cred.get('name') + ','
            output += cred.get('username')
            password = cred.get('password')
            sshkeyfile = cred.get('ssh_key_file')
            if not password == '':
                output += ',******'
            if not sshkeyfile == '':
                output += ',' + sshkeyfile

            print(output)
示例#14
0
    def _do_command(self):
        if not os.path.isfile(utilities.PROFILES_PATH):
            print(_("All network profiles removed"))
            return

        if self.options.name:
            vault = get_vault(self.options.vaultfile)
            profile = self.options.name
            profiles_list = vault.load_as_json(utilities.PROFILES_PATH)
            profile_found = False

            for index, curr_profile in enumerate(profiles_list):
                if curr_profile.get('name') == profile:
                    del profiles_list[index]
                    print(_('Profile "%s" was removed' % profile))
                    profile_found = True
                    break

            if not profile_found:
                print(_("No such profile: '%s'") % profile)
                sys.exit(1)

            vault.dump_as_json_to_file(profiles_list, utilities.PROFILES_PATH)

            # removes inventory associated with the profile
            profile_hosts_path = get_config_path(profile + PROFILE_HOSTS_SUFIX)
            if os.path.isfile(profile_hosts_path):
                os.remove(profile_hosts_path)
            _backup_host_auth_mapping(profile)

        # removes all inventories ever.
        elif self.options.all:
            os.remove(utilities.PROFILES_PATH)
            wildcard_hosts_path = get_config_path('*' + PROFILE_HOSTS_SUFIX)
            for file_list in glob.glob(wildcard_hosts_path):
                os.remove(file_list)
                file_list = os.path.basename(file_list)
                profile = file_list[:file_list.rfind(PROFILE_HOSTS_SUFIX)]
                _backup_host_auth_mapping(profile)
            print(_("All network profiles removed"))
示例#15
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_('No credentials exist yet.'))
            sys.exit(1)

        cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)

        if not cred_list:
            print(_('No credentials exist yet.'))
            sys.exit(1)
        else:
            for cred in cred_list:
                if cred.get('password') is not None:
                    cred['password'] = utilities.PASSWORD_MASKING
                if cred.get('sudo_password') is not None:
                    cred['sudo_password'] = utilities.PASSWORD_MASKING
            data = json.dumps(cred_list,
                              sort_keys=True,
                              indent=4,
                              separators=(',', ': '))
            print(data)
示例#16
0
    def _do_command(self):
        vault = get_vault(self.options.vaultfile)
        cred = {}
        ssh_file = 'empty'
        pass_to_store = ''
        auth_name = self.options.name
        cred_list = []

        if os.path.isfile(utilities.CREDENTIALS_PATH):
            cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
            auth_found = auth_exists(cred_list, auth_name)
            if auth_found:
                print(_("Auth with name exists"))
                sys.exit(1)

        if self.options.password:
            pass_prompt = getpass()
            pass_to_store = 'empty' if pass_prompt == '' else pass_prompt

        if self.options.filename:
            # using sshkey
            ssh_file = self.options.filename

            cred = OrderedDict([("id", str(uuid.uuid4())),
                                ("name", self.options.name),
                                ("username", self.options.username),
                                ("password", pass_to_store),
                                ("ssh_key_file", ssh_file)])

        elif self.options.username and self.options.password:
            cred = OrderedDict([("id", str(uuid.uuid4())),
                                ("name", self.options.name),
                                ("username", self.options.username),
                                ("password", pass_to_store),
                                ("ssh_key_file", ssh_file)])

        _save_cred(vault, cred, cred_list)
示例#17
0
    def _do_command(self):
        # pylint: disable=too-many-locals, too-many-branches
        # pylint: disable=too-many-statements, too-many-nested-blocks
        vault = get_vault(self.options.vaultfile)
        cred_list = []
        profiles_list = []
        range_list = []
        profile_found = False
        auth_found = False

        if not os.path.isfile(utilities.CREDENTIALS_PATH):
            print(_('No credentials exist yet.'))
            sys.exit(1)

        if not os.path.isfile(utilities.PROFILES_PATH):
            print(_('No profiles exist yet.'))
            sys.exit(1)

        cred_list = vault.load_as_json(utilities.CREDENTIALS_PATH)
        profiles_list = vault.load_as_json(utilities.PROFILES_PATH)

        if self.options.hosts:
            hosts_list = self.options.hosts
            range_list = hosts_list
            # pylint: disable=len-as-condition
            if len(hosts_list) > 0 and os.path.isfile(hosts_list[0]):
                range_list = _read_in_file(hosts_list[0])

            # makes sure the hosts passed in are in a format Ansible
            # understands.
            _check_range_validity(range_list)

        for curr_profile in profiles_list:
            if curr_profile.get('name') == self.options.name:
                profile_found = True
                if self.options.hosts:
                    curr_profile['hosts'] = range_list

                if self.options.sshport:
                    curr_profile['ssh_port'] = str(
                        utilities.validate_port(self.options.sshport))

                if self.options.auth:
                    new_auths = []
                    auth_list = self.options.auth
                    for auth in auth_list:
                        for cred in cred_list:
                            if auth == cred.get('name'):
                                auth_found = True
                                store_cred = {'id': cred.get('id'),
                                              'name': cred.get('name')}
                                new_auths.append(store_cred)
                    if not auth_found:
                        print(_("Auths do not exist."))
                        sys.exit(1)

                    curr_profile['auth'] = new_auths
                break

        if not profile_found:
            print(_("Profile '%s' does not exist.") % self.options.name)
            sys.exit(1)

        vault.dump_as_json_to_file(profiles_list, utilities.PROFILES_PATH)
        print(_("Profile '%s' edited" % self.options.name))