Regular expressions : How to exclude specific string inside a line - regex

I'm trying to filter out API call names using regex.
The problem is that I can't filter out specific strings from the API calls which I don't need.
In the following examples I need to filter out/cut from the API call which contains the string "sg-" (if exists) what comes after it including the string itself and the others leave untouched.
Here is the example:
GET /api/cloudsecuritygroup/sg-91f988f7/history - 443
GET /api/cloudsecuritygroup/sg-30333554 - 443
GET /api/cloudaccounts/secret - 443
GET /api/audit/event-types - 443
GET /api/user/me/iam-safe/devices - 443
The result should look like this:
cloudsecuritygroup
cloudsecuritygroup
cloudaccounts/secret
audit/event-types
user/me/iam-safe/devices

Maybe this is regex you are looking for:
(?<=GET \/api)(.+(?=\/sg-)|[^\s]+)
Check it out here

Related

How can I get configmap value where the key is dynamic in Kubernetes

In one of my deployment files, I want to set an environment variable. The variable is KUBE_VERSION and values must be fetched from a ConfigMap.
kube_1_21: 1.21.10_1550
This is part of ConfigMap where I want to set 1.21.10_1550 to KUBE_VERSION, but if the cluster is of IKS 1.20, then the key will be:
kube_1_20: 1.20.21_3456
kube_ is always static. How can I set environment variable using a regex expression?
Something of this sort:
- name: KUBE_VERSION
valueFrom:
configMapKeyRef:
name: cluster-info
key: "kube_1*"
As far as I know it is unfortunately not possible to use the regular expression as you would like. Additionally, you have information about the regular expression that validates the entered data:
regex used for validation is '[-._a-zA-Z0-9]+')
It follows that you have to enter key as an alphanumeric string and additionally you can use the characters -, _ and . So it is not possible to use regex in this place.
To workaround you can write your custom script i.e. in Bash and replace the proper line with sed command.

Spring Cloud Gateway: Rewriting URI in incoming requests

I am pretty new to Spring Cloud Gateway and rusty on my regex. I've googled around, but haven't found an answer to this. In fact, this is my first post on stackoverflow in a 20 yr career :)
I am trying to allow traffic into host1.com/enablement/requests and forward it to host2.com/enablement/request. Notice the lack of an s in the second URI. I haven't seen any examples of how to do this url rewrite. Is it because its a simple regex that I'm overlooking?
I've seen this in many examples. Can you explain what it is doing?
- RewritePath=/enablement/workflow(?<segment>/?.*), $\{segment}
spring:
cloud:
gateway:
routes:
- id: data_service_route
uri: host2.com/enablement
predicates:
- Path=/enablement/requests
- Method=POST,GET,PUT
filters:
- RewritePath=???
Any advice/example would be greatly appreciated.
Geoff
I've been playing around with spring-cloud-gateway last couple of days, let me try and explain:
RewritePath=/enablement/workflow(?<segment>/?.*), $\{segment}
There are 2 expressions here separated by the comma, a regex used for patterning matching and the replacement string.
(?<segment>/?.*) creates a named capturing group, the value will be stored under the key 'segment'
$\{segment} is the replacement string - in this example this will be the value of captured group segment. The backlash \ is required for escaping(yaml).
so /enablement/workflow/endpoint becomes /endpoint
A very basic code snippet below:
String regexp = "/enablement/requests/(?<path>.*)";
String replacement = "/enablement/request/${path}";
String path = "hostname:port/enablement/requests/endpoint";
System.out.println(path.replaceAll(regexp, replacement));
Below is a very useful link for refreshing regex:
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
Links to relevant spring-cloud-gateway classes below:
https://github.com/spring-cloud/spring-cloud-gateway/blob/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java
https://github.com/spring-cloud/spring-cloud-gateway/blob/main/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java

Regex capture group in Varnish VCL

I have a URL in the form of:
http://some-site.com/api/v2/portal-name/some/webservice/call
The data I want to fetch needs
http://portal-name.com/webservices/v2/some/webservice/call
(Yes I can rewrite the application so it uses other URL's but we are testing varnish at the moment so for now it cannot be intrusive.)
But I'm having trouble getting the URL correctly in varnish VCL. The api part is replaced by an empty string, no worries but now the portal-name.
Things I've tried:
if (req.url ~ ".*/(.*)/") {
set req.http.portalhostname = re.group.0;
set req.http.portalhostname = $1;
}
From https://docs.fastly.com/guides/vcl/vcl-regular-expression-cheat-sheet and Extracting capturing group contents in Varnish regex
And yes, std is imported.
But this gives me either a
Syntax error at
('/etc/varnish/default.vcl' Line 36 Pos 35)
set req.http.portalhostname = $1;
or a
Symbol not found: 're.group.0' (expected type STRING_LIST):
So: how can I do this? When I have extracted the portalhostname I should be able to simply do a regsub to replace that value with an empty string and then prepend "webservices" and my URL is complete.
The varnish version i'm using: varnish-4.1.8 revision d266ac5c6
Sadly re.group seems to have been removed at some version. Similar functionality appears to be accessible via one of several vmods. See https://varnish-cache.org/vmods/

express router not working with routes that include regex

I'm new to node and unable to create a simple route which will include regex as on of the parameter
// student.js - route file for route /student
app.get('/student/:/^[a-z0-9-]+$/', function(req,res){
res.send('student found');
});
when i hit localhost:3000/student/student-slug it says Cannot GET /student/student-slug
two more question
1) how to get param which is of regex, usually we can do this var _student = res.param.student_name but i'm unable to think for the regex
2) how to set optional param, let's say for pagination, route is like
/list/students/ will show list of last x student but /list/students/48 will offset that value to 48th row
this question may be duplicate but i'm unable to find answer
You need to encode the uri string before pass to request and decode it in your route handler.
Usage is very clear:
encodeURIComponent(str);
And for decoding use:
decodeURIComponent(str);
check the official documentation here
also do checkout this blog post on escape vs encode vs encodeURIComponent

How to configure Fiddler's Autoresponder to "map" a host to a folder?

I'm already using Fiddler to intercept requests for specific remote files while I'm working on them (so I can tweak them locally without touching the published contents).
i.e. I use many rules like this
match: regex:(?insx).+/some_file([?a-z0-9-=&]+\.)*
respond: c:\somepath\some_file
This works perfectly.
What I'd like to do now is taking this a step further, with something like this
match: regex:http://some_dummy_domain/(anything)?(anything)
respond: c:\somepath\(anything)?(anything)
or, in plain text,
Intercept any http request to 'some_dummy_domain', go inside 'c:\somepath' and grab the file with the same path and name that was requested originally. Query string should pass through.
Some scenarios to further clarify:
http://some_domain/somefile --> c:\somepath\somefile
http://some_domain/path1/somefile --> c:\somepath\path1\somefile
http://some_domain/path1/somefile?querystring --> c:\somepath\path1\somefile?querystring
I tried to leverage what I already had:
match: regex:(?insx).+//some_dummy_domain/([?a-z0-9-=&]+\.)*
respond: ...
Basically, I'm looking for //some_dummy_domain/ in requests. This seems to match correctly when testing, but I'm missing how to respond.
Can Fiddler use matches in responses, and how could I set this up properly ?
I tried to respond c:\somepath\$1 but Fiddler seems to treat it verbatim:
match: regex:(?insx).+//some_domain/([?a-z0-9-=&]+\.)*
respond: c:\somepath\$1
request: http://some_domain/index.html
response: c:\somepath\$1html <-----------
The problem is your use of insx at the front of your expression; the n means that you want to require explicitly-named capture groups, meaning that a group $1 isn't automatically created. You can either omit the n or explicitly name the capture group.
From the Fiddler Book:
Use RegEx Replacements in Action Text
Fiddler’s AutoResponder permits you to use regular expression group replacements to map text from the Match Condition into the Action Text. For instance, the rule:
Match Text: REGEX:.+/assets/(.*)
Action Text: http://example.com/mockup/$1
...maps a request for http://example.com/assets/Test1.gif to http://example.com/mockup/Test1.gif.
The following rule:
Match Text: REGEX:.+example\.com.*
Action Text: http://proxy.webdbg.com/p.cgi?url=$0
...rewrites the inbound URL so that all URLs containing example.com are passed as a URL parameter to a page on proxy.webdbg.com.
Match Text: REGEX:(?insx).+/assets/(?'fname'[^?]*).*
Action Text C:\src\${fname}
...maps a request for http://example.com/‌assets/img/1.png?bunnies to C:\src\‌img\‌1.png.