def test_handles_error_on_API(self): error_message = factory.make_string() exception = MAASAPINotFound(error_message) response = self.process_exception(exception) self.assertIsInstance(response.content, bytes) self.assertEqual((http.client.NOT_FOUND, error_message), (response.status_code, response.content.decode(settings.DEFAULT_CHARSET)))
def test_handles_error_on_API(self): middleware = APIErrorsMiddleware() non_api_request = fake_request("/api/1.0/hello") error_message = factory.getRandomString() exception = MAASAPINotFound(error_message) response = middleware.process_exception(non_api_request, exception) self.assertEqual( (httplib.NOT_FOUND, error_message), (response.status_code, response.content))
def test_handles_error_on_API(self): middleware = APIErrorsMiddleware() api_request = factory.make_fake_request("/api/2.0/hello") error_message = factory.make_string() exception = MAASAPINotFound(error_message) response = middleware.process_exception(api_request, exception) self.assertIsInstance(response.content, bytes) self.assertEqual((http.client.NOT_FOUND, error_message), (response.status_code, response.content.decode(settings.DEFAULT_CHARSET)))
def get_node_for_mac(mac): """Identify node being queried based on its MAC address. This form of access is a security hazard, and thus it is permitted only on development systems where ALLOW_UNSAFE_METADATA_ACCESS is enabled. """ if not settings.ALLOW_UNSAFE_METADATA_ACCESS: raise PermissionDenied( "Unauthenticated metadata access is not allowed on this MAAS.") match = get_one(Interface.objects.filter(mac_address=mac)) if match is None: raise MAASAPINotFound() return match.node
def get_file_by_name(handler, request): """Get a named file from the file storage. :param filename: The exact name of the file you want to get. :type filename: string :return: The file is returned in the response content. """ filename = get_mandatory_param(request.GET, 'filename') try: db_file = FileStorage.objects.filter( owner=request.user, filename=filename).latest('id') except FileStorage.DoesNotExist: raise MAASAPINotFound("File not found") return HttpResponse(db_file.content, status=int(http.client.OK))
def read(self, request, version, item=None): check_version(version) # Requesting the list of attributes, not any particular attribute. if item is None or len(item) == 0: keys = sorted(self.data.keys()) # There's nothing in public-keys, so we don't advertise it. # But cloud-init does ask for it and it's not worth logging # a traceback for. keys.remove('public-keys') return make_list_response(keys) if item not in self.data: raise MAASAPINotFound("Unknown metadata attribute: %s" % item) return make_text_response(self.data[item])
def get_attribute_producer(self, item): """Return a callable to deliver a given metadata item. :param item: Sub-path for the attribute, e.g. "local-hostname" to get a handler that returns the logged-in node's hostname. :type item: unicode :return: A callable that accepts as arguments the logged-in node; the requested metadata version (e.g. "latest"); and `item`. It returns an HttpResponse. :rtype: Callable """ field = item.split('/')[0] if field not in self.fields: raise MAASAPINotFound("Unknown metadata attribute: %s" % field) producers = { 'local-hostname': self.local_hostname, 'instance-id': self.instance_id, 'public-keys': self.public_keys, } return producers[field]
def get_file_by_name(handler, request): """@description-title Get a named file @description Get a named file from the file storage. @param (string) "filename" [required=true] The name of the file. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the requested file. @success-example "success-json" [exkey=files-placeholder] placeholder text @error (http-status-code) "404" 404 @error (content) "not-found" The requested file is not found. @error-example "not-found" Not Found """ filename = get_mandatory_param(request.GET, "filename") try: db_file = FileStorage.objects.filter(owner=request.user, filename=filename).latest("id") except FileStorage.DoesNotExist: raise MAASAPINotFound("File not found") return HttpResponse(db_file.content, status=int(http.client.OK))
def test_ignores_error_outside_API(self): middleware = APIErrorsMiddleware() non_api_request = factory.make_fake_request("/middleware/api/hello") exception = MAASAPINotFound(factory.make_string()) self.assertIsNone( middleware.process_exception(non_api_request, exception))
def test_ignores_paths_outside_path_prefix(self): middleware = self.make_middleware(self.make_base_path()) request = factory.make_fake_request(self.make_base_path()) exception = MAASAPINotFound("Huh?") self.assertIsNone(middleware.process_exception(request, exception))
def not_found_handler(request): """An API handler that returns API 404 responses.""" raise MAASAPINotFound("Unknown API endpoint: %s." % request.path)