IBM-Cloud / get-started-python

A Python application and tutorial that use Flask framework to provide a REST API to receive requests from the UI. The API then persists the data to a Cloudant database.
https://console.ng.bluemix.net/docs/runtimes/python/getting-started.html
Apache License 2.0
121 stars 446 forks source link

Cloudant credentials do not have password #35

Closed Kieran7741 closed 3 years ago

Kieran7741 commented 3 years ago

When linking a cloudant service to the app there is no password parameter in the credentials. It seems there is only a api key is present.

{
  "apikey": "xxxxx-T3GcA6wZM",
  "host": "xxxx-227-bluemix.cloudantnosqldb.appdomain.cloud",
  "iam_apikey_description": "Auto-generated for key xxxxx-3951d1c1a606",
  "iam_apikey_name": "Service credentials-1",
  "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",
  "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/xxxxx-dc39468353e4::serviceid:ServiceId-xxxxx-0ec8d791aafe",
  "url": "https://xxxxx5a29e017a227-bluemix.cloudantnosqldb.appdomain.cloud",
  "username": "xxxx-5a29e017a227-bluemix"
}

The app then crashed due to no password:

# hello.py
if 'VCAP_SERVICES' in os.environ:
    vcap = json.loads(os.getenv('VCAP_SERVICES'))
    print('Found VCAP_SERVICES')
    if 'cloudantNoSQLDB' in vcap:
        creds = vcap['cloudantNoSQLDB'][0]['credentials']
        user = creds['username']
        password = creds['password']
        url = 'https://' + creds['host']
        client = Cloudant(user, password, url=url, connect=True)
        db = client.create_database(db_name, throw_on_exists=False)

Error

2021-04-05T17:44:13.52+0100 [CELL/0] OUT Starting health monitoring of container
   2021-04-05T17:44:17.79+0100 [APP/PROC/WEB/0] OUT Found VCAP_SERVICES
   2021-04-05T17:44:17.79+0100 [APP/PROC/WEB/0] ERR Traceback (most recent call last):
   2021-04-05T17:44:17.79+0100 [APP/PROC/WEB/0] ERR   File "hello.py", line 19, in <module>
   2021-04-05T17:44:17.79+0100 [APP/PROC/WEB/0] ERR     password = creds['password']
   2021-04-05T17:44:17.79+0100 [APP/PROC/WEB/0] ERR KeyError: 'password'
   2021-04-05T17:44:17.90+0100 [APP/PROC/WEB/0] OUT Exit status 1
   2021-04-05T17:44:17.92+0100 [CELL/SSHD/0] OUT Exit status 0

Maybe client = Cloudant.iam(ACCOUNT_NAME, API_KEY, connect=True) should be used instead to establish a connection

Kieran7741 commented 3 years ago

Closing due to misreading docs. I should have created the Cloudant instance with Use both legacy credentials and IAM