forked from pypi/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace conflicting repository-service-tuf dep
Previously, `repository-service-tuf` (i.e. the RSTUF cli) was used to bootstrap an RSTUF repo for development. This PR re-implements the relevant parts of the cli locally in Warehouse and removes the `repository-service-tuf` dependency, which conflicts with other dependencies. Change details - Add lightweight RSTUF API client library (can be re-used for pypi#15815) - Add local `warehouse tuf bootstrap` cli subcommand, to wraps lib calls - Invoke local cli via `make inittuf` - Remove dependency supersedes pypi#15958 (cc @facutuesca @woodruffw) Signed-off-by: Lukas Puehringer <[email protected]>
- Loading branch information
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,3 @@ hupper>=1.9 | |
pip-tools>=1.0 | ||
pyramid_debugtoolbar>=2.5 | ||
pip-api | ||
repository-service-tuf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
|
||
import click | ||
|
||
from warehouse.cli import warehouse | ||
from warehouse.tuf import post_bootstrap, wait_for_success | ||
|
||
|
||
@warehouse.group() | ||
def tuf(): | ||
"""Manage TUF.""" | ||
|
||
|
||
@tuf.command() | ||
@click.argument("payload", type=click.File("rb"), required=True) | ||
@click.option("--api-server", required=True) | ||
def bootstrap(payload, api_server): | ||
"""Use payload file to bootstrap RSTUF server.""" | ||
task_id = post_bootstrap(api_server, json.load(payload)) | ||
wait_for_success(api_server, task_id) | ||
print(f"Bootstrap completed using `{payload.name}`. 🔐 🎉") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
RSTUF API client library | ||
""" | ||
|
||
import time | ||
|
||
from typing import Any | ||
|
||
import requests | ||
|
||
|
||
class RSTUFError(Exception): | ||
pass | ||
|
||
|
||
def get_task_state(server: str, task_id: str) -> str: | ||
resp = requests.get(f"{server}/api/v1/task?task_id={task_id}") | ||
resp.raise_for_status() | ||
return resp.json()["data"]["state"] | ||
|
||
|
||
def post_bootstrap(server: str, payload: Any) -> str: | ||
resp = requests.post(f"{server}/api/v1/bootstrap", json=payload) | ||
resp.raise_for_status() | ||
|
||
# TODO: Ask upstream to not return 200 on error | ||
resp_json = resp.json() | ||
resp_data = resp_json.get("data") | ||
if not resp_data: | ||
raise RSTUFError(f"Error in RSTUF job: {resp_json}") | ||
|
||
return resp_data["task_id"] | ||
|
||
|
||
def wait_for_success(server: str, task_id: str): | ||
"""Poll RSTUF task state API until success or error.""" | ||
|
||
retries = 20 | ||
delay = 1 | ||
|
||
for _ in range(retries): | ||
state = get_task_state(server, task_id) | ||
|
||
match state: | ||
case "SUCCESS": | ||
break | ||
|
||
case "PENDING" | "RUNNING" | "RECEIVED" | "STARTED": | ||
time.sleep(delay) | ||
continue | ||
|
||
case "FAILURE": | ||
raise RSTUFError("RSTUF job failed, please check payload and retry") | ||
|
||
case "ERRORED" | "REVOKED" | "REJECTED": | ||
raise RSTUFError("RSTUF internal problem, please check RSTUF health") | ||
|
||
case _: | ||
raise RSTUFError(f"RSTUF job returned unexpected state: {state}") | ||
|
||
else: | ||
raise RSTUFError("RSTUF job failed, please check payload and retry") |