envoy how to configure query_parameters not exist match? - istio

"sign" not exist match. how to configure?
routes:
- match:
prefix: "/log"
query_parameters:
- name: "sign"
present_match: true

You can use queryParams of https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPMatchRequest and specify regex to not match your parameter

Related

Regex: yaml finding match

I am trying to get a match in regex for all root yaml entries with their values. So only with entries with a value are considered (not matching any nested entries). I have been messing around with it but to no avail. thanks!
so with this example:
metadata:
url: "https://www.google.com"
booleanvalue: 'false'
tls:
host:
google_net: "google.net"
secret:
big_secert_net: "cert"
API_HOST: 'https://api.test.com'
DOMAIN: 'api.domain'
METRIC_ENVIRONMENT: 'test'
Regex would return this match:
booleanvalue: 'false'
API_HOST: 'https://api.test.com'
DOMAIN: 'api.domain'
METRIC_ENVIRONMENT: 'test'
grep -E '^[^\r\n]+:[^\S\r\n]+[^[{\r\n][^\r\n]*$' test.yaml
What I'm doing:
^[^\r\n]+: Match key at the beginning of the line
[^\S\r\n]+ Match inline whitespace (some implementations provide \h for this). There must be at least one whitespace characters after the colon.
[^[{\r\n][^\r\n]*$ Match the content. Ensure it starts with something that is not [ or { on the same line (those would start nested YAML objects). Then, match everything until the end of the line.

My regex does not effective in spring-cloud-gateway

I'm learning spring-cloud-gateway, when I practice predicates, I want to try some regex like followings:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://www.google.com/
predicates:
- Cookie=token, hello*
I think hello* will matches hello,helloa,helloaaaaa..., but when I test by curl --cookies ..., it only matches hello, why helloa and helloaaaaa does not matches correctly?
Does regex in spring cloud application.yml need some changes?
I doesn't have anything to do with yaml or gateway just java regex
"helloaaaa".matches("hello*") // returns false.
But this works
"helloaaaa".matches("hello.*") // returns true.
Patter javadoc says X* matches X, zero or more times
So your regex would match "hellooooo"
"hellooooo".matches("hello*") // returns true.

Using regular expressions in Prometheus relabel_configs source_labels

I'm trying to save the meta labels retrieved from EC2 Service Discovery as target labels. I'm mostly concerned about the tags - every instance contains a lot of them and I would love to gather them all using one simple configuration entry with the usage of regular expression.
The perfect solution seems to be something like this:
relabel_configs:
- source_labels:
- '__meta_ec2_tag_(.*)'
target_label: '### use extracted regex group here'
Unfortunately, I get the following error:
\"__meta_ec2_tag_(.*)\" is not a valid label name"
Does that mean that I can't use regular expressions to describe source labels and that I have to specify each source label separately like in the sample below?
- source_labels:
- '__meta_ec2_tag_Name'
target_label: 'instance_name'
- source_labels:
- '__meta_ec2_tag_environment'
target_label: 'environment'
- source_labels:
- '__meta_ec2_tag_project'
target_label: 'project'
Try this:
relabel_configs:
- regex: '__meta_ec2_tag_(.*)'
replacement: $1
I just encountered the same problem but the previous answer didn't work for me with this error :
relabel configuration for replace action requires 'target_label' value
I found out that Prometheus now has a labelmap option for relabel_configs that does this : https://grafana.com/blog/2022/03/21/how-relabeling-in-prometheus-works/#labelmap
TLDR;
relabel_configs:
- action: labelmap
regex: "__meta_ec2_tag_(.*)"
replacement: "$1"

regex filter in replace module for ansible

Trying to get a regex replace in an ansible role for update autoscales going.
In my CFT I have the following mapping:
DevRegionSettings:
us-east-1:
primaryZone: us-east-1a
# secondaryZone: us-east-1b
# autoscale is wrong at point of instantiation
amiAutoscale: ami-234sefsrwerwer21
amiDB: ami-12313123
amiCoord: ami-12312312
amiWeb: ami-13123123
amiWorker: ami-12312312
I want to replace just the value of amiAutoscale with the latest ami that I find earlier on in the role.
I'm a regex noob and cannot figure it out for the life of me.
Been playing around with some of the regex from this thread:
Regex to match key in YAML
But still cant get it to do what I want :(
Any help would be appreciated!
The ansible task I had running was as follows:
- name: Replacing ami in the Dev Cloudformation Template
replace:
regexp: '(^\s*(?P<key>\w+_amiAutoscale):\s*(?P<value>\d+))'
replace: "{{ latest_ami.image_id }}"
path: "$path_to_cft.yaml"
So a couple of issues with your regex:
\w+_amiAutoscale - The line amiAutoscale: ami-234sefsrwerwer21 does not have an _ before amiAutoscale
(?P<value>\d+) - ami-234sefsrwerwer21 is not a sequence of digits.
This worked for me, but may be too open of a pattern: (^\s*(?P<key>amiAutoscale):\s*(?P<value>.+))
Example: https://regex101.com/r/76VGlJ/1
- name: Replacing ami in the Dev Cloudformation Template
replace:
regexp: '(^\s*(?P<key>amiAutoscale):\s*(?P<value>.+))'
replace: "{{ latest_ami.image_id }}"
path: "$path_to_cft.yaml"
Your regex does not match because the regex expect to match 1+ word characters followed by an underscore \w+_ right after the starting whitespace characters ^\s* which are not in the data.
Also, in the named capturing group (?P<value>\d+) you match 1+ digits which does not match ami-234sefsrwerwer21
What you also might do for your example data is to use only 2 capturing groups and a character class in the second group to specify what you would allow to match:
^\s*(?P<key>amiAutoscale)\s*:\s*(?P<value>[\w-]+)
Regex demo

How to specify case insensitive mode in ansible lineinfile regexp?

Simple question.
I'm trying to match "UseDns", "usedns" and other variations.
- name: Disable DNS checking on login (huge speedup)
sudo: true
lineinfile:
dest: "/etc/ssh/sshd_config"
regexp: "^[# \t]*[Uu][Ss][Ee][Dd][Nn][Ss] "
# how does one specify case insensitive regexp in lineinfile?
line: "UseDNS no"
state: "present"
create: true
insertafter: EOF
notify:
- sshd restart
Ansible uses Python re module. You can use inline modifiers, such as (?ism) in your pattern. Use the i for case-insensitive matching:
regexp: "(?i)^[# \t]*usedns "
Inline modifiers apply to the part of the regular experssion to the right of the modifier, and can be disabled with a - e.g. (?-i). This can be applied to implement case-insensitivity to only a part of a regular expression.
For example, the regex (?i)use(?-i)DNS should match useDNS and UseDNS, but not useDns or USEdns.