def prepare(self): app_log.debug("Incoming request from %s: %s", self.request.remote_ip, self.request) if settings['auth_enabled'] and self.request.method != "OPTIONS": is_authenticated = userauth.is_user_authenticated(self) else: is_authenticated = True if not is_authenticated: self.set_status(httplib.UNAUTHORIZED) self.set_header("Link", "/login") self.finish() else: self.resource_path = parse_url_path(self.request.path, self.schema, self.idl, self.request.method) if self.resource_path is None: self.set_status(httplib.NOT_FOUND) self.finish() else: #If Match support match = self.process_if_match() if not match: self.finish()
def verify_forward_reference(data, resource, schema, idl): """ converts the forward reference URIs to corresponding Row references Parameters: data - post/put data resource - Resource object being accessed schema = restparser schema object idl - ovs.db.idl.Idl object """ reference_keys = schema.ovs_tables[resource.table].references verified_references = {} # check for invalid keys for key in reference_keys: if key in data: category = reference_keys[key].category relation = reference_keys[key].relation if category != OVSDB_SCHEMA_CONFIG or \ relation == 'parent': error = "Invalid reference: %s" % key raise DataValidationFailed(error) for key in reference_keys: if key in data: # this is either a URI or list of URIs _refdata = data[key] notList = False if type(_refdata) is not types.ListType: notList = True _refdata = [_refdata] # check range _min = reference_keys[key].n_min _max = reference_keys[key].n_max if len(_refdata) < _min or len(_refdata) > _max: error = "Reference list is out of range for key %s" % key raise DataValidationFailed(error) references = [] for uri in _refdata: verified_resource = parse.parse_url_path(uri, schema, idl) if verified_resource is None: error = "Reference %s could not be identified" % uri raise DataValidationFailed(error) # get the Row instance of the reference we are adding while verified_resource.next is not None: verified_resource = verified_resource.next row = utils.get_row_from_resource(verified_resource, idl) references.append(row) if notList: references = references[0] verified_references[key] = references return verified_references
def create_subscription(self, subscription_name, subscription_row, resource_uri, idl): app_log.debug("Creating subscription for %s with URI %s" % (subscription_name, resource_uri)) resource = parse.parse_url_path(resource_uri, self._schema, idl) if resource is None: raise SubscriptionInvalidResource("Invalid resource URI " + resource_uri) # Get the subscription's URI subscriber_ref_col = \ utils.get_parent_column_ref(consts.SUBSCRIBER_TABLE, consts.SUBSCRIPTION_TABLE, self._schema) subscriber_row, unused_value = \ utils.get_parent_row(consts.SUBSCRIBER_TABLE, subscription_row, subscriber_ref_col, self._schema, idl) subscription_uri = utils.get_reference_uri(consts.SUBSCRIPTION_TABLE, subscription_row, self._schema, idl) # Get the last resource while preserving the parent resource. # None parent resource indicates the System table. parent_resource = None while resource.next is not None: parent_resource = resource resource = resource.next subscriber_name = self._get_subscriber_name(subscriber_row, idl) subscription = None if parent_resource and is_resource_type_collection(parent_resource): row_uuids = self.get_collection_row_uuids(parent_resource, idl) uris = yield get_collection_json(parent_resource, self._schema, idl, resource_uri, None, 0) if isinstance(uris, dict): uris = uris.values() rows_to_uri = dict(zip(row_uuids, uris)) subscription = CollectionSubscription(resource.table, subscriber_name, subscription_uri, resource_uri, rows_to_uri) else: subscription = RowSubscription(resource.table, subscriber_name, subscription_uri, resource_uri, resource.row) raise gen.Return(subscription)
def _verify_valid_resource_uri(self, subscriber_name, subscription_row, resource_uri, schema, idl): app_log.debug("Verifying a valid resource URI") resource_path = parse_url_path(resource_uri, schema, idl, REQUEST_TYPE_READ) if resource_path is None: app_log.debug("Invalid resource URI detected") details = "Subscriber: %s. " % subscriber_name details += "Invalid URI %s" % resource_uri raise ValidationError(error.VERIFICATION_FAILED, details)
def _get_row_from_uri(uri, schema, idl): verified_resource = parse.parse_url_path(uri, schema, idl) if verified_resource is None: error = "Reference %s could not be identified" % uri raise DataValidationFailed(error) # get the Row instance of the reference we are adding while verified_resource.next is not None: verified_resource = verified_resource.next row = utils.get_row_from_resource(verified_resource, idl) return row
def prepare(self): try: # Call parent's prepare to check authentication super(OVSDBAPIHandler, self).prepare() # Check ovsdb connection before each request if not self.ref_object.manager.connected: self.set_status(httplib.SERVICE_UNAVAILABLE) self.finish() self.resource_path = parse_url_path(self.request.path, self.schema, self.idl, self.request.method) if self.resource_path is None: self.set_status(httplib.NOT_FOUND) self.finish() else: filters = get_filters_args(self.request.query_arguments, self.schema, self.resource_path) if self.request.method != \ REQUEST_TYPE_READ and len(filters) > 0: raise ParameterNotAllowed("argument filter is only " "allowed in %s" % REQUEST_TYPE_READ) # If Match support match = yield self.process_if_match() app_log.debug("If-Match result: %s" % match) if not match: self.finish() if self.request.method == REQUEST_TYPE_CREATE \ or self.request.method == REQUEST_TYPE_UPDATE \ or self.request.method == REQUEST_TYPE_PATCH: if int(self.request.headers['Content-Length']) \ > MAX_BODY_SIZE: raise DataValidationFailed("Content-Length too long") except APIException as e: self.on_exception(e) self.finish() except Exception, e: self.on_exception(e) self.finish()
def verify_referenced_by(data, resource, schema, idl): ''' subroutine to validate referenced_by uris/attribute JSON { "referenced_by": [ { "uri": "URI1", "attributes": [ "a", "b" ] }, { "uri": "URI2" }, { "uri": "URI3", "attributes":[] } ] } ''' table = resource.table verified_referenced_by = {OVSDB_SCHEMA_REFERENCED_BY: []} for item in data: uri = item['uri'] attributes = None if 'attributes' in item: attributes = item['attributes'] # verify URI uri_resource = parse.parse_url_path(uri, schema, idl, REQUEST_TYPE_CREATE) if uri_resource is None: error = "referenced_by resource error" raise DataValidationFailed(error) # go to the last resource while uri_resource.next is not None: uri_resource = uri_resource.next if uri_resource.row is None: app_log.debug('uri: ' + uri + ' not found') error = "referenced_by resource error" raise DataValidationFailed(error) # attributes references = schema.ovs_tables[uri_resource.table].references reference_keys = references.keys() if attributes is not None and len(attributes) > 0: for attribute in attributes: if attribute not in reference_keys: error = "Attribute %s not found" % attribute raise DataValidationFailed(error) # check attribute is not a parent or child if references[attribute].relation is not 'reference': error = "Attribute should be a reference" raise DataValidationFailed(error) # if attribute list has only one element, make it a non-list to keep # it consistent with single attribute case (that need not be mentioned) if len(attributes) == 1: attributes = attributes[0] else: # find the lone attribute _found = False for key, value in references.iteritems(): if value.ref_table == table: if _found: error = "multiple attributes possible, specify one" raise DataValidationFailed(error) else: _found = True attributes = key # found the uri and attributes uri_resource.column = attributes verified_referenced_by[OVSDB_SCHEMA_REFERENCED_BY].append(uri_resource) return verified_referenced_by
def verify_referenced_by(data, resource, schema, idl): ''' subroutine to validate referenced_by uris/attribute JSON { "referenced_by": [ { "uri": "URI1", "attributes": [ "a", "b" ] }, { "uri": "URI2" }, { "uri": "URI3", "attributes":[] } ] } ''' table = resource.table verified_referenced_by = {OVSDB_SCHEMA_REFERENCED_BY: []} for item in data: uri = item['uri'] attributes = None if 'attributes' in item: attributes = item['attributes'] # verify URI uri_resource = parse.parse_url_path(uri, schema, idl, REQUEST_TYPE_CREATE) if uri_resource is None: error = "referenced_by resource error" raise DataValidationFailed(error) # go to the last resource while uri_resource.next is not None: uri_resource = uri_resource.next if uri_resource.row is None: app_log.debug('uri: ' + uri + ' not found') error = "referenced_by resource error" raise DataValidationFailed(error) # attributes references = uri_resource.keys[OVSDB_SCHEMA_REFERENCE] reference_keys = references.keys() if attributes is not None and len(attributes) > 0: for attribute in attributes: if attribute not in reference_keys: error = "Attribute %s not found" % attribute raise DataValidationFailed(error) # check attribute is not a parent or child if references[attribute].relation is not 'reference': error = "Attribute should be a reference" raise DataValidationFailed(error) # if attribute list has only one element, make it a non-list to keep # it consistent with single attribute case (that need not be mentioned) if len(attributes) == 1: attributes = attributes[0] else: # find the lone attribute _found = False for key, value in references.iteritems(): if value.ref_table == table: if _found: error = "multiple attributes possible, specify one" raise DataValidationFailed(error) else: _found = True attributes = key # found the uri and attributes uri_resource.column = attributes verified_referenced_by[OVSDB_SCHEMA_REFERENCED_BY].append(uri_resource) return verified_referenced_by