def clean(self, value, *args, **kwargs):
value = super().clean(value, *args, **kwargs)
if isinstance(value, UploadedFile):
value.open('rb')
value.seek(0)
content = value.read()
if content.startswith(b'-----BEGIN CERTIFICATE-----') and b'-----BEGIN CERTIFICATE-----' in content:
return SimpleUploadedFile('cert.pem', content, 'text/plain')
openssl_cmd = [
'openssl',
'x509',
'-inform',
'DER',
'-outform',
'PEM',
]
process = subprocess.Popen(
openssl_cmd,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
)
process.stdin.write(content)
pem, error = process.communicate()
if process.returncode != 0:
logger.info('Trying to convert a DER to PEM failed: {}'.format(error))
raise ValidationError(
_('This does not look like a X509 certificate in either PEM or DER format'),
)
return SimpleUploadedFile('cert.pem', pem, 'text/plain')
return value
评论列表
文章目录