Skip to content

Commit a25cfdc

Browse files
Getting employee login command working
1 parent 2b62252 commit a25cfdc

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

SoftLayer/API.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:license: MIT, see LICENSE for more details.
77
"""
88
# pylint: disable=invalid-name
9+
import os
910
import time
1011
import warnings
1112

@@ -144,7 +145,8 @@ def create_client_from_env(username=None,
144145

145146

146147
def employee_client(username=None,
147-
api_key=None,
148+
access_token=None,
149+
password=None,
148150
endpoint_url=None,
149151
timeout=None,
150152
auth=None,
@@ -159,7 +161,8 @@ def employee_client(username=None,
159161
config file.
160162
161163
:param username: your user ID
162-
:param api_key: hash from SoftLayer_User_Employee::performExternalAuthentication(username, password, 2fa_string)
164+
:param access_token: hash from SoftLayer_User_Employee::performExternalAuthentication(username, password, 2fa_string)
165+
:param password: password to use for employee authentication
163166
:param endpoint_url: the API endpoint base URL you wish to connect to.
164167
Set this to API_PRIVATE_ENDPOINT to connect via SoftLayer's private
165168
network.
@@ -185,15 +188,21 @@ def employee_client(username=None,
185188
186189
"""
187190
settings = config.get_client_settings(username=username,
188-
api_key=api_key,
191+
api_key=None,
189192
endpoint_url=endpoint_url,
190193
timeout=timeout,
191194
proxy=proxy,
192195
verify=verify,
193196
config_file=config_file)
194197

198+
url = settings.get('endpoint_url') or consts.API_EMPLOYEE_ENDPOINT
199+
200+
if 'internal' not in url:
201+
raise exceptions.SoftLayerError("{} does not look like an Internal Employee url. Try {}".format(
202+
url, consts.API_EMPLOYEE_ENDPOINT))
203+
195204
if transport is None:
196-
url = settings.get('endpoint_url')
205+
197206
if url is not None and '/rest' in url:
198207
# If this looks like a rest endpoint, use the rest transport
199208
transport = transports.RestTransport(
@@ -214,24 +223,26 @@ def employee_client(username=None,
214223
)
215224

216225

217-
if auth is None and settings.get('username') and settings.get('api_key'):
218-
real_transport = getattr(transport, 'transport', transport)
219-
if isinstance(real_transport, transports.XmlRpcTransport):
220-
auth = slauth.EmployeeAuthentication(
221-
settings.get('username'),
222-
settings.get('api_key'),
223-
)
226+
if access_token is None:
227+
access_token = settings.get('access_token')
228+
229+
user_id = settings.get('user_id')
230+
231+
# Assume access_token is valid for now, user has logged in before at least.
232+
if access_token and user_id:
233+
auth = slauth.EmployeeAuthentication(user_id, access_token)
234+
return EmployeeClient(auth=auth, transport=transport)
235+
else:
236+
return EmployeeClient(auth=None, transport=transport)
224237

225-
return BaseClient(auth=auth, transport=transport)
226238

227239

228240
def Client(**kwargs):
229241
"""Get a SoftLayer API Client using environmental settings.
230242
231243
Deprecated in favor of create_client_from_env()
232244
"""
233-
warnings.warn("use SoftLayer.create_client_from_env() instead",
234-
DeprecationWarning)
245+
warnings.warn("use SoftLayer.create_client_from_env() instead", DeprecationWarning)
235246
return create_client_from_env(**kwargs)
236247

237248

SoftLayer/CLI/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def ensure_client(self, config_file=None, is_demo=False, proxy=None):
198198
)
199199
else:
200200
# Create SL Client
201-
client = SoftLayer.create_client_from_env(
201+
client = SoftLayer.employee_client(
202202
proxy=proxy,
203203
config_file=config_file,
204204
)

SoftLayer/CLI/login.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,51 @@
22
# :license: MIT, see LICENSE for more details.
33

44
import click
5+
import os
56

7+
8+
from SoftLayer.API import EmployeeClient
69
from SoftLayer.CLI.command import SLCommand as SLCommand
7-
from SoftLayer.CLI import config
10+
from SoftLayer import config
811
from SoftLayer.CLI import environment
912

1013

14+
# def get_username(env):
15+
# """Gets the username from config or env"""
16+
# settings =
17+
18+
def censor_password(value):
19+
if value:
20+
value = '*' * len(value)
21+
return value
22+
1123
@click.command(cls=SLCommand)
1224
@environment.pass_env
1325
def cli(env):
1426
"""Logs you into the internal SoftLayer Network.
1527
16-
username and password can be set in SL_USER and SL_PASSWORD env variables. You will be prompted for them otherwise.
28+
username: Set this in either the softlayer config, or SL_USER ENV variable
29+
password: Set this in SL_PASSWORD env variable. You will be prompted for them otherwise.
1730
"""
31+
settings = config.get_config(config_file=env.config_file)
32+
username = settings.get('username') or os.environ.get('SLCLI_USER')
33+
password = os.environ.get('SLCLI_PASSWORD', '')
34+
yubi = 123456
35+
36+
url = settings.get('endpoint_url') or consts.API_EMPLOYEE_ENDPOINT
37+
click.echo("URL: {}".format(url))
38+
if username is None:
39+
username = input("Username: ")
40+
click.echo("Username: {}".format(username))
41+
if password is None:
42+
password = env.getpass("Password: ")
43+
click.echo("Password: {}".format(censor_password(password)))
44+
# yubi = input("Yubi: ")
45+
46+
client = EmployeeClient(config_file=env.config_file)
47+
try:
48+
client.authenticate_with_password(username, password, yubi)
49+
except Exception as e:
50+
click.echo("EXCEPTION: {}".format(e))
1851

1952
print("OK")

SoftLayer/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class EmployeeAuthentication(AuthenticationBase):
143143
"""Token-based authentication class.
144144
145145
:param username str: a user's username
146-
:param api_key str: a user's API key
146+
:param user_hash str: a user's Authentication hash
147147
"""
148148
def __init__(self, user_id, user_hash):
149149
self.user_id = user_id

SoftLayer/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
API_PRIVATE_ENDPOINT = 'https://api.service.softlayer.com/xmlrpc/v3.1/'
1111
API_PUBLIC_ENDPOINT_REST = 'https://api.softlayer.com/rest/v3.1/'
1212
API_PRIVATE_ENDPOINT_REST = 'https://api.service.softlayer.com/rest/v3.1/'
13+
API_EMPLOYEE_ENDPOINT = 'https://internal.app0lb.dal10.softlayer.local/v3/internal/xmlrpc/'
1314
USER_AGENT = "softlayer-python/%s" % VERSION
1415
CONFIG_FILE = "~/.softlayer"

0 commit comments

Comments
 (0)