xwang
April 28, 2021, 1:20pm
1
When I tried to create a datasource using the HTTP API for a sql server, the datasource was created fine, but there is no password there even though it is provided during the API call.
I have been googling the topic for a few days, there is seldom any mention of people doing this. Does any one have a working sample for creating SQL server datasource?
data = {
“name”:“testDB”,
“type”:“mssql”,
“url”:"{0}:{1}".format(db_host,db_port),
“database”:"{0}".format(db_name),
“user”:"{0}".format(db_user),
“access”:“proxy”,
“password”: “{0}”.format(db_pass),
“basicAuth”: False
}
bkgann
April 28, 2021, 1:41pm
2
the password needs to be put into the “secureJsonData” block
data = {
“name”:“testDB”,
“type”:“mssql”,
“url”:"{0}:{1}".format(db_host,db_port),
“database”:"{0}".format(db_name),
“user”:"{0}".format(db_user),
“access”:“proxy”,
“basicAuth”: False,
“secureJsonData:” {
“password”: “{0}”.format(db_pass),
}
}
1 Like
xwang
April 28, 2021, 2:14pm
3
Thanks for the reply. I forgot to mention I tried placing the password in the secureJsonData part as well it did not populate the password field.
bkgann
April 28, 2021, 3:11pm
4
I posted this to the api:
{
"name":"test_datasource",
"type":"mssql",
"url":"server:1521",
"access":"proxy",
"database": "contoso",
"basicAuth":false,
"user": "bogus",
"secureJsonData": {
"password": "abc123"
}
}
and it showed up correctly:
xwang
April 28, 2021, 3:42pm
5
I tried the your data but it came up with blank password. Maybe there is a setting somewhere? My grafana is V7.4.0 and I am using basic auth to send in the request.
bkgann
April 28, 2021, 3:47pm
6
i tried with 7.4.0, though i’m using an api token and not basic auth to post to the api:
curl --location --request POST 'http://grafana.staged-by-discourse.com/api/datasources' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJrIjoidlFhS3Q4eUJiTVRlTDNYbjhlRkFyR2NjTXRrZVdkSk8iLCJuIjoiYXV0bzIiLCJpZCI6MX0=' \
--data-raw '{
"name": "test_datasource",
"type": "mssql",
"url": "server:1521",
"access": "proxy",
"database": "contoso",
"basicAuth": false,
"user": "bogus",
"secureJsonData": {
"password": "abc123"
}
}'
The docs only show using an api key: Data source HTTP API | Grafana Labs
xwang
April 28, 2021, 3:51pm
7
Thanks for the help. My task is to auto-config the Grafana server with a script so that nobody need to login to administer it, so I cannot use a API token. All other part works fine with basic auth, including users, dashboards. This is only thing holding up now.
bkgann
April 28, 2021, 3:52pm
8
you have the option of datasource provisioning during startup of the grafana server instead of using the api, if that helps.
xwang
April 28, 2021, 3:53pm
9
Unfortunately I will have no access to the Grafana server except through this API. Thanks a lot.
bkgann
April 28, 2021, 3:57pm
10
i just tried basic auth and that worked also (using postman)
curl --location --request POST 'http://grafana.staged-by-discourse.com/api/datasources' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YWRtaW46YWRtaW4=' \
--data-raw '{
"name": "test_datasource",
"type": "mssql",
"url": "server:1521",
"access": "proxy",
"database": "contoso",
"basicAuth": false,
"user": "bogus",
"secureJsonData": {
"password": "abc123"
}
}'
xwang
April 28, 2021, 3:59pm
11
You basic auth looks different, mine is through a url like “http://user:pass@localhost:3000/api/datasources ”
bkgann
April 28, 2021, 4:01pm
12
i haven’t had any luck with that syntax, have had to put it into the header
xwang
April 28, 2021, 4:02pm
13
OK, I will try it this afternoon and let you know.
xwang
April 28, 2021, 5:40pm
14
I tried using the auth header like you did, but still getting the blank password. Might be some configuration in my Grafana is different.
xwang
April 28, 2021, 5:56pm
15
I checked my defaul.ini everything seems normal. My Grafana server is running SSL, could it be a bug somewhere? What else might cause this behavior?
bkgann
April 29, 2021, 3:51am
16
can you try it with something like postman?
1 Like
xwang
April 29, 2021, 12:51pm
17
Good suggestion! I tried SOAPUI it actually worked, the password is set. Now I will need to figured out where the difference is in these messages.
xwang
April 29, 2021, 1:02pm
18
Finally got it all figured out. The problem was due to an error in my python script. The json data need to passed in using “json” parameter instead of “data” I was using.
Correct:
response = requests.post(url, json =data, verify=False, auth=(username, password))
Wrong:
response = requests.post(url, data =data, verify=False, auth=(username, password))
Thanks a lot, bkgann.
1 Like
I’m facing the same issue when I try to pre configure Postgres Sql using YAML file ,password is not displaying. Can someone help me out on this.
my YAML file looks like
datasources:
name: “PostgreSQL-Energy-Usage”
type: “postgres”
access: “proxy”
orgId: 1
url: “localhost:5432”
password: “”
user: “postgres”
database: “DbTest”
basicAuth: false
isDefault: true
jsonData:
sslmode: “disable”
postgresVersion: 903
tlsAuth: false
tlsAuthWithCACert: false
tlsConfigurationMethod: “file-path”
tlsSkipVerify: true
secureJsonData:
password: “abc123”
version: 1
editable: true
system
Closed
April 28, 2022, 1:20pm
20
This topic was automatically closed after 365 days. New replies are no longer allowed.