]>
git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.api/test_cryptokeys.py
6 from test_helper
import ApiTestCase
, is_auth
, pdnsutil
, unique_zone_name
8 @unittest.skipIf(not is_auth(), "Not applicable")
9 class Cryptokeys(ApiTestCase
):
12 super(Cryptokeys
, self
).setUp()
14 self
.zone
= unique_zone_name()
15 self
.zone_nodot
= self
.zone
[:-1]
19 'nameservers': ['ns1.example.com.', 'ns2.example.com.']
21 r
= self
.session
.post(
22 self
.url("/api/v1/servers/localhost/zones"),
23 data
=json
.dumps(payload
),
24 headers
={'content-type': 'application/json'})
25 self
.assert_success_json(r
)
26 self
.assertEquals(r
.status_code
, 201)
29 super(Cryptokeys
, self
).tearDown()
30 self
.remove_zone_key(self
.keyid
)
32 # Adding a key to self.zone using the pdnsutil command
33 def add_zone_key(self
, status
='inactive'):
34 return pdnsutil("add-zone-key", self
.zone_nodot
, "ksk", status
)
36 # Removes a key from self.zone by id using the pdnsutil command
37 def remove_zone_key(self
, key_id
):
38 return pdnsutil("remove-zone-key", self
.zone_nodot
, str(key_id
))
40 # This method tests the DELETE api call.
41 def test_delete(self
):
42 self
.keyid
= self
.add_zone_key()
44 #checks the status code. I don't know how to test explicit that the backend fail removing a key.
45 r
= self
.session
.delete(self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/"+self
.keyid
))
46 self
.assertEquals(r
.status_code
, 204)
47 self
.assertEquals(r
.content
, b
"")
49 # Check that the key is actually deleted
50 out
= pdnsutil("list-keys", self
.zone
)
51 self
.assertNotIn(self
.zone
, out
)
53 def test_get_wrong_zone(self
):
54 self
.keyid
= self
.add_zone_key()
55 r
= self
.session
.get(self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"fail/cryptokeys/"+self
.keyid
))
56 self
.assertEquals(r
.status_code
, 404)
58 def test_delete_wrong_id(self
):
59 self
.keyid
= self
.add_zone_key()
60 r
= self
.session
.delete(self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/1234567"))
61 self
.assertEquals(r
.status_code
, 404)
63 def test_delete_wrong_zone(self
):
64 self
.keyid
= self
.add_zone_key()
65 #checks for not covered zonename
66 r
= self
.session
.delete(self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"fail/cryptokeys/"+self
.keyid
))
67 self
.assertEquals(r
.status_code
, 404)
69 def test_delete_key_is_gone(self
):
70 self
.keyid
= self
.add_zone_key()
71 self
.remove_zone_key(self
.keyid
)
72 #checks for key is gone. Its ok even if no key had to be deleted. Or something went wrong with the backend.
73 r
= self
.session
.delete(self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/"+self
.keyid
))
74 self
.assertEquals(r
.status_code
, 404)
76 # Prepares the json object for Post and sends it to the server
77 def add_key(self
, content
='', type='ksk', active
='true', algo
='', bits
=None):
83 payload
['algorithm'] = algo
85 payload
['bits'] = bits
87 payload
['content'] = content
88 print("create key with payload:", payload
)
89 r
= self
.session
.post(
90 self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys"),
91 data
=json
.dumps(payload
),
92 headers
={'content-type': 'application/json'})
96 # Test POST for a positive result and delete the added key
97 def post_helper(self
, content
='', algo
='', bits
=None):
98 r
= self
.add_key(content
=content
, algo
=algo
, bits
=bits
)
99 self
.assert_success_json(r
)
100 self
.assertEquals(r
.status_code
, 201)
102 # Only a ksk added, so expected type is csk
103 self
.assertEquals(response
['keytype'], 'csk')
104 self
.keyid
= response
['id']
105 # Check if the key is actually added
106 out
= pdnsutil("list-keys", self
.zone_nodot
)
107 self
.assertIn(self
.zone_nodot
, out
)
109 # Test POST to add a key with default algorithm
113 # Test POST to add a key with specific algorithm number
114 def test_post_specific_number(self
):
115 self
.post_helper(algo
=10, bits
=1024)
117 # Test POST to add a key with specific name and bits
118 def test_post_specific_name_bits(self
):
119 self
.post_helper(algo
="rsasha256", bits
=2048)
121 # Test POST to add a key with specific name
122 def test_post_specific_name(self
):
123 self
.post_helper(algo
='ecdsa256')
125 # Test POST to add a private key from external resource
126 def test_post_content(self
):
127 self
.post_helper(content
="Private-key-format: v1.2\n"+
128 "Algorithm: 8 (RSASHA256)\n"+
129 "Modulus: 4GlYLGgDI7ohnP8SmEW8EBERbNRusDcg0VQda/EPVHU=\n"+
130 "PublicExponent: AQAB\n"+
131 "PrivateExponent: JBnuXF5zOtkjtSz3odV+Fk5UNUTTeCsiI16dkcM7TVU=\n"+
132 "Prime1: /w7TM4118RoSEvP8+dgnCw==\n"+
133 "Prime2: 4T2KhkYLa3w7rdK3Cb2ifw==\n"+
134 "Exponent1: 3aeKj9Ct4JuhfWsgPBhGxQ==\n"+
135 "Exponent2: tfh1OMPQKBdnU6iATjNR2w==\n"+
136 "Coefficient: eVrHe/kauqOewSKndIImrg==)\n")
138 def test_post_wrong_key_format(self
):
139 r
= self
.add_key(content
="trollololoooolll")
140 self
.assert_error_json(r
)
141 self
.assertEquals(r
.status_code
, 422)
142 self
.assertIn("Key could not be parsed. Make sure your key format is correct.",r
.json()['error'])
144 def test_post_wrong_keytype(self
):
145 r
= self
.add_key(type='sdfdhhgj')
146 self
.assert_error_json(r
)
147 self
.assertEquals(r
.status_code
, 422)
148 self
.assertIn("Invalid keytype",r
.json()['error'])
150 def test_post_wrong_bits_format(self
):
151 r
= self
.add_key(bits
='sdfdhhgj')
152 self
.assert_error_json(r
)
153 self
.assertEquals(r
.status_code
, 422)
154 self
.assertIn("'bits' must be a positive integer value",r
.json()['error'])
156 r
= self
.add_key(bits
='5.5')
157 self
.assert_error_json(r
)
158 self
.assertEquals(r
.status_code
, 422)
159 self
.assertIn("'bits' must be a positive integer value",r
.json()['error'])
161 r
= self
.add_key(bits
='-6')
162 self
.assert_error_json(r
)
163 self
.assertEquals(r
.status_code
, 422)
164 self
.assertIn("'bits' must be a positive integer value",r
.json()['error'])
166 def test_post_unsupported_algorithm(self
):
167 r
= self
.add_key(algo
='lkjhgf')
168 self
.assert_error_json(r
)
169 self
.assertEquals(r
.status_code
, 422)
170 self
.assertIn("Unknown algorithm:",r
.json()['error'])
172 def test_post_forgot_bits(self
):
173 r
= self
.add_key(algo
="rsasha256")
174 self
.assert_error_json(r
)
175 self
.assertEquals(r
.status_code
, 422)
176 self
.assertIn("key requires the size (in bits) to be passed", r
.json()['error'])
178 def test_post_wrong_bit_size(self
):
179 r
= self
.add_key(algo
=10, bits
=30)
180 self
.assert_error_json(r
)
181 self
.assertEquals(r
.status_code
,422)
182 self
.assertIn("The algorithm does not support the given bit size.", r
.json()['error'])
184 def test_post_can_not_guess_key_size(self
):
185 r
= self
.add_key(algo
=17)
186 self
.assert_error_json(r
)
187 self
.assertEquals(r
.status_code
,422)
188 self
.assertIn("Can not guess key size for algorithm", r
.json()['error'])
190 def test_put_activate_key(self
):
191 self
.keyid
= self
.add_zone_key()
196 r
= self
.session
.put(
197 self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/"+self
.keyid
),
198 data
=json
.dumps(payload
),
199 headers
={'content-type': 'application/json'})
200 self
.assertEquals(r
.status_code
, 204)
201 self
.assertEquals(r
.content
, b
"")
203 # check if key is activated
204 out
= pdnsutil("show-zone", self
.zone_nodot
)
205 self
.assertIn("Active", out
)
207 def test_put_deactivate_key(self
):
208 self
.keyid
= self
.add_zone_key(status
='active')
214 r
= self
.session
.put(
215 self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/"+self
.keyid
),
216 data
=json
.dumps(payload2
),
217 headers
={'content-type': 'application/json'})
218 self
.assertEquals(r
.status_code
, 204)
219 self
.assertEquals(r
.content
, b
"")
221 # check if key is deactivated
222 out
= pdnsutil("show-zone", self
.zone_nodot
)
223 self
.assertIn("Inactive", out
)
225 def test_put_deactivate_inactive_key(self
):
226 self
.keyid
= self
.add_zone_key()
233 r
= self
.session
.put(
234 self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/"+self
.keyid
),
235 data
=json
.dumps(payload
),
236 headers
={'content-type': 'application/json'})
237 self
.assertEquals(r
.status_code
, 204)
238 self
.assertEquals(r
.content
, b
"")
240 # check if key is still deactivated
241 out
= pdnsutil("show-zone", self
.zone_nodot
)
242 self
.assertIn("Inactive", out
)
244 def test_put_activate_active_key(self
):
245 self
.keyid
=self
.add_zone_key(status
='active')
251 r
= self
.session
.put(
252 self
.url("/api/v1/servers/localhost/zones/"+self
.zone
+"/cryptokeys/"+self
.keyid
),
253 data
=json
.dumps(payload2
),
254 headers
={'content-type': 'application/json'})
255 self
.assertEquals(r
.status_code
, 204)
256 self
.assertEquals(r
.content
, b
"")
258 # check if key is activated
259 out
= pdnsutil("show-zone", self
.zone_nodot
)
260 self
.assertIn("Active", out
)