Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache all root metadata versions #2767

Draft
wants to merge 12 commits into
base: develop
Choose a base branch
from
31 changes: 22 additions & 9 deletions examples/client/client
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import sys
import traceback
from hashlib import sha256
from pathlib import Path
from urllib import request

import requests

from tuf.api.exceptions import DownloadError, RepositoryError
from tuf.ngclient import Updater
Expand All @@ -29,18 +30,25 @@ def build_metadata_dir(base_url: str) -> str:

def init_tofu(base_url: str) -> bool:
"""Initialize local trusted metadata (Trust-On-First-Use) and create a
directory for downloads"""
directory for downloads

NOTE: This is unsafe and for demonstration only: the bootstrap root
should be deployed alongside your updater application
"""

metadata_dir = build_metadata_dir(base_url)

if not os.path.isdir(metadata_dir):
os.makedirs(metadata_dir)

root_url = f"{base_url}/metadata/1.root.json"
try:
request.urlretrieve(root_url, f"{metadata_dir}/root.json")
except OSError:
print(f"Failed to download initial root from {root_url}")
return False
data = requests.get(f"{base_url}/metadata/1.root.json").content
Updater(
metadata_dir=metadata_dir,
metadata_base_url=f"{base_url}/metadata/",
target_base_url=f"{base_url}/targets/",
target_dir=DOWNLOAD_DIR,
bootstrap=data,
)

print(f"Trust-on-First-Use: Initialized new root in {metadata_dir}")
return True
Expand Down Expand Up @@ -73,6 +81,9 @@ def download(base_url: str, target: str) -> bool:
os.mkdir(DOWNLOAD_DIR)

try:
# NOTE: initial root should be provided with ``bootstrap`` argument:
# This examples uses unsafe Trust-On-First-Use initialization so it is
# not possible here.
updater = Updater(
metadata_dir=metadata_dir,
metadata_base_url=f"{base_url}/metadata/",
Expand Down Expand Up @@ -104,7 +115,7 @@ def download(base_url: str, target: str) -> bool:
return True


def main() -> None:
def main() -> str | None:
"""Main TUF Client Example function"""

client_args = argparse.ArgumentParser(description="TUF Client Example")
Expand Down Expand Up @@ -169,6 +180,8 @@ def main() -> None:
else:
client_args.print_help()

return None


if __name__ == "__main__":
sys.exit(main())

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading