add mongodb authentication

This commit is contained in:
Joel Schmid 2021-04-02 12:41:22 +02:00
parent 73a57482c8
commit 5bb48d6ddc
4 changed files with 19 additions and 3 deletions

View file

@ -26,6 +26,8 @@ const mqttMinDistToLastValue = 250 * time.Millisecond
const mongodbURL = "mongodb://default-address.com:27017"
const mongodbName = "weathersensors"
const mongodbCollection = "sensordata"
const mongodbUser = "mongoUser"
const mongdbPassword = "mongoPassword"
//other config stuff
const allowUnregisteredSensors = false
@ -97,6 +99,14 @@ func GetMongodbCollection() string {
return getVariableWithDefault("WEATHER-API-MONGODB_COLLECTION", mongodbCollection)
}
func GetMongodbUserName() string {
return getVariableWithDefault("WEATHER-API-MONGODB_USER", mongodbUser)
}
func GetMongodbPassword() string {
return getVariableWithDefault("WEATHER-API-MONGODB_PASSWORD", mongdbPassword)
}
//common config
func AllowUnregisteredSensors() bool {
return getVariableWithDefaultBool("WEATHER-API-ALLOW_UNREGISTERED_SENSORS", allowUnregisteredSensors)

View file

@ -21,7 +21,9 @@ func main() {
sensorRegistry, err = storage.NewMongodbSensorRegistry(
config.GetMongodbURL(),
config.GetMongodbName(),
config.GetMongodbCollection())
config.GetMongodbCollection(),
config.GetMongodbUserName(),
config.GetMongodbPassword())
if err != nil {
os.Exit(1)

View file

@ -17,6 +17,8 @@ Set-Item -Path "Env:WEATHER-API-MQTT_MIN_DIST_TO_LAST_VALUE" -Value "250"
Set-Item -Path "Env:WEATHER-API-MONGODB_URL" -Value "mongodb://default-address.com:27017"
Set-Item -Path "Env:WEATHER-API-MONGODB_NAME" -Value "weathersensors"
Set-Item -Path "Env:WEATHER-API-MONGODB_COLLECTION" -Value "sensordata"
Set-Item -Path "Env:WEATHER-API-MONGODB_USER" -Value "mongoUser"
Set-Item -Path "Env:WEATHER-API-MONGODB_PASSWORD" -Value "mongoPassword"
Set-Item -Path "Env:WEATHER-API-ANONYMOUS_MQTT_AUTHENTICATION" -Value "false"
Set-Item -Path "Env:WEATHER-API-ALLOW_UNREGISTERED_SENSORS" -Value "true"

View file

@ -19,10 +19,12 @@ type mongodbSensorRegistry struct {
client *mongo.Client
}
func NewMongodbSensorRegistry(connection, database, collection string) (*mongodbSensorRegistry, error) {
func NewMongodbSensorRegistry(connection, database, collection, user, password string) (*mongodbSensorRegistry, error) {
sensorRegistry := new(mongodbSensorRegistry)
client, err := mongo.NewClient(options.Client().ApplyURI(connection))
options := options.Client().ApplyURI(connection).SetAuth(options.Credential{Username: user, Password: password})
client, err := mongo.NewClient(options)
if err != nil {
return nil, err
}