2015-05-30 22:28:33 +03:00
|
|
|
// Copyright © 2015 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package remote integrates the remote features of Viper.
|
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-11-14 09:09:00 +03:00
|
|
|
"strings"
|
2017-12-07 07:26:31 +03:00
|
|
|
|
2021-09-15 21:44:04 +03:00
|
|
|
crypt "github.com/sagikazarmark/crypt/config"
|
2020-09-11 18:48:38 +03:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2015-05-30 22:28:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type remoteConfigProvider struct{}
|
|
|
|
|
|
|
|
func (rc remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
|
|
|
|
cm, err := getConfigManager(rp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, err := cm.Get(rp.Path())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewReader(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
|
|
|
|
cm, err := getConfigManager(rp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-23 08:47:47 +03:00
|
|
|
resp, err := cm.Get(rp.Path())
|
2015-05-30 22:28:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-15 16:43:09 +03:00
|
|
|
return bytes.NewReader(resp), nil
|
2015-05-30 22:28:33 +03:00
|
|
|
}
|
2017-07-23 08:47:47 +03:00
|
|
|
|
2023-12-01 22:51:58 +03:00
|
|
|
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
2017-03-15 16:43:09 +03:00
|
|
|
cm, err := getConfigManager(rp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
quit := make(chan bool)
|
|
|
|
quitwc := make(chan bool)
|
2017-07-23 08:47:47 +03:00
|
|
|
viperResponsCh := make(chan *viper.RemoteResponse)
|
2017-03-15 16:43:09 +03:00
|
|
|
cryptoResponseCh := cm.Watch(rp.Path(), quit)
|
|
|
|
// need this function to convert the Channel response form crypt.Response to viper.Response
|
2017-07-23 08:47:47 +03:00
|
|
|
go func(cr <-chan *crypt.Response, vr chan<- *viper.RemoteResponse, quitwc <-chan bool, quit chan<- bool) {
|
2017-03-15 16:43:09 +03:00
|
|
|
for {
|
|
|
|
select {
|
2017-07-23 08:47:47 +03:00
|
|
|
case <-quitwc:
|
2017-03-15 16:43:09 +03:00
|
|
|
quit <- true
|
|
|
|
return
|
|
|
|
case resp := <-cr:
|
|
|
|
vr <- &viper.RemoteResponse{
|
|
|
|
Error: resp.Error,
|
|
|
|
Value: resp.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-23 08:47:47 +03:00
|
|
|
}(cryptoResponseCh, viperResponsCh, quitwc, quit)
|
2017-03-15 16:43:09 +03:00
|
|
|
|
2017-07-23 08:47:47 +03:00
|
|
|
return viperResponsCh, quitwc
|
2017-03-15 16:43:09 +03:00
|
|
|
}
|
|
|
|
|
2015-05-30 22:28:33 +03:00
|
|
|
func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) {
|
|
|
|
var cm crypt.ConfigManager
|
|
|
|
var err error
|
|
|
|
|
2022-11-14 09:09:00 +03:00
|
|
|
endpoints := strings.Split(rp.Endpoint(), ";")
|
2015-05-30 22:28:33 +03:00
|
|
|
if rp.SecretKeyring() != "" {
|
2019-09-18 18:47:54 +03:00
|
|
|
var kr *os.File
|
|
|
|
kr, err = os.Open(rp.SecretKeyring())
|
2015-05-30 22:28:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-18 18:47:54 +03:00
|
|
|
defer kr.Close()
|
2020-01-16 19:20:46 +03:00
|
|
|
switch rp.Provider() {
|
|
|
|
case "etcd":
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewEtcdConfigManager(endpoints, kr)
|
2022-05-26 17:52:44 +03:00
|
|
|
case "etcd3":
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewEtcdV3ConfigManager(endpoints, kr)
|
2020-01-16 19:20:46 +03:00
|
|
|
case "firestore":
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewFirestoreConfigManager(endpoints, kr)
|
2023-07-21 22:10:15 +03:00
|
|
|
case "nats":
|
|
|
|
cm, err = crypt.NewNatsConfigManager(endpoints, kr)
|
2020-01-16 19:20:46 +03:00
|
|
|
default:
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewConsulConfigManager(endpoints, kr)
|
2015-05-30 22:28:33 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-16 19:20:46 +03:00
|
|
|
switch rp.Provider() {
|
|
|
|
case "etcd":
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewStandardEtcdConfigManager(endpoints)
|
2022-05-26 17:52:44 +03:00
|
|
|
case "etcd3":
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewStandardEtcdV3ConfigManager(endpoints)
|
2020-01-16 19:20:46 +03:00
|
|
|
case "firestore":
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewStandardFirestoreConfigManager(endpoints)
|
2023-07-21 22:10:15 +03:00
|
|
|
case "nats":
|
|
|
|
cm, err = crypt.NewStandardNatsConfigManager(endpoints)
|
2020-01-16 19:20:46 +03:00
|
|
|
default:
|
2022-11-14 09:09:00 +03:00
|
|
|
cm, err = crypt.NewStandardConsulConfigManager(endpoints)
|
2015-05-30 22:28:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
viper.RemoteConfig = &remoteConfigProvider{}
|
|
|
|
}
|