Reputation: 713
Please bare with me for a question for which it is nearly impossible to create a reproducible example.
I have an API setup with FastAPI using Docker, Serverless and deployed on AWS API Gateway. All routes discussed are protected with an api-key that is passed into the header (x-api-key
).
I'm trying to accomplish a simple redirect from one route to another using fastapi.responses.RedirectResponse
. The redirect works perfectly fine locally (though, this is without api-key), and both routes work perfectly fine when deployed on AWS and connected to directly, but something is blocking the redirect from route one (abc/item
) to route two (xyz/item
) when I deploy to AWS. I'm not sure what could be the issue, because the logs in CloudWatch aren't giving me much to work with.
To illustrate my issue let's say we have route abc/item
that looks like this:
@router.get("/abc/item")
async def get_item(item_id: int, request: Request, db: Session = Depends(get_db)):
if False:
redirect_url = f"/xyz/item?item_id={item_id}"
logging.info(f"Redirecting to {redirect_url}")
return RedirectResponse(redirect_url, headers=request.headers)
else:
execution = db.execute(text(items_query))
return convert_to_json(execution)
So, we check if some value is True
/False
and if it is False
we redirect from abc/item
to xyz/item
using RedirectResponse()
. We pass the redirect_url
, which is just the xyz/item
route including query parameters and we pass request.headers
(as suggested here and here), because I figured we need to pass along the x-api-key
to the new route. In the second route we again try a query in a different table (other_items
) and return some value.
I have also tried passing status_code=status.HTTP_303_SEE_OTHER
and status_code=status.HTTP_307_TEMPORARY_REDIRECT
to RedirectResponse()
as suggested by some tangentially related questions I found on StackOverflow and the FastAPI discussions, but that didn't help either.
@router.get("/xyz/item")
async def get_item(item_id: int, db: Session = Depends(get_db)):
execution = db.execute(text(other_items_query))
return convert_to_json(execution)
Like I said, when deployed I can successfully connect directly to both abc/item
and get a return value if True
and I can also connect to xyz/item
directly and get a correct value from that, but when I pass a value to abc/item
that is False
(and thus it should redirect) I get {"message": "Forbidden"}
.
In case it can be of any help, I try debugging this using a "curl" tool, and the headers I get returned give the following info:
Content-Type: application/json
Content-Length: 23
Connection: keep-alive
Date: Wed, 27 Jul 2022 08:43:06 GMT
x-amzn-RequestId: XXXXXXXXXXXXXXXXXXXX
x-amzn-ErrorType: ForbiddenException
x-amz-apigw-id: XXXXXXXXXXXXXXXX
X-Cache: Error from cloudfront
Via: 1.1 XXXXXXXXXXXXXXXXXXXXXXXXX.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: XXXXX
X-Amz-Cf-Id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
So, this is hinting at a CloudFront error. Unfortunately I don't see anything slightly hinting at this API when I look into my CloudFront dashboard on AWS, there literally is nothing there (I do have permissions to view the contents though...)
The API logs in CloudWatch look like this:
2022-07-27T03:43:06.495-05:00 Redirecting to /xyz/item?item_id=1234...
2022-07-27T03:43:06.495-05:00 [INFO] 2022-07-27T08:43:06.495Z Redirecting to /xyz/item?item_id=1234...
2022-07-27T03:43:06.496-05:00 2022-07-27 08:43:06,496 INFO sqlalchemy.engine.Engine ROLLBACK
2022-07-27T03:43:06.496-05:00 [INFO] 2022-07-27T08:43:06.496Z ROLLBACK
2022-07-27T03:43:06.499-05:00 END RequestId: 6f449762-6a60189e4314
2022-07-27T03:43:06.499-05:00 REPORT RequestId: 6f449762-6a60189e4314 Duration: 85.62 ms Billed Duration: 86 ms Memory Size: 256 MB Max Memory Used: 204 MB
I have been wondering if my issue could be related to something I need to add to somewhere in my serverless.yml
, perhaps in the functions:
part. That currently looks like this for these two routes:
events:
- http:
path: abc/item
method: get
cors: true
private: true
request:
parameters:
querystrings:
item_id: true
- http:
path: xyz/item
method: get
cors: true
private: true
request:
parameters:
querystrings:
item_id: true
Finally, it's probably good to note that I have added custom middleware to FastAPI to handle the two different database connections I need for connecting to other_items
and items
tables, though I'm not sure how relevant this is, considering this functions fine when redirecting locally. For this I implemented the solution found here. This custom middleware is the reason for the redirect in the first place (we change connection URI based on route with that middleware), so I figured it's good to share this bit of info as well.
Thanks!
Upvotes: 1
Views: 2977
Reputation: 34541
As noted here and here, it is mpossible to redirect to a page with custom headers set. A redirection in the HTTP
protocol doesn't support adding any headers to the target location. It is basically just a header in itself and only allows for a URL (a redirect response though could also include body content, if needed—see this answer). When you add the authorization
header to the RedirectResponse
, you only send that header back to the client.
A suggested here, you could use the set-cookie
HTTP response header:
The
Set-Cookie
HTTP response header is used to send a cookie from the server to the user agent (client), so that the user agent can send it back to the server later.
In FastAPI—documentation can be found here and here—this can be done as follows:
from fastapi import Request
from fastapi.responses import RedirectResponse
@app.get("/abc/item")
def get_item(request: Request):
redirect_url = request.url_for('your_endpoints_function_name') #e.g., 'get_item'
response = RedirectResponse(redirect_url)
response.set_cookie(key="fakesession", value="fake-cookie-session-value", httponly=True)
return response
Inside the other endpoint, where you are redirecting the user to, you can extract that cookie
to authenticate the user. The cookie can be found in request.cookies
—which should return, for example, {'fakesession': 'fake-cookie-session-value-MANUAL'}
—and you retrieve it using request.cookies.get('fakesession')
, as demonstrated in this answer.
On a different note, request.url_for()
function accepts only path
parameters, not query
parameters (such as item_id
in your /abc/item
and /xyz/item
endpoints). Thus, you can either create the URL in the way you already do, or use the CustomURLProcessor
suggested here, here and here, which allows you to pass both path
and query
parameters.
If the redirection takes place from one domain to another (e.g., from abc.com
to xyz.com
), please have a look at this answer.
Upvotes: 3