1Password / connect-sdk-python

Python SDK for 1Password Connect
https://developer.1password.com/docs/connect
MIT License
200 stars 31 forks source link

new_client_from_environment() requires a URL passed in as an argument #22

Closed agile-jtdressel closed 3 years ago

agile-jtdressel commented 3 years ago

Your environment

SDK Version: onepasswordconnectsdk==1.0.1

Connect Server Version: 1.2.0

OS: macOS 11.4 Intel

Python Version: Python 3.8.2

What happened?

After setting OP_CONNECT_TOKEN and OP_CONNECT_HOST I called client_from_env: Client = new_client_from_environment()

This caused an error, because I was missing an argument.

Traceback (most recent call last):
  File "./experiment.py", line 27, in main
    client_from_env: Client = new_client_from_environment()
TypeError: new_client_from_environment() missing 1 required positional argument: 'url'

What did you expect to happen?

I expected a client to be created.

I worked around this issue by adding the url to the call. client_from_env: Client = new_client_from_environment(os.getenv('OP_CONNECT_HOST'))

Steps to reproduce

Script output:

./experiment.py --log DEBUG 
DEBUG:root:OP_CONNECT_HOST: http://localhost:8080
DEBUG:root:OP_CONNECT_TOKEN length: 665. (To show the environment variable is set)
DEBUG:root:use sample code to create a client using my environment
ERROR:root:Unable to create a client with new_client_from_environment()
Traceback (most recent call last):
  File "./experiment.py", line 27, in main
    client_from_env: Client = new_client_from_environment()
TypeError: new_client_from_environment() missing 1 required positional argument: 'url'
DEBUG:root:modify sample code to pass in url
INFO:root:Client created with a modified call to new_client_from_environment
DEBUG:root:Get a list of vaults
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): localhost:8080
DEBUG:urllib3.connectionpool:http://localhost:8080 "GET /v1/vaults HTTP/1.1" 200 202
[{'attribute_version': 1,
 'content_version': 5,
 'created_at': datetime.datetime(2021, 5, 27, 13, 53, 6, tzinfo=tzutc()),
 'description': None,
 'id': 't2tabyg7x33bybglznejs2loz4',
 'items': 2,
 'name': 'sa test vault',
 'type': 'USER_CREATED',
 'updated_at': datetime.datetime(2021, 5, 27, 14, 3, 20, tzinfo=tzutc())}]

Script:

#!/usr/bin/env python3

import logging
import argparse
import sys
import os
import onepasswordconnectsdk

if sys.version_info < (3, 6):
    print('Please upgrade your Python version to 3.6.0 or higher')
    sys.exit()

def main():
    from onepasswordconnectsdk.client import (
        Client,
        new_client_from_environment,
        new_client
    )

    logging.debug(f"OP_CONNECT_HOST: {os.getenv('OP_CONNECT_HOST')}")
    logging.debug(f"OP_CONNECT_TOKEN length: {len(os.getenv('OP_CONNECT_TOKEN'))}. (To show the environment variable is set)")

    client_from_env = None

    try:
        logging.debug(f"use sample code to create a client using my environment")
        client_from_env: Client = new_client_from_environment()
        logging.info("client created using sample code")
    except: 
        logging.exception("Unable to create a client with new_client_from_environment()")

    try:
        logging.debug(f"modify sample code to pass in url")
        client_from_env: Client = new_client_from_environment(os.getenv('OP_CONNECT_HOST'))
        logging.info("Client created with a modified call to new_client_from_environment")
    except: 
        logging.error("Unable to create a client")

    logging.debug("Get a list of vaults")
    print(client_from_env.get_vaults())

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--log', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'], default = os.getenv("LOG_LEVEL", 'INFO'), help="Can also be set as env var LOG_LEVEL")
    args = parser.parse_args()
    logging.getLogger().setLevel(args.log)
    main()

Notes & Logs

SimonBarendse commented 3 years ago

https://github.com/1Password/connect-sdk-python/pull/16 introduced support for calling new_client_from_environment() without passing a url. It will be included in our next release.

The latest released version doesn't include this yet. You can find documentation for this version here: https://github.com/1Password/connect-sdk-python/tree/v1.0.1#usage .

Your proposed workaround will continue to work when we release the changes that make passing the URL optional, as we've kept the URL parameter for backwards compatibility.