Last active
April 6, 2019 10:15
-
-
Save felixfontein/376748017ad65ead093d56a45a5bf376 to your computer and use it in GitHub Desktop.
Create _OID_MAP for Ansible's module_utils/crypto.py from OpenSSL's objects.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import re | |
OIDs = dict() | |
names_lookup = dict() | |
def convert_for_lookup(name): | |
return name.replace('-', '_') | |
def parse_oid(oid_str): | |
parts = [part.strip() for part in oid_str.split(' ') if part.strip()] | |
for i, part in enumerate(parts): | |
if part[0] not in '0123456789': | |
res = names_lookup.get(convert_for_lookup(part)) | |
if res is None: | |
raise Exception('Cannot decode OID "{0}": part #{1} ("{2}") not in lookup'.format(oid_str, i, part)) | |
parts[i] = res | |
return '.'.join(parts) | |
def put_oid(oid, alias, names): | |
if oid not in OIDs: | |
OIDs[oid] = list() | |
for name in names: | |
# print('Registering "{0}" as "{1}"{2}'.format(name, oid, '' if OIDs[oid] else ' (primary)')) | |
names_lookup[convert_for_lookup(name if module is None else (module + '_' + name))] = oid | |
OIDs[oid].append(name) | |
with open('objects.txt', 'rt') as f: | |
module = None | |
aliases = [] | |
for line in f: | |
line = line.strip() | |
if not line or line.startswith('#'): | |
continue | |
if line.startswith('!'): | |
if line.startswith('!global'): | |
module = None | |
elif line.startswith('!module '): | |
module = line[len('!module '):].strip() | |
elif line.startswith('!Cname '): | |
cname = line[len('!Cname '):].strip() | |
aliases.append(cname) | |
elif line.startswith('!Alias '): | |
alias = line[len('!Alias '):].strip() | |
alias, oid = re.split(r'[ \t]', alias, 1) | |
oid = parse_oid(oid.strip()) | |
put_oid(oid, module, (alias.strip(), )) | |
continue | |
parts = [part.strip() for part in line.split(':')] | |
if not parts[0]: | |
continue | |
oid = parse_oid(parts[0]) | |
names = [part for part in reversed(parts[1:]) if part] | |
put_oid(oid, module, names) | |
for alias in aliases: | |
names_lookup[convert_for_lookup(alias if module is None else (module + '_' + alias))] = oid | |
aliases = [] | |
import natsort | |
print('_OID_MAP = {') | |
for oid, names in natsort.humansorted(OIDs.items()): | |
print(" '{0}': ({1}{2}),".format(oid, ', '.join(["'{0}'".format(name) for name in names]), '' if len(names) > 1 else ', ')) | |
print('}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment