source: mod_gnutls/test/softhsm-init.py @ b2546f0

asynciomainproxy-ticket
Last change on this file since b2546f0 was b2546f0, checked in by Fiona Klute <fiona.klute@…>, 3 years ago

mgstest.softhsm: Include type when searching object URLs

The p11tool documentation notes that some tokens require the same
label to be used for a certificate and its private key. That isn't the
case for SoftHSM, but I still want to support the case where a key
pair shares a label.

  • Property mode set to 100755
File size: 2.0 KB
Line 
1#!/usr/bin/python3
2# PYTHON_ARGCOMPLETE_OK
3
4# Copyright 2020 Fiona Klute
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18import os
19import mgstest.softhsm
20import shutil
21from pathlib import Path
22
23if __name__ == '__main__':
24    import argparse
25    parser = argparse.ArgumentParser(
26        description='Initialize a SoftHSM test token')
27    parser.add_argument('--token-dir', type=str, required=True,
28                        help='private key to store in the token')
29    parser.add_argument('--privkey', type=str, required=True,
30                        help='private key to store in the token')
31    parser.add_argument('--certificate', type=str, default=None,
32                        help='certificate to store in the token')
33
34    # enable bash completion if argcomplete is available
35    try:
36        import argcomplete
37        argcomplete.autocomplete(parser)
38    except ImportError:
39        pass
40
41    args = parser.parse_args()
42
43    softhsm_conf = mgstest.softhsm.tmp_softhsm_conf(args.token_dir)
44    try:
45        token = mgstest.softhsm.Token(config_file=softhsm_conf)
46        token.reset_db()
47        token.init_token()
48        token.store_key(args.privkey, mgstest.softhsm.test_label)
49        if args.certificate:
50            token.store_cert(args.certificate, mgstest.softhsm.test_label)
51    except:
52        # Don't leave a half-done token around, the next make call
53        # only checks the directory and would assume it's done.
54        shutil.rmtree(args.token_dir)
55        raise
56    finally:
57        Path(softhsm_conf).unlink()
Note: See TracBrowser for help on using the repository browser.