-
Notifications
You must be signed in to change notification settings - Fork 13
/
key.py
61 lines (38 loc) · 1.65 KB
/
key.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import logging
import os
import sys
def detectPublicKey(gpg, key_dir, pub_key):
logging.info('Detecting public key')
logging.debug('Detecting existing public key')
key_exists = os.path.isfile(key_dir)
logging.debug('Existing public key file exists? {}'.format(key_exists))
if not key_exists:
logging.info('Directory doesn\'t contain public.key trying to import')
if pub_key is None:
logging.error('Please specify public key for setup')
sys.exit(1)
logging.debug('Trying to import key')
public_import_result = gpg.import_keys(pub_key)
public_import_result.ok_reason
logging.debug(public_import_result)
if public_import_result.count != 1:
logging.error('Invalid public key provided, please provide 1 valid key')
sys.exit(1)
with open(key_dir, 'w') as key_file:
key_file.write(pub_key)
logging.info('Public key valid')
def importPrivateKey(gpg, sign_key):
logging.info('Importing private key')
private_import_result = gpg.import_keys(sign_key)
if private_import_result.count != 1:
logging.error('Invalid private key provided, please provide 1 valid key')
sys.exit(1)
logging.debug(private_import_result)
if not any(data['ok'] >= '16' for data in private_import_result.results):
logging.error('Key provided is not a secret key')
sys.exit(1)
private_key_id = private_import_result.results[0]['fingerprint']
logging.info('Private key valid')
logging.debug('Key id: {}'.format(private_key_id))
logging.info('-- Done importing key --')
return private_key_id