Skip to content

Commit

Permalink
Merge pull request #190 from jku/probe-pkcs-module
Browse files Browse the repository at this point in the history
signer: Try to guess where libykcs11 could be
  • Loading branch information
jku authored Feb 16, 2024
2 parents a30ae35 + 65e049f commit 4873e28
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
7 changes: 4 additions & 3 deletions docs/SIGNER-SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ $ brew install swig

```
[settings]
# Path to PKCS#11 module
pykcs11lib = /usr/lib/x86_64-linux-gnu/libykcs11.so
# Path to PKCS#11 module (optional)
# If not provided, tuf-on-ci-sign will probe some known install locations
# pykcs11lib = /usr/lib/x86_64-linux-gnu/libykcs11.so
# GitHub username
user-name = @my-github-username
Expand All @@ -62,6 +63,6 @@ $ brew install swig
pull-remote = origin
# push-remote: If you are allowed to push to the TUF repository, you can use the same value
# as pull-remote. Otherwise use the rmeote name of your fork
# as pull-remote. Otherwise use the remote name of your fork
push-remote = origin
```
28 changes: 27 additions & 1 deletion signer/tuf_on_ci_sign/_user.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import logging
import os
import platform
import sys
from configparser import ConfigParser

import click
from securesystemslib.signer import Key, Signer

logger = logging.getLogger(__name__)

# some known locations where we might find libykcs11.
# These should all be _system_ locations (not user writable)
LIBYKCS11_LOCATIONS = {
"Linux": [
"/usr/lib/x86_64-linux-gnu/libykcs11.so",
"/usr/lib64/libykcs11.so",
"/usr/local/lib/libykcs11.so",
],
"Darwin": ["/opt/homebrew/lib/libykcs11.dylib", "/usr/local/lib/libykcs11.dylib"],
}


def bold(text: str) -> str:
return click.style(text, bold=True)
Expand All @@ -23,7 +39,6 @@ def __init__(self, path: str):
raise click.ClickException(f"Settings file {path} not found")
try:
self.name = self._config["settings"]["user-name"]
self.pykcs11lib = self._config["settings"]["pykcs11lib"]
self.push_remote = self._config["settings"]["push-remote"]
self.pull_remote = self._config["settings"]["pull-remote"]
except KeyError as e:
Expand All @@ -35,6 +50,17 @@ def __init__(self, path: str):
else:
self._signing_key_uris = {}

# probe for pykcs11lib if it's not set
self.pykcs11lib = self._config["settings"].get("pykcs11lib")
if self.pykcs11lib is None:
for loc in LIBYKCS11_LOCATIONS.get(platform.system(), []):
if os.path.exists(loc):
self.pykcs11lib = loc
break
if self.pykcs11lib is None:
raise click.ClickException("Failed to find libykcs11")
logger.debug("Using probed YKCS11 location %s", self.pykcs11lib)

# signer cache gets populated as they are used the first time
self._signers: dict[str, Signer] = {}

Expand Down

0 comments on commit 4873e28

Please sign in to comment.