Skip to content

Commit

Permalink
Added support for pywbem using requests
Browse files Browse the repository at this point in the history
This is done in such a way that pywbemcli can work with pywbem versions
before and after the pywbem change (i.e. pywbem 1.0.0). There is no
need to increase the minimum required pywbem version to 1.0.0.

Details:

* Changed the `--ca-certs` general option to support the changes as of
  pywbem version 1.0.0 (new value 'certifi', and default changed
  from a fixed set of directories to 'certifi'). The pywbem support
  for requests is determined via its new pywbem.PYWBEM_USES_REQUESTS
  attribute.

* Download and invoke pywbem_os_setup.* script for installing pywbemtools
  only when using pywbem before 1.0.0 on Python 2. Note that the script
  is still downloaded and invoked for development of pywbemtools because
  pywbemtools needs some of the same packages pywbem needs for development.

* Adjusted a test case in test_general_options.py to accomodate the different
  exception message when using pywbem with requests.

* Adjusted the Makefile to the change in the downloaded pywbem_os_setup.*
  script by removing it in target clobber. This ensures that there is a
  way to get out of errors resulting from using a previously downloaded
  script that does not fit to the pywbem version that is used.

* Adjusted the Makefile to specify the name for the downloaded
  pywbem_os_setup.* script. This avoids that wget creates numbered
  versions of the file if it already exists.

Signed-off-by: Andreas Maier <andreas.r.maier@gmx.de>
  • Loading branch information
andy-maier committed Dec 20, 2019
1 parent de6ae84 commit 7189916
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 57 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,11 @@ install_basic_$(pymn).done: Makefile pip_upgrade_$(pymn).done
# Scripts are required to install the OS-level components of pywbem.
# Makefile gets the required scripts from the pywbem project on GitHub.
pywbem_os_setup.sh:
wget -q https://raw.githubusercontent.com/pywbem/pywbem/master/pywbem_os_setup.sh
wget -q -O pywbem_os_setup.sh https://raw.githubusercontent.com/pywbem/pywbem/master/pywbem_os_setup.sh
chmod 755 pywbem_os_setup.sh

pywbem_os_setup.bat:
wget -q https://raw.githubusercontent.com/pywbem/pywbem/master/pywbem_os_setup.bat
wget -q -O pywbem_os_setup.bat https://raw.githubusercontent.com/pywbem/pywbem/master/pywbem_os_setup.bat

.PHONY: install_os
install_os: install_os_$(pymn).done
Expand Down Expand Up @@ -479,7 +479,7 @@ all: install develop build builddoc check pylint test
.PHONY: clobber
clobber: clean
@echo "makefile: Removing everything for a fresh start"
-$(call RM_FUNC,*.done epydoc.log $(dist_files) $(pywbemcli_module_path)/*cover)
-$(call RM_FUNC,*.done epydoc.log $(dist_files) $(pywbemcli_module_path)/*cover pywbem_os_setup.*)
-$(call RMDIR_FUNC,$(doc_build_dir) .tox $(coverage_html_dir))
@echo "makefile: Done removing everything for a fresh start"
@echo "makefile: Target $@ done."
Expand Down
11 changes: 4 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
pywbemtools: Python tools for communicating with WBEM servers
=============================================================

.. |os-setup-link| raw:: html

<a href="https://pywbemtools.readthedocs.io/en/stable/_downloads/pywbem_os_setup.sh">pywbem_os_setup.sh</a>

.. image:: https://img.shields.io/pypi/v/pywbemtools.svg
:target: https://pypi.python.org/pypi/pywbemtools/
:alt: Version on Pypi
Expand Down Expand Up @@ -84,7 +80,8 @@ Requirements:
2. Operating Systems: Linux, OS-X, native Windows, UNIX-like environments on
Windows (e.g. Cygwin)

3. When using Python 2, the following OS-level packages:
3. When using a pywbem version before 1.0.0 on Python 2, the following
OS-level packages are needed:

* On native Windows:

Expand All @@ -99,10 +96,10 @@ Requirements:
- ``wget`` - Download tool. Can be installed using the OS-level package
manager for the platform.


Installation:

* When using Python 2, install OS-level packages needed by the pywbem package:
* When using a pywbem version before 1.0.0 on Python 2, install OS-level
packages needed by the pywbem package:

- On native Windows:

Expand Down
6 changes: 4 additions & 2 deletions README_PYPI.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Requirements:
2. Operating Systems: Linux, OS-X, native Windows, UNIX-like environments on
Windows (e.g. Cygwin)

3. When using Python 2, the following OS-level packages:
3. When using a pywbem version before 1.0.0 on Python 2, the following
OS-level packages are needed:

* On native Windows:

Expand All @@ -69,7 +70,8 @@ Requirements:

Installation:

* When using Python 2, install OS-level packages needed by the pywbem package:
* When using a pywbem version before 1.0.0 on Python 2, install OS-level
packages needed by the pywbem package:

- On native Windows:

Expand Down
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ Released: not yet
class/instance delete, instance modify and the commands that display
cim objects. (See issue #123)

* Changed the `--ca-certs` general option to support the changes as of
pywbem version 1.0.0 (new values 'system' and 'certifi', and default changed
from a fixed set of directories to 'certifi'). The pywbem version is
determined at run time and pywbem versions before 1.0.0 are still supported.

**Cleanup:**

* Test: Enabled Python warning suppression for PendingDeprecationWarning
Expand Down
17 changes: 6 additions & 11 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ prerequisites and its setup is described in the :ref:`Pywbemtools development` c
Installation prerequisites
^^^^^^^^^^^^^^^^^^^^^^^^^^

.. _pywbem installation documentation: https://pywbem.readthedocs.io/en/stable/intro.html#installation

The Python environment into which you want to install must have the following
Python packages installed:

Expand All @@ -108,14 +106,11 @@ Python packages installed:
- pip - generally installed with Python 3.x but may be a separate install
with Python 2.7 and with Cygwin Python releases.

Pywbemtools installs the pywbem package which requires a number of OS-level
packages as documented in the `pywbem installation documentation`_.
The pywbem package provides the ``pywbem_os_setup.sh/.bat`` scripts that
install the needed OS-level packages. These scripts are only needed when
using Python 2.
Pywbemtools installs the pywbem package.

To use these scripts, your system must have the following commands installed
when using Python 2:
When using pywbem versions before 1.0.0 on Python 2, pywbem requires a number
of OS-level packages, and your system must have the following commands
installed:

* On native Windows:

Expand All @@ -130,13 +125,13 @@ when using Python 2:
- ``wget`` - Download tool. Can be installed using the OS-level package
manager for the platform.


.. _`Installation with pip`:

Installation with pip
^^^^^^^^^^^^^^^^^^^^^

When using Python 2, install OS-level packages needed by the pywbem package:
When using pywbem versions before 1.0.0 on Python 2, install the OS-level
packages needed by the pywbem package as follows:

* On native Windows:

Expand Down
15 changes: 9 additions & 6 deletions docs/pywbemcli/cmdshelp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,16 @@ Help text for ``pywbemcli``:
verify client bypasses verification.
Default: EnvVar PYWBEMCLI_VERIFY, or "--
verify".
--ca-certs FILE Path name of a file or directory containing
certificates that will be matched against
the server certificate presented by the WBEM
server during TLS/SSL handshake. Default:
EnvVar PYWBEMCLI_CA_CERTS, or [/etc/pki/ca-
--ca-certs CACERTS CA certificates to be used for verifying the
server certificate presented by the WBEM
server during TLS/SSL handshake: FILE: use
the certs in the specified cert file; DIR:
use the certs in the specified cert
directory. Default: EnvVar
PYWBEMCLI_CA_CERTS, or the first existing
directory in: /etc/pki/ca-
trust/extracted/openssl/ca-bundle.trust.crt,
/etc/ssl/certs, /etc/ssl/certificates].
/etc/ssl/certs, /etc/ssl/certificates
-c, --certfile FILE Path name of a PEM file containing a X.509
client certificate that is used to enable
TLS/SSL 2-way authentication by presenting
Expand Down
32 changes: 24 additions & 8 deletions pywbemtools/pywbemcli/_pywbem_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from collections import OrderedDict
import click

import pywbem
from pywbem import WBEMServer, configure_loggers_from_string

from .config import DEFAULT_URL_SCHEME, DEFAULT_CONNECTION_TIMEOUT, \
Expand Down Expand Up @@ -455,14 +456,29 @@ def create_connection(self, log=None, use_pull=None, pull_max_cnt=None,
else:
no_verification = not self.verify

conn = PYWBEMCLIConnection(
self.server, creds,
default_namespace=self.default_namespace,
no_verification=no_verification,
x509=x509_dict, ca_certs=self.ca_certs,
timeout=self.timeout,
use_pull_operations=use_pull,
stats_enabled=timestats)
# Convert ca_certs command line option to ca_certs parameter
if getattr(pywbem, 'PYWBEM_USES_REQUESTS', False):
if self.ca_certs == 'certifi':
ca_certs = None
else:
ca_certs = self.ca_certs
else:
ca_certs = self.ca_certs

try:
conn = PYWBEMCLIConnection(
self.server, creds,
default_namespace=self.default_namespace,
no_verification=no_verification,
x509=x509_dict, ca_certs=ca_certs,
timeout=self.timeout,
use_pull_operations=use_pull,
stats_enabled=timestats)
except IOError as exc:
raise click.ClickException(
'Cannot create connection to {}: {}'.
format(self.server, exc))

# Create a WBEMServer object
self._wbem_server = WBEMServer(conn)

Expand Down
35 changes: 24 additions & 11 deletions pywbemtools/pywbemcli/pywbemcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory


import pywbem
from pywbem import DEFAULT_CA_CERT_PATHS, LOGGER_SIMPLE_NAMES, \
from pywbem import LOGGER_SIMPLE_NAMES, \
LOG_DESTINATIONS, DEFAULT_LOG_DESTINATION, LOG_DETAIL_LEVELS, \
DEFAULT_LOG_DETAIL_LEVEL

Expand All @@ -42,7 +41,6 @@
from .config import DEFAULT_OUTPUT_FORMAT, DEFAULT_NAMESPACE, \
PYWBEMCLI_PROMPT, PYWBEMCLI_HISTORY_FILE, DEFAULT_MAXPULLCNT, \
DEFAULT_CONNECTION_TIMEOUT, MAX_TIMEOUT, USE_AUTOSUGGEST

from ._connection_repository import ConnectionRepository
from ._click_extensions import PywbemcliTopGroup

Expand All @@ -59,6 +57,26 @@
# enable -h as additional help option
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

if getattr(pywbem, 'PYWBEM_USES_REQUESTS', False):
CA_CERTS_HELP = \
'CA certificates to be used for verifying the server certificate ' \
'presented by the WBEM server during TLS/SSL handshake: ' \
'"certifi": use certs from certifi Python package; ' \
'FILE: use the certs in the specified cert file; ' \
'DIR: use the certs in the specified cert directory. ' \
'Default: EnvVar {ev}, or "certifi".'. \
format(ev=PywbemServer.ca_certs_envvar)
else:
default_paths = getattr(pywbem, 'DEFAULT_CA_CERT_PATHS')
CA_CERTS_HELP = \
'CA certificates to be used for verifying the server certificate ' \
'presented by the WBEM server during TLS/SSL handshake: ' \
'FILE: use the certs in the specified cert file; ' \
'DIR: use the certs in the specified cert directory. ' \
'Default: EnvVar {ev}, or the first existing directory in: {dirs}'. \
format(ev=PywbemServer.ca_certs_envvar,
dirs=', '.join(default_paths))


# pylint: disable=bad-continuation
# PywbemcliTopGroup sets order commands listed in help output
Expand Down Expand Up @@ -119,15 +137,10 @@
'handshake. If --no-verify client bypasses verification. '
'Default: EnvVar {ev}, or "--verify".'.
format(ev=PywbemServer.verify_envvar))
@click.option('--ca-certs', type=str, metavar="FILE",
@click.option('--ca-certs', type=str, metavar="CACERTS",
default=None, # defaulted in code
envvar=PywbemServer.ca_certs_envvar,
help='Path name of a file or directory containing certificates '
'that will be matched against the server certificate '
'presented by the WBEM server during TLS/SSL handshake. '
'Default: EnvVar {ev}, or [{dirs}].'.
format(ev=PywbemServer.ca_certs_envvar,
dirs=', '.join(DEFAULT_CA_CERT_PATHS)))
help=CA_CERTS_HELP)
@click.option('-c', '--certfile', type=str, metavar="FILE",
# defaulted in code
envvar=PywbemServer.certfile_envvar,
Expand Down Expand Up @@ -359,7 +372,7 @@ def create_server_instance(svr_name):
resolved_default_namespace = default_namespace or DEFAULT_NAMESPACE
resolved_timestats = timestats or DEFAULT_TIMESTATS
resolved_verify = DEFAULT_VERIFY if verify is None else verify
resolved_ca_certs = DEFAULT_CA_CERT_PATHS if ca_certs is None else ca_certs
resolved_ca_certs = ca_certs # None should be passed on

if server and svr_name:
raise click.ClickException(
Expand Down
41 changes: 32 additions & 9 deletions tests/unit/test_general_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
import sys
import pytest
import pywbem

from .cli_test_extensions import CLITestsBase

Expand All @@ -37,6 +38,30 @@
BAD_PY_ERR_STRTUP_PATH = os.path.join(SCRIPT_DIR, 'py_err_processatstartup.py')
MOCK_PW_PROMPT_PATH = os.path.join(SCRIPT_DIR, 'mock_password_prompt.py')

if getattr(pywbem, 'PYWBEM_USES_REQUESTS', False):
CA_CERTS_HELP = """
--ca-certs CACERTS CA certificates to be used for verifying the
server certificate presented by the WBEM
server during TLS/SSL handshake: "system":
use system-provided certs; "certifi": use
certs from certifi Python package; FILE: use
the certs in the specified cert file; DIR:
use the certs in the specified cert
directory. Default: EnvVar
PYWBEMCLI_CA_CERTS, or "certifi"."""
else:
CA_CERTS_HELP = """
--ca-certs CACERTS CA certificates to be used for verifying the
server certificate presented by the WBEM
server during TLS/SSL handshake: FILE: use
the certs in the specified cert file; DIR:
use the certs in the specified cert
directory. Default: EnvVar
PYWBEMCLI_CA_CERTS, or the first existing
directory in: /etc/pki/ca-
trust/extracted/openssl/ca-bundle.trust.crt,
/etc/ssl/certs, /etc/ssl/certificates"""

GENERAL_HELP = """
Usage: pywbemcli [GENERAL-OPTIONS] COMMAND [ARGS]...
Expand Down Expand Up @@ -104,14 +129,9 @@
server during TLS/SSL handshake. If --no-
verify client bypasses verification.
Default: EnvVar PYWBEMCLI_VERIFY, or "--
verify".
--ca-certs FILE Path name of a file or directory containing
certificates that will be matched against
the server certificate presented by the WBEM
server during TLS/SSL handshake. Default:
EnvVar PYWBEMCLI_CA_CERTS, or [/etc/pki/ca-
trust/extracted/openssl/ca-bundle.trust.crt,
/etc/ssl/certs, /etc/ssl/certificates].
verify".""" + \
CA_CERTS_HELP + \
"""
-c, --certfile FILE Path name of a PEM file containing a X.509
client certificate that is used to enable
TLS/SSL 2-way authentication by presenting
Expand Down Expand Up @@ -278,7 +298,10 @@ class Command group for CIM classes.
{'general': ['-s', 'http://blah:abcd'],
'cmdgrp': 'class',
'args': ['get', 'blah']},
{'stderr': ['Error:', 'ConnectionError', 'Socket error'],
{'stderr': ['Error:', 'ConnectionError',
'Failed to parse'
if getattr(pywbem, 'PYWBEM_USES_REQUESTS', False)
else 'Socket error'],
'rc': 1,
'test': 'regex'},
None, OK],
Expand Down

0 comments on commit 7189916

Please sign in to comment.