Пример #1
0
def main():
    parser = argparse.ArgumentParser(
        description='List IP addresses of X-Road Security Servers.',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='You need to provide either Security Server or Central Server address.\n\n'
               'NB! Global configuration signature is not validated when using Central Server.\n'
               'Use local Security Server whenever possible.'
    )
    parser.add_argument(
        '-s', metavar='SECURITY_SERVER', help='DNS name/IP/URL of local Security Server')
    parser.add_argument(
        '-c', metavar='CENTRAL_SERVER',
        help='DNS name/IP/URL of Central Server/Configuration Proxy')
    parser.add_argument('-t', metavar='TIMEOUT', help='timeout for HTTP query', type=float)
    parser.add_argument(
        '--verify', metavar='CERT_PATH',
        help='validate peer TLS certificate using CA certificate file.')
    parser.add_argument(
        '--cert', metavar='CERT_PATH', help='use TLS certificate for HTTPS requests.')
    parser.add_argument('--key', metavar='KEY_PATH', help='private key for TLS certificate.')
    parser.add_argument(
        '--instance', metavar='INSTANCE',
        help='use this instance instead of local X-Road instance (works only with "-s" argument)')
    args = parser.parse_args()

    instance = None
    if args.instance:
        instance = args.instance

    timeout = DEFAULT_TIMEOUT
    if args.t:
        timeout = args.t

    verify = False
    if args.verify:
        verify = args.verify

    cert = None
    if args.cert and args.key:
        cert = (args.cert, args.key)

    shared_params = None
    if args.s:
        try:
            shared_params = xrdinfo.shared_params_ss(
                addr=args.s, instance=instance, timeout=timeout, verify=verify, cert=cert)
        except xrdinfo.XrdInfoError as e:
            print_error(e)
            exit(1)
    elif args.c:
        try:
            shared_params = xrdinfo.shared_params_cs(
                addr=args.c, timeout=timeout, verify=verify, cert=cert)
        except xrdinfo.XrdInfoError as e:
            print_error(e)
            exit(1)
    else:
        parser.print_help()
        exit(1)

    try:
        for ip in xrdinfo.servers_ips(shared_params):
            # Should never contain non ASCII characters
            print(ip)
    except xrdinfo.XrdInfoError as e:
        print_error(e)
        exit(1)
Пример #2
0
def main():
    parser = argparse.ArgumentParser(
        description='X-Road getWsdl request to all members.',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='By default peer TLS certificate is not validated.'
    )
    parser.add_argument(
        'url', metavar='SERVER_URL',
        help='URL of local Security Server accepting X-Road requests.')
    parser.add_argument(
        'client', metavar='CLIENT',
        help='slash separated Client identifier (e.g. '
             '"INSTANCE/MEMBER_CLASS/MEMBER_CODE/SUBSYSTEM_CODE" '
             'or "INSTANCE/MEMBER_CLASS/MEMBER_CODE").')
    parser.add_argument('path', metavar='PATH', help='path for storing results.')
    parser.add_argument('-v', help='verbose output', action='store_true')
    parser.add_argument('-t', metavar='TIMEOUT', help='timeout for HTTP query', type=float)
    parser.add_argument(
        '--threads', metavar='THREADS', help='amount of threads to use', type=int, default=0)
    parser.add_argument(
        '--verify', metavar='CERT_PATH',
        help='validate peer TLS certificate using CA certificate file.')
    parser.add_argument(
        '--cert', metavar='CERT_PATH', help='use TLS certificate for HTTPS requests.')
    parser.add_argument('--key', metavar='KEY_PATH', help='private key for TLS certificate.')
    parser.add_argument(
        '--instance', metavar='INSTANCE',
        help='use this instance instead of local X-Road instance.')
    parser.add_argument('--no-html', action='store_true', help='disable HTML generation.')
    args = parser.parse_args()

    params = {
        'verbose': False,
        'path': args.path,
        'url': args.url,
        'client': args.client,
        'instance': None,
        'timeout': DEFAULT_TIMEOUT,
        'verify': False,
        'cert': None,
        'thread_cnt': DEFAULT_THREAD_COUNT,
        'work_queue': queue.Queue(),
        'results': {},
        'results_lock': Lock(),
        'shutdown': Event(),
        'html': True
    }

    if args.v:
        params['verbose'] = True

    if six.PY2:
        # Convert to unicode
        params['path'] = params['path'].decode('utf-8')

    makedirs(params['path'])

    if six.PY2:
        # Convert to unicode
        params['client'] = params['client'].decode('utf-8')

    params['client'] = params['client'].split('/')
    if not (len(params['client']) in (3, 4)):
        safe_print(u'Client name is incorrect: "{}"'.format(args.client))
        exit(1)

    if args.instance and six.PY2:
        # Convert to unicode
        params['instance'] = args.instance.decode('utf-8')
    elif args.instance:
        params['instance'] = args.instance

    if args.t:
        params['timeout'] = args.t

    if args.verify:
        params['verify'] = args.verify

    if args.cert and args.key:
        params['cert'] = (args.cert, args.key)

    if args.threads and args.threads > 0:
        params['thread_cnt'] = args.threads

    if args.no_html:
        params['html'] = False

    shared_params = None
    try:
        shared_params = xrdinfo.shared_params_ss(
            addr=args.url, instance=params['instance'], timeout=params['timeout'],
            verify=params['verify'], cert=params['cert'])
    except xrdinfo.XrdInfoError as e:
        safe_print(u'Cannot download Global Configuration: {}'.format(e))
        exit(1)

    # Create and start new threads
    threads = []
    for _ in range(params['thread_cnt']):
        t = Thread(target=worker, args=(params,))
        t.daemon = True
        t.start()
        threads.append(t)

    # Populate the queue
    try:
        for subsystem in xrdinfo.registered_subsystems(shared_params):
            params['work_queue'].put(subsystem)
    except xrdinfo.XrdInfoError as e:
        safe_print(u'Cannot process Global Configuration: {}'.format(e))
        exit(1)

    # Block until all tasks in queue are done
    params['work_queue'].join()

    # Set shutdown event and wait until all daemon processes finish
    params['shutdown'].set()
    for t in threads:
        t.join()

    results = params['results']

    body = ''
    card_nr = 0
    json_data = []
    for subsystem_key in sorted(results.keys()):
        card_nr += 1
        subsystem_result = results[subsystem_key]
        methods = subsystem_result['methods']
        if subsystem_result['ok'] and len(methods) > 0:
            subsystem_status = 'ok'
            subsystem_badge = u''
        elif subsystem_result['ok']:
            subsystem_status = 'empty'
            subsystem_badge = u' <span class="badge badge-secondary">Empty</span>'
        else:
            subsystem_status = 'error'
            subsystem_badge = u' <span class="badge badge-danger">Error</span>'
        body += u'<div class="card">\n' \
                u'<div class="card-header">\n' \
                u'<a class="card-link" data-toggle="collapse" href="#collapse{}">\n' \
                u'{}{}\n' \
                u'</a>\n' \
                u'</div>\n'.format(card_nr, subsystem_key, subsystem_badge)
        body += u'<div id="collapse{}" class="collapse">\n' \
                u'<div class="card-body">\n'.format(card_nr)
        if subsystem_status == 'empty':
            body += u'<p>No services found</p>'
        elif subsystem_status == 'error':
            body += u'<p>Error while getting list of services</p>'
        subsystem = subsystem_key.split('/')
        json_subsystem = {
            'xRoadInstance': subsystem[0],
            'memberClass': subsystem[1],
            'memberCode': subsystem[2],
            'subsystemCode': subsystem[3],
            'subsystemStatus': 'ERROR' if subsystem_status == 'error' else 'OK',
            'methods': []
        }
        for method_key in sorted(methods.keys()):
            method = method_key.split('/')
            json_method = {
                'serviceCode': method[4],
                'serviceVersion': method[5],
            }
            if methods[method_key] == 'SKIPPED':
                body += u'<p>{} <span class="badge badge-warning">WSDL skipped due to ' \
                        u'previous Timeout</span></p>\n'.format(method_key)
                json_method['methodStatus'] = 'SKIPPED'
                json_method['wsdl'] = ''
            elif methods[method_key] == 'TIMEOUT':
                body += u'<p>{} <span class="badge badge-danger">WSDL query timed out' \
                        u'</span></p>\n'.format(method_key)
                json_method['methodStatus'] = 'TIMEOUT'
                json_method['wsdl'] = ''
            elif methods[method_key]:
                body += u'<p>{}: <a href="{}" class="badge badge-success">WSDL</a></p>\n'.format(
                    method_key, methods[method_key])
                json_method['methodStatus'] = 'OK'
                json_method['wsdl'] = methods[method_key]
            else:
                body += u'<p>{} <span class="badge badge-danger">Error while downloading ' \
                        u'or parsing of WSDL</span></p>\n'.format(method_key)
                json_method['methodStatus'] = 'ERROR'
                json_method['wsdl'] = ''

            json_subsystem['methods'].append(json_method)
        # Closing: card-body, collapseX, card
        body += u'</div>\n' \
                u'</div>\n' \
                u'</div>\n'
        json_data.append(json_subsystem)

    report_time = time.localtime(time.time())
    formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', report_time)
    suffix = time.strftime('%Y%m%d%H%M%S', report_time)

    s = re.search('<instanceIdentifier>(.+?)</instanceIdentifier>', shared_params)
    if s and s.group(1):
        instance = s.group(1)
    else:
        instance = u'???'

    # JSON output
    with open(u'{}/index_{}.json'.format(args.path, suffix), 'w') as f:
        if six.PY2:
            f.write(json.dumps(json_data, indent=2, ensure_ascii=False).encode('utf-8'))
        else:
            json.dump(json_data, f, indent=2, ensure_ascii=False)

    json_history = []
    try:
        with open(u'{}/history.json'.format(args.path), 'r') as f:
            if six.PY2:
                json_history = json.loads(f.read().decode('utf-8'))
            else:
                json_history = json.load(f)
    except IOError:
        # Cannot open history.html
        pass

    json_history.append({'reportTime': formatted_time, 'reportPath': u'index_{}.json'.format(
        suffix)})
    json_history.sort(key=sort_by_time, reverse=True)

    with open(u'{}/history.json'.format(args.path), 'w') as f:
        if six.PY2:
            f.write(json.dumps(json_history, indent=2, ensure_ascii=False).encode('utf-8'))
        else:
            json.dump(json_history, f, indent=2, ensure_ascii=False)

    # Replace index.json with latest report
    shutil.copy(u'{}/index_{}.json'.format(args.path, suffix), u'{}/index.json'.format(args.path))

    # HTML output
    if params['html']:
        html = METHODS_HTML_TEMPL.format(
            instance=instance, report_time=formatted_time, suffix=suffix, body=body)
        with open(u'{}/index_{}.html'.format(args.path, suffix), 'w') as f:
            if six.PY2:
                f.write(html.encode('utf-8'))
            else:
                f.write(html)

        history_item = u'<p><a href="{}">{}</a></p>\n'.format(
            u'index_{}.html'.format(suffix), formatted_time)
        try:
            html = u''
            with open(u'{}/history.html'.format(args.path), 'r') as f:
                for line in f:
                    if six.PY2:
                        line = line.decode('utf-8')
                    if line == HISTORY_HEADER:
                        line = line + history_item
                    html = html + line
        except IOError:
            # Cannot open history.html
            html = HISTORY_HTML_TEMPL.format(body=history_item)

        with open(u'{}/history.html'.format(args.path), 'w') as f:
            if six.PY2:
                f.write(html.encode('utf-8'))
            else:
                f.write(html)

        # Replace index.html with latest report
        shutil.copy(u'{}/index_{}.html'.format(args.path, suffix), u'{}/index.html'.format(
            args.path))
Пример #3
0
def main():
    parser = argparse.ArgumentParser(
        description='X-Road listMethods request to all members.',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='By default peer TLS certificate is not validated.')
    parser.add_argument(
        'url',
        metavar='SERVER_URL',
        help='URL of local Security Server accepting X-Road requests.')
    parser.add_argument(
        'client',
        metavar='CLIENT',
        help=
        'Client identifier consisting of slash separated Percent-Encoded parts (e.g. '
        '"INSTANCE/MEMBER_CLASS/MEMBER_CODE/SUBSYSTEM_CODE" '
        'or "INSTANCE/MEMBER_CLASS/MEMBER_CODE").')
    parser.add_argument('-t',
                        metavar='TIMEOUT',
                        help='timeout for HTTP query',
                        type=float)
    parser.add_argument('--allowed',
                        help='return only allowed methods',
                        action='store_true')
    parser.add_argument('--rest',
                        help='return REST methods instead of SOAP',
                        action='store_true')
    parser.add_argument('--threads',
                        metavar='THREADS',
                        help='amount of threads to use',
                        type=int)
    parser.add_argument(
        '--verify',
        metavar='CERT_PATH',
        help='validate peer TLS certificate using CA certificate file.')
    parser.add_argument('--cert',
                        metavar='CERT_PATH',
                        help='use TLS certificate for HTTPS requests.')
    parser.add_argument('--key',
                        metavar='KEY_PATH',
                        help='private key for TLS certificate.')
    parser.add_argument(
        '--instance',
        metavar='INSTANCE',
        help='use this instance instead of local X-Road instance.')
    args = parser.parse_args()

    params = {
        'url': args.url,
        'client': xrdinfo.identifier_parts(args.client),
        'method': DEFAULT_METHOD,
        'instance': None,
        'timeout': DEFAULT_TIMEOUT,
        'verify': False,
        'cert': None,
        'rest': args.rest,
        'thread_cnt': DEFAULT_THREAD_COUNT,
        'work_queue': queue.Queue(),
        'shutdown': Event()
    }

    if not (len(params['client']) in (3, 4)):
        print_error('Client name is incorrect: "{}"'.format(args.client))
        exit(1)

    if args.allowed:
        params['method'] = 'allowedMethods'

    if args.instance:
        params['instance'] = args.instance

    if args.t:
        params['timeout'] = args.t

    if args.verify:
        params['verify'] = args.verify

    if args.cert and args.key:
        params['cert'] = (args.cert, args.key)

    if args.threads and args.threads > 0:
        params['thread_cnt'] = args.threads

    shared_params = None
    try:
        shared_params = xrdinfo.shared_params_ss(addr=args.url,
                                                 instance=params['instance'],
                                                 timeout=params['timeout'],
                                                 verify=params['verify'],
                                                 cert=params['cert'])
    except xrdinfo.XrdInfoError as e:
        print_error('Cannot download Global Configuration: {}'.format(e))
        exit(1)

    # Create and start new threads
    threads = []
    for _ in range(params['thread_cnt']):
        t = Thread(target=worker, args=(params, ))
        t.daemon = True
        t.start()
        threads.append(t)

    # Populate the queue
    try:
        for subsystem in xrdinfo.registered_subsystems(shared_params):
            params['work_queue'].put(subsystem)
    except xrdinfo.XrdInfoError as e:
        print_error(e)
        exit(1)

    # Block until all tasks in queue are done
    params['work_queue'].join()

    # Set shutdown event and wait until all daemon processes finish
    params['shutdown'].set()
    for t in threads:
        t.join()
Пример #4
0
def main():
    parser = argparse.ArgumentParser(
        description='List X-Road Subsystems with Security Server identifiers.',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=
        'You need to provide either Security Server or Central Server address.\n\n'
        'NB! Global configuration signature is not validated when using Central Server.\n'
        'Use local Security Server whenever possible.')
    parser.add_argument('-s',
                        metavar='SECURITY_SERVER',
                        help='DNS name/IP/URL of local Security Server')
    parser.add_argument(
        '-c',
        metavar='CENTRAL_SERVER',
        help='DNS name/IP/URL of Central Server/Configuration Proxy')
    parser.add_argument('-t',
                        metavar='TIMEOUT',
                        help='timeout for HTTP query',
                        type=float)
    parser.add_argument(
        '--verify',
        metavar='CERT_PATH',
        help='validate peer TLS certificate using CA certificate file.')
    parser.add_argument('--cert',
                        metavar='CERT_PATH',
                        help='use TLS certificate for HTTPS requests.')
    parser.add_argument('--key',
                        metavar='KEY_PATH',
                        help='private key for TLS certificate.')
    parser.add_argument(
        '--instance',
        metavar='INSTANCE',
        help=
        'use this instance instead of local X-Road instance (works only with "-s" argument)'
    )
    args = parser.parse_args()

    instance = None
    if args.instance and six.PY2:
        # Convert to unicode
        instance = args.instance.decode('utf-8')
    elif args.instance:
        instance = args.instance

    timeout = DEFAULT_TIMEOUT
    if args.t:
        timeout = args.t

    verify = False
    if args.verify:
        verify = args.verify

    cert = None
    if args.cert and args.key:
        cert = (args.cert, args.key)

    shared_params = None
    if args.s:
        try:
            shared_params = xrdinfo.shared_params_ss(addr=args.s,
                                                     instance=instance,
                                                     timeout=timeout,
                                                     verify=verify,
                                                     cert=cert)
        except xrdinfo.XrdInfoError as e:
            print_error(e)
            exit(1)
    elif args.c:
        try:
            shared_params = xrdinfo.shared_params_cs(addr=args.c,
                                                     timeout=timeout,
                                                     verify=verify,
                                                     cert=cert)
        except xrdinfo.XrdInfoError as e:
            print_error(e)
            exit(1)
    else:
        parser.print_help()
        exit(1)

    try:
        for subsystem in xrdinfo.subsystems_with_server(shared_params):
            line = xrdinfo.stringify(subsystem)
            if len(subsystem) == 4:
                # No server found
                line = u"{} NOSERVER".format(line)
            if six.PY2:
                print(line.encode('utf-8'))
            else:
                print(line)
    except xrdinfo.XrdInfoError as e:
        print_error(e)
        exit(1)