Instantly share code, notes, and snippets.

@bartv /auth.py
Last active Aug 29, 2015

Embed
What would you like to do?
#!/usr/bin/env python
import httplib, socket, sys, os
if len(sys.argv) != 2:
print("The first argument should be the path of the file with the user credentials")
sys.exit(2)
if not os.path.exists(sys.argv[1]):
print("Unable to read the credential file")
sys.exit(2)
lines = []
with open(sys.argv[1], "r") as fd:
lines = fd.readlines()
if len(lines) != 2:
print("Invalid file")
sys.exit(2)
username = lines[0].strip()
password = lines[1].strip()
tenant = "openvpn"
body = '{"auth": {"tenantName": "%s", "passwordCredentials": {"username": "%s", "password": "%s"}}}' % (tenant, username, password)
# do the request
try:
conn = httplib.HTTPConnection("keystone.example.com", 5000)
conn.request("POST", "/v2.0/tokens", body = body, headers = {
"Content-Type" : "application/json",
"Accept" : "application/json",
"User-Agent": "python-httplib"})
r1 = conn.getresponse()
if r1.status == 200:
sys.exit(0)
except socket.error as e:
print("Unable to connect to keystone")
sys.exit(10)
print("Authentication to openstack failed.")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment