How to execute a API call using OAuth authentication method via python script in Red Hat Satellite 6 ?
Environment
- Red Hat Satellite 6
Issue
- Execute a Satellite API using OAuth authentication method via Python Script.
Resolution
-
This method eliminates the direct\visible use of any satellite
usernameorpasswordinside a REST API call to perform any operation on Red Hat Satellite 6. -
To perform an API call using OAuth authentication, first retrieve the values of oauth_consumer_key and oauth_consumer_secret from satellite server.
# egrep "oauth_consumer" /etc/foreman/settings.yaml
:oauth_consumer_key: kjvcwBJGHmWPntS53KRFzutkr7tP6Gyf
:oauth_consumer_secret: pYJh67uh9x76aeAaoe3exwgVoTZw3bN8
- Below is an example
python scriptto collect the list of organization present in satellite server.
#!/usr/bin/env python
# Make sure to install "requests_oauthlib" module using pip i.e "pip install requests_oauthlib"
# Import necessary modules to work with
import requests
from requests_oauthlib import OAuth1Session
import json
import sys
# Define the API endpoint that you want to work with
# URL=("https://satellite.example.com/api/v2/hosts")
URL=("https://satellite.example.com/api/v2/organizations")
# Define oauth session parameters and authentication details.
# Use the command 'egrep "oauth_consumer" /etc/foreman/settings.yaml' on satellite server to get the value of oauth key and secret.
Key='kjvcwBJGHmWPntS53KRFzutkr7tP6Gyf'
Secret='pYJh67uh9x76aeAaoe3exwgVoTZw3bN8'
User='admin'
session=OAuth1Session(Key, Secret)
session.headers.update({'FOREMAN-USER': User})
# You can set the SSL verification to False. Uncomment the line below and comment out any other "SSL_VERIFY" parameters defined.
# SSL_VERIFY = False
# If you are running this script from satellite server then uncomment the line below and comment out any other "SSL_VERIFY" parameters defined.
# SSL_VERIFY = '/etc/pki/katello/certs/katello-server-ca.crt'
# If you are running this script from any client host of satellite server then uncomment the line below and comment out any other "SSL_VERIFY" parameters defined.
SSL_VERIFY = '/etc/rhsm/ca/katello-server-ca.pem'
# Send the API request and collect it in JSON format
request=session.get(URL, verify=SSL_VERIFY).json()
# print the collected json data in pretty format
print(json.dumps(request, indent = 4, sort_keys=True))
- In order to execute the above script using python, the system should have the requests_oauthlib module installed via pip , from where this script will be executed.
pip install requests_oauthlib
- This module requests_oauthlib is responsible for generating the required OAuth Signature in mac-sha1 method using oauth_consumer_key and oauth_consumer_secret and helps in authenticating with the API session.
SBR
Product(s)
Components
Category
Tags
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.