|
| 1 | +"""CLI: bootstrap a publishing bot's cryptographic identity. |
| 2 | +
|
| 3 | +Generates (or reuses) an RSA keypair and builds a **self-signed introduction** |
| 4 | +nanopublication that declares the bot agent and binds it to that key. By default |
| 5 | +it stops at signing — it does **not** publish and does **not** touch any secret |
| 6 | +store. That is deliberate: the private key is the bot's identity, so generating |
| 7 | +it locally/offline and reviewing the introduction before anything goes out (then |
| 8 | +adding the key as a CI secret by hand, once) is far safer than minting an |
| 9 | +identity inside automation. |
| 10 | +
|
| 11 | +Typical flow:: |
| 12 | +
|
| 13 | + pubmate-bootstrap-identity \\ |
| 14 | + --bot-name "Biochementity bot" --bot-id biochementity-bot \\ |
| 15 | + --owner-orcid https://orcid.org/0000-0002-1267-0234 --owner-name "Tobias Kuhn" \\ |
| 16 | + --generate-keys --output-dir ./bot-identity |
| 17 | +
|
| 18 | +Review ``bot-identity/introduction.trig``; if it looks right, publish it once |
| 19 | +(``--publish``) and add ``bot-identity/id_rsa`` as the repo's signing secret. |
| 20 | +""" |
| 21 | + |
| 22 | +import logging |
| 23 | +import pathlib |
| 24 | +import stat |
| 25 | + |
| 26 | +import click |
| 27 | + |
| 28 | +from nanopub import generate_keyfiles |
| 29 | +from pubmate.introduction import build_introduction |
| 30 | +from pubmate.utils import serialize_nanopub |
| 31 | + |
| 32 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +def _code(np_uri: str) -> str: |
| 37 | + return np_uri.rstrip("/").rsplit("/", 1)[-1] |
| 38 | + |
| 39 | + |
| 40 | +@click.command() |
| 41 | +@click.option("--bot-name", required=True, help="foaf:name for the agent, e.g. \"Biochementity bot\".") |
| 42 | +@click.option("--owner-orcid", required=True, help="ORCID URI of the human owner (frbr:owner + attribution).") |
| 43 | +@click.option("--bot-id", default="bot", show_default=True, help="Local URI segment for the agent (…/RA<code>/<bot-id>).") |
| 44 | +@click.option("--owner-name", help="Optional foaf:name for the owner ORCID.") |
| 45 | +@click.option("--output-dir", required=True, type=click.Path(file_okay=False, path_type=pathlib.Path)) |
| 46 | +@click.option("--generate-keys", is_flag=True, help="Generate a fresh RSA keypair into --output-dir (id_rsa / id_rsa.pub).") |
| 47 | +@click.option("--private-key", type=click.Path(exists=True, dir_okay=False), help="Existing private key (instead of --generate-keys).") |
| 48 | +@click.option("--public-key", type=click.Path(exists=True, dir_okay=False), help="Existing public key (with --private-key).") |
| 49 | +@click.option("--license", "license_", default=None, help="Override pubinfo license URI (default CC BY 4.0).") |
| 50 | +@click.option("--test-server", is_flag=True, help="Mark the introduction for the nanopub test server.") |
| 51 | +@click.option("--publish", is_flag=True, help="Publish the signed introduction to the network (default: sign only).") |
| 52 | +def cli( |
| 53 | + bot_name: str, |
| 54 | + owner_orcid: str, |
| 55 | + bot_id: str, |
| 56 | + owner_name: str | None, |
| 57 | + output_dir: pathlib.Path, |
| 58 | + generate_keys: bool, |
| 59 | + private_key: str | None, |
| 60 | + public_key: str | None, |
| 61 | + license_: str | None, |
| 62 | + test_server: bool, |
| 63 | + publish: bool, |
| 64 | +) -> None: |
| 65 | + """Generate a bot keypair + self-signed introduction nanopublication. |
| 66 | +
|
| 67 | + Signs offline by default and writes everything under --output-dir; nothing is |
| 68 | + published or stored as a secret unless you ask (--publish), so you can review |
| 69 | + the introduction first. |
| 70 | + """ |
| 71 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 72 | + |
| 73 | + if generate_keys: |
| 74 | + if private_key or public_key: |
| 75 | + raise click.UsageError("Pass either --generate-keys or --private-key/--public-key, not both.") |
| 76 | + priv_path = output_dir / "id_rsa" |
| 77 | + if priv_path.exists(): |
| 78 | + raise click.UsageError(f"{priv_path} already exists; refusing to overwrite an identity key.") |
| 79 | + generate_keyfiles(output_dir) |
| 80 | + priv_path.chmod(stat.S_IRUSR | stat.S_IWUSR) # 0600 |
| 81 | + priv, pub = priv_path, output_dir / "id_rsa.pub" |
| 82 | + logger.info("Generated keypair: %s (private, chmod 600) / %s", priv, pub) |
| 83 | + elif private_key and public_key: |
| 84 | + priv, pub = pathlib.Path(private_key), pathlib.Path(public_key) |
| 85 | + else: |
| 86 | + raise click.UsageError("Provide --generate-keys, or both --private-key and --public-key.") |
| 87 | + |
| 88 | + kwargs = {} if license_ is None else {"license": license_} |
| 89 | + np = build_introduction( |
| 90 | + private_key=priv, public_key=pub, bot_name=bot_name, |
| 91 | + owner_orcid=owner_orcid, bot_local_name=bot_id, owner_name=owner_name, |
| 92 | + test_server=test_server, **kwargs, |
| 93 | + ) |
| 94 | + np.sign() |
| 95 | + agent_uri = f"{np.metadata.np_uri}/{bot_id}" |
| 96 | + intro_path = output_dir / "introduction.trig" |
| 97 | + intro_path.write_text(serialize_nanopub(np), encoding="utf-8") |
| 98 | + |
| 99 | + if publish: |
| 100 | + np.publish() |
| 101 | + logger.info("Published introduction: %s", np.metadata.np_uri) |
| 102 | + logger.info("Wrote introduction -> %s (artifact code %s)", intro_path, _code(np.metadata.np_uri)) |
| 103 | + click.echo(f"\nBot agent URI : {agent_uri}") |
| 104 | + click.echo(f"Introduction : {np.metadata.np_uri}") |
| 105 | + click.echo(f"is_valid : {np.is_valid}") |
| 106 | + if not publish: |
| 107 | + click.echo( |
| 108 | + "\nNot published. Next: review introduction.trig, then publish it once " |
| 109 | + "(--publish) and add id_rsa as your signing secret (e.g. gh secret set …)." |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + cli() |
0 commit comments