Updating a database's password using API

Hello. I’m trying to update a database’s connection details within metabase using the API. My reference is: https://github.com/metabase/metabase/blob/master/docs/api-documentation.md

I can successfully get my authentication token, then I do the following:

curl -X PUT -H "Content-Type: application/json" -H "X-Metabase-Session: " -d '{ "id": "1", "engine": "mongo", "name": "Name", "details": { "dbname":"dbname", "host": "hostname", "port": 27017, "username": "username", "password": "newpassword" } }' https://myurl.com/api/database/1

I get the following error:

{"valid":false,"dbname":"Timed out after 3000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=...

I’m only interested in updating the password for this connection in metabase as the password is rotated frequently, I wanted to automate the connection change with the API.

Thanks!

Hi there,

It looks like when you’re submitting the request, Metabase is having trouble connecting to the backend database (Timed out after 3000 ms). Are you able to update the database within the Metabase UI? Are all the parameters the same? And are there any errors in the Metabase server logs?

Thanks,

Damon

Hi. I’m able to update it with the UI. I’ve been doing in through the UI on a weekly basis :slight_smile: All the parameters are the same, except I am providing a new password as it has changed.

If I only supply the password to be updated, it complains of missing parameters.

[7355191a-1a04-4055-8b23-43f7f4a5e486] 2020-01-08T10:57:03+11:00 ERROR metabase.driver.util Database connection error [7355191a-1a04-4055-8b23-43f7f4a5e486] 2020-01-08T10:57:03+11:00 DEBUG metabase.middleware.log PUT /api/database/10 400 3.0 s (1 DB calls) {:valid false, :dbname "Timed out after 3000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=....mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: ....mongodb.net}, caused by {java.net.UnknownHostException: ....mongodb.net}}]", :message "Timed out after 3000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=....mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: ....mongodb.net}, caused by {java.net.UnknownHostException: ....mongodb.net}}]"}

Interesting - the behavior should be the same regardless of whether you’re submitting the request via the GUI or via the API.

For the most recent error message you posted, I see that there’s a java.net.UnknownHostException - is that because you only supplied the password to be updated? And what version of Metabase are you using?

Thanks,

Damon

You're on version v0.33.6

Built on 2019-11-19

I supplied all the details. And the host is exactly the same as in the GUI.

Thanks, I’ll look into this a little bit tomorrow to see if I can reproduce.

Thanks heaps. Appreciate the help.

Heyo,

One thing I noticed is that in your curl content is that you have username and password fields, however it looks like when updating the credentials via the GUI it passes these fields over as user and pass.

I would recommend capturing the network request that’s being made in your browser and comparing the JSON request that’s being sent over. This will show up under the XHR tab if you open your browser’s developer tools. Feel free to post the two different requests with sensitive details removed. Chrome has a nice “copy as cURL” feature that could help here. My guess is that there’s some setting that’s getting passed in the GUI that isn’t in your curl request. The mention of the mongodb.net error message makes me think it might be related to the “Use DNS SRV” setting, which gets passed as use-srv in the API request.

Hope that helps!

Damon

Perfect! The culprit was password != pass…

Thanks so much for looking into this.