Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
pre-commit-hooks/pre_commit_hooks/detect_aws_credentials.py at master · mkshgh/pre-commit-hooks · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mkshgh
/
pre-commit-hooks
Public
forked from
pre-commit/pre-commit-hooks
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
pre-commit-hooks
/
pre_commit_hooks
/
detect_aws_credentials.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
152 lines (126 loc) · 4.47 KB
master
Breadcrumbs
pre-commit-hooks
/
pre_commit_hooks
/
detect_aws_credentials.py
Copy path
Top
File metadata and controls
Code
Blame
152 lines (126 loc) · 4.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import
argparse
import
configparser
import
os
from
typing
import
List
from
typing
import
NamedTuple
from
typing
import
Optional
from
typing
import
Sequence
from
typing
import
Set
class
BadFile
(
NamedTuple
):
filename
:
str
key
:
str
def
get_aws_cred_files_from_env
()
->
Set
[
str
]:
"""Extract credential file paths from environment variables."""
return
{
os
.
environ
[
env_var
]
for
env_var
in
(
'AWS_CONFIG_FILE'
,
'AWS_CREDENTIAL_FILE'
,
'AWS_SHARED_CREDENTIALS_FILE'
,
'BOTO_CONFIG'
,
)
if
env_var
in
os
.
environ
}
def
get_aws_secrets_from_env
()
->
Set
[
str
]:
"""Extract AWS secrets from environment variables."""
keys
=
set
()
for
env_var
in
(
'AWS_SECRET_ACCESS_KEY'
,
'AWS_SECURITY_TOKEN'
,
'AWS_SESSION_TOKEN'
,
):
if
os
.
environ
.
get
(
env_var
):
keys
.
add
(
os
.
environ
[
env_var
])
return
keys
def
get_aws_secrets_from_file
(
credentials_file
:
str
)
->
Set
[
str
]:
"""Extract AWS secrets from configuration files.
Read an ini-style configuration file and return a set with all found AWS
secret access keys.
"""
aws_credentials_file_path
=
os
.
path
.
expanduser
(
credentials_file
)
if
not
os
.
path
.
exists
(
aws_credentials_file_path
):
return
set
()
parser
=
configparser
.
ConfigParser
()
try
:
parser
.
read
(
aws_credentials_file_path
)
except
configparser
.
MissingSectionHeaderError
:
return
set
()
keys
=
set
()
for
section
in
parser
.
sections
():
for
var
in
(
'aws_secret_access_key'
,
'aws_security_token'
,
'aws_session_token'
,
):
try
:
key
=
parser
.
get
(
section
,
var
).
strip
()
if
key
:
keys
.
add
(
key
)
except
configparser
.
NoOptionError
:
pass
return
keys
def
check_file_for_aws_keys
(
filenames
:
Sequence
[
str
],
keys
:
Set
[
bytes
],
)
->
List
[
BadFile
]:
"""Check if files contain AWS secrets.
Return a list of all files containing AWS secrets and keys found, with all
but the first four characters obfuscated to ease debugging.
"""
bad_files
=
[]
for
filename
in
filenames
:
with
open
(
filename
,
'rb'
)
as
content
:
text_body
=
content
.
read
()
for
key
in
keys
:
# naively match the entire file, low chance of incorrect
# collision
if
key
in
text_body
:
key_hidden
=
key
.
decode
()[:
4
].
ljust
(
28
,
'*'
)
bad_files
.
append
(
BadFile
(
filename
,
key_hidden
))
return
bad_files
def
main
(
argv
:
Optional
[
Sequence
[
str
]]
=
None
)
->
int
:
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'filenames'
,
nargs
=
'+'
,
help
=
'Filenames to run'
)
parser
.
add_argument
(
'--credentials-file'
,
dest
=
'credentials_file'
,
action
=
'append'
,
default
=
[
'~/.aws/config'
,
'~/.aws/credentials'
,
'/etc/boto.cfg'
,
'~/.boto'
,
],
help
=
(
'Location of additional AWS credential file from which to get '
'secret keys. Can be passed multiple times.'
),
)
parser
.
add_argument
(
'--allow-missing-credentials'
,
dest
=
'allow_missing_credentials'
,
action
=
'store_true'
,
help
=
'Allow hook to pass when no credentials are detected.'
,
)
args
=
parser
.
parse_args
(
argv
)
credential_files
=
set
(
args
.
credentials_file
)
# Add the credentials files configured via environment variables to the set
# of files to to gather AWS secrets from.
credential_files
|=
get_aws_cred_files_from_env
()
keys
:
Set
[
str
]
=
set
()
for
credential_file
in
credential_files
:
keys
|=
get_aws_secrets_from_file
(
credential_file
)
# Secrets might be part of environment variables, so add such secrets to
# the set of keys.
keys
|=
get_aws_secrets_from_env
()
if
not
keys
and
args
.
allow_missing_credentials
:
return
0
if
not
keys
:
print
(
'No AWS keys were found in the configured credential files and '
'environment variables.
\n
Please ensure you have the correct '
'setting for --credentials-file'
,
)
return
2
keys_b
=
{
key
.
encode
()
for
key
in
keys
}
bad_filenames
=
check_file_for_aws_keys
(
args
.
filenames
,
keys_b
)
if
bad_filenames
:
for
bad_file
in
bad_filenames
:
print
(
f'AWS secret found in
{
bad_file
.
filename
}
:
{
bad_file
.
key
}
'
)
return
1
else
:
return
0
if
__name__
==
'__main__'
:
exit
(
main
())
You can’t perform that action at this time.