-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgit_notarize.py
469 lines (412 loc) · 14.5 KB
/
git_notarize.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env python3
# -*- mode:python; coding:utf-8; -*-
import argparse
import logging
import os
import re
import sys
from git import Repo
from git.exc import GitCommandError, InvalidGitRepositoryError
from immudb_wrapper import ImmudbWrapper
def create_parser():
parser = argparse.ArgumentParser(
"git_notarize.py",
description="Notarize commits in AlmaLinux git repositories",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'--immudb-username',
type=str,
help=(
'Provide your immudb username if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-password',
type=str,
help=(
'Provide your immudb password if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-database',
type=str,
help=(
'Provide your immudb database if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-address',
type=str,
help=(
'Provide your immudb address if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-public-key-file',
type=str,
help=(
'Provide your immudb public key file if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
"--local-git-repo",
help=(
"Path to a local AlmaLinux git source repository. "
"If not provided, uses the current working directory"
),
type=str,
required=False,
default=os.getcwd(),
)
parser.add_argument(
"--notarize-without-upstream-hash",
help=(
"Force notarization of AlmaLinux commit even when "
"there's no matching upstream tag"
),
action="store_true",
required=False,
)
parser.add_argument(
"--notarize-upstream-tag",
help=(
"Force notarization of upstream tag before "
"notarizing AlmaLinux commit"
),
action="store_true",
required=False,
)
parser.add_argument(
"--notarize-without-imported-source-notarization",
help=(
"Force notarization of upstream tag without an "
"imported source notarization"
),
action="store_true",
required=False,
)
parser.add_argument(
"--debug",
help=(
"Display debug information while running git_notarize.py. "
"This info could be useful when diagnosing a problem in the tool"
),
action="store_true",
required=False,
)
return parser
class GitRepo(Repo):
def get_branches(self):
branches = []
# remote branches
for ref in self.remotes.origin.refs:
if ref.remote_head not in branches and ref.remote_head != "HEAD":
branches.append(ref.remote_head)
return branches
def get_tags(self):
return [tag.name for tag in self.tags]
def get_current_commit(self):
return self.head.object
def get_current_tag(self):
current_tag = None
try:
current_tag = self.git.describe(
"--tags",
self.get_current_commit().hexsha,
)
except GitCommandError:
pass
return current_tag
def get_split_tag(self, name: str, tag: str):
tag_type, tag_distro, tag_nvr = tag.split("/")
# Let the user know that the tag doesn't follow AlmaLinux tag format.
# We do not fail as there are packages that don't include AlmaLinux
# branding, i.e.: kernel
if tag_type != "changed":
logging.warning(
"Current tag's type is '%s' and should be 'changed'",
tag_type,
)
if not tag_distro.startswith("a"):
logging.warning(
"Current tag's distro %s doesn't start with 'a'",
tag_distro,
)
alma_nvr = re.search(r"\.alma.*$", tag_nvr)
if not alma_nvr:
logging.warning(
"Current tag's nvr '%s' doesn't include 'alma' branding",
alma_nvr,
)
return tag_type, tag_distro, tag_nvr
def get_origin_url(self):
origin = self.remote("origin")
return list(origin.urls)[0]
def get_name(self):
return self.get_origin_url().split("/")[-1].replace(".git", "")
def find_matching_imports_tag(self, name: str, tag: str):
# Return tag if the source comes directly
# from CentOS sources
if tag.startswith('imports'):
return tag
# We want to find "imports" that match a given "changed" tag
# As an example, we will receive a "changed" tag like this:
# changed/a8-beta/anaconda-33.16.7.10-1.el8.alma
# And we want to find an "imports" tag that looks like:
# imports/c8-beta/anaconda-33.16.7.10-1.el8
debranded_tag = self.get_debranded_imports_tag(tag)
imports_tag = (
debranded_tag if debranded_tag in self.get_tags() else None
)
return imports_tag
def get_debranded_imports_tag(self, tag: str):
tag_type, tag_distro, tag_nvr = tag.split("/")
debranded_type = "imports"
# a8, a8s, a8s-beta, a8-stream ...
# We only need to replace the first character
debranded_distro = tag_distro.replace("a", "c", 1)
# this will get, i.e.: anaconda-33.16.7.10-1.el8
# Without AlmaLinux custom suffixes (.alma, .alma.2), if any
debranded_nvr = re.sub(r"\.alma.*$", "", tag_nvr)
debranded_tag = (debranded_type, debranded_distro, debranded_nvr)
return "/".join(debranded_tag)
def notarize(
immudb_wrapper: ImmudbWrapper,
repo_path: str,
upstream_commit_sbom_hash: str = None,
):
metadata = {
"sbom_api_ver": "0.2",
}
if upstream_commit_sbom_hash:
logging.info(
"Notarizing AlmaLinux source using the "
"upstream_commit_sbom_hash %s",
upstream_commit_sbom_hash,
)
metadata['upstream_commit_sbom_hash'] = upstream_commit_sbom_hash
result = immudb_wrapper.notarize_git_repo(
repo_path=repo_path,
user_metadata=metadata,
)
notarized_hash = result.get('value', {}).get('Hash')
return notarized_hash
def cli_main():
args = create_parser().parse_args()
logging.basicConfig(
format="%(levelname)-8s %(message)s",
level=logging.DEBUG if args.debug else logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[
logging.StreamHandler(),
],
)
logging.info("Starting git_notarize.py")
immudb_username, immudb_password = (
args.immudb_username or os.getenv('IMMUDB_USERNAME'),
args.immudb_password or os.getenv('IMMUDB_PASSWORD'),
)
if not immudb_username:
logging.error(
"No IMMUDB_USERNAME found, and it's required to operate with immudb. "
"You can either set it as an environmental variable or providing "
"it using the --immudb-username option."
)
sys.exit(1)
if not immudb_password:
logging.error(
"No IMMUDB_PASSWORD found, and it's required to operate with immudb. "
"You can either set it as an environmental variable or providing "
"it using the --immudb-password option."
)
sys.exit(1)
immudb_wrapper = ImmudbWrapper(
username=immudb_username,
password=immudb_password,
database=(
args.immudb_database
or os.getenv('IMMUDB_DATABASE')
or ImmudbWrapper.almalinux_database_name()
),
immudb_address=(
args.immudb_address
or os.getenv('IMMUDB_ADDRESS')
or ImmudbWrapper.almalinux_database_address()
),
public_key_file=(
args.immudb_public_key_file or os.getenv('IMMUDB_PUBLIC_KEY_FILE')
),
)
alma_repo_path = os.path.abspath(args.local_git_repo)
try:
alma_repo = GitRepo(alma_repo_path)
logging.info("Using git repository %s", alma_repo_path)
except InvalidGitRepositoryError:
logging.error("Current folder is not a git repository")
sys.exit(1)
logging.debug("Git repo name: %s", alma_repo.get_name())
logging.debug("Git origin url: %s", alma_repo.get_origin_url())
logging.debug("Git branches: %s", str(alma_repo.get_branches()))
logging.debug("Git tags: %s", str(alma_repo.get_tags()))
if alma_repo.head.is_detached:
logging.warning(
"The git repo is in DETACHED state. This means that if "
"git_notarize.py stops its execution, you will probably "
"need to manually return to this detached state before "
"running the tool again."
)
current_branch = None
else:
current_branch = alma_repo.active_branch.name
logging.info("Current branch is %s", current_branch)
current_commit = alma_repo.get_current_commit()
logging.debug(
"Current commit is:\n\n%s\nAuthor: %s <%s>\nDate: %s\n\t%s",
current_commit.hexsha,
current_commit.author.name,
current_commit.author.email,
current_commit.authored_datetime.strftime("%a %b %d %H:%M:%S %Y %z"),
current_commit.message,
)
current_tag = alma_repo.get_current_tag()
if current_tag:
logging.debug("Tag associated with current commit is: %s", current_tag)
else:
logging.error(
"Couldn't find any tag associated with current commit.\n"
"Please, create a tag of this change before keep going"
)
sys.exit(1)
tag_type, tag_distro, tag_nvr = alma_repo.get_split_tag(
alma_repo.get_name(), current_tag
)
if (
tag_type.startswith("imports")
and tag_distro.startswith("c")
and not args.notarize_without_imported_source_notarization
):
logging.error(
"Exiting as the current tag %s looks like an imported source\n"
"Imported sources should have been automatically "
"notarized at import time, and this tool is meant to help with "
"notarization of modified sources. Are you maybe in the wrong "
"branch/tag?\n"
"Use --notarize-without-imported-source-notarization if you "
"really want to notarize this AlmaLinux source without "
"an imported source notarization",
current_tag,
)
sys.exit(1)
logging.debug("Authenticating current tag %s", current_tag)
current_tag_is_authenticated = False
current_immudb_hash = None
try:
result = immudb_wrapper.authenticate_git_repo(
alma_repo_path,
)
current_tag_is_authenticated = result.get('verified', False)
current_immudb_hash = result.get('value', {}).get('Hash')
except Exception:
pass
if current_tag_is_authenticated:
logging.info(
"Current tag is already notarized, its Immudb hash is %s.\n"
"Nothing to do, exiting now.",
current_immudb_hash,
)
sys.exit(0)
matching_tag = None
if tag_type.startswith("changed"):
matching_tag = alma_repo.find_matching_imports_tag(
alma_repo.get_name(),
current_tag,
)
if not matching_tag:
logging.warning(
"Couldn't find a matching imports tag for %s",
current_tag,
)
if not args.notarize_without_upstream_hash:
logging.error(
"Use --notarize-without-upstream-hash if you really want to "
"notarize this AlmaLinux source without a corresponding "
"upstream Immudb hash"
)
sys.exit(1)
else:
logging.info(
"Notarizing tag without a corresponding upstream Immudb hash"
)
immudb_hash = notarize(immudb_wrapper, alma_repo_path)
logging.info(
"The tag %s has been notarized. Immudb hash: %s",
current_tag,
immudb_hash,
)
sys.exit(0)
else:
logging.info(
"Found a matching tag for %s: %s",
current_tag,
matching_tag,
)
# git checkout the matching tag
logging.debug("git checkout %s", matching_tag)
alma_repo.git.checkout(matching_tag)
matching_tag_is_authenticated = False
matching_immudb_hash = None
logging.debug("Authenticating matching tag %s", matching_tag)
try:
result = immudb_wrapper.authenticate_git_repo(
alma_repo_path,
)
matching_tag_is_authenticated = result.get('verified', False)
matching_immudb_hash = result.get('value', {}).get('Hash')
except Exception:
logging.warning(
"Couldn't authenticate the matching tag %s",
matching_tag,
)
if not matching_tag_is_authenticated:
if not args.notarize_upstream_tag:
logging.error(
"Use --notarize-upstream-tag to notarize the matching "
"tag and keep going with the notarization of the "
"AlmaLinux tag"
)
alma_repo.git.checkout(current_branch)
sys.exit(1)
else:
logging.info("Notarizing the matching upstream tag")
matching_immudb_hash = notarize(immudb_wrapper, alma_repo_path)
logging.info(
"The matching upstream tag %s has been notarized. "
"Immudb hash: %s",
matching_tag,
matching_immudb_hash,
)
# git checkout to the current_branch
alma_repo.git.checkout(current_branch)
alma_immudb_hash = notarize(
immudb_wrapper,
alma_repo_path,
matching_immudb_hash,
)
logging.info(
"The AlmaLinux tag %s has been notarized. Immudb hash: %s",
current_tag,
alma_immudb_hash,
)
if __name__ == "__main__":
cli_main()