def delete_value(self, name, local=True): """ Delete the provided key. By default, value is deleted from a namespace local to the pack/class. If you want to delete a global value, pass local=False to this method. :param name: Name of the key to delete. :type name: ``str`` :param local: Delete a value in a namespace local to the pack/class. Defaults to True. :type: local: ``bool`` :return: ``True`` on success, ``False`` otherwise. :rtype: ``bool`` """ name = self._get_full_key_name(name=name, local=local) client = self._get_api_client() instance = KeyValuePair() instance.id = name instance.name = name self._logger.audit('Deleting value from the datastore (name=%s)', name) try: client.keys.delete(instance=instance) except Exception: return False return True
def _store_cache_configuration(self): ews_url = self.account.protocol.service_endpoint ews_auth_type = self.account.protocol.auth_type primary_smtp_address = self.account.primary_smtp_address self.client.keys.update(KeyValuePair(name='exchange_ews_url', value=ews_url)) self.client.keys.update(KeyValuePair(name='exchange_ews_auth_type', value=ews_auth_type)) self.client.keys.update(KeyValuePair(name='exchange_primary_smtp_address', value=primary_smtp_address))
def run(self, endpoint_uri, **kwargs): """Base get action endpoint_uri is pased from metadata file """ if kwargs.get('id', False): # modify the `endpoint_uri` to use the detail route endpoint_uri = '{}{}/'.format(endpoint_uri, str(kwargs.pop('id'))) self.logger.debug( 'endpoint_uri transformed to {} because id was passed'.format( endpoint_uri)) if kwargs.get('save_in_key_store' ) and not kwargs.get('save_in_key_store_key_name'): return ( False, 'save_in_key_store_key_name MUST be used with save_in_key_store!' ) result = self.get(endpoint_uri, **kwargs) if kwargs['save_in_key_store']: # save the result in the st2 keystore client = Client(base_url='http://localhost') key_name = kwargs['save_in_key_store_key_name'] client.keys.update( KeyValuePair(name=key_name, value=json.dumps(result), ttl=kwargs['save_in_key_store_ttl'])) return (True, "Result stored in st2 key {}".format(key_name)) return result
def run(self, user, url): client = Client() gitorgs = client.keys.get_by_name(name='git-orgs', decrypt=True) if gitorgs: dict = json.loads(gitorgs.value) else: dict = {} user = user.strip() url = url.strip() key = user + '|' + url if key in dict: org = dict[key] del dict[key] gitorgs = json.dumps(dict) client.keys.update( KeyValuePair(name='git-orgs', value=gitorgs, secret=True)) return list((key, filter_org(org))) return False
def set_value(self, name, value, ttl=None, local=True, scope=SYSTEM_SCOPE, encrypt=False): """ Set a value for the provided key. By default, value is set in a namespace local to the pack/class. If you want to set a global value, pass local=False to this method. :param name: Key name. :type name: ``str`` :param value: Key value. :type value: ``str`` :param ttl: Optional TTL (in seconds). :type ttl: ``int`` :param local: Set value in a namespace local to the pack/class. Defaults to True. :type: local: ``bool`` :param scope: Scope under which to place the item. Defaults to system scope. :type: local: ``str`` :param encrypt: Encrypt the value when saving. Defaults to False. :type: local: ``bool`` :return: ``True`` on success, ``False`` otherwise. :rtype: ``bool`` """ if scope != SYSTEM_SCOPE: raise ValueError('Scope %s is unsupported.', scope) name = self._get_full_key_name(name=name, local=local) value = str(value) client = self._get_api_client() self._logger.audit('Setting value in the datastore (name=%s)', name) instance = KeyValuePair() instance.id = name instance.name = name instance.value = value instance.scope = scope if encrypt: instance.secret = True if ttl: instance.ttl = ttl client.keys.update(instance=instance) return True
def encrypt_password(self, input_token, log_file=True): self.print_log("Encrypt xmc password...", log_file) st2_client = Client(api_url='https://{0}/api'.format(self.st2ip), token=input_token) st2_client.keys.update( KeyValuePair(name=XMC_KEY_NAME, value=self.xmcpassword, secret=True))
def delete_value(self, name, local=True, scope=SYSTEM_SCOPE): """ Delete the provided key. By default, value is deleted from a namespace local to the pack/class. If you want to delete a global value, pass local=False to this method. :param name: Name of the key to delete. :type name: ``str`` :param local: Delete a value in a namespace local to the pack/class. Defaults to True. :type: local: ``bool`` :param scope: Scope under which item is saved. Defaults to system scope. :type: local: ``str`` :return: ``True`` on success, ``False`` otherwise. :rtype: ``bool`` """ if scope != SYSTEM_SCOPE: raise ValueError('Scope %s is unsupported.' % scope) name = self._get_full_key_name(name=name, local=local) client = self.get_api_client() instance = KeyValuePair() instance.id = name instance.name = name self._logger.debug('Deleting value from the datastore (name=%s)', name) try: params = {'scope': scope} client.keys.delete(instance=instance, params=params) except Exception as e: self._logger.exception( 'Exception deleting value from datastore (name=%s): %s', name, e ) return False return True
def run(self, ns, key, value): """ Entry into the action script """ client = Client(base_url='http://localhost') keyname = ns + "." + key client.keys.update(KeyValuePair(name=keyname, value=value))
def encrypt_passwords(self, firewall_list): for firewall_key, firewall_value in firewall_list.iteritems(): for key, value in firewall_value.iteritems(): if key == 'password': if self.print_log: self.logger.debug( 'Encrypt password for firewall {0}'.format( firewall_key)) self.st2_client.keys.update( KeyValuePair(name=firewall_key, value=value, secret=True))
def set_value(self, name, value, ttl=None, local=True, scope=SYSTEM_SCOPE, encrypt=False): """ Set a value for the provided key. By default, value is set in a namespace local to the pack/class. If you want to set a global value, pass local=False to this method. :param name: Key name. :type name: ``str`` :param value: Key value. :type value: ``str`` :param ttl: Optional TTL (in seconds). :type ttl: ``int`` :param local: Set value in a namespace local to the pack/class. Defaults to True. :type: local: ``bool`` :param scope: Scope under which to place the item. Defaults to system scope. :type: local: ``str`` :param encrypt: Encrypt the value when saving. Defaults to False. :type: local: ``bool`` :return: ``True`` on success, ``False`` otherwise. :rtype: ``bool`` """ if scope != SYSTEM_SCOPE: raise ValueError('Scope %s is unsupported.' % scope) name = self._get_full_key_name(name=name, local=local) value = str(value) client = self.get_api_client() self._logger.debug('Setting value in the datastore (name=%s)', name) instance = KeyValuePair() instance.id = name instance.name = name instance.value = value instance.scope = scope if encrypt: instance.secret = True if ttl: instance.ttl = ttl client.keys.update(instance=instance) return True
def select(self, query, data, key): q = query if data: values = self._list_to_string(data) q = q.format(values) c = self.db.cursor() try: c.execute(q) output = self._format_results(c) if key is not None: client = Client(base_url='http://localhost') client.keys.update(KeyValuePair(name=key, value=str(output))) return key else: return output except MySQLdb.Error as e: # pylint: disable=no-member raise Exception(e)
def _test_datastore_actions_via_client(self): print('Test datastore access via raw client.') client = Client(base_url='http://localhost') test_name = 'st2tests.putin' test_value = 'putout' # Put kv = KeyValuePair(name=test_name, value=test_value) client.keys.update(kv) # print('Wrote key: %s value: %s to datastore.' % (test_name, test_value)) # Get val = client.keys.get_by_name(name=test_name) if val.value != test_value: raise Exception('KeyValue access failed on GET: %s' % test_name) # print('Got value: %s from datastore.' % val.value) # Delete client.keys.delete(val)
def select(self, query, data, key): q = query if data: values = self._list_to_string(data) q = q.format(values) self.logger.debug("Generated query: %s" % q) c = self.db.cursor() try: c.execute(q) output = self._format_results(c) if key != None: client = Client(base_url='http://localhost') client.keys.update(KeyValuePair(name=key, value=str(output))) return key else: return output except MySQLdb.Error, e: print str(e) return False
def run(self, packs, dependencies_list_name): self.client = Client(base_url='http://localhost') # check keystore dependencies_list_json = self.client.keys.get_by_name( name=dependencies_list_name) dependencies_list = [] if dependencies_list_json: dependencies_list = json.loads(dependencies_list_json.value) packs_to_install = [] for pack in packs: if pack not in dependencies_list and pack not in packs_to_install: packs_to_install.append(pack) dependencies_list.append(pack) # add to keystore { "status": "install", "execution_id": execution_id } self.client.keys.update( KeyValuePair(name=dependencies_list_name, value=json.dumps(dependencies_list))) return packs_to_install
def set_value(self, name, value, ttl=None, local=True): """ Set a value for the provided key. By default, value is set in a namespace local to the pack/class. If you want to set a global value, pass local=False to this method. :param name: Key name. :type name: ``str`` :param value: Key value. :type value: ``str`` :param ttl: Optional TTL (in seconds). :type ttl: ``int`` :param local: Set value in a namespace local to the pack/class. Defaults to True. :type: local: ``bool`` :return: ``True`` on success, ``False`` otherwise. :rtype: ``bool`` """ name = self._get_full_key_name(name=name, local=local) value = str(value) client = self._get_api_client() self._logger.audit('Setting value in the datastore (name=%s)', name) instance = KeyValuePair() instance.id = name instance.name = name instance.value = value if ttl: instance.ttl = ttl client.keys.update(instance=instance) return True
def setkvpair(self, key, data): self.client = Client(base_url='http://localhost') """Set KV data using key and data.""" # string_data = json.dumps(data) self.client.keys.update(KeyValuePair(name=str(key), value=str(data)))
def delkvpair(self, key): """Set KV data using key and data.""" self.client = Client(base_url='http://localhost') # string_data = json.dumps(data) self.client.keys.delete(KeyValuePair(name=str(key)))