-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf-os.py
More file actions
128 lines (114 loc) · 3.73 KB
/
Copy pathcf-os.py
File metadata and controls
128 lines (114 loc) · 3.73 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
#
# This is demonstration code for using Python as a client for the
# CF environment.
#
# It lists applications running in the cloud foundry environment, and
# then prints out the route mappings associated with each.
#
# This leverages existing CF credentials (if they exist) and attempts
# to refresh the credentials. If that fails, it reverts to requesting
# username/password for the CF environment.
#
# It's not bug-free, and could probably be improved - but it should
# get anyone started.
#
# inspiration / borrowed from
# http://stackoverflow.com/questions/34717166/how-to-list-all-apps-in-cloudfoundry-using-python
import os
import sys
import getpass
import requests
import json
should_verify = False
if not should_verify:
import urllib3
urllib3.disable_warnings()
# attempt a login; get username/password
def cf_login(config):
username = input('username: ')
password = getpass.getpass('password: ')
oauth_r = requests.post(
config['AuthorizationEndpoint'] + "/oauth/token",
data={
'username': username,
'password': password,
'grant_type': 'password',
'client_id': 'cf'},
auth=('cf', ''),
verify=should_verify)
if not oauth_r.ok:
print("Error in authentication: ", oauth_r.json()['error_description'])
return {}
return oauth_r.json()
# attempt to refresh an existing token - expect "refresh token"
def cf_refresh(config):
oauth_r = requests.post(
config['AuthorizationEndpoint'] + "/oauth/token",
data={
'refresh_token': config['RefreshToken'],
'grant_type': 'refresh_token',
'client_id': 'cf'},
auth=('cf', ''),
verify=should_verify)
if not oauth_r.ok:
print("Error in token refresh:", oauth_r.json()['error_description'])
return {}
return oauth_r.json()
# initialize to "no access key"
access_key = ''
# try to refresh any existing credentials
config_file = os.getenv('CF_HOME')
if len(config_file) == 0:
config_file = os.getenv('HOME')
config_file = config_file + "/.cf/config.json"
with open(config_file, "r") as f:
config = json.load(f)
refresh_json = cf_refresh(config)
# print json.dumps(config, indent=2)
if len(refresh_json):
access_key = "{} {}".format(
refresh_json['token_type'],
refresh_json['access_token'])
else:
print("Credentials refresh failed")
# if a refresh was unsuccessful, revert to username/password
if access_key == '':
# use new credentials from scratch
login_json = cf_login(config)
if len(login_json):
access_key = "{} {}".format(
login_json['token_type'],
login_json['access_token'])
else:
print("Login failed")
if access_key == '':
print("All access methods failed. Cannot connect")
sys.exit(2)
else:
print("Authorization permitted, getting list of apps")
# get list of applications
apps_r = requests.get(
config['Target'] + "/v2/apps",
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'Request-Type': 'application/json',
'Authorization': access_key},
verify=should_verify)
print(json.dumps(apps_r.json(), indent=2))
# get their routes
resources = apps_r.json()['resources']
for r in resources:
route = r['entity']['route_mappings_url']
print("Entity Route", route)
r = requests.get(
config['Target'] + route,
headers={'client_id': 'cf',
'Authorization': access_key,
'Request-Type': 'application/json',
'Content-Type': 'application/json'},
verify=should_verify)
if r.ok:
print(json.dumps(r.json(), indent=2))
else:
print("Error:", r.text)