def handle(self, *args, **options): role = options['role'] if role not in ROLES: self.stdout.write(self.style.ERROR('Invalid role: %s' % role)) self.stdout.write('Valid roles: %s' % ', '.join(ROLES)) return alt_names = [] for alt_name in options['altNames']: kind, sep, value = alt_name.partition(':') if sep != ':' or kind not in dict(ALT_TYPES): self.stdout.write( self.style.ERROR( 'Altname %s must be of form KIND:VALUE with KIND one of %s' % (alt_name, ', '.join(ALT_TYPES)))) return alt_names.append((kind, value)) for key in 'cert', 'key', 'ssh', 'ca', 'pubssh', 'pubkey': if not options[key]: continue try: with open(options[key], 'wb') as fd: fd.write(b'') except OSError: self.stdout.write( self.style.ERROR('Unable to write file: %s' % options[key])) return entry = CertificateEntry( options['commonName'], organizationalUnitName=options['organizationalUnitName'], emailAddress=options['emailAddress'], localityName=options['localityName'], countryName=options['countryName'], stateOrProvinceName=options['stateOrProvinceName'], altNames=alt_names, role=role) pki = PKI() pki.initialize() if options['initialize']: pki.ensure_ca(entry) else: pki.ensure_certificate(entry) ca_crt_path, ca_key_path = pki.get_subca_infos(entry) for key, attr in ( ('cert', 'crt_filename'), ('key', 'key_filename'), ('ssh', 'key_filename'), ('ca', 'ca_filename'), ('pubssh', 'ssh_filename'), ('pubkey', 'pub_filename'), ): dst_filename = options[key] if not dst_filename: continue if key == 'ca': open(dst_filename, 'ab').write(open(ca_crt_path, 'rb').read()) src_filename = getattr(entry, attr) open(dst_filename, 'ab').write(open(src_filename, 'rb').read()) self.stdout.write('File %s written' % dst_filename)
def handle(self, *args, **options): role = options['role'] if role not in ROLES: self.stdout.write(self.style.ERROR('Invalid role: %s' % role)) self.stdout.write('Valid roles: %s' % ', '.join(ROLES)) return alt_names = [] for alt_name in options['altNames']: kind, sep, value = alt_name.partition(':') if sep != ':' or kind not in dict(ALT_TYPES): self.stdout.write(self.style.ERROR('Altname %s must be of form KIND:VALUE with KIND one of %s' % (alt_name, ', '.join(ALT_TYPES)))) return alt_names.append((kind, value)) for key in 'cert', 'key', 'ssh', 'ca', 'pubssh', 'pubkey': if not options[key]: continue try: with open(options[key], 'wb') as fd: fd.write(b'') except OSError: self.stdout.write(self.style.ERROR('Unable to write file: %s' % options[key])) return entry = CertificateEntry(options['commonName'], organizationalUnitName=options['organizationalUnitName'], emailAddress=options['emailAddress'], localityName=options['localityName'], countryName=options['countryName'], stateOrProvinceName=options['stateOrProvinceName'], altNames=alt_names, role=role) pki = PKI() pki.initialize() if options['initialize']: pki.ensure_ca(entry) else: pki.ensure_certificate(entry) ca_crt_path, ca_key_path = pki.get_subca_infos(entry) for key, attr in (('cert', 'crt_filename'), ('key', 'key_filename'), ('ssh', 'key_filename'), ('ca', 'ca_filename'), ('pubssh', 'ssh_filename'), ('pubkey', 'pub_filename'), ): dst_filename = options[key] if not dst_filename: continue if key == 'ca': open(dst_filename, 'ab').write(open(ca_crt_path, 'rb').read()) src_filename = getattr(entry, attr) open(dst_filename, 'ab').write(open(src_filename, 'rb').read()) self.stdout.write('File %s written' % dst_filename)
def __init__(self, entry, ensure_entry=True, **kwargs): pki = PKI() if ensure_entry: pki.ensure_certificate(entry) content = b'' # noinspection PyTypeChecker with open(entry.key_filename, 'rb') as fd: content += fd.read() # noinspection PyTypeChecker with open(entry.crt_filename, 'rb') as fd: content += fd.read() ca_crt_path, ca_key_path = pki.get_subca_infos(entry) # noinspection PyTypeChecker with open(ca_crt_path, 'rb') as fd: content += fd.read() # noinspection PyTypeChecker with open(entry.ca_filename, 'rb') as fd: content += fd.read() super(CertificateEntryResponse, self).__init__(content=content, content_type='text/plain', **kwargs)
def __init__(self, entry, ensure_entry=True, **kwargs): pki = PKI() if ensure_entry: pki.ensure_certificate(entry) content = b'' # noinspection PyTypeChecker with open(entry.key_filename, 'rb') as fd: content += fd.read() # noinspection PyTypeChecker with open(entry.crt_filename, 'rb') as fd: content += fd.read() ca_crt_path, ca_key_path = pki.get_subca_infos(entry) # noinspection PyTypeChecker with open(ca_crt_path, 'rb') as fd: content += fd.read() # noinspection PyTypeChecker with open(entry.ca_filename, 'rb') as fd: content += fd.read() super(CertificateEntryResponse, self).__init__(content=content, content_type='text/plain', **kwargs)