From 5cc3228c8d8899f4e98a55b3420d591a5a543c77 Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Oct 03 2017 15:11:52 +0000 Subject: Merge #81 `Add a test script for submitting oidc composes.` --- diff --git a/client/odcs/client/odcs.py b/client/odcs/client/odcs.py index e9b1ab5..847b802 100644 --- a/client/odcs/client/odcs.py +++ b/client/odcs/client/odcs.py @@ -167,8 +167,12 @@ class ODCS(object): request_method = getattr(requests, method) resource_url = self._make_endpoint(resource_path) r = request_method(resource_url, **request_data) + + # Print error, for debugging if r.status_code not in (200, 202): - r.raise_for_status() + print r.text + + r.raise_for_status() return r def _get(self, resource_path, data=None): diff --git a/submit_test_compose_oidc.py b/submit_test_compose_oidc.py new file mode 100755 index 0000000..f5e8b6d --- /dev/null +++ b/submit_test_compose_oidc.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python +""" submit_test_compose_oidc.py - Submit a test compose, via OpenID Connect. + +If you have problems authenticating, try:: + + $ rm -rf ~/.openidc/ + +Example usage:: + + export PYTHONPATH=.:$VIRTUAL_ENV/lib/python2.7/site-packages:client + ./submit_test_compose_oidc.py \ + --staging \ + --source f26 \ + --source-type tag \ + --flag no_deps \ + python-requests python-urllib3 + +""" +from __future__ import print_function + +import argparse +import sys +import textwrap + +import openidc_client +import requests.exceptions + +import odcs.client.odcs + + +parser = argparse.ArgumentParser( + description=textwrap.dedent(__doc__), + formatter_class=argparse.RawDescriptionHelpFormatter, +) +parser.add_argument( + '--staging', default=False, action='store_true', + help="Use the Fedora Infra staging enbironment.") +parser.add_argument( + '--source', default=None, + help="Source for the compose. May be a koji tag or a " + "whitespace separated list of modules.") +parser.add_argument( + '--source-type', default=None, choices=['tag', 'module'], + help="Type for the source. Must be 'tag' or 'module'") +parser.add_argument( + '--flag', default=None, choices=['no_deps'], + help="Flag to pass to influence the compose.") +parser.add_argument( + 'packages', metavar='package', nargs='*', + help='Packages to be included in the compose.') + +args = parser.parse_args() + +required = ['source', 'source_type'] +for attr in required: + if getattr(args, attr, None) is None: + print("%r is required" % attr) + sys.exit(1) + +if args.staging: + odcs_url = 'https://odcs.stg.fedoraproject.org' + id_provider = 'https://id.stg.fedoraproject.org/openidc/' +else: + odcs_url = 'https://odcs.fedoraproject.org' + id_provider = 'https://id.fedoraproject.org/openidc/' + + +# Get the auth token using the OpenID client. +oidc = openidc_client.OpenIDCClient( + 'odcs', + id_provider, + {'Token': 'Token', 'Authorization': 'Authorization'}, + 'odcs-authorizer', + 'notsecret', +) + +scopes = [ + 'openid', + 'https://id.fedoraproject.org/scope/groups', + 'https://pagure.io/odcs/new-compose', + 'https://pagure.io/odcs/renew-compose', + 'https://pagure.io/odcs/delete-compose', +] +try: + token = oidc.get_token(scopes, new_token=True) +except requests.exceptions.HTTPError as e: + print(e.response.text) + raise + +client = odcs.client.odcs.ODCS( + odcs_url, + auth_mech=odcs.client.odcs.AuthMech.OpenIDC, + openidc_token=token, +) + +result = client.new_compose( + source=args.source, + source_type=args.source_type, + packages=args.packages, + flags=[args.flag], +) +print(result)