read certificate(.crt) and key(.key) file in python

2024/10/1 19:58:17

So i'm using the JIRA-Python module to connect to my company's instance on JIRA and it requires me to pass the certificate and key for this. However using the OpenSSL module,i'm unable to read my local certificate and key to pass it along the request.

the code for reading is below

import OpenSSL.crypto 
c = open('/Users/mpadakan/.certs/mpadakan-blr-mpsot-20160704.crt').read()
cert = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, c)

the error i get is

Traceback (most recent call last):
File "flaskApp.py", line 19, in <module>
cert = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, c)
TypeError: must be X509, not str

could someone tell me how to read my local .crt and .key file into x509 objects?

Answer

@can-ibanoglu was right on:

import OpenSSL.crypto 
cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, open('/tmp/server.crt').read()
)>>> cert
<OpenSSL.crypto.X509 object at 0x7f79906a6f50>
https://en.xdnf.cn/q/70882.html

Related Q&A

Admin FileField current url incorrect

In the Django admin, wherever I have a FileField, there is a "currently" box on the edit page, with a hyperlink to the current file. However, this link is appended to the current page url, an…

Difference between generator expression and generator function

Is there any difference — performance or otherwise — between generator expressions and generator functions?In [1]: def f():...: yield from range(4)...:In [2]: def g():...: return (i for i in…

Django performance testing suite thatll report on metrics (db queries etc.)

I have a complex Django web application that has many person-years of work put into it. It might need optimisation sometime. There are several common operation/flows that I could script with (say) djan…

dev_appserver.py Opens a Text File, Does Not Deploy

It works fine on my other computer, but after setting up Google App Engine and creating the main.py and app.yaml files, I run dev_appserver.py app.yaml in Windows command prompt and instead of deployin…

How to pass a list from a view to template in django

I am trying pass to list from a view to template in Django.In my file wiew.py I define the view named hour # This Python file uses the following encoding: utf-8from django.shortcuts import render from …

Probing/sampling/interpolating VTK data using python TVTK or MayaVi

I would like to visualise a VTK data file (OpenFOAM output) using python. The plot I would like to make is a 1-d line plot of a quantity between two endpoints. To do so, the unstructured data should be…

Make Sphinx generate RST class documentation from pydoc

Im currently migrating all existing (incomplete) documentation to Sphinx.The problem is that the documentation uses Python docstrings (the module is written in C, but it probably does not matter) and t…

inspect.getfile () vs inspect.getsourcefile()

I was just going through the inspect module docs.What exactly is the difference between:inspect.getfile()andinspect.getsourcefile()I get exactly the same file path (of the module) for both.

Get a row of data in pandas as a dict

To get a row of data in pandas by index I can do:df.loc[100].tolist()Is there a way to get that row of data as a dict, other than doing:dict(zip(df.columns.tolist(),df.loc[100], tolist() ))

Find duplicate records in large text file

Im on a linux machine (Redhat) and I have an 11GB text file. Each line in the text file contains data for a single record and the first n characters of the line contains a unique identifier for the rec…