Skip to content

Commit 09e689e

Browse files
akostadinovdjzager
authored andcommitted
Bug 1573714 - PoC token arg support (#258)
Bug 1573714 - PoC token arg support
1 parent a956a1d commit 09e689e

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/apb/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ def main():
611611
default=os.getcwd()
612612
)
613613

614+
parser.add_argument(
615+
'--token',
616+
action='store',
617+
dest='auth_token',
618+
help=u'Specify OpenShift auth token to be used',
619+
default=None
620+
)
621+
614622
subparsers = parser.add_subparsers(title='subcommand', dest='subcommand')
615623
subparsers.required = True
616624

src/apb/engine.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ def relist_service_broker(kwargs):
430430
base64.b64encode("{0}:{1}".format(kwargs['basic_auth_username'],
431431
kwargs['basic_auth_password']))
432432
}
433+
elif kwargs['auth_token'] is not None:
434+
headers = {'Authorization': kwargs['auth_token']}
433435
else:
434436
headers = {'Authorization': token}
435437

@@ -755,6 +757,8 @@ def broker_request(broker, service_route, method, **kwargs):
755757
base64.b64encode("{0}:{1}".format(kwargs['basic_auth_username'],
756758
kwargs['basic_auth_password']))
757759
}
760+
elif kwargs['auth_token'] is not None:
761+
headers = {'Authorization': kwargs['auth_token']}
758762
else:
759763
token = openshift_client.Configuration().get_api_key_with_prefix('authorization')
760764
headers = {'Authorization': token}
@@ -771,7 +775,8 @@ def cmdrun_list(**kwargs):
771775
response = broker_request(kwargs['broker'], "/v2/catalog", "get",
772776
verify=kwargs["verify"],
773777
basic_auth_username=kwargs.get("basic_auth_username"),
774-
basic_auth_password=kwargs.get("basic_auth_password"))
778+
basic_auth_password=kwargs.get("basic_auth_password"),
779+
auth_token=kwargs.get("auth_token"))
775780

776781
if response.status_code != 200:
777782
print("Error: Attempt to list APBs in the broker returned status: %d" % response.status_code)
@@ -947,15 +952,21 @@ def delete_old_images(image_name):
947952
return
948953

949954

950-
def push_apb(registry, tag):
955+
def push_apb(registry, tag, **kwargs):
951956
try:
952957
client = create_docker_client()
953958
openshift_config.load_kube_config()
954-
api_key = openshift_client.Configuration().get_api_key_with_prefix('authorization')
955-
if api_key is None:
956-
raise Exception("No api key found in kubeconfig. NOTE: system:admin " +
957-
"*cannot* be used with apb, since it does not have a token.")
958-
token = api_key.split(" ")[1]
959+
if kwargs['auth_token'] is not None:
960+
token = kwargs['auth_token']
961+
else:
962+
api_key = openshift_client.Configuration().get_api_key_with_prefix('authorization')
963+
if api_key is None:
964+
raise Exception(
965+
"No api key found in kubeconfig. NOTE: " +
966+
"system:admin *cannot* be used with apb, since it " +
967+
"does not have a token."
968+
)
969+
token = api_key.split(" ")[1]
959970
username = "developer" if is_minishift() else "unused"
960971
client.login(username=username, password=token, registry=registry, reauth=True)
961972
delete_old_images(tag)
@@ -1133,7 +1144,8 @@ def cmdrun_push(**kwargs):
11331144
response = broker_request(broker, "/v2/apb", "post", data=data_spec,
11341145
verify=kwargs["verify"],
11351146
basic_auth_username=kwargs.get("basic_auth_username"),
1136-
basic_auth_password=kwargs.get("basic_auth_password"))
1147+
basic_auth_password=kwargs.get("basic_auth_password"),
1148+
auth_token=kwargs.get("auth_token"))
11371149

11381150
if response.status_code != 200:
11391151
print("Error: Attempt to add APB to the Broker returned status: %d" % response.status_code)
@@ -1147,11 +1159,12 @@ def cmdrun_push(**kwargs):
11471159
tag = registry + "/" + kwargs['namespace'] + "/" + dict_spec['name']
11481160

11491161
build_apb(project, kwargs['dockerfile'], tag)
1150-
push_apb(registry, tag)
1162+
push_apb(registry, tag, **kwargs)
11511163
bootstrap(
11521164
broker,
11531165
kwargs.get("basic_auth_username"),
11541166
kwargs.get("basic_auth_password"),
1167+
kwargs.get("auth_token"),
11551168
kwargs["verify"]
11561169
)
11571170

@@ -1195,6 +1208,7 @@ def cmdrun_remove(**kwargs):
11951208
kwargs["broker"],
11961209
kwargs.get("basic_auth_username"),
11971210
kwargs.get("basic_auth_password"),
1211+
kwargs.get("auth_token"),
11981212
kwargs["verify"]
11991213
)
12001214
exit()
@@ -1204,15 +1218,17 @@ def cmdrun_remove(**kwargs):
12041218
response = broker_request(kwargs["broker"], route, "delete",
12051219
verify=kwargs["verify"],
12061220
basic_auth_username=kwargs.get("basic_auth_username"),
1207-
basic_auth_password=kwargs.get("basic_auth_password"))
1221+
basic_auth_password=kwargs.get("basic_auth_password"),
1222+
auth_token=kwargs.get("auth_token"))
12081223

12091224
if response.status_code == 404:
12101225
print("Received a 404 trying to remove APB with id: %s" % kwargs["id"])
12111226
print("Attempting to contact 3.7 endpoint before erroring out.")
12121227
response = broker_request(kwargs["broker"], old_route, "delete",
12131228
verify=kwargs["verify"],
12141229
basic_auth_username=kwargs.get("basic_auth_username"),
1215-
basic_auth_password=kwargs.get("basic_auth_password"))
1230+
basic_auth_password=kwargs.get("basic_auth_password"),
1231+
auth_token=kwargs.get("auth_token"))
12161232

12171233
if response.status_code != 204:
12181234
print("Error: Attempt to remove an APB from Broker returned status: %d" % response.status_code)
@@ -1225,11 +1241,12 @@ def cmdrun_remove(**kwargs):
12251241
print("Successfully deleted APB")
12261242

12271243

1228-
def bootstrap(broker, username, password, verify):
1244+
def bootstrap(broker, username, password, token, verify):
12291245
response = broker_request(broker, "/v2/bootstrap", "post", data={},
12301246
verify=verify,
12311247
basic_auth_username=username,
1232-
basic_auth_password=password)
1248+
basic_auth_password=password,
1249+
auth_token=token)
12331250

12341251
if response.status_code != 200:
12351252
print("Error: Attempt to bootstrap Broker returned status: %d" % response.status_code)
@@ -1240,7 +1257,9 @@ def bootstrap(broker, username, password, verify):
12401257

12411258

12421259
def cmdrun_bootstrap(**kwargs):
1243-
bootstrap(kwargs["broker"], kwargs.get("basic_auth_username"), kwargs.get("basic_auth_password"), kwargs["verify"])
1260+
bootstrap(kwargs["broker"], kwargs.get("basic_auth_username"),
1261+
kwargs.get("basic_auth_password"), kwargs.get("auth_token"),
1262+
kwargs["verify"])
12441263

12451264
if not kwargs['no_relist']:
12461265
relist_service_broker(kwargs)
@@ -1331,7 +1350,7 @@ def cmdrun_test(**kwargs):
13311350
tag = registry + "/" + kwargs['namespace'] + "/" + spec['name']
13321351

13331352
build_apb(project, kwargs['dockerfile'], tag)
1334-
push_apb(registry, tag)
1353+
push_apb(registry, tag, **kwargs)
13351354

13361355
spec = get_spec(project)
13371356
test_name = 'apb-test-{}-{}'.format(spec['name'], rand_str())
@@ -1375,7 +1394,7 @@ def cmdrun_run(**kwargs):
13751394
kwargs['dockerfile'],
13761395
tag
13771396
)
1378-
push_apb(registry, tag)
1397+
push_apb(registry, tag, **kwargs)
13791398

13801399
plans = [plan['name'] for plan in spec['plans']]
13811400
if len(plans) > 1:

0 commit comments

Comments
 (0)