@@ -19,294 +19,365 @@ def test_missing_principal(self):
19
19
self .assertRaises (ValueError , krbContext , using_keytab = True )
20
20
21
21
def test_all_defaults (self ):
22
- context = krbContext (using_keytab = True ,
23
- principal = 'HTTP/hostname@EXAMPLE.COM' )
22
+ context = krbContext (
23
+ using_keytab = True , principal = "HTTP/hostname@EXAMPLE.COM"
24
+ )
24
25
25
- self .assertTrue (context ._cleaned_options [' using_keytab' ])
26
+ self .assertTrue (context ._cleaned_options [" using_keytab" ])
26
27
expected_princ = gssapi .Name (
27
- 'HTTP/hostname@EXAMPLE.COM' ,
28
- gssapi .NameType .kerberos_principal )
29
- self .assertEqual (expected_princ , context ._cleaned_options ['principal' ])
30
- self .assertEqual (kctx .DEFAULT_CCACHE ,
31
- context ._cleaned_options ['ccache' ])
32
- self .assertEqual (kctx .DEFAULT_KEYTAB ,
33
- context ._cleaned_options ['keytab' ])
34
-
35
- @patch ('os.path.exists' )
28
+ "HTTP/hostname@EXAMPLE.COM" , gssapi .NameType .kerberos_principal
29
+ )
30
+ self .assertEqual (expected_princ , context ._cleaned_options ["principal" ])
31
+ self .assertEqual (
32
+ kctx .DEFAULT_CCACHE , context ._cleaned_options ["ccache" ]
33
+ )
34
+ self .assertEqual (
35
+ kctx .DEFAULT_KEYTAB , context ._cleaned_options ["keytab" ]
36
+ )
37
+
38
+ @patch ("os.path.exists" )
36
39
def test_specify_existing_keytab (self , exists ):
37
40
exists .return_value = True
38
41
39
- context = krbContext (using_keytab = True ,
40
- principal = 'HTTP/hostname@EXAMPLE.COM' ,
41
- keytab_file = '/etc/app/app.keytab' )
42
- self .assertEqual ('/etc/app/app.keytab' ,
43
- context ._cleaned_options ['keytab' ])
42
+ context = krbContext (
43
+ using_keytab = True ,
44
+ principal = "HTTP/hostname@EXAMPLE.COM" ,
45
+ keytab_file = "/etc/app/app.keytab" ,
46
+ )
47
+ self .assertEqual (
48
+ "/etc/app/app.keytab" , context ._cleaned_options ["keytab" ]
49
+ )
44
50
45
- @patch (' os.path.exists' )
51
+ @patch (" os.path.exists" )
46
52
def test_specify_nonexisting_keytab (self , exists ):
47
53
exists .return_value = False
48
54
49
- self .assertRaises (ValueError ,
50
- krbContext ,
51
- using_keytab = True ,
52
- principal = 'HTTP/hostname@EXAMPLE.COM' ,
53
- keytab_file = '/etc/app/app.keytab' )
55
+ self .assertRaises (
56
+ ValueError ,
57
+ krbContext ,
58
+ using_keytab = True ,
59
+ principal = "HTTP/hostname@EXAMPLE.COM" ,
60
+ keytab_file = "/etc/app/app.keytab" ,
61
+ )
54
62
55
63
def test_specify_ccache (self ):
56
- context = krbContext (using_keytab = True ,
57
- principal = 'HTTP/hostname@EXAMPLE.COM' ,
58
- ccache_file = '/var/app/krb5_ccache' )
59
- self .assertEqual ('/var/app/krb5_ccache' ,
60
- context ._cleaned_options ['ccache' ])
64
+ context = krbContext (
65
+ using_keytab = True ,
66
+ principal = "HTTP/hostname@EXAMPLE.COM" ,
67
+ ccache_file = "/var/app/krb5_ccache" ,
68
+ )
69
+ self .assertEqual (
70
+ "/var/app/krb5_ccache" , context ._cleaned_options ["ccache" ]
71
+ )
61
72
62
73
63
74
class CleanArgumentsAsRegularUserTest (unittest .TestCase ):
64
75
"""Test clean_context_options for not using keytab"""
65
76
66
- @patch (' krbcontext.context.get_login' )
77
+ @patch (" krbcontext.context.get_login" )
67
78
def test_all_defaults (self , get_login ):
68
- get_login .return_value = ' cqi'
79
+ get_login .return_value = " cqi"
69
80
70
81
context = krbContext ()
71
82
72
- expected_princ = gssapi .Name (get_login .return_value ,
73
- gssapi .NameType .user )
74
- self .assertEqual (expected_princ ,
75
- context ._cleaned_options ['principal' ])
76
- self .assertEqual (kctx .DEFAULT_CCACHE ,
77
- context ._cleaned_options ['ccache' ])
78
- self .assertFalse (context ._cleaned_options ['using_keytab' ])
83
+ expected_princ = gssapi .Name (
84
+ get_login .return_value , gssapi .NameType .user
85
+ )
86
+ self .assertEqual (expected_princ , context ._cleaned_options ["principal" ])
87
+ self .assertEqual (
88
+ kctx .DEFAULT_CCACHE , context ._cleaned_options ["ccache" ]
89
+ )
90
+ self .assertFalse (context ._cleaned_options ["using_keytab" ])
79
91
80
92
def test_specify_ccache (self ):
81
- context = krbContext (principal = 'cqi' ,
82
- ccache_file = '/var/app/krb5_ccache' )
83
- self .assertEqual ('/var/app/krb5_ccache' ,
84
- context ._cleaned_options ['ccache' ])
93
+ context = krbContext (
94
+ principal = "cqi" , ccache_file = "/var/app/krb5_ccache"
95
+ )
96
+ self .assertEqual (
97
+ "/var/app/krb5_ccache" , context ._cleaned_options ["ccache" ]
98
+ )
85
99
86
100
def test_specify_principal (self ):
87
- context = krbContext (principal = 'cqi' )
88
- expected_princ = gssapi .Name ('cqi' , gssapi .names .NameType .user )
89
- self .assertEqual (expected_princ ,
90
- context ._cleaned_options ['principal' ])
101
+ context = krbContext (principal = "cqi" )
102
+ expected_princ = gssapi .Name ("cqi" , gssapi .names .NameType .user )
103
+ self .assertEqual (expected_princ , context ._cleaned_options ["principal" ])
91
104
92
105
93
106
class TestInitWithKeytab (unittest .TestCase ):
94
107
"""Test krbContext.init_with_keytab"""
95
108
96
109
def setUp (self ):
97
- self .service_principal = ' HTTP/hostname@EXAMPLE.COM'
110
+ self .service_principal = " HTTP/hostname@EXAMPLE.COM"
98
111
self .princ_name = gssapi .Name (
99
- self .service_principal ,
100
- gssapi . NameType . kerberos_principal )
112
+ self .service_principal , gssapi . NameType . kerberos_principal
113
+ )
101
114
102
- self .Lock = patch (' krbcontext.context.Lock' )
115
+ self .Lock = patch (" krbcontext.context.Lock" )
103
116
self .Lock .start ()
104
117
105
118
# No need to create a real temp file for tests
106
- self .tmp_dir = '/tmp/test-krbcontext'
107
- self .tmp_ccache = os .path .join (self .tmp_dir , 'ccache' )
108
- self .mkdtemp = patch ('tempfile.mkdtemp' ,
109
- return_value = self .tmp_dir )
119
+ self .tmp_dir = "/tmp/test-krbcontext"
120
+ self .tmp_ccache = os .path .join (self .tmp_dir , "ccache" )
121
+ self .mkdtemp = patch ("tempfile.mkdtemp" , return_value = self .tmp_dir )
110
122
self .mkdtemp .start ()
111
123
112
- self .os_close = patch (' os.close' )
124
+ self .os_close = patch (" os.close" )
113
125
self .os_close .start ()
114
126
115
127
def tearDown (self ):
116
128
self .os_close .stop ()
117
129
self .mkdtemp .stop ()
118
130
self .Lock .stop ()
119
131
120
- @patch (' gssapi.Credentials' )
132
+ @patch (" gssapi.Credentials" )
121
133
def test_cred_not_expired (self , Credentials ):
122
- context = krbContext (using_keytab = True ,
123
- principal = self .service_principal )
134
+ context = krbContext (
135
+ using_keytab = True , principal = self .service_principal
136
+ )
124
137
context .init_with_keytab ()
125
138
126
139
self .assertEqual (1 , Credentials .call_count )
127
140
Credentials .return_value .store .assert_not_called ()
128
141
129
- @patch (' gssapi.Credentials' )
142
+ @patch (" gssapi.Credentials" )
130
143
def test_init_in_default_ccache_with_default_keytab (self , Credentials ):
131
144
type(Credentials .return_value ).lifetime = PropertyMock (
132
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
145
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
146
+ )
133
147
134
- context = krbContext (using_keytab = True ,
135
- principal = self .service_principal )
148
+ context = krbContext (
149
+ using_keytab = True , principal = self .service_principal
150
+ )
136
151
context .init_with_keytab ()
137
152
138
- Credentials .assert_has_calls ([
139
- call (usage = 'initiate' , name = self .princ_name ),
140
- call (usage = 'initiate' , name = self .princ_name ,
141
- store = {'ccache' : self .tmp_ccache }),
142
- ])
153
+ Credentials .assert_has_calls (
154
+ [
155
+ call (usage = "initiate" , name = self .princ_name ),
156
+ call (
157
+ usage = "initiate" ,
158
+ name = self .princ_name ,
159
+ store = {"ccache" : self .tmp_ccache },
160
+ ),
161
+ ]
162
+ )
143
163
Credentials .return_value .store .assert_called_once_with (
144
- store = None ,
145
- usage = 'initiate' ,
146
- set_default = True ,
147
- overwrite = True )
164
+ store = None , usage = "initiate" , set_default = True , overwrite = True
165
+ )
148
166
149
- @patch (' gssapi.Credentials' )
150
- @patch (' os.path.exists' , return_value = True )
151
- def test_init_in_default_ccache_with_given_keytab (self ,
152
- exists ,
153
- Credentials ):
167
+ @patch (" gssapi.Credentials" )
168
+ @patch (" os.path.exists" , return_value = True )
169
+ def test_init_in_default_ccache_with_given_keytab (
170
+ self , exists , Credentials
171
+ ):
154
172
type(Credentials .return_value ).lifetime = PropertyMock (
155
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
173
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
174
+ )
156
175
157
- keytab = '/etc/app/app.keytab'
158
- context = krbContext (using_keytab = True ,
159
- principal = self .service_principal ,
160
- keytab_file = keytab )
176
+ keytab = "/etc/app/app.keytab"
177
+ context = krbContext (
178
+ using_keytab = True ,
179
+ principal = self .service_principal ,
180
+ keytab_file = keytab ,
181
+ )
161
182
context .init_with_keytab ()
162
183
163
- Credentials .assert_has_calls ([
164
- call (usage = 'initiate' , name = self .princ_name ,
165
- store = {'client_keytab' : keytab }),
166
- call (usage = 'initiate' , name = self .princ_name ,
167
- store = {'ccache' : self .tmp_ccache , 'client_keytab' : keytab }),
168
- call ().store (usage = 'initiate' , store = None ,
169
- set_default = True , overwrite = True ),
170
- ])
184
+ Credentials .assert_has_calls (
185
+ [
186
+ call (
187
+ usage = "initiate" ,
188
+ name = self .princ_name ,
189
+ store = {"client_keytab" : keytab },
190
+ ),
191
+ call (
192
+ usage = "initiate" ,
193
+ name = self .princ_name ,
194
+ store = {"ccache" : self .tmp_ccache , "client_keytab" : keytab },
195
+ ),
196
+ call ().store (
197
+ usage = "initiate" ,
198
+ store = None ,
199
+ set_default = True ,
200
+ overwrite = True ,
201
+ ),
202
+ ]
203
+ )
171
204
Credentials .return_value .store .assert_called_once_with (
172
- store = None ,
173
- usage = 'initiate' ,
174
- set_default = True ,
175
- overwrite = True )
205
+ store = None , usage = "initiate" , set_default = True , overwrite = True
206
+ )
176
207
177
- @patch (' gssapi.Credentials' )
208
+ @patch (" gssapi.Credentials" )
178
209
def test_init_in_given_ccache_with_default_keytab (self , Credentials ):
179
210
type(Credentials .return_value ).lifetime = PropertyMock (
180
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
211
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
212
+ )
181
213
182
- ccache = '/tmp/mycc'
183
- context = krbContext (using_keytab = True ,
184
- principal = self .service_principal ,
185
- ccache_file = ccache )
214
+ ccache = "/tmp/mycc"
215
+ context = krbContext (
216
+ using_keytab = True ,
217
+ principal = self .service_principal ,
218
+ ccache_file = ccache ,
219
+ )
186
220
context .init_with_keytab ()
187
221
188
- Credentials .assert_has_calls ([
189
- call (usage = 'initiate' , name = self .princ_name ,
190
- store = {'ccache' : ccache }),
191
- call (usage = 'initiate' , name = self .princ_name ,
192
- store = {'ccache' : self .tmp_ccache }),
193
- ])
222
+ Credentials .assert_has_calls (
223
+ [
224
+ call (
225
+ usage = "initiate" ,
226
+ name = self .princ_name ,
227
+ store = {"ccache" : ccache },
228
+ ),
229
+ call (
230
+ usage = "initiate" ,
231
+ name = self .princ_name ,
232
+ store = {"ccache" : self .tmp_ccache },
233
+ ),
234
+ ]
235
+ )
194
236
Credentials .return_value .store .assert_called_once_with (
195
- store = {' ccache' : ccache },
196
- usage = ' initiate' ,
237
+ store = {" ccache" : ccache },
238
+ usage = " initiate" ,
197
239
set_default = True ,
198
- overwrite = True )
240
+ overwrite = True ,
241
+ )
199
242
200
- @patch (' gssapi.Credentials' )
201
- @patch (' os.path.exists' , return_value = True )
243
+ @patch (" gssapi.Credentials" )
244
+ @patch (" os.path.exists" , return_value = True )
202
245
def test_init_with_given_keytab_and_ccache (self , exists , Credentials ):
203
246
type(Credentials .return_value ).lifetime = PropertyMock (
204
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
205
-
206
- keytab = '/etc/app/app.keytab'
207
- ccache = '/tmp/mycc'
208
- context = krbContext (using_keytab = True ,
209
- principal = self .service_principal ,
210
- keytab_file = keytab ,
211
- ccache_file = ccache )
247
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
248
+ )
249
+
250
+ keytab = "/etc/app/app.keytab"
251
+ ccache = "/tmp/mycc"
252
+ context = krbContext (
253
+ using_keytab = True ,
254
+ principal = self .service_principal ,
255
+ keytab_file = keytab ,
256
+ ccache_file = ccache ,
257
+ )
212
258
context .init_with_keytab ()
213
259
214
- Credentials .assert_has_calls ([
215
- call (usage = 'initiate' , name = self .princ_name ,
216
- store = {'client_keytab' : keytab , 'ccache' : ccache }),
217
- call (usage = 'initiate' , name = self .princ_name ,
218
- store = {'client_keytab' : keytab , 'ccache' : self .tmp_ccache }),
219
- ])
260
+ Credentials .assert_has_calls (
261
+ [
262
+ call (
263
+ usage = "initiate" ,
264
+ name = self .princ_name ,
265
+ store = {"client_keytab" : keytab , "ccache" : ccache },
266
+ ),
267
+ call (
268
+ usage = "initiate" ,
269
+ name = self .princ_name ,
270
+ store = {"client_keytab" : keytab , "ccache" : self .tmp_ccache },
271
+ ),
272
+ ]
273
+ )
220
274
Credentials .return_value .store .assert_called_once_with (
221
- store = {' ccache' : ccache },
222
- usage = ' initiate' ,
275
+ store = {" ccache" : ccache },
276
+ usage = " initiate" ,
223
277
set_default = True ,
224
- overwrite = True )
278
+ overwrite = True ,
279
+ )
225
280
226
281
227
282
class TestInitWithPassword (unittest .TestCase ):
228
283
"""Test krbContext.init_with_password"""
229
284
230
285
def setUp (self ):
231
- self .principal = ' cqi'
286
+ self .principal = " cqi"
232
287
self .princ_name = gssapi .Name (self .principal , gssapi .NameType .user )
233
288
234
- @patch (' gssapi.Credentials' )
235
- @patch (' gssapi.raw.acquire_cred_with_password' )
236
- @patch (' gssapi.raw.store_cred_into' )
289
+ @patch (" gssapi.Credentials" )
290
+ @patch (" gssapi.raw.acquire_cred_with_password" )
291
+ @patch (" gssapi.raw.store_cred_into" )
237
292
def test_no_need_init_if_not_expired (
238
- self , store_cred_into , acquire_cred_with_password , Credentials ):
239
- context = krbContext (using_keytab = False ,
240
- principal = self .principal ,
241
- password = 'security' )
293
+ self , store_cred_into , acquire_cred_with_password , Credentials
294
+ ):
295
+ context = krbContext (
296
+ using_keytab = False , principal = self .principal , password = "security"
297
+ )
242
298
context .init_with_password ()
243
299
244
300
self .assertEqual (1 , Credentials .call_count )
245
301
store_cred_into .assert_not_called ()
246
302
acquire_cred_with_password .assert_not_called ()
247
303
248
- @patch (' gssapi.Credentials' )
249
- @patch (' gssapi.raw.acquire_cred_with_password' )
250
- @patch (' gssapi.raw.store_cred' )
304
+ @patch (" gssapi.Credentials" )
305
+ @patch (" gssapi.raw.acquire_cred_with_password" )
306
+ @patch (" gssapi.raw.store_cred" )
251
307
def test_init_in_default_ccache (
252
- self , store_cred , acquire_cred_with_password , Credentials ):
308
+ self , store_cred , acquire_cred_with_password , Credentials
309
+ ):
253
310
type(Credentials .return_value ).lifetime = PropertyMock (
254
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
311
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
312
+ )
255
313
256
- context = krbContext (using_keytab = False ,
257
- principal = self .principal ,
258
- password = 'security' )
314
+ context = krbContext (
315
+ using_keytab = False , principal = self .principal , password = "security"
316
+ )
259
317
context .init_with_password ()
260
318
261
319
acquire_cred_with_password .assert_called_once_with (
262
- self .princ_name , b'security' )
320
+ self .princ_name , b"security"
321
+ )
263
322
264
323
store_cred .assert_called_once_with (
265
324
acquire_cred_with_password .return_value .creds ,
266
- usage = 'initiate' , overwrite = True , set_default = True
325
+ usage = "initiate" ,
326
+ overwrite = True ,
327
+ set_default = True ,
267
328
)
268
329
269
- @patch (' gssapi.Credentials' )
270
- @patch (' gssapi.raw.acquire_cred_with_password' )
271
- @patch (' gssapi.raw.store_cred_into' )
330
+ @patch (" gssapi.Credentials" )
331
+ @patch (" gssapi.raw.acquire_cred_with_password" )
332
+ @patch (" gssapi.raw.store_cred_into" )
272
333
def test_init_in_given_ccache (
273
- self , store_cred_into , acquire_cred_with_password , Credentials ):
334
+ self , store_cred_into , acquire_cred_with_password , Credentials
335
+ ):
274
336
type(Credentials .return_value ).lifetime = PropertyMock (
275
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
337
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
338
+ )
276
339
277
- ccache = '/tmp/mycc'
278
- context = krbContext (using_keytab = False ,
279
- principal = self .principal ,
280
- ccache_file = ccache ,
281
- password = 'security' )
340
+ ccache = "/tmp/mycc"
341
+ context = krbContext (
342
+ using_keytab = False ,
343
+ principal = self .principal ,
344
+ ccache_file = ccache ,
345
+ password = "security" ,
346
+ )
282
347
context .init_with_password ()
283
348
284
349
Credentials .assert_called_once_with (
285
- usage = 'initiate' ,
286
- name = self .princ_name ,
287
- store = {'ccache' : ccache })
350
+ usage = "initiate" , name = self .princ_name , store = {"ccache" : ccache }
351
+ )
288
352
289
353
acquire_cred_with_password .assert_called_once_with (
290
- self .princ_name , b'security' )
354
+ self .princ_name , b"security"
355
+ )
291
356
292
357
store_cred_into .assert_called_once_with (
293
- {' ccache' : ' /tmp/mycc' },
358
+ {" ccache" : " /tmp/mycc" },
294
359
acquire_cred_with_password .return_value .creds ,
295
- usage = 'initiate' ,
296
- overwrite = True )
297
-
298
- @patch ('gssapi.Credentials' )
299
- @patch ('sys.stdin.isatty' , return_value = True )
300
- @patch ('getpass.getpass' )
301
- @patch ('gssapi.raw.acquire_cred_with_password' )
302
- @patch ('gssapi.raw.store_cred' )
303
- def test_init_cred_with_need_enter_password (self , store_cred ,
304
- acquire_cred_with_password ,
305
- getpass , isatty ,
306
- Credentials ):
360
+ usage = "initiate" ,
361
+ overwrite = True ,
362
+ )
363
+
364
+ @patch ("gssapi.Credentials" )
365
+ @patch ("sys.stdin.isatty" , return_value = True )
366
+ @patch ("getpass.getpass" )
367
+ @patch ("gssapi.raw.acquire_cred_with_password" )
368
+ @patch ("gssapi.raw.store_cred" )
369
+ def test_init_cred_with_need_enter_password (
370
+ self ,
371
+ store_cred ,
372
+ acquire_cred_with_password ,
373
+ getpass ,
374
+ isatty ,
375
+ Credentials ,
376
+ ):
307
377
type(Credentials .return_value ).lifetime = PropertyMock (
308
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
309
- getpass .return_value = 'mypassword'
378
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
379
+ )
380
+ getpass .return_value = "mypassword"
310
381
311
382
context = krbContext (using_keytab = False , principal = self .principal )
312
383
context .init_with_password ()
@@ -315,31 +386,36 @@ def test_init_cred_with_need_enter_password(self, store_cred,
315
386
# Ensure this must be called.
316
387
getpass .assert_called_once ()
317
388
318
- Credentials .assert_called_once_with (usage = 'initiate' ,
319
- name = self .princ_name )
389
+ Credentials .assert_called_once_with (
390
+ usage = "initiate" , name = self .princ_name
391
+ )
320
392
321
393
acquire_cred_with_password .assert_called_once_with (
322
- self .princ_name , b'mypassword' )
394
+ self .princ_name , b"mypassword"
395
+ )
323
396
324
397
store_cred .assert_called_once_with (
325
398
acquire_cred_with_password .return_value .creds ,
326
- usage = 'initiate' , overwrite = True , set_default = True
399
+ usage = "initiate" ,
400
+ overwrite = True ,
401
+ set_default = True ,
327
402
)
328
403
329
- @patch (' gssapi.Credentials' )
330
- @patch (' sys.stdin.isatty' , return_value = False )
331
- def test_init_with_entering_password_but_not_in_atty (self ,
332
- isatty ,
333
- Credentials ):
404
+ @patch (" gssapi.Credentials" )
405
+ @patch (" sys.stdin.isatty" , return_value = False )
406
+ def test_init_with_entering_password_but_not_in_atty (
407
+ self , isatty , Credentials
408
+ ):
334
409
type(Credentials .return_value ).lifetime = PropertyMock (
335
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
410
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
411
+ )
336
412
337
413
context = krbContext (using_keytab = False , principal = self .principal )
338
414
self .assertRaises (IOError , context .init_with_password )
339
415
340
- context = krbContext (using_keytab = False ,
341
- principal = self .principal ,
342
- password = '' )
416
+ context = krbContext (
417
+ using_keytab = False , principal = self .principal , password = ""
418
+ )
343
419
self .assertRaises (IOError , context .init_with_password )
344
420
345
421
@@ -348,121 +424,138 @@ class TestKrbContextManager(unittest.TestCase):
348
424
349
425
def setUp (self ):
350
426
# Do not actually operate threading lock
351
- self .init_Lock = patch (' krbcontext.context.Lock' )
427
+ self .init_Lock = patch (" krbcontext.context.Lock" )
352
428
self .init_Lock .start ()
353
429
354
430
def tearDown (self ):
355
431
self .init_Lock .stop ()
356
432
357
- @patch (' gssapi.Credentials' )
358
- @patch .dict (' os.environ' , {}, clear = True )
433
+ @patch (" gssapi.Credentials" )
434
+ @patch .dict (" os.environ" , {}, clear = True )
359
435
def test_init_with_default_keytab (self , Credentials ):
360
436
type(Credentials .return_value ).lifetime = PropertyMock (
361
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
362
-
363
- with krbContext (using_keytab = True ,
364
- principal = 'app/hostname@EXAMPLE.COM' ,
365
- ccache_file = '/tmp/my_cc' ):
366
- self .assertEqual ('/tmp/my_cc' , os .environ ['KRB5CCNAME' ])
437
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
438
+ )
367
439
368
- @patch ('gssapi.Credentials' )
369
- @patch ('gssapi.raw.acquire_cred_with_password' )
370
- @patch ('gssapi.raw.store_cred' )
371
- @patch .dict ('os.environ' , {}, clear = True )
440
+ with krbContext (
441
+ using_keytab = True ,
442
+ principal = "app/hostname@EXAMPLE.COM" ,
443
+ ccache_file = "/tmp/my_cc" ,
444
+ ):
445
+ self .assertEqual ("/tmp/my_cc" , os .environ ["KRB5CCNAME" ])
446
+
447
+ @patch ("gssapi.Credentials" )
448
+ @patch ("gssapi.raw.acquire_cred_with_password" )
449
+ @patch ("gssapi.raw.store_cred" )
450
+ @patch .dict ("os.environ" , {}, clear = True )
372
451
def test_init_in_default_ccache_with_password (
373
- self , store_cred , acquire_cred_with_password , Credentials ):
452
+ self , store_cred , acquire_cred_with_password , Credentials
453
+ ):
374
454
type(Credentials .return_value ).lifetime = PropertyMock (
375
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
455
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
456
+ )
376
457
377
- with krbContext (using_keytab = False ,
378
- principal = ' cqi' ,
379
- password = 'security' ):
380
- self .assertNotIn (' KRB5CCNAME' , os .environ )
458
+ with krbContext (
459
+ using_keytab = False , principal = " cqi" , password = "security"
460
+ ):
461
+ self .assertNotIn (" KRB5CCNAME" , os .environ )
381
462
382
- self .assertNotIn (' KRB5CCNAME' , os .environ )
463
+ self .assertNotIn (" KRB5CCNAME" , os .environ )
383
464
384
- @patch (' gssapi.Credentials' )
385
- @patch (' gssapi.raw.acquire_cred_with_password' )
386
- @patch (' gssapi.raw.store_cred' )
387
- @patch .dict (' os.environ' , {' KRB5CCNAME' : ' /tmp/my_cc' }, clear = True )
465
+ @patch (" gssapi.Credentials" )
466
+ @patch (" gssapi.raw.acquire_cred_with_password" )
467
+ @patch (" gssapi.raw.store_cred" )
468
+ @patch .dict (" os.environ" , {" KRB5CCNAME" : " /tmp/my_cc" }, clear = True )
388
469
def test_after_init_in_default_ccache_original_ccache_should_be_restored (
389
- self , store_cred , acquire_cred_with_password , Credentials ):
470
+ self , store_cred , acquire_cred_with_password , Credentials
471
+ ):
390
472
type(Credentials .return_value ).lifetime = PropertyMock (
391
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
473
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
474
+ )
392
475
393
- with krbContext (using_keytab = False ,
394
- principal = ' cqi' ,
395
- password = 'security' ):
396
- self .assertNotIn (' KRB5CCNAME' , os .environ )
476
+ with krbContext (
477
+ using_keytab = False , principal = " cqi" , password = "security"
478
+ ):
479
+ self .assertNotIn (" KRB5CCNAME" , os .environ )
397
480
398
- self .assertIn (' KRB5CCNAME' , os .environ )
399
- self .assertEqual (' /tmp/my_cc' , os .environ [' KRB5CCNAME' ])
481
+ self .assertIn (" KRB5CCNAME" , os .environ )
482
+ self .assertEqual (" /tmp/my_cc" , os .environ [" KRB5CCNAME" ])
400
483
401
- @patch (' gssapi.Credentials' )
402
- @patch .dict (' os.environ' , {' KRB5CCNAME' : ' /tmp/my_cc' }, clear = True )
484
+ @patch (" gssapi.Credentials" )
485
+ @patch .dict (" os.environ" , {" KRB5CCNAME" : " /tmp/my_cc" }, clear = True )
403
486
def test_original_ccache_should_be_restored (self , Credentials ):
404
487
type(Credentials .return_value ).lifetime = PropertyMock (
405
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
488
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
489
+ )
406
490
407
- with krbContext (using_keytab = True ,
408
- principal = 'app/hostname@EXAMPLE.COM' ,
409
- ccache_file = '/tmp/app_pid_cc' ):
491
+ with krbContext (
492
+ using_keytab = True ,
493
+ principal = "app/hostname@EXAMPLE.COM" ,
494
+ ccache_file = "/tmp/app_pid_cc" ,
495
+ ):
410
496
# Inside context, given ccache should be used.
411
- self .assertEqual (' /tmp/app_pid_cc' , os .environ [' KRB5CCNAME' ])
497
+ self .assertEqual (" /tmp/app_pid_cc" , os .environ [" KRB5CCNAME" ])
412
498
413
- self .assertIn (' KRB5CCNAME' , os .environ )
414
- self .assertEqual (' /tmp/my_cc' , os .environ [' KRB5CCNAME' ])
499
+ self .assertIn (" KRB5CCNAME" , os .environ )
500
+ self .assertEqual (" /tmp/my_cc" , os .environ [" KRB5CCNAME" ])
415
501
416
- @patch (' gssapi.Credentials' )
417
- @patch .dict (' os.environ' , {}, clear = True )
502
+ @patch (" gssapi.Credentials" )
503
+ @patch .dict (" os.environ" , {}, clear = True )
418
504
def test_init_in_default_ccache_without_original_krb5ccname_is_set (
419
- self , Credentials ):
505
+ self , Credentials
506
+ ):
420
507
type(Credentials .return_value ).lifetime = PropertyMock (
421
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
508
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
509
+ )
422
510
423
- with krbContext (using_keytab = True ,
424
- principal = 'app/hostname@EXAMPLE.COM' ):
425
- self .assertNotIn ('KRB5CCNAME' , os .environ )
511
+ with krbContext (
512
+ using_keytab = True , principal = "app/hostname@EXAMPLE.COM"
513
+ ):
514
+ self .assertNotIn ("KRB5CCNAME" , os .environ )
426
515
427
516
# Originally, no KRB5CCNAME is set, it should be cleaned after exit.
428
- self .assertNotIn (' KRB5CCNAME' , os .environ )
517
+ self .assertNotIn (" KRB5CCNAME" , os .environ )
429
518
430
- @patch (' gssapi.Credentials' )
431
- @patch .dict (' os.environ' , {' KRB5CCNAME' : ' /tmp/my_cc' }, clear = True )
519
+ @patch (" gssapi.Credentials" )
520
+ @patch .dict (" os.environ" , {" KRB5CCNAME" : " /tmp/my_cc" }, clear = True )
432
521
def test_init_in_default_ccache_and_original_krb5ccname_is_set (
433
- self , Credentials ):
522
+ self , Credentials
523
+ ):
434
524
type(Credentials .return_value ).lifetime = PropertyMock (
435
- side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 ))
525
+ side_effect = gssapi .exceptions .ExpiredCredentialsError (1 , 1 )
526
+ )
436
527
437
- with krbContext (using_keytab = True ,
438
- principal = 'app/hostname@EXAMPLE.COM' ):
439
- self .assertNotIn ('KRB5CCNAME' , os .environ )
528
+ with krbContext (
529
+ using_keytab = True , principal = "app/hostname@EXAMPLE.COM"
530
+ ):
531
+ self .assertNotIn ("KRB5CCNAME" , os .environ )
440
532
441
- self .assertIn (' KRB5CCNAME' , os .environ )
442
- self .assertEqual (' /tmp/my_cc' , os .environ [' KRB5CCNAME' ])
533
+ self .assertIn (" KRB5CCNAME" , os .environ )
534
+ self .assertEqual (" /tmp/my_cc" , os .environ [" KRB5CCNAME" ])
443
535
444
- @patch (' gssapi.Credentials' )
445
- @patch .dict (os .environ , {' KRB5CCNAME' : ' /tmp/my_cc' }, clear = True )
536
+ @patch (" gssapi.Credentials" )
537
+ @patch .dict (os .environ , {" KRB5CCNAME" : " /tmp/my_cc" }, clear = True )
446
538
def test_do_nothing_if_unnecessary_to_init (self , Credentials ):
447
- with krbContext (using_keytab = True ,
448
- principal = 'app/hostname@EXAMPLE.COM' ):
539
+ with krbContext (
540
+ using_keytab = True , principal = "app/hostname@EXAMPLE.COM"
541
+ ):
449
542
# Nothing is changed, but original KRB5CCNAME must be removed
450
543
# since default ccache is used.
451
- self .assertNotIn (' KRB5CCNAME' , os .environ )
544
+ self .assertNotIn (" KRB5CCNAME" , os .environ )
452
545
453
546
# Original ccache must be restored.
454
- self .assertEqual (' /tmp/my_cc' , os .environ [' KRB5CCNAME' ])
547
+ self .assertEqual (" /tmp/my_cc" , os .environ [" KRB5CCNAME" ])
455
548
456
549
457
550
class TestGetLogin (unittest .TestCase ):
458
551
"""Test get_login"""
459
552
460
- @patch (' os.getuid' , return_value = 1001 )
461
- @patch (' pwd.getpwuid' )
553
+ @patch (" os.getuid" , return_value = 1001 )
554
+ @patch (" pwd.getpwuid" )
462
555
def test_get_login (self , getpwuid , getuid ):
463
- getpwuid .return_value .pw_name = ' user'
556
+ getpwuid .return_value .pw_name = " user"
464
557
465
558
user = get_login ()
466
559
467
560
getpwuid .assert_called_once_with (getuid .return_value )
468
- self .assertEqual (' user' , user )
561
+ self .assertEqual (" user" , user )
0 commit comments