-
Notifications
You must be signed in to change notification settings - Fork 4
/
getBucketCred.py
executable file
·99 lines (80 loc) · 3.1 KB
/
getBucketCred.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
"""
This script will automatically connect to the ParallelWorks gateway to retrieve
information about storage resources using the user's API key.
It will then generate short-term credentials for the buckets provided
Critical files that must exist:
$HOME/.ssh/pw_api.key - this file must contain the API key in the first and only
line. Treat this file as any secure file and place in
.ssh directory. Change permissions to mode 600.
"""
import subprocess
import json
import requests
import sys
import time
import os
from client import Client
# inputs
PW_PLATFORM_HOST = None
if 'PW_PLATFORM_HOST' in os.environ:
PW_PLATFORM_HOST = os.environ['PW_PLATFORM_HOST']
else:
print("No PW_PLATFORM_HOST environment variable found. Please set it to the Parallel Works platform host name. e.g. cloud.parallel.works")
sys.exit(1)
pw_url = "https://" + PW_PLATFORM_HOST
# specify the clusters to start and wait for activation
buckets_to_access = sys.argv[1].split(',')
print('\nGenerating credentials for buckets:', buckets_to_access)
# Get user specific files
homedir = os.environ['HOME']
# The .hosts file will get re-written every time
keyfile = homedir + '/.ssh/pw_api.key'
# get my personal API key
# with the environment variable PW_API_KEY taking precedence
# over the file $HOME/.ssh/pw_api.key
api_key = None
if 'PW_API_KEY' in os.environ:
api_key = os.environ['PW_API_KEY']
else:
try:
f = open(keyfile, "r")
api_key = f.readline().strip()
f.close()
except:
pass
if api_key is None or api_key == "":
print("No API key found. Please set the environment variable PW_API_KEY or create the file $HOME/.ssh/pw_api.key.")
sys.exit(1)
# create a new Parallel Works client
c = Client(pw_url, api_key)
# get the account username
session = c.get_identity()
user = session['username']
print("\nRunning as user", user+'...')
my_buckets = c.get_storages()
for bucket_name in buckets_to_access:
try:
bucket_name = bucket_name.split('/')
bucket_namespace = bucket_name[0]
bucket_name = bucket_name[1]
except IndexError:
print("No namespace provided for", bucket_name[0]+".", "Default to current user", user)
bucket_name = bucket_name[0]
bucket_namespace = user
print("\nLooking for bucket", bucket_name, "in namespace", bucket_namespace+"...")
# check if resource exists
# find bucket_name in my_storages and map to ID
# this logic currently only lets you get creds for buckets you own
bucket = next(
(item for item in my_buckets if item["name"] == bucket_name and item["namespace"] == bucket_namespace), None)
if "bucket" not in bucket['type']:
print("Storage provided is not a bucket.")
elif bucket['provisioned'] != True:
print("Bucket provided is not currently provisioned.")
elif bucket:
print("Identified bucket", bucket['name'], "as", bucket['id'])
# generate short-term bucket credentials
print(c.get_bucket_cred(bucket['id']))
else:
print("No bucket found.")