Skip to content

Commit 5eb1f83

Browse files
committed
Add --file-pattern argument
1 parent 23f5d68 commit 5eb1f83

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ pip3 install find-gcp-keys
1818

1919
## Usage
2020

21-
As a command-line utility:
21+
### Command line
2222

2323
```
24-
find-gcp-keys <dir_path> [--no-validate/-n]
24+
find-gcp-keys <dir_path> [--no-validate/-n] [--file-pattern <regex>]
2525
```
2626

27-
As a library:
27+
Note that by default, the CLI only searches for the JSON key files
28+
matching a particular pattern (`<project-id>-<key-id>.json`). You can
29+
override this behavior, e.g. to search for _all_ JSON files:
30+
```
31+
find-gcp-keys <dir_path> -p '.*\.json'
32+
```
33+
34+
### Library:
2835

2936
```py
3037
from find_gcp_keys import find_key_paths, find_valid_keys, is_valid_key

find_gcp_keys/__main__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,25 @@ def parse_args():
2727
'--no-validate', '-n', action='store_true',
2828
help='Directory path to search recursively',
2929
)
30+
parser.add_argument(
31+
'--file-pattern', '-p', default=PROJECT_JSON_PATTERN,
32+
help='Pattern to match JSON file names against',
33+
)
3034
return parser.parse_args()
3135

3236

3337
# For requirements on GCP project IDs, see
3438
# https://cloud.google.com/resource-manager/docs/creating-managing-projects
3539
PROJECT_PATTERN = r"[a-z][a-z0-9\-]{4,28}[a-z0-9]"
36-
FILE_PATTERN = re.compile(PROJECT_PATTERN + r"-[0-9a-f]{12}\.json")
40+
PROJECT_JSON_PATTERN = PROJECT_PATTERN + r"-[0-9a-f]{12}\.json"
3741

3842

39-
def find_key_paths(dir_path: str):
40-
""" Finds files whose name matches the JSON SA key pattern """
43+
def find_key_paths(dir_path: str, file_pattern: str):
44+
""" Finds files whose name matches `file_pattern` """
45+
file_re = re.compile(file_pattern)
4146
for dirpath, _, files in os.walk(dir_path):
4247
for file in files:
43-
if FILE_PATTERN.match(file):
48+
if file_re.match(file):
4449
yield os.path.join(dirpath, file)
4550

4651

@@ -52,13 +57,13 @@ def is_valid_key(file_path: str):
5257
)
5358
credentials.refresh(google.auth.transport.requests.Request())
5459
return True
55-
except (ValueError, google.auth.exceptions.RefreshError):
60+
except (AttributeError, ValueError, google.auth.exceptions.RefreshError):
5661
return False
5762

5863

59-
def find_valid_keys(dir_path: str):
64+
def find_valid_keys(dir_path: str, file_pattern: str):
6065
""" Recursively walks `dir_path` and finds valid GCP SA keys """
61-
for path in find_key_paths(dir_path):
66+
for path in find_key_paths(dir_path, file_pattern):
6267
if is_valid_key(path):
6368
yield path
6469

@@ -68,12 +73,12 @@ def main():
6873
args = parse_args()
6974

7075
if args.no_validate:
71-
for path in find_key_paths(args.dir_path):
76+
for path in find_key_paths(args.dir_path, args.file_pattern):
7277
print(path)
7378
sys.exit(0)
7479

7580
found = False
76-
for path in find_valid_keys(args.dir_path):
81+
for path in find_valid_keys(args.dir_path, args.file_pattern):
7782
print(path, file=sys.stderr)
7883
found = True
7984

0 commit comments

Comments
 (0)