@@ -107,8 +107,7 @@ def check_verif_timestamp(target, dampener_limit=None):
107
107
new_ts = time ()
108
108
verif_log_timestamps [target ] = new_ts
109
109
return True
110
- else :
111
- return False
110
+ return False
112
111
113
112
114
113
class STATUS :
@@ -143,7 +142,7 @@ def __getattribute__(self, name):
143
142
pass
144
143
try :
145
144
default = getattr (self .Defaults , name )
146
- return __salt__ ['config.get' ]('repo_signing:{} ' .format (name ), default )
145
+ return __salt__ ['config.get' ]('repo_signing:%s ' .format (name ), default )
147
146
except AttributeError :
148
147
raise
149
148
@@ -169,12 +168,12 @@ def split_certs(fh):
169
168
try :
170
169
log_level = log .debug
171
170
yield ossl .load_certificate (ossl .FILETYPE_PEM , ret )
172
- except Exception as E :
171
+ except Exception as exception_object :
173
172
status = STATUS .UNKNOWN
174
- if check_verif_timestamp (fh ) == True :
173
+ if check_verif_timestamp (fh ):
175
174
log_level = log .error
176
- msg = '{} : | file: "{} " | cert decoding status: {} | attempting as PEM encoded private key'
177
- log_level ( msg . format ( short_fname , fh .name , status , digest , code , depth , message ) )
175
+ log_level ( '%s : | file: "%s " | cert decoding status: %s | attempting as PEM encoded private key',
176
+ short_fname , fh .name , status )
178
177
yield load_pem_private_key (ret , password = None , backend = default_backend ())
179
178
ret = None
180
179
@@ -193,11 +192,11 @@ def read_certs(*fnames):
193
192
with open (fname , 'r' ) as fh :
194
193
for i in split_certs (fh ):
195
194
yield i
196
- except Exception as E :
195
+ except Exception as exception_object :
197
196
log_level = log .debug
198
- if check_verif_timestamp (fname ) == True :
197
+ if check_verif_timestamp (fname ):
199
198
log_level = log .error
200
- log_level ('error while reading "{} ": {}' . format ( fname , E ) )
199
+ log_level ('error while reading "%s ": %s' , fname , exception_object )
201
200
202
201
203
202
def stringify_cert_files (cert ):
@@ -206,8 +205,7 @@ def stringify_cert_files(cert):
206
205
return ', ' .join ([str (c ) for c in cert ])
207
206
elif type (cert ) is file :
208
207
return cert .name
209
- else :
210
- return str (cert )
208
+ return str (cert )
211
209
212
210
213
211
class X509AwareCertBucket :
@@ -232,7 +230,7 @@ def authenticate_cert(self):
232
230
def __init__ (self , public_crt , ca_crt ):
233
231
try :
234
232
import hubblestack .pre_packaged_certificates as HPPC
235
- # if we have hardcoded certs then we're meant to ignore any other
233
+ # iff we have hardcoded certs then we're meant to ignore any other
236
234
# configured value
237
235
if hasattr (HPPC , 'public_crt' ):
238
236
log .debug ('using pre-packaged-public_crt' )
@@ -243,11 +241,11 @@ def __init__(self, public_crt, ca_crt):
243
241
except ImportError :
244
242
pass
245
243
244
+ untrusted_crt = list ()
245
+
246
246
if isinstance (ca_crt , (list , tuple )):
247
247
untrusted_crt = ca_crt [1 :]
248
248
ca_crt = ca_crt [0 ]
249
- else :
250
- untrusted_crt = list ()
251
249
252
250
if not isinstance (public_crt , (list , tuple )):
253
251
public_crt = [ public_crt ]
@@ -268,12 +266,12 @@ def __init__(self, public_crt, ca_crt):
268
266
self .store .add_cert (i )
269
267
self .trusted .append (digest )
270
268
log_level = log .debug
271
- if check_verif_timestamp (digest , dampener_limit = seconds_day ) == True :
269
+ if check_verif_timestamp (digest , dampener_limit = seconds_day ):
272
270
log_level = log .splunk
273
271
status = STATUS .VERIFIED
274
272
str_ca = stringify_cert_files (ca_crt )
275
- msg = 'ca cert | file: "{} " | status: {} | digest "{} " | added to verify store'
276
- log_level ( msg . format ( str_ca , status , digest ) )
273
+ log_level ( 'ca cert | file: "%s " | status: %s | digest "%s " | added to verify store' ,
274
+ str_ca , status , digest )
277
275
278
276
for i in read_certs (* untrusted_crt ):
279
277
digest = i .digest ('sha1' )
@@ -285,19 +283,21 @@ def __init__(self, public_crt, ca_crt):
285
283
ossl .X509StoreContext (self .store , i ).verify_certificate ()
286
284
self .store .add_cert (i )
287
285
self .trusted .append (digest )
288
- status = STATUS .VERIFIED
286
+ status = STATUS .VERIFIED
289
287
log_level = log .debug
290
288
except ossl .X509StoreContextError as exception_object :
291
289
# log at either log.error or log.critical according to the error code
292
290
status = STATUS .FAIL
293
291
pass
294
- if check_verif_timestamp (digest , dampener_limit = seconds_day ) == True :
295
- if status == STATUS .FAIL : log_level = log .critical
296
- elif status == STATUS .UNKNOWN : log_level = log .error
297
- else : log_level = log .splunk
292
+ if check_verif_timestamp (digest , dampener_limit = seconds_day ):
293
+ log_level = log .splunk
294
+ if status == STATUS .FAIL :
295
+ log_level = log .critical
296
+ elif status == STATUS .UNKNOWN :
297
+ log_level = log .error
298
298
str_untrusted = stringify_cert_files (untrusted_crt )
299
- msg = 'intermediate certs | file: "{} " | status: {} | digest "{}"'
300
- log_level ( msg . format ( str_untrusted , status , digest ) )
299
+ log_level ( 'intermediate certs | file: "%s " | status: %s | digest "%s"' ,
300
+ str_untrusted , status , digest )
301
301
302
302
self .public_crt = list ()
303
303
for i in read_certs (* public_crt ):
@@ -312,13 +312,15 @@ def __init__(self, public_crt, ca_crt):
312
312
ossl .X509StoreContext (self .store , i ).verify_certificate ()
313
313
status = STATUS .VERIFIED
314
314
self .trusted .append (digest )
315
- if check_verif_timestamp (digest , dampener_limit = seconds_day ) == True :
316
- if status == STATUS .FAIL : log_level = log .critical
317
- elif status == STATUS .UNKNOWN : log_level = log .error
318
- else : log_level = log .splunk
315
+ if check_verif_timestamp (digest , dampener_limit = seconds_day ):
316
+ log_level = log .splunk
317
+ if status == STATUS .FAIL :
318
+ log_level = log .critical
319
+ elif status == STATUS .UNKNOWN :
320
+ log_level = log .error
319
321
str_public = stringify_cert_files (public_crt )
320
- msg = 'public cert | file: "{} " | status : "{} " | digest: "{}"'
321
- log_level ( msg . format ( str_public , status , digest ) )
322
+ log_level ( 'public cert | file: "%s " | status : "%s " | digest: "%s"' ,
323
+ str_public , status , digest )
322
324
except ossl .X509StoreContextError as exception_object :
323
325
code , depth , message = exception_object .args [0 ]
324
326
if code in (2 ,3 ,20 ,27 ,33 ):
@@ -335,12 +337,14 @@ def __init__(self, public_crt, ca_crt):
335
337
# X509_V_ERR_CRL_HAS_EXPIRED 12
336
338
status = STATUS .FAIL
337
339
# log at either log.error or log.critical according to the error code
338
- if check_verif_timestamp (digest , dampener_limit = seconds_day ) == True :
339
- if status == STATUS .FAIL : log_level = log .critical
340
- elif status == STATUS .UNKNOWN : log_level = log .error
341
- else : log_level = log .splunk
342
- msg = 'public cert | file: "{}" | status: {} | digest: "{}" | X509 error code: {} | depth: {} | message: "{}"'
343
- log_level (msg .format (str_public , status , digest , code , depth , message ))
340
+ if check_verif_timestamp (digest , dampener_limit = seconds_day ):
341
+ log_level = log .splunk
342
+ if status == STATUS .FAIL :
343
+ log_level = log .critical
344
+ elif status == STATUS .UNKNOWN :
345
+ log_level = log .error
346
+ log_level ('public cert | file: "%s" | status: %s | digest: "%s" | X509 error code: %s | depth: %s | message: "%s"' ,
347
+ str_public , status , digest , code , depth , message )
344
348
345
349
self .public_crt .append (self .PublicCertObj (i , digest , status ))
346
350
@@ -440,7 +444,7 @@ def sign_target(fname, ofname, private_key='private.key', **kwargs): # pylint: d
440
444
args ['algorithm' ] = utils .Prehashed (chosen_hash )
441
445
sig = first_key .sign (** args )
442
446
with open (ofname , 'w' ) as fh :
443
- log .debug ('writing signature of {} to {}' . format ( os .path .abspath (fname ), os .path .abspath (ofname ) ))
447
+ log .debug ('writing signature of %s to %s' , os .path .abspath (fname ), os .path .abspath (ofname ))
444
448
fh .write (PEM .encode (sig , 'Detached Signature of {}' .format (fname )))
445
449
fh .write ('\n ' )
446
450
@@ -461,10 +465,9 @@ def verify_signature(fname, sfname, public_crt='public.crt', ca_crt='ca-root.crt
461
465
except IOError :
462
466
status = STATUS .UNKNOWN
463
467
verif_key = ':' .join ([fname , sfname ])
464
- if check_verif_timestamp (verif_key ) == True :
468
+ if check_verif_timestamp (verif_key ):
465
469
log_level = log .error
466
- msg = '{} | file "{}" | status: {} '
467
- log_level (msg .format (short_fname , fname , status ))
470
+ log_level ('%s | file "%s" | status: %s ' , short_fname , fname , status )
468
471
return STATUS .UNKNOWN
469
472
x509 = X509AwareCertBucket (public_crt , ca_crt )
470
473
hasher , chosen_hash = hash_target (fname , obj_mode = True )
@@ -474,21 +477,22 @@ def verify_signature(fname, sfname, public_crt='public.crt', ca_crt='ca-root.crt
474
477
for crt ,txt ,status in x509 .public_crt :
475
478
log_level = log .debug
476
479
sha256sum = hash_target (fname )
477
- msg = '{} | file "{}" | status: {} | sha256sum: "{}" | public cert fingerprint and requester: "{}"'
478
480
pubkey = crt .get_pubkey ().to_cryptography_key ()
479
481
if isinstance (pubkey , rsa .RSAPublicKey ):
480
482
args ['padding' ] = padding .PSS ( mgf = padding .MGF1 (hashes .SHA256 ()),
481
483
salt_length = padding .PSS .MAX_LENGTH )
482
484
args ['algorithm' ] = utils .Prehashed (chosen_hash )
483
485
try :
484
486
pubkey .verify (** args )
485
- log_level (msg .format (short_fname , fname , status , sha256sum , txt ))
487
+ log_level ('%s | file "%s" | status: %s | sha256sum: "%s" | public cert fingerprint and requester: "%s"' ,
488
+ short_fname , fname , status , sha256sum , txt )
486
489
return status
487
490
except InvalidSignature :
488
491
status = STATUS .FAIL
489
- if check_verif_timestamp (fname ) == True :
492
+ if check_verif_timestamp (fname ):
490
493
log_level = log .critical
491
- log_level (msg .format (short_fname , fname , status , sha256sum , txt ))
494
+ log_level ('%s | file "%s" | status: %s | sha256sum: "%s" | public cert fingerprint and requester: "%s"' ,
495
+ short_fname , fname , status , sha256sum , txt )
492
496
pass
493
497
return STATUS .FAIL
494
498
@@ -521,8 +525,9 @@ def verify_files(targets, mfname='MANIFEST', sfname='SIGNATURE', public_crt='pub
521
525
522
526
return a mapping from the input target list to the status values (a dict of filename: status)
523
527
"""
524
- msg = "verifying: files: {} | mfname: {} | sfname: {} | public_crt: {}| ca_crt: {}"
525
- log .debug (msg .format (targets , mfname , sfname , public_crt , ca_crt ))
528
+
529
+ log .debug ("verifying: files: %s | mfname: %s | sfname: %s | public_crt: %s| ca_crt: %s" ,
530
+ targets , mfname , sfname , public_crt , ca_crt )
526
531
ret = OrderedDict ()
527
532
ret [mfname ] = verify_signature (mfname , sfname = sfname , public_crt = public_crt , ca_crt = ca_crt )
528
533
# ret[mfname] is the strongest claim we can make about the files we're
@@ -547,7 +552,7 @@ def verify_files(targets, mfname='MANIFEST', sfname='SIGNATURE', public_crt='pub
547
552
for otarget in targets :
548
553
target = normalize_path (otarget , trunc = trunc )
549
554
550
- log .debug ('found manifest for {} ({})' . format ( otarget , target ) )
555
+ log .debug ('found manifest for %s (%s)' , otarget , target )
551
556
if otarget != target :
552
557
xlate [target ] = otarget
553
558
if target in digests or target in (mfname , sfname ):
@@ -590,12 +595,14 @@ def verify_files(targets, mfname='MANIFEST', sfname='SIGNATURE', public_crt='pub
590
595
# We do have a MANIFEST entry and it doesn't match: FAIL with or
591
596
# without a matching SIGNATURE
592
597
status = STATUS .FAIL
593
- if check_verif_timestamp (digest ) == True :
594
- if status == STATUS .FAIL : log_level = log .critical
595
- elif status == STATUS .UNKNOWN : log_level = log .error
598
+ if check_verif_timestamp (digest ):
599
+ if status == STATUS .FAIL :
600
+ log_level = log .critical
601
+ elif status == STATUS .UNKNOWN :
602
+ log_level = log .error
596
603
# logs according to the STATUS of target file
597
- msg = 'file: "{} " | status: {} | manifest sha256: "{} " | real sha256: "{}"'
598
- log_level ( msg . format ( vfname , status , digest , new_hash ) )
604
+ log_level ( 'file: "%s " | status: %s | manifest sha256: "%s " | real sha256: "%s"' ,
605
+ vfname , status , digest , new_hash )
599
606
ret [vfname ] = status
600
607
601
608
# fix any normalized names so the caller gets back their specified targets
@@ -625,13 +632,13 @@ def inner(path, saltenv, *a, **kwargs):
625
632
real_path = _p (f_path )
626
633
mani_path = _p (f_mani )
627
634
sign_path = _p (f_sign )
628
- log .debug ('path: {} | manifest: "{} " | signature: "{}"' . format (
629
- path , mani_path , sign_path ) )
635
+ log .debug ('path: %s | manifest: "%s " | signature: "%s"' ,
636
+ path , mani_path , sign_path )
630
637
if not real_path :
631
638
return f_path
632
639
verify_res = verify_files ([real_path ],
633
- mfname = mani_path , sfname = sign_path ,
634
- public_crt = Options .public_crt , ca_crt = Options .ca_crt )
640
+ mfname = mani_path , sfname = sign_path ,
641
+ public_crt = Options .public_crt , ca_crt = Options .ca_crt )
635
642
log .debug ('verify: %s' , dict (** verify_res ))
636
643
vrg = verify_res .get (real_path , STATUS .UNKNOWN )
637
644
if vrg == STATUS .VERIFIED :
0 commit comments