func (config *Config) AddSecret(tenants string, pw []byte) ([]byte, error) {
	var err error
	secret, err := config.GetSecretMap()
	if err != nil {
		return nil, errors.Wrap(err, "AddSecret(GetSecretMap)")
	}
	if _, ok := secret.Name["secretkey"]; !ok {
		secret.Name = make(map[string][]byte)
		secret.Name["secretkey"], err = GetSecretKey()
		if err != nil {
			return nil, errors.Wrap(err, "AddSecret(GetSecretKey)")
		}
	}
	encPw, err := PwEncrypt(pw, secret.Name["secretkey"])
	if err != nil {
		return nil, errors.Wrap(err, "AddSecret(PwEncrypt)")
	}
	for _, tenant := range strings.Split(tenants, ",") {
		tInfo := config.FindTenant(low(tenant))
		if "" == tInfo.Name {
			log.WithFields(log.Fields{
				"tenant": low(tenant),
			}).Error("missing tenant")
			return nil, errors.New("Did not find tenant in configfile tenants slice.")
		}
		secret.Name[low(tenant)] = encPw
	}
	newSecret, err := proto.Marshal(&secret)
	if err != nil {
		return nil, errors.Wrap(err, "AddSecret(Marshal)")
	}
	return newSecret, nil
}
AddSecret - create encrypted secret for tenant(s)
package cmd
import (
	crypt "crypto/rand"
	"fmt"
	"io"
	"math/rand"
	"strings"
	"syscall"
	"time"
	"github.com/golang/protobuf/proto"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/ulranh/hana_sql_exporter/internal"
	"golang.org/x/crypto/nacl/secretbox"
	"golang.org/x/crypto/ssh/terminal"
)
var pwCmd = &cobra.Command{
	Use:   "pw",
	Short: "Set passwords for the tenants in the config file",
	Long: `With the command pw you can set the passwords for the tenants you want to monitor. You can set the password for one tenant or several tenants separated by comma. For example:
	hana_sql_exporter pw --tenant d01
	hana_sql_exporter pw -t d01,d02 --config ./.hana_sql_exporter.toml`,
	Run: func(cmd *cobra.Command, args []string) {
		config, err := getConfig()
		if err != nil {
			exit("Can't handle config file: ", err)
		}
		config.Timeout = 5
		err = config.SetPw(cmd)
		if err != nil {
			exit("Can't set password: ", err)
		}
	},
}
func init() {
	RootCmd.AddCommand(pwCmd)
	pwCmd.PersistentFlags().StringP("tenant", "t", "", "name(s) of tenant(s) separated by comma")
	pwCmd.MarkPersistentFlagRequired("tenant")
}
func (config *Config) SetPw(cmd *cobra.Command) error {
	fmt.Print("Password: ")
	pw, err := terminal.ReadPassword(int(syscall.Stdin))
	if err != nil {
		return errors.Wrap(err, "setPw(ReadPassword)")
	}
	tenants, err := cmd.Flags().GetString("tenant")
	if err != nil {
		return errors.Wrap(err, "setPw(GetString)")
	}
	config.Secret, err = config.AddSecret(tenants, pw)
	if err != nil {
		return errors.Wrap(err, "setPw(newSecret)")
	}
	viper.Set("secret", config.Secret)
	err = viper.WriteConfig()
	if err != nil {
		return errors.Wrap(err, "setPw(WriteConfig)")
	}
	secretMap, err := config.GetSecretMap()
	if err != nil {
		return errors.Wrap(err, "prepare(getSecretMap)")
	}
	for i := range config.Tenants {
		db := config.getConnection(i, secretMap)
		if db == nil {
			continue
		}
		db.Close()
	}
	return nil
}