I am writing a python script that will validate AWS IAM policies and want to know how to utilize Regular expressions for this use case.
I have this policy for example:
{'Action': ['s3:PutObject', 'kms:GenerateKey'],
'Effect': 'Allow'
'Resource': ['arn:aws:kms:us-west-2:<account_id>:key/*',
'arn:aws:s3:::bucket]}
I then have this code:
for value in policy:
Resource = value['Resource']
for resource in Resources:
if resource == 'arn:aws:s3:::<bucket>'
return True
In the case there's multiple S3 actions that are emitted to the policy, I would like to do something like this....
if resource =='arn:aws:s3:::<regular expression>'
With the regular expression being any character & integer. Is this possible?
CodePudding user response:
you can do :
import re
if re.match(r'arn:aws:s3:::.*$', resource):
...
The .
means any character, *
means zero or more, $
means to the end of the string.
You can replace .
by [a-zA-Z0-9]
if you only want letter and number not symbols.
You can also replace the *
by
if you want at least one character (one or more)