|
| 1 | +""" |
| 2 | +Copyright (C) 2022 Red Hat, Inc. (https://github.com/Commonjava/charon) |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | +from typing import List |
| 17 | + |
| 18 | +from charon.config import get_config, RadasConfig |
| 19 | +from charon.cmd.internal import ( |
| 20 | + _decide_mode, _safe_delete |
| 21 | +) |
| 22 | +from click import command, option, argument |
| 23 | + |
| 24 | +import traceback |
| 25 | +import logging |
| 26 | +import sys |
| 27 | +import datetime |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +@argument( |
| 33 | + "repo_url", |
| 34 | + type=str |
| 35 | +) |
| 36 | +@option( |
| 37 | + "--requester", |
| 38 | + "-r", |
| 39 | + help=""" |
| 40 | + The requester who sends the signing request. |
| 41 | + """ |
| 42 | +) |
| 43 | +@option( |
| 44 | + "--result_path", |
| 45 | + "-p", |
| 46 | + help=""" |
| 47 | + The path which will save the sign result file. |
| 48 | + """ |
| 49 | +) |
| 50 | +@option( |
| 51 | + "--ignore_patterns", |
| 52 | + "-i", |
| 53 | + multiple=True, |
| 54 | + help=""" |
| 55 | + The regex patterns list to filter out the files which should |
| 56 | + not be allowed to upload to S3. Can accept more than one pattern. |
| 57 | + """ |
| 58 | +) |
| 59 | +@option( |
| 60 | + "--work_dir", |
| 61 | + "-w", |
| 62 | + help=""" |
| 63 | + The temporary working directory into which archives should |
| 64 | + be extracted, when needed. |
| 65 | + """ |
| 66 | +) |
| 67 | +@option( |
| 68 | + "--config", |
| 69 | + "-c", |
| 70 | + help=""" |
| 71 | + The charon configuration yaml file path. Default is |
| 72 | + $HOME/.charon/charon.yaml |
| 73 | + """ |
| 74 | +) |
| 75 | +@option( |
| 76 | + "--sign_key", |
| 77 | + "-k", |
| 78 | + help=""" |
| 79 | + rpm-sign key to be used, will replace {{ key }} in default configuration for signature. |
| 80 | + Does noting if detach_signature_command does not contain {{ key }} field. |
| 81 | + """ |
| 82 | +) |
| 83 | +@option( |
| 84 | + "--debug", |
| 85 | + "-D", |
| 86 | + help="Debug mode, will print all debug logs for problem tracking.", |
| 87 | + is_flag=True, |
| 88 | + default=False |
| 89 | +) |
| 90 | +@option( |
| 91 | + "--quiet", |
| 92 | + "-q", |
| 93 | + help="Quiet mode, will shrink most of the logs except warning and errors.", |
| 94 | + is_flag=True, |
| 95 | + default=False |
| 96 | +) |
| 97 | +@command() |
| 98 | +def sign( |
| 99 | + repo_url: str, |
| 100 | + requester: str, |
| 101 | + result_path: str, |
| 102 | + ignore_patterns: List[str] = None, |
| 103 | + work_dir: str = None, |
| 104 | + config: str = None, |
| 105 | + sign_key: str = "redhatdevel", |
| 106 | + debug=False, |
| 107 | + quiet=False, |
| 108 | + dryrun=False |
| 109 | +): |
| 110 | + """Do signing against files in the repo zip in repo_url through |
| 111 | + radas service. The repo_url points to the maven zip repository |
| 112 | + in quay.io, which will be sent as the source of the signing. |
| 113 | + """ |
| 114 | + tmp_dir = work_dir |
| 115 | + logger.debug("%s", ignore_patterns) |
| 116 | + try: |
| 117 | + current = datetime.datetime.now().strftime("%Y%m%d%I%M") |
| 118 | + _decide_mode("radas_sign", current, is_quiet=quiet, is_debug=debug) |
| 119 | + if dryrun: |
| 120 | + logger.info("Running in dry-run mode, no files will signed.") |
| 121 | + conf = get_config(config) |
| 122 | + if not conf: |
| 123 | + logger.error("The charon configuration is not valid!") |
| 124 | + sys.exit(1) |
| 125 | + radas_conf = conf.get_radas_config() |
| 126 | + if not radas_conf or not radas_conf.validate(): |
| 127 | + logger.error("The configuration for radas is not valid!") |
| 128 | + sys.exit(1) |
| 129 | + sign_in_radas(repo_url, requester, sign_key, result_path, radas_conf) |
| 130 | + except Exception: |
| 131 | + print(traceback.format_exc()) |
| 132 | + sys.exit(2) # distinguish between exception and bad config or bad state |
| 133 | + finally: |
| 134 | + if not debug and tmp_dir: |
| 135 | + _safe_delete(tmp_dir) |
| 136 | + |
| 137 | + |
| 138 | +def sign_in_radas(repo_url: str, |
| 139 | + requester: str, |
| 140 | + sign_key: str, |
| 141 | + result_path: str, |
| 142 | + radas_config: RadasConfig): |
| 143 | + '''This function will be responsible to do the overall controlling of the whole process, |
| 144 | + like trigger the send and register the receiver, and control the wait and timeout there. |
| 145 | + ''' |
| 146 | + logger.debug("params. repo_url: %s, requester: %s, sign_key: %s, result_path: %s," |
| 147 | + "radas_config: %s", repo_url, requester, sign_key, result_path, radas_config) |
| 148 | + logger.info("Not implemented yet!") |
0 commit comments