Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tigrmeli opened this issue Jun 29, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@tigrmeli
Copy link

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

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
):
@arkajyoti-cloud arkajyoti-cloud added the bug Something isn't working label Mar 21, 2024
@arkajyoti-cloud arkajyoti-cloud assigned alakendu and unassigned Vibaswan Mar 21, 2024
@indraniBh indraniBh self-assigned this Nov 5, 2024
@indraniBh indraniBh added enhancement New feature or request and removed bug Something isn't working labels Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants