[ff039b1] | 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 os |
---|
| 19 | import mgstest.softhsm |
---|
| 20 | import shutil |
---|
| 21 | from pathlib import Path |
---|
| 22 | |
---|
| 23 | if __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_key_label) |
---|
| 49 | if args.certificate: |
---|
| 50 | token.store_cert(args.certificate, mgstest.softhsm.test_cert_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() |
---|