Coverage for adm.py: 82%

50 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-05 20:41 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23import sys 

24from argparse import ArgumentParser 

25import logging 

26from ledger.hsm2dongle import HSM2DongleError 

27from admin.misc import not_implemented, info, AdminError 

28from admin.unlock import do_unlock 

29from admin.onboard import do_onboard 

30from admin.pubkeys import do_get_pubkeys 

31from admin.changepin import do_changepin 

32from admin.attestation import do_attestation 

33from admin.verify_attestation import do_verify_attestation 

34from admin.authorize_signer import do_authorize_signer 

35 

36DEFAULT_PIN_FILE = "pin.txt" 

37DEFAULT_PIN_CHANGE_FILE = "changePIN" 

38DEFAULT_ATT_UD_SOURCE = "https://public-node.rsk.co" 

39 

40 

41def main(): 

42 logging.disable(logging.CRITICAL) 

43 

44 actions = { 

45 "unlock": do_unlock, 

46 "onboard": do_onboard, 

47 "pubkeys": do_get_pubkeys, 

48 "changepin": do_changepin, 

49 "attestation": do_attestation, 

50 "verify_attestation": do_verify_attestation, 

51 "authorize_signer": do_authorize_signer, 

52 } 

53 

54 parser = ArgumentParser(description="powHSM Administrative tool") 

55 parser.add_argument("operation", choices=list(actions.keys())) 

56 parser.add_argument("-p", "--pin", dest="pin", help="PIN.") 

57 parser.add_argument( 

58 "-n", 

59 "--newpin", 

60 dest="new_pin", 

61 help="New PIN (only valid for 'changepin' operation).", 

62 ) 

63 parser.add_argument( 

64 "-a", 

65 "--anypin", 

66 dest="any_pin", 

67 action="store_const", 

68 help="Allow any pin (only valid for 'changepin' operation).", 

69 default=False, 

70 const=True, 

71 ) 

72 parser.add_argument( 

73 "-o", 

74 "--output", 

75 dest="output_file_path", 

76 help="Output file (only valid for 'onboard', 'pubkeys' and 'attestation' " 

77 "operations).", 

78 ) 

79 parser.add_argument( 

80 "-u", 

81 "--nounlock", 

82 dest="no_unlock", 

83 action="store_const", 

84 help="Do not attempt to unlock (only valid for 'changepin' and 'pubkeys' " 

85 "operations).", 

86 default=False, 

87 const=True, 

88 ) 

89 parser.add_argument( 

90 "-e", 

91 "--noexec", 

92 dest="no_exec", 

93 action="store_const", 

94 help="Do not attempt to execute the signer after unlocking (only valid for the " 

95 "'unlock' operation).", 

96 default=False, 

97 const=True, 

98 ) 

99 parser.add_argument( 

100 "-t", 

101 "--attcert", 

102 dest="attestation_certificate_file_path", 

103 help="Attestation key certificate file (only valid for 'attestation' and " 

104 "'verify_attestation' operations).", 

105 ) 

106 parser.add_argument( 

107 "-r", 

108 "--root", 

109 dest="root_authority", 

110 help="Root attestation authority (only valid for 'verify_attestation' " 

111 "operation). Defaults to Ledger's root authority.", 

112 ) 

113 parser.add_argument( 

114 "-b", 

115 "--pubkeys", 

116 dest="pubkeys_file_path", 

117 help="Public keys file (only valid for 'verify_attestation' operation).", 

118 ) 

119 parser.add_argument( 

120 "--attudsource", 

121 dest="attestation_ud_source", 

122 default=DEFAULT_ATT_UD_SOURCE, 

123 help="JSON-RPC endpoint used to retrieve the latest RSK block hash used " 

124 "as the user defined value for the attestation (defaults to " 

125 f"{DEFAULT_ATT_UD_SOURCE}). Can also specify a 32-byte hex string to use as" 

126 " the value.", 

127 ) 

128 parser.add_argument( 

129 "-z", 

130 "--signauth", 

131 dest="signer_authorization_file_path", 

132 help="Signer authorization file (only valid for 'authorize_signer' " 

133 "operations).", 

134 ) 

135 parser.add_argument( 

136 "-v", 

137 "--verbose", 

138 dest="verbose", 

139 action="store_const", 

140 help="Enable verbose mode", 

141 default=False, 

142 const=True, 

143 ) 

144 

145 try: 

146 options = parser.parse_args() 

147 actions.get(options.operation, not_implemented)(options) 

148 sys.exit(0) 

149 except AdminError as e: 

150 info(str(e)) 

151 sys.exit(1) 

152 except HSM2DongleError as e: 

153 info(str(e)) 

154 sys.exit(2) 

155 except KeyboardInterrupt: 

156 info("Interrupted by user!") 

157 sys.exit(3) 

158 except Exception as e: 

159 info(str(e)) 

160 sys.exit(4) 

161 

162 

163if __name__ == "__main__": 

164 main()