Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from c4dt/release-candidate
Browse files Browse the repository at this point in the history
general clean-up
  • Loading branch information
PascalinDe authored Sep 15, 2023
2 parents e23e899 + 9d0f154 commit e7cee8b
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 264 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
max-line-length = 88
extend-ignore =
E203,
W503
20 changes: 20 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ on:
- main

jobs:
linter:
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
- 3.7
- 3.8
- 3.9
steps:
- uses: actions/checkout@v2
- name: Install linters
run: |
python -m pip install black flake8
- name: Check Python code formatting with Black
run: |
black --check --quiet .
- name: Lint Python code with Flake8
run: |
flake8
test-script:
runs-on: ubuntu-latest
strategy:
Expand Down
61 changes: 36 additions & 25 deletions tools/gen_dir_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
Automate the generation of a directory structure
"""

from argparse import (
ArgumentParser,
Namespace
)
from datetime import (
datetime,
timedelta
)
from argparse import ArgumentParser, Namespace
from datetime import datetime, timedelta
from pathlib import Path
from posix import R_OK
from typing import List

import os
Expand All @@ -29,7 +22,7 @@
generate_churninfo,
generate_customized_consensus,
InvalidConsensus,
LOGGER
LOGGER,
)

DIRNAME_AUTHORITY_PRIVATE = "private"
Expand All @@ -56,7 +49,8 @@
# Minimal validity dUration of the customized consensus.
CONSENSUS_VALIDITY_DAYS = 14

# Feel free to modify these values as you want, they are used to fill fields in the certificate
# Feel free to modify these values as you want,
# they are used to fill fields in the certificate
# and customized consensus but do not impact on Lightarti-rest behavior.
AUTHORITY_NAME = "Center for Digital Trust"
AUTHORITY_HOSTNAME = "c4dt.org"
Expand Down Expand Up @@ -93,7 +87,8 @@ def ensure_dir_rw_access(directory: Path) -> None:
Ensures that a directory exists and that we have read/write access to it.
:param directory: directory we want to ensure to have these properties
:raises InconsistentDirectoryStructure: The directory does not have these properties.
:raises InconsistentDirectoryStructure: The directory does not have
these properties.
"""
if not directory.exists():
directory.mkdir()
Expand All @@ -102,7 +97,9 @@ def ensure_dir_rw_access(directory: Path) -> None:
raise InconsistentDirectoryStructure(f"File {directory} should be a directory.")

if not os.access(directory, os.R_OK | os.W_OK):
raise InconsistentDirectoryStructure(f"Directory {directory} do not have enough permission.")
raise InconsistentDirectoryStructure(
f"Directory {directory} do not have enough permission."
)


def _ensure_file_access(filepath: Path, access_flags: int) -> None:
Expand All @@ -118,7 +115,9 @@ def _ensure_file_access(filepath: Path, access_flags: int) -> None:
raise InconsistentDirectoryStructure(f"File {filepath} does not exists.")

if not os.access(filepath, access_flags):
raise InconsistentDirectoryStructure(f"File {filepath} do not have enough permission.")
raise InconsistentDirectoryStructure(
f"File {filepath} do not have enough permission."
)


def ensure_file_r_access(filepath: Path) -> None:
Expand Down Expand Up @@ -147,7 +146,8 @@ def ensure_valid_authority_dir(dir_auth: Path, date_utc: datetime) -> None:
:param dir_auth: directory containing or to contain the authority files
:param date_utc: date for which the authority should be valid (in UTC)
:raises InconsistentDirectoryStructure: At least one of the authority file is invalid.
:raises InconsistentDirectoryStructure: At least one of the authority file
is invalid.
"""
if not dir_auth.exists():
LOGGER.info(f"Create new authority in {dir_auth}.")
Expand All @@ -163,15 +163,19 @@ def ensure_valid_authority_dir(dir_auth: Path, date_utc: datetime) -> None:
identity_key_path = dir_auth / DIRNAME_AUTHORITY_PRIVATE / FILENAME_AUTHORITY_KEY_ID
ensure_file_r_access(identity_key_path)

signing_key_path = dir_auth / DIRNAME_AUTHORITY_PRIVATE / FILENAME_AUTHORITY_KEY_SIGNATURE
signing_key_path = (
dir_auth / DIRNAME_AUTHORITY_PRIVATE / FILENAME_AUTHORITY_KEY_SIGNATURE
)
ensure_file_r_access(signing_key_path)

certificate_path = dir_auth / DIRNAME_AUTHORITY_PUBLIC / FILENAME_CERTIFICATE
ensure_file_r_access(certificate_path)
certificate_raw = certificate_path.read_bytes()
certificate = KeyCertificate(certificate_raw)

authority_path = dir_auth / DIRNAME_AUTHORITY_PUBLIC / FILENAME_AUTHORITY_FINGERPRINT
authority_path = (
dir_auth / DIRNAME_AUTHORITY_PUBLIC / FILENAME_AUTHORITY_FINGERPRINT
)
ensure_file_r_access(authority_path)

if certificate.expires <= date_utc:
Expand Down Expand Up @@ -203,7 +207,7 @@ def update_authority_dir(dir_auth: Path) -> None:
certificate_path,
authority_path,
AUTHORITY_NAME,
CERTIFICATE_VALIDITY_MONTHS
CERTIFICATE_VALIDITY_MONTHS,
)


Expand All @@ -218,7 +222,9 @@ def create_current_dir(dir_auth: Path, dir_current: Path) -> None:

dir_current.mkdir()

signing_key_path = dir_auth / DIRNAME_AUTHORITY_PRIVATE / FILENAME_AUTHORITY_KEY_SIGNATURE
signing_key_path = (
dir_auth / DIRNAME_AUTHORITY_PRIVATE / FILENAME_AUTHORITY_KEY_SIGNATURE
)
certificate_path = dir_auth / DIRNAME_AUTHORITY_PUBLIC / FILENAME_CERTIFICATE
consensus_path = dir_current / FILENAME_CONSENSUS
microdescriptor_path = dir_current / FILENAME_MICRODESCRIPTORS
Expand All @@ -235,7 +241,7 @@ def create_current_dir(dir_auth: Path, dir_current: Path) -> None:
AUTHORITY_DIR_PORT,
AUTHORITY_OR_PORT,
AUTHORITY_CONTACT,
CONSENSUS_VALIDITY_DAYS
CONSENSUS_VALIDITY_DAYS,
)


Expand Down Expand Up @@ -274,7 +280,9 @@ def update_dir(dir_auth: Path, root: Path, date_utc: datetime) -> None:
potential_documents_dirs = list()

for dt_days in range(1, CONSENSUS_VALIDITY_DAYS):
dir_documents_name: str = (date_utc - timedelta(days=dt_days)).strftime("%Y%m%d")
dir_documents_name: str = (date_utc - timedelta(days=dt_days)).strftime(
"%Y%m%d"
)
potential_documents_dirs.append(dir_documents_name)

for dir_documents_name in potential_documents_dirs:
Expand All @@ -285,12 +293,15 @@ def update_dir(dir_auth: Path, root: Path, date_utc: datetime) -> None:
consensus = fetch_valid_consensus()
update_churn(dir_documents, consensus)
else:
raise InconsistentDirectoryStructure(f"Directory {dir_documents} is inconsistent.")
raise InconsistentDirectoryStructure(
f"Directory {dir_documents} is inconsistent."
)


def directory_structure(namespace: Namespace) -> None:
"""
Function to build or update a directory structure containing up-to-date network info.
Function to build or update a directory structure containing up-to-date network
info.
:param namespace: namespace containing the parsed arguments.
"""
Expand All @@ -317,13 +328,13 @@ def main(program: str, arguments: List[str]) -> None:
"--authority-directory",
help="Directory containing files related to the Tor directory authority.",
type=Path,
default="authority"
default="authority",
)
parser.add_argument(
"--root-directory",
help="Directory containing up-to-date documents for using Lightarti-rest.",
type=Path,
default="documents-public"
default="documents-public",
)

namespace = parser.parse_args(arguments)
Expand Down
Loading

0 comments on commit e7cee8b

Please sign in to comment.