Compare commits
15 Commits
add_bom_ar
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
d5ae64321c | ||
b5a2b87568 | |||
|
d68685bf72 | ||
|
d9c09a5d58 | ||
a78784fee4 | |||
4ced19c78c | |||
|
211d4521c0 | ||
|
de5d91217f | ||
|
7093f13a10 | ||
|
49c4c97645 | ||
|
d2f8cdda42 | ||
|
e5fd2bf3b1 | ||
d6eca7c2e3 | |||
|
9bb706e2d0 | ||
f93f95fca5 |
@ -1,4 +1,3 @@
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
import json
|
||||
import logging
|
||||
import typing
|
||||
@ -14,16 +13,20 @@ class CasWrapper:
|
||||
|
||||
binary_name = 'cas'
|
||||
|
||||
@classmethod
|
||||
def _is_binary_present(cls):
|
||||
if cls.binary_name not in local:
|
||||
raise FileNotFoundError(
|
||||
'Binary CAS is not found in PATH on the machine',
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
cas_api_key: str,
|
||||
cas_signer_id: str,
|
||||
logger: logging.Logger = None,
|
||||
):
|
||||
if self.binary_name not in local:
|
||||
raise FileNotFoundError(
|
||||
'Binary CAS is not found in PATH on the machine',
|
||||
)
|
||||
self._is_binary_present()
|
||||
self._cas_api_key = cas_api_key
|
||||
self._cas_signer_id = cas_signer_id
|
||||
self._cas = local['cas']
|
||||
@ -31,6 +34,13 @@ class CasWrapper:
|
||||
if self._logger is None:
|
||||
self._logger = logging.getLogger()
|
||||
|
||||
@classmethod
|
||||
def get_version(cls):
|
||||
cls._is_binary_present()
|
||||
command = local['cas']['--version']
|
||||
version = command().split()[-1].split('v')[1]
|
||||
return version
|
||||
|
||||
def ensure_login(self):
|
||||
with local.env(
|
||||
CAS_API_KEY=self._cas_api_key,
|
||||
@ -52,7 +62,6 @@ class CasWrapper:
|
||||
"""
|
||||
command = self._cas[
|
||||
'notarize',
|
||||
'--bom',
|
||||
local_path,
|
||||
'-o',
|
||||
'json',
|
||||
@ -70,11 +79,36 @@ class CasWrapper:
|
||||
result_of_execution = command()
|
||||
return json.loads(result_of_execution)['hash']
|
||||
|
||||
def notarize_no_exc(
|
||||
self,
|
||||
local_path: str,
|
||||
metadata: typing.Dict = None,
|
||||
) -> typing.Tuple[bool, str]:
|
||||
"""
|
||||
Wrapper for avoiding raising exceptions during notarization.
|
||||
Return `success` flag instead for library user to react respectively.
|
||||
:param local_path: path to a local Git repo
|
||||
:param metadata: additional metadata
|
||||
:return: boolean flag for operation success and the hash
|
||||
of the notarized artifact.
|
||||
:rtype: tuple
|
||||
"""
|
||||
success = False
|
||||
try:
|
||||
cas_hash = self.notarize(local_path, metadata=metadata)
|
||||
success = True
|
||||
except Exception:
|
||||
self._logger.exception('Cannot notarize artifact: %s',
|
||||
local_path)
|
||||
cas_hash = ''
|
||||
return success, cas_hash
|
||||
|
||||
def authenticate(
|
||||
self,
|
||||
local_path: str,
|
||||
return_json: bool = False,
|
||||
use_hash: bool = False,
|
||||
signer_id: str = None,
|
||||
):
|
||||
"""
|
||||
Wrapper around `cas authenticate`
|
||||
@ -89,7 +123,9 @@ class CasWrapper:
|
||||
"""
|
||||
command_args = ['authenticate', local_path]
|
||||
if use_hash:
|
||||
command_args = ['authenticate', '--bom', '--hash', local_path]
|
||||
command_args = ['authenticate', '--hash', local_path]
|
||||
if signer_id:
|
||||
command_args.extend(('--signerID', signer_id))
|
||||
command_args.extend(('-o', 'json'))
|
||||
command = self._cas[command_args]
|
||||
try:
|
||||
@ -113,6 +149,7 @@ class CasWrapper:
|
||||
def authenticate_source(
|
||||
self,
|
||||
local_path: str,
|
||||
signer_id: str = None,
|
||||
) -> typing.Tuple[bool, typing.Optional[str]]:
|
||||
"""
|
||||
Authenticates source by git path.
|
||||
@ -120,9 +157,13 @@ class CasWrapper:
|
||||
"""
|
||||
is_authenticated = False
|
||||
commit_cas_hash = None
|
||||
self.ensure_login()
|
||||
try:
|
||||
result_json = self.authenticate(local_path, return_json=True)
|
||||
self._logger.error(result_json)
|
||||
result_json = self.authenticate(
|
||||
local_path,
|
||||
return_json=True,
|
||||
signer_id=signer_id
|
||||
)
|
||||
is_authenticated = result_json['verified']
|
||||
commit_cas_hash = result_json['hash']
|
||||
# we can fall with ProcessExecutionError,
|
||||
@ -135,17 +176,20 @@ class CasWrapper:
|
||||
self,
|
||||
local_path: str,
|
||||
use_hash: bool = False,
|
||||
signer_id: str = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Authenticates artifact by artifact path or hash if `use_hash` is True.
|
||||
Returns authenticate result.
|
||||
"""
|
||||
is_authenticated = False
|
||||
self.ensure_login()
|
||||
try:
|
||||
is_authenticated = self.authenticate(
|
||||
local_path,
|
||||
use_hash=use_hash,
|
||||
return_json=True,
|
||||
signer_id=signer_id
|
||||
)['verified']
|
||||
# we can fall with ProcessExecutionError,
|
||||
# because artifact can be not notarized
|
||||
@ -166,19 +210,19 @@ class CasWrapper:
|
||||
all_artifacts_is_notarized = True
|
||||
notarized_artifacts = {}
|
||||
self.ensure_login()
|
||||
with ThreadPoolExecutor(max_workers=4) as executor:
|
||||
futures = {
|
||||
executor.submit(self.notarize, artifact_path, metadata): artifact_path
|
||||
for artifact_path in artifact_paths
|
||||
}
|
||||
for future in as_completed(futures):
|
||||
artifact_path = futures[future]
|
||||
try:
|
||||
cas_artifact_hash = future.result()
|
||||
except Exception:
|
||||
self._logger.exception('Cannot notarize artifact: %s',
|
||||
artifact_path)
|
||||
all_artifacts_is_notarized = False
|
||||
continue
|
||||
notarized_artifacts[artifact_path] = cas_artifact_hash
|
||||
|
||||
# ALBS-576: We stopped doing this process in parallel due to the
|
||||
# problems experienced and described in this CAS issue:
|
||||
# https://github.com/codenotary/cas/issues/275
|
||||
# Hence, we decided to go sequential here until the problem is
|
||||
# resolved in CAS itself.
|
||||
for artifact_path in artifact_paths:
|
||||
try:
|
||||
cas_artifact_hash = self.notarize(artifact_path, metadata)
|
||||
except Exception:
|
||||
self._logger.exception('Cannot notarize artifact: %s',
|
||||
artifact_path)
|
||||
all_artifacts_is_notarized = False
|
||||
continue
|
||||
notarized_artifacts[artifact_path] = cas_artifact_hash
|
||||
return all_artifacts_is_notarized, notarized_artifacts
|
||||
|
Loading…
Reference in New Issue
Block a user