open-traffic-generator / snappi-ixnetwork

The Keysight IxNetwork implementation of the open-traffic-generator models.
MIT License
17 stars 11 forks source link

snappi_convergence.py: api function not accepting the username and password arguments #564

Open tigrmeli opened 1 year ago

tigrmeli commented 1 year ago

Version information The pypi version or branch of snappi-ixnetwork snappi 0.11.15 snappi-convergence 0.4.1 snappi-ixnetwork 0.9.1

Describe the bug The api function in file snappi_convergence.py snappi_convergence.py.txt not accepting username and password as an argument.

def api(
    location=None,
    transport=None,
    verify=True,
    logger=None,
    loglevel=logging.INFO,
    ext=None,
):
    """Create an instance of an Api class

    generator.Generator outputs a base Api class with the following:
    - an abstract method for each OpenAPI path item object
    - a concrete properties for each unique OpenAPI path item parameter.

    generator.Generator also outputs an HttpApi class that inherits the base
    Api class, implements the abstract methods and uses the common HttpTransport
    class send_recv method to communicate with a REST based server.

    Args
    ----
    - location (str): The location of an Open Traffic Generator server.
    - transport (enum["http", "grpc"]): Transport Type
    - verify (bool): Verify the server's TLS certificate, or a string, in which
      case it must be a path to a CA bundle to use. Defaults to `True`.
      When set to `False`, requests will accept any TLS certificate presented by
      the server, and will ignore hostname mismatches and/or expired
      certificates, which will make your application vulnerable to
      man-in-the-middle (MitM) attacks. Setting verify to `False`
      may be useful during local development or testing.
    - logger (logging.Logger): A user defined logging.logger, if none is provided
      then a default logger with a stdout handler will be provided
    - loglevel (logging.loglevel): The logging package log level.
      The default loglevel is logging.INFO
    - ext (str): Name of an extension package
    """
    params = locals()
    transport_types = ["http", "grpc"]
    if ext is None:
        transport = "http" if transport is None else transport
        if transport not in transport_types:
            raise Exception(
                "{transport} is not within valid transport types {transport_types}".format(
                    transport=transport, transport_types=transport_types
                )
            )
        if transport == "http":
            return HttpApi(**params)
        else:
            return GrpcApi(**params)
    try:
        if transport is not None:
            raise Exception(
                "ext and transport are not mutually exclusive. Please configure one of them."
            )
        lib = importlib.import_module("snappi_{}.snappi_convergence_api".format(ext))
        return lib.Api(**params)
    except ImportError as err:
        msg = "Extension %s is not installed or invalid: %s"
        raise Exception(msg % (ext, err))

When using the api function to create an Api class instance: api = snappi_convergence.api(location=snappi_api_serv_ip + ':' + str(snappi_api_serv_port))

the api function in snappi_convergence.py doesn’t accept username and password parameters. This function takes the local variables, which are (location, transport, verify, logger, loglevel, ext), and passes them to the Api class: return lib.Api(**params)

This then initializes the Api class found in https://github.com/open-traffic-generator/snappi-ixnetwork/blob/8fd30b0bb3685be4836e1fd61ab1d5b706265429/snappi_ixnetwork/snappi_api.py#L4

Within the init method of this class, there are two lines of code that attempt to retrieve the username and password from the keyword arguments: line 47-48 snappi_api.py

username = kwargs.get("username")
password = kwargs.get("password")

Since username and password were not among the parameters passed to the api function, they won't be part of kwargs, and therefore username and password will always be None.

As a consequence, the Api class defaults to using "admin" for both the username and password: line 57-58 snappi_api.py

self._username = "admin" if username is None else username
self._password = "admin" if password is None else password

To Reproduce Run the pytest https://github.com/sonic-net/sonic-mgmt/blob/948dd1483e5bf9e4a57497c6cc6d668896d538cb/tests/snappi/bgp/test_bgp_scalability.py#L4 The test throw error on line 905 file https://github.com/sonic-net/sonic-mgmt/blob/948dd1483e5bf9e4a57497c6cc6d668896d538cb/tests/snappi/bgp/files/bgp_convergence_helper.py#L4 The error is "Invalid username or password"

I fixed it by adding arguments to api function in snappi_convergence.py

def api(
    location=None,
    transport=None,
    verify=True,
    logger=None,
    loglevel=logging.INFO,
    ext=None,
    username=None,
    password=None
):