1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# SPDX-FileCopyrightText: 2024 Jade Lovelace
# SPDX-FileCopyrightText: 2026 Yureka Lilian <yureka@cyberchaos.dev>
# SPDX-License-Identifier: MIT
import argparse
import json
import sys
import datetime
import re
from typing import Any
import requests
import os
import logging

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

fmt = logging.Formatter(
    "{asctime} {levelname} {name}: {message}",
    datefmt="%b %d %H:%M:%S",
    style="{",
)

if not any(isinstance(h, logging.StreamHandler) for h in log.handlers):
    hand = logging.StreamHandler()
    hand.setFormatter(fmt)
    log.addHandler(hand)

API_BASE = os.environ.get("GARAGE_ADMIN_API_BASE", "http://localhost:3903")
API_KEY = os.environ["GARAGE_ADMIN_TOKEN"]

BUCKET_REGEX_STR = os.environ.get("BUCKET_REGEX", ".*")
BUCKET_REGEX = re.compile(BUCKET_REGEX_STR)


def api(method, endpoint: str, resp_json=True, **kwargs) -> Any:
    log.info("http %s %s", method, endpoint)
    if not endpoint.startswith("https"):
        endpoint = API_BASE + endpoint
    resp = requests.request(
        method,
        endpoint,
        headers={"Authorization": f"Bearer {API_KEY}"},
        **kwargs,
    )
    resp.raise_for_status()
    if resp_json:
        return resp.json()
    else:
        return resp


def get_bucket_id(bucket_name: str) -> str:
    resp: dict = api(
        "GET", "/v2/GetBucketInfo", params={"globalAlias": bucket_name}
    )
    return resp["id"]


DATEFMT = "%Y%m%d%H%M%S"


def do_new(args):
    for b in args.buckets:
        if not BUCKET_REGEX.match(b):
            print(f"Bucket {b} not in allowed buckeds '{BUCKET_REGEX_STR}'")
            exit(1)
    bucket_ids = [get_bucket_id(b) for b in args.buckets]

    key_name = args.name + "-" if args.name else ""
    expiration = datetime.datetime.now(tz=datetime.UTC) + datetime.timedelta(
        seconds=args.age_secs
    )
    key_name += "ephemeral-" + expiration.strftime(DATEFMT)

    key_resp: dict = api(
        "POST",
        "/v2/CreateKey",
        json={
            "name": key_name,
            "expiration": expiration.isoformat(),
            "neverExpires": False,
        },
    )

    for b in bucket_ids:
        api(
            "POST",
            "/v2/AllowBucketKey",
            json={
                "accessKeyId": key_resp["accessKeyId"],
                "bucketId": b,
                "permissions": {
                    "read": args.read,
                    "write": args.write,
                    "owner": args.owner,
                },
            },
        )

    print(
        json.dumps(
            {
                "name": key_resp["name"],
                "id": key_resp["accessKeyId"],
                "secret_key": key_resp["secretAccessKey"],
            },
            indent=2,
        )
    )


def main():
    ap = argparse.ArgumentParser(description="Garage ephemeral API keys tool")

    def fail(*args):
        ap.print_help()
        sys.exit(1)

    ap.set_defaults(cmd=fail)

    sps = ap.add_subparsers()

    new = sps.add_parser("new", help="Make an ephemeral key")
    new.add_argument("--name", help="Name prefix for the key")
    new.add_argument(
        "--read", action="store_true", help="Grant read access to buckets"
    )
    new.add_argument(
        "--write", action="store_true", help="Grant write access to buckets"
    )
    new.add_argument(
        "--owner", action="store_true", help="Grant owner access to buckets"
    )
    new.add_argument(
        "--age-secs",
        type=int,
        required=True,
        help="Maximum key lifetime in seconds",
    )
    new.add_argument("buckets", nargs="*", help="Buckets to grant access to")
    new.set_defaults(cmd=do_new)

    args = ap.parse_args()
    args.cmd(args)


if __name__ == "__main__":
    main()