def ensure_channel_is_unique(self, session, url_path, http_accept, http_method, soap_action, cluster_id): existing_ones = session.query(HTTPSOAP).\ filter(HTTPSOAP.cluster_id==cluster_id).\ filter(HTTPSOAP.url_path==url_path).\ filter(HTTPSOAP.soap_action==soap_action).\ filter(HTTPSOAP.connection==CONNECTION.CHANNEL).\ all() # At least one channel with this kind of basic information already exists # but it is possible that it requires different HTTP headers (e.g. Accept, Method) # so we need to check each one manually. if existing_ones: for item in existing_ones: opaque = parse_instance_opaque_attr(item) item_http_accept = opaque.get('http_accept') # Raise an exception if the existing channel's method is equal to ours # but only if they use different Accept headers. if http_method: if item.method == http_method: if item_http_accept == http_accept: self._raise_error(item.name, url_path, http_accept, http_method, soap_action, 'chk1') # Similar, but from the Accept header's perspective if item_http_accept == http_accept: if item.method == http_method: self._raise_error(item.name, url_path, http_accept, http_method, soap_action, 'chk2')
def handle(self): with closing(self.odb.session()) as session: try: item = session.query(HTTPSOAP).\ filter(HTTPSOAP.id==self.request.input.id).\ one() opaque = parse_instance_opaque_attr(item) old_name = item.name old_transport = item.transport old_url_path = item.url_path old_soap_action = item.soap_action old_http_method = item.method old_http_accept = opaque.get('http_accept') session.delete(item) session.commit() if item.connection == CONNECTION.CHANNEL: action = CHANNEL.HTTP_SOAP_DELETE.value else: action = OUTGOING.HTTP_SOAP_DELETE.value self.notify_worker_threads( { 'id': self.request.input.id, 'name': old_name, 'transport': old_transport, 'old_url_path': old_url_path, 'old_soap_action': old_soap_action, 'old_http_method': old_http_method, 'old_http_accept': old_http_accept, }, action) except Exception: session.rollback() self.logger.error('Object could not be deleted, e:`%s`', format_exc()) raise
def handle(self): # If we have a rate limiting definition, let's check it upfront DefinitionParser.check_definition_from_input(self.request.input) input = self.request.input input.sec_use_rbac = input.get('sec_use_rbac') or (input.security_id == ZATO_SEC_USE_RBAC) input.security_id = input.security_id if input.security_id not in (ZATO_NONE, ZATO_SEC_USE_RBAC) else None input.soap_action = input.soap_action if input.soap_action else '' if input.content_encoding and input.content_encoding != 'gzip': raise Exception('Content encoding must be empty or equal to `gzip`') with closing(self.odb.session()) as session: existing_one = session.query(HTTPSOAP.id).\ filter(HTTPSOAP.cluster_id==input.cluster_id).\ filter(HTTPSOAP.id!=input.id).\ filter(HTTPSOAP.name==input.name).\ filter(HTTPSOAP.connection==input.connection).\ filter(HTTPSOAP.transport==input.transport).\ first() if existing_one: raise Exception('An object of that input.name:`{}` already exists in this cluster ' \ '(input.connection:`{}` input.transport:`{}` input.id:`{}` existing_one.id:`{}`)'.format( input.name, input.connection, input.transport, input.id, existing_one.id)) # Is the service's name correct? service = session.query(Service).\ filter(Cluster.id==input.cluster_id).\ filter(Service.cluster_id==Cluster.id).\ filter(Service.name==input.service).first() if input.connection == CONNECTION.CHANNEL and not service: msg = 'Service `{}` does not exist on this cluster'.format(input.service) self.logger.error(msg) raise Exception(msg) # Will raise exception if the security type doesn't match connection # type and transport sec_info = self._handle_security_info(session, input.security_id, input.connection, input.transport) # TLS data comes in combinations, i.e. certain elements are required only if TLS keys/certs are used self._validate_tls(input, sec_info) try: item = session.query(HTTPSOAP).filter_by(id=input.id).one() opaque = parse_instance_opaque_attr(item) old_name = item.name old_url_path = item.url_path old_soap_action = item.soap_action old_http_method = item.method old_http_accept = opaque.get('http_accept') item.name = input.name item.is_active = input.is_active item.host = input.host item.url_path = input.url_path item.security_id = input.security_id or None # So that SQLite does not reject '' item.connection = input.connection item.transport = input.transport item.cluster_id = input.cluster_id item.method = input.method item.soap_action = input.soap_action item.soap_version = input.soap_version or None item.data_format = input.data_format item.service = service item.ping_method = input.get('ping_method') or DEFAULT_HTTP_PING_METHOD item.pool_size = input.get('pool_size') or DEFAULT_HTTP_POOL_SIZE item.merge_url_params_req = input.get('merge_url_params_req') or False item.url_params_pri = input.get('url_params_pri') or URL_PARAMS_PRIORITY.DEFAULT item.params_pri = input.get('params_pri') or PARAMS_PRIORITY.DEFAULT item.serialization_type = input.get('serialization_type') or HTTP_SOAP_SERIALIZATION_TYPE.DEFAULT.id item.timeout = input.get('timeout') or MISC.DEFAULT_HTTP_TIMEOUT item.has_rbac = input.get('has_rbac') or input.sec_use_rbac or False item.content_type = input.get('content_type') item.sec_use_rbac = input.sec_use_rbac item.cache_id = input.cache_id or None item.cache_expiry = input.cache_expiry item.content_encoding = input.content_encoding sec_tls_ca_cert_id = input.get('sec_tls_ca_cert_id') item.sec_tls_ca_cert_id = sec_tls_ca_cert_id if sec_tls_ca_cert_id and sec_tls_ca_cert_id != ZATO_NONE else None # Opaque attributes set_instance_opaque_attrs(item, input) session.add(item) session.commit() if input.connection == CONNECTION.CHANNEL: input.impl_name = service.impl_name input.service_id = service.id input.service_name = service.name input.merge_url_params_req = item.merge_url_params_req input.url_params_pri = item.url_params_pri input.params_pri = item.params_pri cache = cache_by_id(session, input.cluster_id, item.cache_id) if item.cache_id else None if cache: input.cache_type = cache.cache_type input.cache_name = cache.name else: input.cache_type = None input.cache_name = None else: input.ping_method = item.ping_method input.pool_size = item.pool_size input.is_internal = item.is_internal input.old_name = old_name input.old_url_path = old_url_path input.old_soap_action = old_soap_action input.old_http_method = old_http_method input.old_http_accept = old_http_accept input.update(sec_info) if item.sec_tls_ca_cert_id and item.sec_tls_ca_cert_id != ZATO_NONE: self.add_tls_ca_cert(input, item.sec_tls_ca_cert_id) if input.connection == CONNECTION.CHANNEL: action = CHANNEL.HTTP_SOAP_CREATE_EDIT.value else: action = OUTGOING.HTTP_SOAP_CREATE_EDIT.value self.notify_worker_threads(input, action) self.response.payload.id = item.id self.response.payload.name = item.name except Exception: self.logger.error('Object could not be updated, e:`%s`', format_exc()) session.rollback() raise