Skip to main content

No project description provided

Project description

Build Status

CERN Requests

Enables using requests without having to configure the CERN Root certificates or getting an API access token manually.

Inspired by certifi, requests-kerberos, cern-sso-python and api-access-examples.

The Root certificate bundle is copied from the linuxsoft cern page and can also be created manually by downloading the CERN Grid Certification Authority files from cafiles.cern.ch/cafiles.

Installation

Requires Python 2.7 or Python 3.4+

pip install cernrequests

Prerequisites

For sites requiring an SSL Grid certificate

Request a Grid User Certificate (with password) and convert into public and private key:

mkdir -p ~/private
openssl pkcs12 -in myCertificate.p12 -clcerts -nokeys -out ~/private/usercert.pem  # Will ask for the certificate password
openssl pkcs12 -in myCertificate.p12 -nocerts -nodes -out ~/private/userkey.pem  # Will ask for the certificate password

The .pem certificates have to be passwordless.

For CERN APIs using the ""new"" SSO

An .env file at the root of your project with the following variables set:

  • SSO_CLIENT_ID
  • SSO_CLIENT_SECRET

(You can rename the .env_sample file to .env and add the values there).

To request them, you will need to register your application:

  1. Create an SSO registration for your application on the CERN Application Portal:

  2. Add an application identifier and description:

    The Application Identifier can be anything, it's like a username for your application.t

    Click Submit.

  3. Go back to the Application Portal and edit the SSO application (green button). Then, go to the SSO Registration tab and click the plus button:

  4. Fill out the form of the new SSO registration as follows:

    • You can put any value in the Redirect URI(s), e.g. http://localhost/*
    • Same for the Base URL
    • Make sure you click My application will need to get tokens using its own client ID and secret.
  5. Submit the form:

    Note the client id and client secret that the form will show you.

Usage

Example

With Grid Certificates

import cernrequests

url = "https://<your-cern-website>"
response = cernrequests.get(url)

With API Token

If you want to access a website which requires a (""new"") CERN Single Sign-on token you can do the following:

import cernrequests

url = "https://<your-cern-website-url>"
reponse = cernrequests.get_with_token(url, target_audience="<the SSO id of the target URL>")

Note The target_audience depends on the SSO registration name of the target application. E.g. if you want to access the development instance of Run Registry, target_audience should be dev-cmsrunregistry-sso-proxy. In case of doubt, communicate with the app's developers directly.

get_api_token

This is a method that needs a target_application parameter and will try and get an API token, using the SSO_CLIENT_ID and SSO_CLIENT_SECRET provided as environment variables.

Alternative usage

If you want to use requests directly without the CERN wrapper you can get the exact same functionality by doing:

import requests
from cernrequests import certs

url = "https://<your-cern-website>"
cert = certs.default_user_certificate_paths()
ca_bundle = certs.where()

response = requests.get(url, cert=cert, verify=ca_bundle)

Configuration

Grid certificates

The default user certificate paths are first ~\private\ and ~\.globus\ for fallback. The default public key file name is usercert.pem and the default private key file name is userkey.pem

You can configure the default grid user certificate path by setting the CERN_CERTIFICATE_PATH environment variable.

For example:

export CERN_CERTIFICATE_PATH=${HOME}/my_custom_folder

This will still assume that your filenames are usercert.pem and userkey.pem Write this line in your .bashrc to make the configuration persistent.

Alternatively you can also specify the paths directly in your code:

import cernrequests

url = "https://<your-cern-website>"
cert = "my/custom/path/cert.pem"    # Public key path
key = "my/custom/path/key.pem"      # Private key path

cernrequests.get(url, cert=(cert,key))

This way you can even use custom names such as cert.pem and key.pem

Testing

python -m venv venv
source venv/bin/activate
pip install -r testing-requirements.txt
pytest

FAQ

I'm getting certificate verify failed! What should I do?

The cernrequests/cern-cacerts.pem file has expired, and will need to be updated by the library maintainer.

  1.  git clone https://gitlab.cern.ch/linuxsupport/rpms/cern-ca-certs/
     cd cern-ca-certs/src
     make
    
    This will create a CERN-bundle.pem file.
  2. Rename it to cern-cacerts.pem and replace the original .pem certificate chain.

Verify that the certs work by running pytest.

I'm getting 403 Client Error: Forbidden for url: https://login.cern.ch/adfs/ls/auth/sslclient errors!1 What should I do?

  1. Your grid certificate may have expired. Try creating a new one.
  2. You may be trying to access a CERN webpage using a grid certificate, but this method may be deprecated. Make sure that the web page allows SSL certificate authentication.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cernrequests-0.4.2.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cernrequests-0.4.2-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file cernrequests-0.4.2.tar.gz.

File metadata

  • Download URL: cernrequests-0.4.2.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for cernrequests-0.4.2.tar.gz
Algorithm Hash digest
SHA256 4522d3ec94dfec88e510bc7f771c775417b5f312f3fdaf6a904c3ada51217c44
MD5 7772606a42df20b432559b1695e9c0e8
BLAKE2b-256 d4b37348618dd472857795978f32b85b36c8d628859616d387a88c35a7f427d0

See more details on using hashes here.

File details

Details for the file cernrequests-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: cernrequests-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for cernrequests-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fb27f1d9f0e696e7b34f76f47dc76d467bf7c5cfc1d994a2ea0c8c5d5433f5d0
MD5 33123087853d351dcd601b4e189d8a17
BLAKE2b-256 ec04a99121c6741988690b4e7339583a0be5e0659441b23631ab02049c3a0ec4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page