Skip to content

Commit 1313133

Browse files
committed
let creds env vars override creds file
1 parent 861dd82 commit 1313133

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

netfoundry/organization.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,50 +62,65 @@ def __init__(self,
6262
os.environ['NETFOUNDRY_API_ACCOUNT'] = self.credentials
6363
elif 'NETFOUNDRY_API_ACCOUNT' in os.environ:
6464
self.credentials = os.environ['NETFOUNDRY_API_ACCOUNT']
65+
# if any credentials var then require all credentials vars
66+
elif ('NETFOUNDRY_CLIENT_ID' in os.environ
67+
or 'NETFOUNDRY_PASSWORD' in os.environ
68+
or 'NETFOUNDRY_OAUTH_URL' in os.environ):
69+
if ('NETFOUNDRY_CLIENT_ID' in os.environ
70+
and 'NETFOUNDRY_PASSWORD' in os.environ
71+
and 'NETFOUNDRY_OAUTH_URL' in os.environ):
72+
client_id = os.environ['NETFOUNDRY_CLIENT_ID']
73+
password = os.environ['NETFOUNDRY_PASSWORD']
74+
token_endpoint = os.environ['NETFOUNDRY_OAUTH_URL']
75+
else:
76+
raise Exception("ERROR: some but not all credentials vars present. Need NETFOUNDRY_CLIENT_ID, NETFOUNDRY_PASSWORD, and NETFOUNDRY_OAUTH_URL or a credentials file in default file locations or NETFOUNDRY_API_ACCOUNT as path to credentials file.")
6577
else:
6678
self.credentials = "credentials.json"
6779

68-
# if no token or near expiry (30 min) then use credentials to obtain a token
80+
# if no token or near expiry (30 min) then use env vars or credentials file to obtain a token
6981
if epoch is None or epoch > (expiry - 600):
70-
# unless a valid path assume relative and search the default chain
71-
if not os.path.exists(self.credentials):
72-
default_creds_chain = [
73-
{
74-
"scope": "project",
75-
"base": str(Path.cwd())
76-
},
77-
{
78-
"scope": "user",
79-
"base": str(Path.home())+"/.netfoundry"
80-
},
81-
{
82-
"scope": "device",
83-
"base": "/netfoundry"
84-
}
85-
]
86-
for link in default_creds_chain:
87-
candidate = link['base']+"/"+self.credentials
88-
if os.path.exists(candidate):
89-
print("INFO: using credentials in {path} (found in {scope}-default directory)".format(
90-
scope=link['scope'],
91-
path=candidate
92-
))
93-
self.credentials = candidate
94-
break
95-
else:
96-
print("INFO: using credentials in {path}".format(
97-
path=self.credentials
98-
))
82+
# if not creds as env vars then look for creds file
83+
if not client_id and password and token_endpoint:
84+
# unless a valid path assume relative and search the default chain
85+
if not os.path.exists(self.credentials):
86+
default_creds_chain = [
87+
{
88+
"scope": "project",
89+
"base": str(Path.cwd())
90+
},
91+
{
92+
"scope": "user",
93+
"base": str(Path.home())+"/.netfoundry"
94+
},
95+
{
96+
"scope": "device",
97+
"base": "/netfoundry"
98+
}
99+
]
100+
for link in default_creds_chain:
101+
candidate = link['base']+"/"+self.credentials
102+
if os.path.exists(candidate):
103+
print("INFO: using credentials in {path} (found in {scope}-default directory)".format(
104+
scope=link['scope'],
105+
path=candidate
106+
))
107+
self.credentials = candidate
108+
break
109+
else:
110+
print("INFO: using credentials in {path}".format(
111+
path=self.credentials
112+
))
113+
114+
try:
115+
with open(self.credentials) as file:
116+
try: account = json.load(file)
117+
except: raise Exception("ERROR: failed to load JSON from {file}".format(file=file))
118+
except: raise Exception("ERROR: failed to open {file} while working in {dir}".format(
119+
file=self.credentials,dir=str(Path.cwd())))
120+
token_endpoint = account['authenticationUrl']
121+
client_id = account['clientId']
122+
password = account['password']
99123

100-
try:
101-
with open(self.credentials) as file:
102-
try: account = json.load(file)
103-
except: raise Exception("ERROR: failed to load JSON from {file}".format(file=file))
104-
except: raise Exception("ERROR: failed to open {file} while working in {dir}".format(
105-
file=self.credentials,dir=str(Path.cwd())))
106-
token_endpoint = account['authenticationUrl']
107-
client_id = account['clientId']
108-
password = account['password']
109124
# extract the environment name from the authorization URL aka token API endpoint
110125
self.environment = re.sub(r'https://netfoundry-([^-]+)-.*', r'\1', token_endpoint, re.IGNORECASE)
111126
# re: scope: we're not using scopes with Cognito, but a non-empty value is required;

0 commit comments

Comments
 (0)