Skip to content

Commit

Permalink
Support unauthenticated dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
meln5674 committed Sep 15, 2022
1 parent b8feee5 commit 914cef1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
11 changes: 9 additions & 2 deletions integration-test/datasets/tweets.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Sat Apr 25 16:19:01 +0000 2015
# Mon Jan 02 15:04:05 -0700 2006
# "RubyDate"
set -x
ARGS=()
if [ "${ALLOW_EMPTY_PASSWORD}" != "yes" ]; then
ARGS+=(
--username "${MONGODB_ROOT_USER}"
--password "${MONGODB_ROOT_PASSWORD}"
)
fi
mongorestore \
--username "${MONGODB_ROOT_USER}" \
--password "${MONGODB_ROOT_PASSWORD}" \
"${ARGS[@]}" \
/mnt/host/grafana-mongodb-community-plugin/integration-test/datasets/download/tweets/dump/
5 changes: 5 additions & 0 deletions integration-test/datasources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ datasources:
secureJsonData:
username: root
password: rootPassword
- name: mongodb-no-auth
type: meln5674-mongodb-community
access: proxy
jsonData:
url: mongodb://mongodb-no-auth:27017

1 change: 1 addition & 0 deletions integration-test/queries/tweets/timeseries-no-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"queries":[{"refId":"A","database":"twitter","collection":"tweets","timestampField":"created_at","labelFields":["user"],"valueFields":["retweet_count"],"valueFieldTypes":["float64"],"aggregation":"[{\"$project\":{\"created_at\":1, \"retweet_count\": 1, \"user\": \"$user.url\" }}, { \"$match\" : { \"retweet_count\" : { \"$gt\": 1000 } } } ]","autoTimeBound":false,"autoTimeSort":true,"queryType":"Timeseries","timestampFormat":"Mon Jan 02 15:04:05 -0700 2006","key":"Q-dcb1733c-b2cd-4a45-bc7b-00e76984d22b-0","datasource":{"uid":"GLlI6wg4z","type":"meln5674-mongodb-community"},"datasourceId":2,"intervalMs":900000,"maxDataPoints":681}],"range":{"from":"2015-04-25T15:15:52.000Z","to":"2015-05-03T09:01:56.000Z","raw":{"from":"2015-04-25T15:15:52.000Z","to":"2015-05-03T09:01:56.000Z"}},"from":"1429974952000","to":"1430643716000"}
1 change: 1 addition & 0 deletions integration-test/queries/weather/table-no-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"queries":[{"refId":"A","database":"test","collection":"weather","timestampField":"timestamp","labelFields":["sensorID"],"valueFields":["timestamp","sensorID","temperature"],"valueFieldTypes":["time","int32","int32"],"aggregation":"[{\"$project\":{\"timestamp\":1,\"sensorID\":\"$metadata.sensorId\",\"temperature\":\"$temp\",\"_id\":0}}]","autoTimeBound":true,"autoTimeSort":true,"queryType":"Table","key":"Q-020c94fc-9c76-4def-a5fd-3c5377238e56-0","datasource":{"uid":"GLlI6wg4z","type":"meln5674-mongodb-community"},"datasourceId":2,"intervalMs":120000,"maxDataPoints":681}],"range":{"from":"2021-05-18T20:00:00.000Z","to":"2021-05-19T20:00:00.000Z","raw":{"from":"2021-05-18T20:00:00.000Z","to":"2021-05-19T20:00:00.000Z"}},"from":"1621368000000","to":"1621454400000"}
1 change: 1 addition & 0 deletions integration-test/queries/weather/timeseries-no-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"queries":[{"database":"test","collection":"weather","timestampField":"timestamp","labelFields":["sensorID"],"valueFields":["temperature","foo"],"valueFieldTypes":["int32","int64"],"aggregation":"[{ \"$project\": { \"timestamp\": 1, \"sensorID\": \"$metadata.sensorId\", \"temperature\": \"$temp\", \"foo\": { \"$literal\" : 1621296000000 } }}]","refId":"A","key":"Q-1658634116513-0.7764272519013262-0","maxDataPoints":681,"liveStreaming":false,"showingGraph":true,"showingTable":true,"datasourceId":2,"intervalMs":300000,"orgId":1}],"range":{"from":"2021-05-18T00:00:00.000Z","to":"2021-05-19T20:00:00.000Z","raw":{"from":"2021-05-18T00:00:00.000Z","to":"2021-05-19T20:00:00.000Z"}},"from":"1621296000000","to":"1621454400000"}
5 changes: 5 additions & 0 deletions integration-test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ MONGODB_ARGS=(

helm upgrade --install --wait mongodb bitnami/mongodb "${MONGODB_ARGS[@]}"

MONGODB_ARGS+=( --set auth.enabled=false )

helm upgrade --install --wait mongodb-no-auth bitnami/mongodb "${MONGODB_ARGS[@]}"

GRAFANA_ARGS=(
--set datasources.secretName=datasources
--set admin.password=adminPassword
Expand Down Expand Up @@ -148,6 +152,7 @@ spec:
args:
- |
curl -v -f -u admin:adminPassword http://grafana:3000/api/datasources/1/health
curl -v -f -u admin:adminPassword http://grafana:3000/api/datasources/2/health
for query in weather/timeseries weather/table tweets/timeseries; do
curl 'http://grafana:3000/api/ds/query' \
-v -f \
Expand Down
18 changes: 11 additions & 7 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,32 @@ func connect(ctx context.Context, pCtx backend.PluginContext) (client *mongo.Cli
if err != nil {
return nil, "", nil, err
}
opts := mongoOpts.Client()

mongoURL, err := url.Parse(data.URL)
if err != nil {
return nil, "Invalid URL: ", err, nil
}

opts = opts.ApplyURI(mongoURL.String())
username, hasUsername := pCtx.DataSourceInstanceSettings.DecryptedSecureJSONData["username"]
password, hasPassword := pCtx.DataSourceInstanceSettings.DecryptedSecureJSONData["password"]

if hasUsername {
if hasPassword {
if hasUsername && username != "" {
if hasPassword && password != "" {
mongoURL.User = url.UserPassword(username, password)
} else {
mongoURL.User = url.User(username)
}
}

credential := options.Credential{
Username: username,
Password: password,
credential := options.Credential{
Username: username,
Password: password,
}
opts = opts.SetAuth(credential)
}

mongoClient, err := mongo.Connect(ctx, mongoOpts.Client().ApplyURI(mongoURL.String()).SetAuth(credential))
mongoClient, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, "Error while connecting to MongoDB: ", err, nil
}
Expand Down

0 comments on commit 914cef1

Please sign in to comment.