Use a regex to keep the access logs with HTTP response 4** and 5** - graylog

I'm using Graylog to manage my server logs.
I would like to filter the apache logs access to keep the logs with a http response code 4** and 5**
So I would like to use a regex:
If I search /HTTP/ I have the corresponding logs like:
[...] "HEAD /register HTTP/1.1" 301 460 "-" [...]
But if I search /HTTP\//, I have no message. I also tried with /HTTP\\// but same result.
I tried with the regex /HTTP(?:.*?)"\s[4|5](?:\d{2})/ but no message found.
How to search a simple pattern like HTTP/ with a regex in Graylog ?
Thx

Why don't you use an extractor to map http-status-codes to fields.
Then you can easily filter and group your logs to find those with special codes.
Please see the following links
Extractors
How to use a JSON extractor

Related

FastAPI RedirectResponse gets {"message": "Forbidden"} when redirecting to a different route

Please bare with me for a question for which it's 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's 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!
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').
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.

Influxdb [[inputs.http]] - Status code 411 (Length Required)

Telegraf plugin:
HTTP Input Plugin
I'm trying to use telegraf to collect data from an vendor API.
test.conf file looks like this:
[[inputs.http]] urls = ["https://10.10.10.10"] method = "POST" body = '{"F_":"LOGIN","DATA":{"ID":"user","PWD":"password"}}'
When debugging i can see this is the error i get:
[inputs.http] Error in plugin: [url=https://10.10.10.10]: received status code 411 (Length Required), expected any value out of [200]
The API documentation for my vendor API states that the field "Content-Length" and "Host" are mandatory, but I can find now way to enable that in the plugin.
I have also tried using the http_response plugin, and I am able to get the JSON reply, but unfortuately i have not been able to find a good way to get the response_body_field JSON appended to the influxdb.
Does anybody know if either:
I can enable the two headers dynamicly(The POST lenght will vary)
or:
use the response_body_field from http_response and smoothly parse it to the influxdb?

does docpad secondary url redirect feature work?

Based on the documentation of docpad primary url, all requests to a document secondary url should be redirected to the primary url. But actually it respond the expected page directly when requesting any secondary urls without any redirection.
For example, you have a docpad document /src/documents/secondary-url.html.md like:
---
urls:
- '/my-secondary-urls1'
- '/my-secondary-urls2'
---
# primary url should be `secondary-url.html`
Then run command $ docpad run
It will responds status 200 when hitting either http://localhost:9778/my-secondary-urls1 or http://localhost:9778/my-secondary-urls2. While expected result is a redirect with status code 301 to http://localhost:9778/secondary-url.html
It seems an expected feature if checking this line of docpad code.
I'm curious if this is a defect or a deprecated feature?
BTW: I have a simple fix here which won't become a pull request until I read the contribution guide: https://github.com/shawnzhu/docpad/commit/731cdec43f9d9d155c8a8310494575d9746a065c
This was addressed in issue 850 of project docpad, and fixed in pull request 905, so further version than v6.70.1 of docpad will contain this fix.

How to make jmeter convert |(pipe) to %7C automatically?

I am getting following error whenever Jmeter gets a | (pipe) symbol in the URL since the "pipe" symbol is not allowed in URL. Is there any way to convert the |(pipes) in URL to %7C automatically?
Response code: Non HTTP response code: java.net.URISyntaxException
Assuming the pipe is in an existing parameter try the following:
Check "Encode?" box in "Send Parameters with the Request" input of HTTP Request
Wrap your variable in __urlEncode function as ${__urlencode(queryTerm)}
Use Beanshell Pre Processor as a child of HTTP Request with the following code:
vars.put("queryTerm", URLEncoder.encode(vars.get("queryTerm")));
This should convert the pipes automatically.
Got the instructions from here: Jmeter - Configure Keywords with spaces using CSV File Configuration

How to check if files exist from list of urls in Google Spreadsheet?

Assume that I have a list of URLs, each URL returns code 200 or 404.
How can I extract HTTP response code from these URLs?
Is there a function like importHTML, but it checks response code only?
|A |B |
-+-------------------------------+------------------------+
1|URL |response code |
-+-------------------------------+------------------------+
2|http://example.com/huge1.tar.gz|=importHTMLResponse($A2)|
3|http://example.com/huge2.tar.gz|=importHTMLResponse($A3)|
4|http://example.com/huge3.tar.gz|=importHTMLResponse($A4)|
...
You would have to right a proxy service to a known url then
importHTMLResponse(http://MYPROXYSERVER/reponsecode?url=$A2)
or something similar.
The proxy service would run what ever language you are comfortable with and just hit the supplied url returning the status code.

Resources