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 | |
---|
18 | import mgstest.softhsm |
---|
19 | import shutil |
---|
20 | from pathlib import Path |
---|
21 | |
---|
22 | if __name__ == '__main__': |
---|
23 | import argparse |
---|
24 | parser = argparse.ArgumentParser( |
---|
25 | description='Initialize a SoftHSM test token') |
---|
26 | parser.add_argument('--token-dir', type=str, required=True, |
---|
27 | help='private key to store in the token') |
---|
28 | parser.add_argument('--privkey', type=str, required=True, |
---|
29 | help='private key to store in the token') |
---|
30 | parser.add_argument('--certificate', type=str, default=None, |
---|
31 | help='certificate to store in the token') |
---|
32 | |
---|
33 | # enable bash completion if argcomplete is available |
---|
34 | try: |
---|
35 | import argcomplete |
---|
36 | argcomplete.autocomplete(parser) |
---|
37 | except ImportError: |
---|
38 | pass |
---|
39 | |
---|
40 | args = parser.parse_args() |
---|
41 | |
---|
42 | softhsm_conf = mgstest.softhsm.tmp_softhsm_conf(args.token_dir) |
---|
43 | try: |
---|
44 | token = mgstest.softhsm.Token(config_file=softhsm_conf) |
---|
45 | token.reset_db() |
---|
46 | token.init_token() |
---|
47 | token.store_key(args.privkey, mgstest.softhsm.test_label) |
---|
48 | if args.certificate: |
---|
49 | token.store_cert(args.certificate, mgstest.softhsm.test_label) |
---|
50 | except: |
---|
51 | # Don't leave a half-done token around, the next make call |
---|
52 | # only checks the directory and would assume it's done. |
---|
53 | shutil.rmtree(args.token_dir) |
---|
54 | raise |
---|
55 | finally: |
---|
56 | Path(softhsm_conf).unlink() |
---|