-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisseminate_honeytokens.py
212 lines (174 loc) · 6.63 KB
/
disseminate_honeytokens.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import argparse
import json
import os
from common import (
GITGUARDIAN_SAAS,
BitBucketCloudNotSupportedError,
CredentialsValidationError,
HoneyTokenCreationError,
PullRequestCreationError,
PullRequestInfo,
RepositoryAccessError,
RepositoryInfo,
dashboard_to_api_url,
get_branch_name_from_commit_message,
get_saas_url_for_vcs,
)
from gg_client import GGClient
from utils import get_client_for_vcs, get_repository_client_from_vcs_client
def parse_vcs_url(vcs_url: str | None, vcs: str) -> str | None:
if not vcs_url:
vcs_url = os.getenv("VCS_URL")
if not vcs_url:
vcs_url = get_saas_url_for_vcs(vcs)
return vcs_url.strip("/")
def parse_gitguardian_url(gitguardian_url: str | None) -> str | None:
if not gitguardian_url:
gitguardian_url = os.getenv("GITGUARDIAN_URL")
if not gitguardian_url:
gitguardian_url = GITGUARDIAN_SAAS
return gitguardian_url.strip("/")
def validate_parameters(args: argparse.Namespace, parser: argparse.ArgumentParser):
try:
vcs_url = parse_vcs_url(args.vcs_url, args.vcs)
except BitBucketCloudNotSupportedError:
parser.error(
"BitBucket Cloud is not supported. Use VCS_URL to provide your BitBucket Server instance."
)
vcs_token = os.getenv("VCS_TOKEN")
if not vcs_token:
parser.error(
"VCS token is required. Configure it via environment variable VCS_TOKEN."
)
gitguardian_url = parse_gitguardian_url(args.gitguardian_url)
gitguardian_token = os.getenv("GITGUARDIAN_TOKEN")
if not gitguardian_token:
parser.error(
"GitGuardian access token is required. Configure it via environment variable GITGUARDIAN_TOKEN."
)
repos_names = []
for value in args.repo_names:
if "," in value:
repos_names.extend(value.split(","))
else:
repos_names.append(value)
return vcs_url, vcs_token, gitguardian_url, gitguardian_token, repos_names
def main():
parser = argparse.ArgumentParser(
description="Script to disseminate honeytokens in your repositories via pull requests."
)
parser.add_argument(
"--vcs",
choices=["github", "gitlab", "ado", "bitbucket"],
required=True,
help="Version control system",
)
parser.add_argument(
"--vcs-url",
help="VCS instance URL. If omitted, the default VCS URL will be used. "
"Can be also configured via environment variable VCS_URL.",
)
parser.add_argument(
"--gitguardian-url",
help="GitGuardian instance URL. If omitted, https://dashboard.gitguardian.com "
"will be used. Can be also configured via environment variable GITGUARDIAN_URL.",
)
parser.add_argument(
"--repo-names",
nargs="+",
required=True,
help="Comma-separated or space-separated list of repository names",
)
parser.add_argument(
"--output", choices=["json", "text"], default="text", help="Output format"
)
args = parser.parse_args()
(
vcs_url,
vcs_token,
gitguardian_url,
gitguardian_token,
repos_names,
) = validate_parameters(args, parser)
try:
vcs_client = get_client_for_vcs(args.vcs, vcs_url, vcs_token)
vcs_client.validate_credentials()
# validate access to all the repositories
vcs_repos = [vcs_client.get_repository_info(repo) for repo in repos_names]
gg_client = GGClient(dashboard_to_api_url(gitguardian_url), gitguardian_token)
gg_client.validate_credentials()
disseminate_honeytokens(vcs_client, gg_client, vcs_repos, args.output)
except (CredentialsValidationError, RepositoryAccessError) as error:
parser.error(error.message)
def disseminate_honeytokens(
vcs_client, gg_client: GGClient, repos: list[RepositoryInfo], output: str
):
result_output = dict()
for repo in repos:
result_output[repo.name] = {
"url": "",
"ok": True,
"error": "",
"honeytoken_id": "",
}
# 1. create a honeytoken with context
try:
error_msg = None
data = gg_client.create_honey_token_with_context(repo)
except HoneyTokenCreationError as error:
error_msg = error.message
except Exception as error:
error_msg = type(error).__name__ + ": " + str(error)
if error_msg:
result_output[repo.name]["ok"] = False
result_output[repo.name][
"error"
] = f"Failed to create a honeytoken: {error_msg}"
continue
# 2. create pull request
pr_info = PullRequestInfo(
content=data["content"],
commit_message=data["suggested_commit_message"],
branch=get_branch_name_from_commit_message(
data["suggested_commit_message"]
),
filename=data["filename"],
)
honey_token_id = data["honeytoken_id"]
result_output[repo.name]["honeytoken_id"] = honey_token_id
try:
error_msg = None
repo_client = get_repository_client_from_vcs_client(vcs_client, repo)
pull_request_url = repo_client.disseminate_in_pull_request(pr_info)
except PullRequestCreationError as error:
error_msg = error.message
except Exception as error:
error_msg = type(error).__name__ + ": " + str(error)
if error_msg:
ht_revoked = gg_client.revoke_honey_token(honey_token_id)
result_output[repo.name]["ok"] = False
result_output[repo.name]["error"] = (
f"Failed to disseminate honeytoken. {error_msg}. Honeytoken {honey_token_id} was created "
+ ("but revoked." if ht_revoked else "and not revoked.")
)
continue
result_output[repo.name]["url"] = pull_request_url
if output == "json":
print(json.dumps(result_output, indent=2))
else:
successful_results = [
repo for repo in result_output if result_output[repo]["ok"]
]
failed_results = [
repo for repo in result_output if not result_output[repo]["ok"]
]
if successful_results:
print("Honeytokens successfully disseminated in:")
for repo in successful_results:
print(f"{repo}: {result_output[repo]['url']}")
if failed_results:
print("Could not disseminate in:")
for repo in failed_results:
print(f"{repo}: {result_output[repo]['error']}")
if __name__ == "__main__":
main()