Skip to main content

Intersight Authentication helper for requests

Project description

CI Tests

intersight-auth

This module provides an authentication helper for requests to make it easy to make Intersight API calls using requests.

Features

  • Supports both v2 and v3 Intersight API keys for authentication
  • Supports OAuth authentication
  • Keys can be supplied as strings or path to a PEM file

Install

pip install intersight-auth

Example using a file for the secret key

import sys

from intersight_auth import IntersightAuth
from requests import Session

session = Session()
session.auth = IntersightAuth("XYZ/XYZ/XYZ", "key.pem")

response = session.get("https://intersight.com/api/v1/ntp/Policies")

if not response.ok:
    print(f"Error: {response.status_code} {response.reason}")
    sys.exit(1)

for policy in response.json()["Results"]:
    print(f"{policy['Name']}")

Example using a multiline (a.k.a. heredoc) string for the secret key

The secret key must still be in PEM format even if it's a string instead of a file.

my_secret_key='''
-----BEGIN RSA PRIVATE KEY-----
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjklmnopqrstuvwxy
ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/abcdefghizjkl=
-----END RSA PRIVATE KEY-----
'''

session = Session()
session.auth = IntersightAuth(
    api_key_id="XYZ/XYZ/XYZ",
    secret_key_string=my_secret_key
    )

response = session.get("https://intersight.com/api/v1/ntp/Policies")

if not response.ok:
    print(f"Error: {response.status_code} {response.reason}")
    sys.exit(1)

for policy in response.json()["Results"]:
    print(f"{policy['Name']}")

Example using OAuth authentication

import sys

from intersight_auth import IntersightAuth
from requests import Session

oauth_client_id = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef123456789"
oauth_client_secret = "1234567890abcdef1234567890abcdef"

session = Session()
session.auth = IntersightAuth(oauth_client_id=oauth_client_id, oauth_client_secret=oauth_client_secret)

response = session.get("https://intersight.com/api/v1/ntp/Policies")

if not response.ok:
    print(f"Error: {response.status_code} {response.reason}")
    sys.exit(1)

for policy in response.json()["Results"]:
    print(f"{policy['Name']}")

Configuration from environment variables

If you leave the parameters empty, IntersightAuth will attempt to automatically discover the API key or OAuth credentials from the environment variables:

session = Session()
session.auth = IntersightAuth()

response = session.get("https://intersight.com/api/v1/ntp/Policies")

...

The following environment variables can be used:

  • IS_KEY_ID
  • IS_KEY
  • IS_KEY_FILE
  • IS_KEY_PASSWORD
  • IS_OAUTH_CLIENT_ID
  • IS_OAUTH_CLIENT_SECRET
  • INTERSIGHT_KEY_ID
  • INTERSIGHT_KEY
  • INTERSIGHT_KEY_FILE
  • INTERSIGHT_KEY_PASSWORD
  • INTERSIGHT_OAUTH_CLIENT_ID
  • INTERSIGHT_OAUTH_CLIENT_SECRET

Example of PEM repair

The need to present the secret key in PEM format can be a challenge with some secret management approaches. The PEM could be collapsed onto a single line, or the whitespace could otherwise be disturbed. A function is provided to attempt to resolve these kinds of issues.

from intersight_auth import IntersightAuth, repair_pem

# This PEM has required the whitespace removed
broken_pem = "-----BEGIN EC PRIVATE KEY-----ABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900abcdefghizjklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900abcdefghizjklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900abcdefghizjklmnopq-----END EC PRIVATE KEY-----"

session = Session()
session.auth = IntersightAuth(
    api_key_id="XYZ/XYZ/XYZ",
    secret_key_string=repair_pem(broken_pem)
    )

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

intersight_auth-0.3.3.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

intersight_auth-0.3.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file intersight_auth-0.3.3.tar.gz.

File metadata

  • Download URL: intersight_auth-0.3.3.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.1 Linux/6.11.0-1015-azure

File hashes

Hashes for intersight_auth-0.3.3.tar.gz
Algorithm Hash digest
SHA256 a3ee56daa71809e8d8ebbd4bb693afa7205a4ca2812643e286073e9f35c3eb46
MD5 6924d4e32a1d63bfa149defc2d6b6d20
BLAKE2b-256 aac3ad49280a47eac8ee6c1ba321fb0e924e318ebc1c8ff4021d0e7bc63ad54a

See more details on using hashes here.

File details

Details for the file intersight_auth-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: intersight_auth-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.1 Linux/6.11.0-1015-azure

File hashes

Hashes for intersight_auth-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7647dc42db37037a96e853fe5b657e95ffec25180bb95e03ee2b6505c23ec74b
MD5 82b12058ea62978e6386633a4d4a2297
BLAKE2b-256 4fbbc35526978374c4a494b226843a91f3e85da13c7032065337b193a2ff655c

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