Browse code

Make kvno_in_tail a per-entry property.

Xavier G authored on 26/04/2020 18:06:21
Showing 1 changed files

  • yamltab index 4edc92a..4b4266e 100755
... ...
@@ -226,10 +226,7 @@ def enrich_keytab(keytab):
226 226
 	"""
227 227
 	Enrich records with extra information suitable for human readers.
228 228
 	"""
229
-	# Reflect whether the keytab uses record tails to store 32-bits kvno:
230
-	records = keytab.pop('records')
231
-	keytab['kvno_in_tail'] = False
232
-	for record in records:
229
+	for record in keytab['records']:
233 230
 		if 'entry' not in record:
234 231
 			continue
235 232
 		entry = record['entry']
... ...
@@ -246,23 +243,17 @@ def enrich_keytab(keytab):
246 243
 			# those 32 bits is not 0. If present, this key version supersedes the 8-bit key version.
247 244
 			if len(record['tail']) >= 4:
248 245
 				tail_kvno = struct.unpack('>I', record['tail'][0:4])[0]
249
-				if tail_kvno:
250
-					entry['tail_kvno'] = tail_kvno
251
-					# tail_kvno overrides kvno if non-zero:
252
-					if entry['tail_kvno']:
253
-						entry['actual_kvno'] = entry['tail_kvno']
254
-						keytab['kvno_in_tail'] = True
246
+				if tail_kvno or not entry['kvno']:
247
+					entry['actual_kvno'] = entry['tail_kvno'] = tail_kvno
255 248
 			if 'actual_kvno' not in entry:
256 249
 				entry['actual_kvno'] = entry['kvno']
257
-	# Reintroduce records after kvno_in_tail for convenience:
258
-	keytab['records'] = records
259 250
 	return keytab
260 251
 
261 252
 def simplify_keytab(keytab):
262 253
 	"""
263 254
 	Simplify the keytab to make it suitable for edition.
264 255
 	"""
265
-	simplified = {'version': keytab['version'], 'kvno_in_tail': keytab['kvno_in_tail'], 'entries': []}
256
+	simplified = {'version': keytab['version'], 'entries': []}
266 257
 	for record in keytab['records']:
267 258
 		if 'entry' not in record:
268 259
 			continue
... ...
@@ -275,6 +266,8 @@ def simplify_keytab(keytab):
275 266
 			if key in entry['principal']:
276 267
 				simple_entry['principal'][key] = entry['principal'][key]
277 268
 		simple_entry['kvno'] = entry.get('actual_kvno', entry['kvno'])
269
+		if 'tail_kvno' in entry:
270
+			simple_entry['kvno_in_tail'] = True
278 271
 		for key in ('date', 'enctype', 'key'):
279 272
 			if key in entry:
280 273
 				simple_entry[key] = entry[key]
... ...
@@ -425,12 +418,12 @@ def simple_principal_to_full(inentry, index, entry):
425 418
 	elif 'spn' in inentry:
426 419
 		entry['principal'] = spn_to_principal(inentry['spn'])
427 420
 
428
-def simple_kvno_to_full(inentry, index, entry, record, kvno_in_tail=False):
421
+def simple_kvno_to_full(inentry, index, entry, record):
429 422
 	if 'kvno' in inentry:
430 423
 		entry['actual_kvno'] = inentry['kvno']
431 424
 		kvno_too_big = inentry['kvno'] > 255
432 425
 		entry['kvno'] = 0 if kvno_too_big else inentry['kvno']
433
-		if kvno_in_tail:
426
+		if inentry.get('kvno_in_tail', False):
434 427
 			entry['tail_kvno'] = inentry['kvno']
435 428
 			record['tail'] = hexlify(struct.pack('>I', inentry['kvno']))
436 429
 		elif kvno_too_big:
... ...
@@ -465,14 +458,13 @@ def simple_keytab_to_full(indata):
465 458
 	now = int(datetime.now().timestamp())
466 459
 	data = {
467 460
 		'version': indata.get('version', 2),
468
-		'kvno_in_tail': indata.get('kvno_in_tail', False),
469 461
 		'records': [],
470 462
 	}
471 463
 	for index, inentry in enumerate(indata.get('entries', [])):
472 464
 		entry = {}
473 465
 		record = {'entry': entry}
474 466
 		simple_principal_to_full(inentry, index, entry)
475
-		simple_kvno_to_full(inentry, index, entry, record, data['kvno_in_tail'])
467
+		simple_kvno_to_full(inentry, index, entry, record)
476 468
 		simple_timestamp_to_full(inentry, index, entry)
477 469
 		simple_enctype_to_full(inentry, index, entry)
478 470
 		if 'key' in inentry: