Access company jira server with python-jira using certificates - python

My company uses its own multi-factor authentication to login to our jira server. Everything I have searched for only suggests how to connect to the server using username/password or an api-token. But I need to use our generated certificates to try and connect.
This is what I have so far:
import os
import requests
from atlassian import Jira
#certificates for request
CERT = '/path/to/cert'
KEY = '/path/to/key'
session = requests.Session()
session.cert = (CERT, KEY)
session.timeout = 5
jira = Jira(
url="url",
session=session)
issue = jira.issue('issue')
When I run this code I get this error:
SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain)
Any ideas on how to go about this?

try this code, if not work please can you contact with me provide me your email
import os
import requests
from atlassian import Jira
#certificates for request
CERT = '/path/to/cert'
KEY = '/path/to/key'
session = requests.Session()
session.cert = (CERT, KEY)
session.timeout = 5
jira = Jira(
url="url",
session=session)
# add this code
jira.verify_ssl = False
issue = jira.issue('issue')

Related

Failed to connect to SharePoint site: list index out of range

import sys
import requests
from bs4 import BeautifulSoup
from shareplum.site import Version
from shareplum import Site, Office365
SHAREPOINT_URL = 'https://***************.com/'
SHAREPOINT_SITE = 'https://***********.com/sites/PowerApp/Lists/changed_class_schedule/AllItems.aspx?skipSignal=true'
SHAREPOINT_LIST = 'changed_class_schedule'
USERNAME = '*****************************'
PASSWORD = '****************'
def authenticate(sp_url, sp_site, user_name, password):
"""
Takes a SharePoint url, site url, username and password to access the SharePoint site.
Returns a SharePoint Site instance if passing the authentication, returns None otherwise.
"""
site = None
try:
authcookie = Office365(SHAREPOINT_URL, username=USERNAME, password=PASSWORD).GetCookies()
site = Site(SHAREPOINT_SITE, version=Version.v365, authcookie=authcookie,verify_ssl=True)
except: s
# We should log the specific type of error occurred.
print('Failed to connect to SP site: {}'.format(sys.exc_info()[1]))
return site
# Test the function
sp_site = authenticate(SHAREPOINT_URL,SHAREPOINT_SITE,USERNAME,PASSWORD)
I am trying to connect to my sharepoint list using SharePlum but i cant debug this "Failed to connect to SP site: list index out of range"
Can anyone help me? Sorry if i have to use *** in my code because of security
The SharePoint site url should be as follows:
SHAREPOINT_SITE = 'https://***********.com
OR
SHAREPOINT_SITE = 'https://***********.com/sites/PowerApp/

How to use a tempfile for certificate based authentication?

I want to fetch an OAuth token using client credentials with an X.509 certificate. I am using requests-oauthlib with the OAuth2 backend application flow. I'm getting the certificate and key at runtime from the framework I'm running in as strings not as file paths. Also I need to override the fetch_token() method of the library's OAuth2Session class to enable use of certificates because that's not yet available in the current 1.3.0 release.
The following works fine (with CertSession being my cert-enabled version of OAuth2Session):
from oauthlib.oauth2 import BackendApplicationClient
from .sessions import CertSession
# ... code to obtain client credentials (client_id, cert, key) from framework...
client = BackendApplicationClient(client_id=client_id)
session = CertSession(client=client)
token = None
with open('cert_file.pem', 'w') as cert_file:
cert_file.write(cert)
with open('key_file.pem', 'w') as key_file:
key_file.write(key)
try:
token = session.fetch_token(token_url=token_url, include_client_id=True, cert=(cert_file.name, key_file.name))
print(token)
except Exception as e:
print(str(e))
However, I feel a bit uncomfortable with using plain old files for storing the certificate files. So I was trying to do the same using tempfile:
from oauthlib.oauth2 import BackendApplicationClient
from .sessions import CertSession
from tempfile import NamedTemporaryFile
# ... code to obtain client credentials (client_id, cert, key) from framework...
client = BackendApplicationClient(client_id=client_id)
session = CertSession(client=client)
token = None
cert_file = NamedTemporaryFile(mode='w', suffix='.pem')
cert_file.write(cert)
cert_file.flush()
key_file = NamedTemporaryFile(mode='w', suffix='.pem')
key_file.write(key)
key_file.flush()
try:
token = session.fetch_token(token_url=token_url, include_client_id=True, cert=(cert_file.name, key_file.name))
print(token)
except Exception as e:
print(str(e))
which gives me
('Connection aborted.', PermissionError(13, 'Permission denied'))
What am I doing wrong?
Edit: It works with tempfiles if I open them with delete=False but that kinda defeats the purpose of using tempfiles in the first place, doesn't it?

Python to sharepoint Forbidden for URL

I was looking to connect to my corporate sharepoint list but for testing purposes i am using this dummy templatei created.
from shareplum import Site
from requests_ntlm import HttpNtlmAuth
cred = HttpNtlmAuth('email I use to login', 'password')
site = Site('https://griffithcollege628.sharepoint.com/sites/Sharepointtest/', auth=cred)
sp_list = site.List('list name')
list_data = sp_list.GetListItems()
But when i get to this line:
site = Site('https://griffithcollege628.sharepoint.com/sites/Sharepointtest/', auth=cred)
I get the following error:
Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url: https://griffithcollege628.sharepoint.com/sites/Sharepointtest//_vti_bin/lists.asmx
I have tried with the below method and I am able to get the list items.
Let me know if it is working.
from shareplum import Office365, Site
from shareplum.site import Version
server_url = "https://my_page.sharepoint.com/"
site_url = server_url + "sites/my_site_name"
authcookie = Office365(server_url, username=Username,password=Password).GetCookies() #here username is my mail ID
site = Site(site_url, version=Version.v365, authcookie=authcookie)
sp_list = site.List('my_list_name')
sp_data = sp_list.GetListItems()
There should only be one / before the _vti_bin. Try removing the trailing / from your site variable.
I don't do python, so I don't know if that authentication thing works, but I know SharePoint enough to see that the URL with the double // is invalid.
Please check if the user ID you are using to connect is added to the SharePoint Site.

Soap call using pfx or p12 authentication in Zeep or suds library Python

I want to use SOAP call service using .p12 certificate file. I tried below code. But it throws path error.
Is there any solution for this, in suds python3?
*TypeError: stat: path should be string, bytes, os.PathLike or integer, not X509*
from requests import Session
from zeep.transports import Transport
from zeep import Client
from OpenSSL import crypto
pkcs12 = crypto.load_pkcs12(open(CertPath, 'rb').read(), Password)
session = Session()
session.cert = (pkcs12.get_certificate(),pkcs12.get_privatekey())
transport = Transport(session=session)
client = Client(
'http://my.own.sslhost.local/service?WSDL',
transport=transport)
You'll need to convert the p12 cert into the pem format before adding into session.cert, e.g. something like below:
pkcs12 = crypto.load_pkcs12(open(CertPath, 'rb').read(), Password)
cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pkcs12.get_certificate())
key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey())
with open('cert.pem', 'wb') as f:
f.write(cert)
with open('key.pem', 'wb') as f:
f.write(key)
session.cert = ('cert.pem', 'key.pem')

Python code for sending requests with certificate, private encrypted key and password

I'm trying to fetch response from an https call which has certificate installed atits side. This is my code
import requests
import urllib3
urllib3.disable_warnings()
cert_file_path = "/path/output-crt-file-name.crt"
key_file_path = "/path/output-key-file-name.key"
passwd = 'secretpass'
print(passwd)
url = "https://url/to/fetch/response"
params = {"AppID": "xxxx", "Safe": "xxxx", "Folder": "Root",
"Object": "xxxx"}
cert = (cert_file_path, key_file_path, passwd)
r = requests.get(url, params=params, cert=cert, verify=True )
print(r.text)
which throws error
Caused by SSLError('Client private key is encrypted, password is required'
Please suggest.
I'm afraid requests doesn't currently support using encrypted private key, see https://2.python-requests.org/en/master/user/advanced/#client-side-certificates:
The private key to your local certificate must be unencrypted.
Currently, Requests does not support using encrypted keys.
See https://security.stackexchange.com/questions/59136/can-i-add-a-password-to-an-existing-private-key for instructions how to remove your key's encryption.

Categories