Skip to content

Commit

Permalink
Create vault_call.py
Browse files Browse the repository at this point in the history
  • Loading branch information
btkrausen authored Oct 9, 2020
1 parent 9d87751 commit 10012fe
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions vault/scripts/vault_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def vault_call(vserv, vact):

crl = pycurl.Curl()
data = BytesIO()
base_url = "https://" + vserv + ":8200/v1/"

if vact == "login":
vault_url = base_url + "auth/approle/login"
role_id = os.environ.get("ROLE_ID")
sec_id = os.environ.get("SEC_ID")
login_data = json.dumps({"role_id": role_id, "secret_id": sec_id})
crl.setopt(crl.POSTFIELDS, login_data)
elif vact == "get":
vault_url = base_url + "secrets/data/myapp"
vault_header = ["X-Vault-Token: " + cluster_token[vserv]]
crl.setopt(crl.HTTPHEADER, vault_header)
else:
print("Invalid Option")
exit()

crl.setopt(crl.URL, vault_url)
crl.setopt(crl.WRITEFUNCTION, data.write)

try:
crl.perform()
resp_data = json.loads(data.getvalue())
resp_code = crl.getinfo(pycurl.RESPONSE_CODE)
except pycurl.error:
crl.close()
return "Connection Failed"

crl.close()

if resp_code != 200:
return resp_code
elif vact == "login":
return resp_data["auth"]["client_token"]
elif vact == "get":
return resp_data["data"]["data"]
else:
return resp_data

0 comments on commit 10012fe

Please sign in to comment.