|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | + |
| 7 | +import click |
| 8 | +import requests |
| 9 | + |
| 10 | +from kcidev.libs.common import kci_err, kci_msg, kcidev_session |
| 11 | + |
| 12 | + |
| 13 | +def resolve_storage_config(cfg, instance, cli_url, cli_token): |
| 14 | + """ |
| 15 | + Resolve storage URL and token. Priority: |
| 16 | + 1. CLI flags (--storage-url, --storage-token) |
| 17 | + 2. Environment variables (KCI_STORAGE_URL, KCI_STORAGE_TOKEN) |
| 18 | + 3. Instance config (storage_url, storage_token in TOML) |
| 19 | +
|
| 20 | + Returns (storage_url, token) tuple. |
| 21 | + Raises click.Abort if no valid credentials found. |
| 22 | + """ |
| 23 | + # 1. CLI flags |
| 24 | + if cli_url or cli_token: |
| 25 | + if not cli_url or not cli_token: |
| 26 | + kci_err("Both --storage-url and --storage-token must be provided together") |
| 27 | + raise click.Abort() |
| 28 | + logging.debug(f"Using storage config from CLI flags: {cli_url}") |
| 29 | + return cli_url, cli_token |
| 30 | + |
| 31 | + # 2. Environment variables |
| 32 | + env_url = os.environ.get("KCI_STORAGE_URL") |
| 33 | + env_token = os.environ.get("KCI_STORAGE_TOKEN") |
| 34 | + if env_url or env_token: |
| 35 | + if env_url and env_token: |
| 36 | + logging.debug(f"Using storage config from env vars: {env_url}") |
| 37 | + return env_url, env_token |
| 38 | + kci_err( |
| 39 | + "Both KCI_STORAGE_URL and KCI_STORAGE_TOKEN env vars must be set together" |
| 40 | + ) |
| 41 | + raise click.Abort() |
| 42 | + |
| 43 | + # 3. Instance config |
| 44 | + if cfg and instance and instance in cfg: |
| 45 | + inst_cfg = cfg[instance] |
| 46 | + storage_url = inst_cfg.get("storage_url") |
| 47 | + storage_token = inst_cfg.get("storage_token") |
| 48 | + if storage_url and storage_token: |
| 49 | + logging.debug( |
| 50 | + f"Using storage config from instance '{instance}': {storage_url}" |
| 51 | + ) |
| 52 | + return storage_url, storage_token |
| 53 | + |
| 54 | + kci_err( |
| 55 | + "No storage credentials found. Provide --storage-url and --storage-token, " |
| 56 | + "set KCI_STORAGE_URL/KCI_STORAGE_TOKEN env vars, " |
| 57 | + "or configure storage_url/storage_token in config file" |
| 58 | + ) |
| 59 | + raise click.Abort() |
| 60 | + |
| 61 | + |
| 62 | +def upload_file(storage_url, token, remote_path, local_file_path, timeout=120): |
| 63 | + """ |
| 64 | + Upload a file to kernelci-storage. |
| 65 | +
|
| 66 | + POST /v1/file with multipart form: |
| 67 | + - path: remote directory path |
| 68 | + - file0: file content |
| 69 | + """ |
| 70 | + url = storage_url.rstrip("/") + "/v1/file" |
| 71 | + headers = { |
| 72 | + "Authorization": f"Bearer {token}", |
| 73 | + } |
| 74 | + |
| 75 | + logging.info(f"Uploading {local_file_path} to {remote_path}/") |
| 76 | + logging.debug(f"POST request to: {url}") |
| 77 | + |
| 78 | + try: |
| 79 | + with open(local_file_path, "rb") as f: |
| 80 | + files = {"file0": (os.path.basename(local_file_path), f)} |
| 81 | + data = {"path": remote_path} |
| 82 | + response = kcidev_session.post( |
| 83 | + url, headers=headers, files=files, data=data, timeout=timeout |
| 84 | + ) |
| 85 | + logging.debug(f"Upload response status: {response.status_code}") |
| 86 | + except requests.exceptions.RequestException as e: |
| 87 | + logging.error(f"Upload request failed: {e}") |
| 88 | + kci_err(f"Storage connection error: {e}") |
| 89 | + raise click.Abort() |
| 90 | + |
| 91 | + if response.status_code == 200: |
| 92 | + logging.info(f"Upload successful: {os.path.basename(local_file_path)}") |
| 93 | + return response.text |
| 94 | + elif response.status_code == 401: |
| 95 | + kci_err("Authentication failed: invalid or expired token") |
| 96 | + raise click.Abort() |
| 97 | + else: |
| 98 | + kci_err(f"Upload failed (HTTP {response.status_code}): {response.text}") |
| 99 | + raise click.Abort() |
| 100 | + |
| 101 | + |
| 102 | +def check_auth(storage_url, token, timeout=30): |
| 103 | + """ |
| 104 | + Validate a JWT token against the storage server. |
| 105 | +
|
| 106 | + GET /v1/checkauth |
| 107 | + Returns the response text on success. |
| 108 | + """ |
| 109 | + url = storage_url.rstrip("/") + "/v1/checkauth" |
| 110 | + headers = { |
| 111 | + "Authorization": f"Bearer {token}", |
| 112 | + } |
| 113 | + |
| 114 | + logging.info("Checking storage authentication") |
| 115 | + logging.debug(f"GET request to: {url}") |
| 116 | + |
| 117 | + try: |
| 118 | + response = kcidev_session.get(url, headers=headers, timeout=timeout) |
| 119 | + logging.debug(f"Auth check response status: {response.status_code}") |
| 120 | + except requests.exceptions.RequestException as e: |
| 121 | + logging.error(f"Auth check request failed: {e}") |
| 122 | + kci_err(f"Storage connection error: {e}") |
| 123 | + raise click.Abort() |
| 124 | + |
| 125 | + if response.status_code == 200: |
| 126 | + logging.info(f"Authentication valid: {response.text}") |
| 127 | + return response.text |
| 128 | + elif response.status_code == 401: |
| 129 | + kci_err("Authentication failed: invalid or expired token") |
| 130 | + raise click.Abort() |
| 131 | + else: |
| 132 | + kci_err(f"Auth check failed (HTTP {response.status_code}): {response.text}") |
| 133 | + raise click.Abort() |
0 commit comments