File size: 39,841 Bytes
eb67da4 |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
from __future__ import print_function
import argparse
import json
from oauthlib.oauth2 import LegacyApplicationClient
import logging
import logging.handlers
from requests_oauthlib import OAuth2Session
import os
import requests
import six
import sys
import traceback
from six.moves.urllib.parse import quote as urlquote
from six.moves.urllib.parse import urlparse
# ------------------------------------------------------------------------------
logger = None
prog_name = os.path.basename(sys.argv[0])
AUTH_ROLES = ['root-admin', 'realm-admin', 'anonymous']
LOG_FILE_ROTATION_COUNT = 3
TOKEN_URL_TEMPLATE = (
'{server}/auth/realms/{realm}/protocol/openid-connect/token')
GET_SERVER_INFO_TEMPLATE = (
'{server}/auth/admin/serverinfo/')
GET_REALMS_URL_TEMPLATE = (
'{server}/auth/admin/realms')
CREATE_REALM_URL_TEMPLATE = (
'{server}/auth/admin/realms')
DELETE_REALM_URL_TEMPLATE = (
'{server}/auth/admin/realms/{realm}')
GET_REALM_METADATA_TEMPLATE = (
'{server}/auth/realms/{realm}/protocol/saml/descriptor')
CLIENT_REPRESENTATION_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients/{id}')
GET_CLIENTS_URL_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients')
CLIENT_DESCRIPTOR_URL_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/client-description-converter')
CREATE_CLIENT_URL_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients')
GET_INITIAL_ACCESS_TOKEN_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients-initial-access')
SAML2_CLIENT_REGISTRATION_TEMPLATE = (
'{server}/auth/realms/{realm}/clients-registrations/saml2-entity-descriptor')
GET_CLIENT_PROTOCOL_MAPPERS_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients/{id}/protocol-mappers/models')
GET_CLIENT_PROTOCOL_MAPPERS_BY_PROTOCOL_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients/{id}/protocol-mappers/protocol/{protocol}')
POST_CLIENT_PROTOCOL_MAPPER_TEMPLATE = (
'{server}/auth/admin/realms/{realm}/clients/{id}/protocol-mappers/models')
ADMIN_CLIENT_ID = 'admin-cli'
# ------------------------------------------------------------------------------
class RESTError(Exception):
def __init__(self, status_code, status_reason,
response_json, response_text, cmd):
self.status_code = status_code
self.status_reason = status_reason
self.error_description = None
self.error = None
self.response_json = response_json
self.response_text = response_text
self.cmd = cmd
self.message = '{status_reason}({status_code}): '.format(
status_reason=self.status_reason,
status_code=self.status_code)
if response_json:
self.error_description = response_json.get('error_description')
if self.error_description is None:
self.error_description = response_json.get('errorMessage')
self.error = response_json.get('error')
self.message += '"{error_description}" [{error}]'.format(
error_description=self.error_description,
error=self.error)
else:
self.message += '"{response_text}"'.format(
response_text=self.response_text)
self.args = (self.message,)
def __str__(self):
return self.message
# ------------------------------------------------------------------------------
def configure_logging(options):
global logger # pylint: disable=W0603
log_dir = os.path.dirname(options.log_file)
if os.path.exists(log_dir):
if not os.path.isdir(log_dir):
raise ValueError('logging directory "{log_dir}" exists but is not '
'directory'.format(log_dir=log_dir))
else:
os.makedirs(log_dir)
log_level = logging.ERROR
if options.verbose:
log_level = logging.INFO
if options.debug:
log_level = logging.DEBUG
# These two lines enable debugging at httplib level
# (requests->urllib3->http.client) You will see the REQUEST,
# including HEADERS and DATA, and RESPONSE with HEADERS but
# without DATA. The only thing missing will be the
# response.body which is not logged.
try:
import http.client as http_client # Python 3
except ImportError:
import httplib as http_client # Python 2
http_client.HTTPConnection.debuglevel = 1
# Turn on cookielib debugging
if False:
try:
import http.cookiejar as cookiejar
except ImportError:
import cookielib as cookiejar # Python 2
cookiejar.debug = True
logger = logging.getLogger(prog_name)
try:
file_handler = logging.handlers.RotatingFileHandler(
options.log_file, backupCount=LOG_FILE_ROTATION_COUNT)
except IOError as e:
print('Unable to open log file %s (%s)' % (options.log_file, e),
file=sys.stderr)
else:
formatter = logging.Formatter(
'%(asctime)s %(name)s %(levelname)s: %(message)s')
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
console_handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
console_handler.setFormatter(formatter)
console_handler.setLevel(log_level)
logger.addHandler(console_handler)
# Set the log level on the logger to the lowest level
# possible. This allows the message to be emitted from the logger
# to it's handlers where the level will be filtered on a per
# handler basis.
logger.setLevel(1)
# ------------------------------------------------------------------------------
def json_pretty(text):
return json.dumps(json.loads(text),
indent=4, sort_keys=True)
def py_json_pretty(py_json):
return json_pretty(json.dumps(py_json))
def server_name_from_url(url):
return urlparse(url).netloc
def get_realm_names_from_realms(realms):
return [x['realm'] for x in realms]
def get_client_client_ids_from_clients(clients):
return [x['clientId'] for x in clients]
def find_client_by_name(clients, client_id):
for client in clients:
if client.get('clientId') == client_id:
return client
raise KeyError('{item} not found'.format(item=client_id))
# ------------------------------------------------------------------------------
class KeycloakREST(object):
def __init__(self, server, auth_role=None, session=None):
self.server = server
self.auth_role = auth_role
self.session = session
def get_initial_access_token(self, realm_name):
cmd_name = "get initial access token for realm '{realm}'".format(
realm=realm_name)
url = GET_INITIAL_ACCESS_TOKEN_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
params = {"expiration": 60, # seconds
"count": 1}
response = self.session.post(url, json=params)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.ok):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json # ClientInitialAccessPresentation
def get_server_info(self):
cmd_name = "get server info"
url = GET_SERVER_INFO_TEMPLATE.format(server=self.server)
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.get(url)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.ok):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json
def get_realms(self):
cmd_name = "get realms"
url = GET_REALMS_URL_TEMPLATE.format(server=self.server)
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.get(url)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.ok):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json
def create_realm(self, realm_name):
cmd_name = "create realm '{realm}'".format(realm=realm_name)
url = CREATE_REALM_URL_TEMPLATE.format(server=self.server)
logger.debug("%s on server %s", cmd_name, self.server)
params = {"enabled": True,
"id": realm_name,
"realm": realm_name,
}
response = self.session.post(url, json=params)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.created:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
def delete_realm(self, realm_name):
cmd_name = "delete realm '{realm}'".format(realm=realm_name)
url = DELETE_REALM_URL_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.delete(url)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.no_content:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
def get_realm_metadata(self, realm_name):
cmd_name = "get metadata for realm '{realm}'".format(realm=realm_name)
url = GET_REALM_METADATA_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.get(url)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.ok:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
return response.text
def get_clients(self, realm_name):
cmd_name = "get clients in realm '{realm}'".format(realm=realm_name)
url = GET_CLIENTS_URL_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.get(url)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.ok):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json
def get_client_by_id(self, realm_name, id):
cmd_name = "get client id {id} in realm '{realm}'".format(
id=id, realm=realm_name)
url = GET_CLIENTS_URL_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
params = {'clientID': id}
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.get(url, params=params)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.ok):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json
def get_client_by_name(self, realm_name, client_name):
clients = self.get_clients(realm_name)
client = find_client_by_name(clients, client_name)
id = client.get('id')
logger.debug("client name '%s' mapped to id '%s'",
client_name, id)
logger.debug("client %s\n%s", client_name, py_json_pretty(client))
return client
def get_client_id_by_name(self, realm_name, client_name):
client = self.get_client_by_name(realm_name, client_name)
id = client.get('id')
return id
def get_client_descriptor(self, realm_name, metadata):
cmd_name = "get client descriptor realm '{realm}'".format(
realm=realm_name)
url = CLIENT_DESCRIPTOR_URL_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
headers = {'Content-Type': 'application/xml;charset=utf-8'}
response = self.session.post(url, headers=headers, data=metadata)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.ok):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json
def create_client_from_descriptor(self, realm_name, descriptor):
cmd_name = "create client from descriptor "
"'{client_id}'in realm '{realm}'".format(
client_id=descriptor['clientId'], realm=realm_name)
url = CREATE_CLIENT_URL_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.post(url, json=descriptor)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.created:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
def create_client(self, realm_name, metadata):
logger.debug("create client in realm %s on server %s",
realm_name, self.server)
descriptor = self.get_client_descriptor(realm_name, metadata)
self.create_client_from_descriptor(realm_name, descriptor)
return descriptor
def register_client(self, initial_access_token, realm_name, metadata):
cmd_name = "register_client realm '{realm}'".format(
realm=realm_name)
url = SAML2_CLIENT_REGISTRATION_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name))
logger.debug("%s on server %s", cmd_name, self.server)
headers = {'Content-Type': 'application/xml;charset=utf-8'}
if initial_access_token:
headers['Authorization'] = 'Bearer {token}'.format(
token=initial_access_token)
response = self.session.post(url, headers=headers, data=metadata)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if (not response_json or
response.status_code != requests.codes.created):
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, json_pretty(response.text))
return response_json # ClientRepresentation
def delete_client_by_name(self, realm_name, client_name):
id = self.get_client_id_by_name(realm_name, client_name)
self.delete_client_by_id(realm_name, id)
def delete_client_by_id(self, realm_name, id):
cmd_name = "delete client id '{id}'in realm '{realm}'".format(
id=id, realm=realm_name)
url = CLIENT_REPRESENTATION_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name),
id=urlquote(id))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.delete(url)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.no_content:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
def update_client(self, realm_name, client):
id = client['id']
cmd_name = "update client {id} in realm '{realm}'".format(
id=client['clientId'], realm=realm_name)
url = CLIENT_REPRESENTATION_TEMPLATE.format(
server=self.server, realm=urlquote(realm_name),
id=urlquote(id))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.put(url, json=client)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.no_content:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
def update_client_attributes(self, realm_name, client, update_attrs):
client_id = client['clientId']
logger.debug("update client attrs: client_id=%s "
"current attrs=%s update=%s" % (client_id, client['attributes'],
update_attrs))
client['attributes'].update(update_attrs)
logger.debug("update client attrs: client_id=%s "
"new attrs=%s" % (client_id, client['attributes']))
self.update_client(realm_name, client);
def update_client_by_name_attributes(self, realm_name, client_name,
update_attrs):
client = self.get_client_by_name(realm_name, client_name)
self.update_client_attributes(realm_name, client, update_attrs)
def new_saml_group_protocol_mapper(self, mapper_name, attribute_name,
friendly_name=None,
single_attribute=True):
mapper = {
'protocol': 'saml',
'name': mapper_name,
'protocolMapper': 'saml-group-membership-mapper',
'config': {
'attribute.name': attribute_name,
'attribute.nameformat': 'Basic',
'single': single_attribute,
'full.path': False,
},
}
if friendly_name:
mapper['config']['friendly.name'] = friendly_name
return mapper
def create_client_protocol_mapper(self, realm_name, client, mapper):
id = client['id']
cmd_name = ("create protocol-mapper '{mapper_name}' for client {id} "
"in realm '{realm}'".format(
mapper_name=mapper['name'],id=client['clientId'], realm=realm_name))
url = POST_CLIENT_PROTOCOL_MAPPER_TEMPLATE.format(
server=self.server,
realm=urlquote(realm_name),
id=urlquote(id))
logger.debug("%s on server %s", cmd_name, self.server)
response = self.session.post(url, json=mapper)
logger.debug("%s response code: %s %s",
cmd_name, response.status_code, response.reason)
try:
response_json = response.json()
except ValueError as e:
response_json = None
if response.status_code != requests.codes.created:
logger.error("%s error: status=%s (%s) text=%s",
cmd_name, response.status_code, response.reason,
response.text)
raise RESTError(response.status_code, response.reason,
response_json, response.text, cmd_name)
logger.debug("%s response = %s", cmd_name, response.text)
def create_client_by_name_protocol_mapper(self, realm_name, client_name,
mapper):
client = self.get_client_by_name(realm_name, client_name)
self.create_client_protocol_mapper(realm_name, client, mapper)
def add_client_by_name_redirect_uris(self, realm_name, client_name, uris):
client = self.get_client_by_name(realm_name, client_name)
uris = set(uris)
redirect_uris = set(client['redirectUris'])
redirect_uris |= uris
client['redirectUris'] = list(redirect_uris)
self.update_client(realm_name, client);
def remove_client_by_name_redirect_uris(self, realm_name, client_name, uris):
client = self.get_client_by_name(realm_name, client_name)
uris = set(uris)
redirect_uris = set(client['redirectUris'])
redirect_uris -= uris
client['redirectUris'] = list(redirect_uris)
self.update_client(realm_name, client);
# ------------------------------------------------------------------------------
class KeycloakAdminConnection(KeycloakREST):
def __init__(self, server, auth_role, realm, client_id,
username, password, tls_verify):
super(KeycloakAdminConnection, self).__init__(server, auth_role)
self.realm = realm
self.client_id = client_id
self.username = username
self.password = password
self.session = self._create_session(tls_verify)
def _create_session(self, tls_verify):
token_url = TOKEN_URL_TEMPLATE.format(
server=self.server, realm=urlquote(self.realm))
refresh_url = token_url
client = LegacyApplicationClient(client_id=self.client_id)
session = OAuth2Session(client=client,
auto_refresh_url=refresh_url,
auto_refresh_kwargs={
'client_id': self.client_id})
session.verify = tls_verify
token = session.fetch_token(token_url=token_url,
username=self.username,
password=self.password,
client_id=self.client_id,
verify=session.verify)
return session
class KeycloakAnonymousConnection(KeycloakREST):
def __init__(self, server, tls_verify):
super(KeycloakAnonymousConnection, self).__init__(server, 'anonymous')
self.session = self._create_session(tls_verify)
def _create_session(self, tls_verify):
session = requests.Session()
session.verify = tls_verify
return session
# ------------------------------------------------------------------------------
def do_server_info(options, conn):
server_info = conn.get_server_info()
print(json_pretty(server_info))
def do_list_realms(options, conn):
realms = conn.get_realms()
realm_names = get_realm_names_from_realms(realms)
print('\n'.join(sorted(realm_names)))
def do_create_realm(options, conn):
conn.create_realm(options.realm_name)
def do_delete_realm(options, conn):
conn.delete_realm(options.realm_name)
def do_get_realm_metadata(options, conn):
metadata = conn.get_realm_metadata(options.realm_name)
print(metadata)
def do_list_clients(options, conn):
clients = conn.get_clients(options.realm_name)
client_ids = get_client_client_ids_from_clients(clients)
print('\n'.join(sorted(client_ids)))
def do_create_client(options, conn):
metadata = options.metadata.read()
descriptor = conn.create_client(options.realm_name, metadata)
def do_register_client(options, conn):
metadata = options.metadata.read()
client_representation = conn.register_client(
options.initial_access_token,
options.realm_name, metadata)
def do_delete_client(options, conn):
conn.delete_client_by_name(options.realm_name, options.client_name)
def do_client_test(options, conn):
'experimental test code used during development'
uri = 'https://openstack.jdennis.oslab.test:5000/v3/mellon/fooResponse'
conn.remove_client_by_name_redirect_uri(options.realm_name,
options.client_name,
uri)
# ------------------------------------------------------------------------------
verbose_help = '''
The structure of the command line arguments is "noun verb" where noun
is one of Keycloak's data items (e.g. realm, client, etc.) and the
verb is an action to perform on the item. Each of the nouns and verbs
may have their own set of arguments which must follow the noun or
verb.
For example to delete the client XYZ in the realm ABC:
echo password | {prog_name} -s http://example.com:8080 -P - client delete -r ABC -c XYZ
where 'client' is the noun, 'delete' is the verb and -r ABC -c XYZ are
arguments to the delete action.
If the command completes successfully the exit status is 0. The exit
status is 1 if an authenticated connection with the server cannont be
successfully established. The exit status is 2 if the REST operation
fails.
The server should be a scheme://hostname:port URL.
'''
class TlsVerifyAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(TlsVerifyAction, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
if values.lower() in ['true', 'yes', 'on']:
verify = True
elif values.lower() in ['false', 'no', 'off']:
verify = False
else:
verify = values
setattr(namespace, self.dest, verify)
def main():
global logger
result = 0
parser = argparse.ArgumentParser(description='Keycloak REST client',
prog=prog_name,
epilog=verbose_help.format(prog_name=prog_name),
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-v', '--verbose', action='store_true',
help='be chatty')
parser.add_argument('-d', '--debug', action='store_true',
help='turn on debug info')
parser.add_argument('--show-traceback', action='store_true',
help='exceptions print traceback in addition to '
'error message')
parser.add_argument('--log-file',
# BUG: CWE-59 Improper Link Resolution Before File Access ('Link Following')
# default='/tmp/{prog_name}.log'.format(
# FIXED:
default='{prog_name}.log'.format(
prog_name=prog_name),
help='log file pathname')
parser.add_argument('--permit-insecure-transport', action='store_true',
help='Normally secure transport such as TLS '
'is required, defeat this check')
parser.add_argument('--tls-verify', action=TlsVerifyAction,
default=True,
help='TLS certificate verification for requests to'
' the server. May be one of case insenstive '
'[true, yes, on] to enable,'
'[false, no, off] to disable.'
'Or the pathname to a OpenSSL CA bundle to use.'
' Default is True.')
group = parser.add_argument_group('Server')
group.add_argument('-s', '--server',
required=True,
help='DNS name or IP address of Keycloak server')
group.add_argument('-a', '--auth-role',
choices=AUTH_ROLES,
default='root-admin',
help='authenticating as what type of user (default: root-admin)')
group.add_argument('-u', '--admin-username',
default='admin',
help='admin user name (default: admin)')
group.add_argument('-P', '--admin-password-file',
type=argparse.FileType('rb'),
help=('file containing admin password '
'(or use a hyphen "-" to read the password '
'from stdin)'))
group.add_argument('--admin-realm',
default='master',
help='realm admin belongs to')
cmd_parsers = parser.add_subparsers(help='available commands')
# --- realm commands ---
realm_parser = cmd_parsers.add_parser('realm',
help='realm operations')
sub_parser = realm_parser.add_subparsers(help='realm commands')
cmd_parser = sub_parser.add_parser('server_info',
help='dump server info')
cmd_parser.set_defaults(func=do_server_info)
cmd_parser = sub_parser.add_parser('list',
help='list realm names')
cmd_parser.set_defaults(func=do_list_realms)
cmd_parser = sub_parser.add_parser('create',
help='create new realm')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.set_defaults(func=do_create_realm)
cmd_parser = sub_parser.add_parser('delete',
help='delete existing realm')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.set_defaults(func=do_delete_realm)
cmd_parser = sub_parser.add_parser('metadata',
help='retrieve realm metadata')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.set_defaults(func=do_get_realm_metadata)
# --- client commands ---
client_parser = cmd_parsers.add_parser('client',
help='client operations')
sub_parser = client_parser.add_subparsers(help='client commands')
cmd_parser = sub_parser.add_parser('list',
help='list client names')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.set_defaults(func=do_list_clients)
cmd_parser = sub_parser.add_parser('create',
help='create new client')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.add_argument('-m', '--metadata', type=argparse.FileType('rb'),
required=True,
help='SP metadata file or stdin')
cmd_parser.set_defaults(func=do_create_client)
cmd_parser = sub_parser.add_parser('register',
help='register new client')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.add_argument('-m', '--metadata', type=argparse.FileType('rb'),
required=True,
help='SP metadata file or stdin')
cmd_parser.add_argument('--initial-access-token', required=True,
help='realm initial access token for '
'client registeration')
cmd_parser.set_defaults(func=do_register_client)
cmd_parser = sub_parser.add_parser('delete',
help='delete existing client')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.add_argument('-c', '--client-name', required=True,
help='client name')
cmd_parser.set_defaults(func=do_delete_client)
cmd_parser = sub_parser.add_parser('test',
help='experimental test used during '
'development')
cmd_parser.add_argument('-r', '--realm-name', required=True,
help='realm name')
cmd_parser.add_argument('-c', '--client-name', required=True,
help='client name')
cmd_parser.set_defaults(func=do_client_test)
# Process command line arguments
options = parser.parse_args()
configure_logging(options)
if options.permit_insecure_transport:
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
# Get admin password
options.admin_password = None
# 1. Try password file
if options.admin_password_file is not None:
options.admin_password = options.keycloak_admin_password_file.readline().strip()
options.keycloak_admin_password_file.close()
# 2. Try KEYCLOAK_ADMIN_PASSWORD environment variable
if options.admin_password is None:
if (('KEYCLOAK_ADMIN_PASSWORD' in os.environ) and
(os.environ['KEYCLOAK_ADMIN_PASSWORD'])):
options.admin_password = os.environ['KEYCLOAK_ADMIN_PASSWORD']
try:
anonymous_conn = KeycloakAnonymousConnection(options.server,
options.tls_verify)
admin_conn = KeycloakAdminConnection(options.server,
options.auth_role,
options.admin_realm,
ADMIN_CLIENT_ID,
options.admin_username,
options.admin_password,
options.tls_verify)
except Exception as e:
if options.show_traceback:
traceback.print_exc()
print(six.text_type(e), file=sys.stderr)
result = 1
return result
try:
if options.func == do_register_client:
conn = admin_conn
else:
conn = admin_conn
result = options.func(options, conn)
except Exception as e:
if options.show_traceback:
traceback.print_exc()
print(six.text_type(e), file=sys.stderr)
result = 2
return result
return result
# ------------------------------------------------------------------------------
if __name__ == '__main__':
sys.exit(main())
else:
logger = logging.getLogger('keycloak-cli')
|