func getValueFromBase64EnvVar(envVar string) ([]byte, error) {
	b64Value, ok := os.LookupEnv(envVar)
	if !ok {
		return nil, fmt.Errorf("environment variable %s not set", envVar)
	}
	return getValueFromBase64(b64Value)
}
Get decoded base64 value from environment variable
package main
import (
	"bytes"
	"crypto/subtle"
	"crypto/tls"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"regexp"
	"strconv"
	"strings"
	"unicode/utf8"
	"github.com/gobwas/glob"
	"github.com/prometheus/client_golang/prometheus"
	goversion "github.com/hashicorp/go-version"
)
func makeMetric(namespace, name string, t metricType, constLabels map[string]string, labels ...string) promMetric {
	promDesc := prometheus.NewDesc(
		namespace+"_"+normalizeMetric(name),
		normalizeDesc(name),
		labels,
		constLabels,
	)
	switch t {
	case mtCounter:
		return promMetric{
			origDesc:  name,
			desc:      promDesc,
			valueType: prometheus.CounterValue,
		}
	case mtGauge:
		return promMetric{
			origDesc:  name,
			desc:      promDesc,
			valueType: prometheus.GaugeValue,
		}
	}
	panic("Should not reach here...")
}
var descReplacerFunc = strings.NewReplacer("_", " ", "-", " ", ".", " ")
func normalizeDesc(s string) string {
	return descReplacerFunc.Replace(s)
}
var metricReplacerFunc = strings.NewReplacer(".", "_", "-", "_", " ", "_")
func normalizeMetric(s string) string {
	return metricReplacerFunc.Replace(s)
}
func parseStats(s, sep string) map[string]string {
	stats := make(map[string]string, strings.Count(s, sep)+1)
	s2 := strings.Split(s, sep)
	for _, s := range s2 {
		list := strings.SplitN(s, "=", 2)
		switch len(list) {
		case 0, 1:
		case 2:
			stats[list[0]] = list[1]
		default:
			stats[list[0]] = strings.Join(list[1:], "=")
		}
	}
	return stats
}
func tryConvert(s string) (float64, error) {
	if f, err := strconv.ParseFloat(s, 64); err == nil {
		return f, nil
	}
	if b, err := strconv.ParseBool(s); err == nil {
		if b {
			return 1, nil
		}
		return 0, nil
	}
	return 0, fmt.Errorf("invalid value `%s`. Only Float or Boolean are accepted", s)
}
func validateBasicAuth(r *http.Request, username string, password string) bool {
	user, pass, ok := r.BasicAuth()
	if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
		return false
	}
	return true
}
var globbingPattern = regexp.MustCompile(`\[|\]|\*|\?|\{|\}|\\|!`)
func getFilteredMetrics(rawMetrics map[string]metricType, allowlist []string, allowlistEnabled bool, blocklist []string, blocklistEnabled bool) map[string]metricType {
	filteredMetrics := filterAllowedMetrics(rawMetrics, allowlist, allowlistEnabled)
	filterBlockedMetrics(filteredMetrics, blocklist, blocklistEnabled)
	return filteredMetrics
}
func filterAllowedMetrics(rawMetrics map[string]metricType, allowlist []string, allowlistEnabled bool) map[string]metricType {
	if !allowlistEnabled {
		return rawMetrics
	}
	filteredMetrics := make(map[string]metricType)
	for _, stat := range allowlist {
		if globbingPattern.MatchString(stat) {
			ge := glob.MustCompile(stat)
			for k, v := range rawMetrics {
				if ge.Match(k) {
					filteredMetrics[k] = v
				}
			}
		} else {
			if val, ok := rawMetrics[stat]; ok {
				filteredMetrics[stat] = val
			}
		}
	}
	return filteredMetrics
}
func filterBlockedMetrics(filteredMetrics map[string]metricType, blocklist []string, blocklistEnabled bool) {
	if !blocklistEnabled {
		return
	}
	for _, stat := range blocklist {
		if globbingPattern.MatchString(stat) {
			ge := glob.MustCompile(stat)
			for k := range filteredMetrics {
				if ge.Match(k) {
					delete(filteredMetrics, k)
				}
			}
		} else {
			delete(filteredMetrics, stat)
		}
	}
}
func getSecret(secretConfig string) ([]byte, error) {
	secretSource := strings.SplitN(secretConfig, ":", 2)
	if len(secretSource) == 2 {
		switch secretSource[0] {
		case "file":
			return readFromFile(secretSource[1])
		case "env":
			secret, ok := os.LookupEnv(secretSource[1])
			if !ok {
				return nil, fmt.Errorf("environment variable %s not set", secretSource[1])
			}
			return []byte(secret), nil
		case "env-b64":
			return getValueFromBase64EnvVar(secretSource[1])
		case "b64":
			return getValueFromBase64(secretSource[1])
		default:
			return nil, fmt.Errorf("invalid source: %s", secretSource[0])
		}
	}
	return []byte(secretConfig), nil
}
func getCertificate(certConfig string) ([]byte, error) {
	certificateSource := strings.SplitN(certConfig, ":", 2)
	if len(certificateSource) == 2 {
		switch certificateSource[0] {
		case "file":
			return readFromFile(certificateSource[1])
		case "env-b64":
			return getValueFromBase64EnvVar(certificateSource[1])
		case "b64":
			return getValueFromBase64(certificateSource[1])
		default:
			return nil, fmt.Errorf("invalid source %s", certificateSource[0])
		}
	}
	return readFromFile(certConfig)
}
func readFromFile(filePath string) ([]byte, error) {
	dataBytes, err := ioutil.ReadFile(filePath)
	if err != nil {
		return nil, fmt.Errorf("failed to read from file `%s`: `%v`", filePath, err)
	}
	data := bytes.TrimSuffix(dataBytes, []byte("\n"))
	return data, nil
}