Debian Codegraph @

exporters/otlp/otlpmetric/otlpmetrichttp/internal/envconfig/envconfig.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/envconfig/envconfig.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 // Package envconfig provides functionality to parse configuration from
8 // environment variables.
9 package envconfig // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/envconfig"
10
11 import (
12 "crypto/tls"
13 "crypto/x509"
14 "errors"
15 "fmt"
16 "net/url"
17 "strconv"
18 "strings"
19 "time"
20 "unicode"
21
22 "go.opentelemetry.io/otel/internal/global"
23 )
24
25 // ConfigFn is the generic function used to set a config.
26 type ConfigFn func(*EnvOptionsReader)
27
28 // EnvOptionsReader reads the required environment variables.
29 type EnvOptionsReader struct {
30 GetEnv func(string) string
31 ReadFile func(string) ([]byte, error)
32 Namespace string
33 }
34
35 // Apply runs every ConfigFn.
36 func (e *EnvOptionsReader) Apply(opts ...ConfigFn) {
37 for _, o := range opts {
38 o(e)
39 }
40 }
41
42 // GetEnvValue gets an OTLP environment variable value of the specified key
43 // using the GetEnv function.
44 // This function prepends the OTLP specified namespace to all key lookups.
45 func (e *EnvOptionsReader) GetEnvValue(key string) (string, bool) {
46 v := strings.TrimSpace(e.GetEnv(keyWithNamespace(e.Namespace, key)))
47 return v, v != ""
48 }
49
50 // WithString retrieves the specified config and passes it to ConfigFn as a string.
51 func WithString(n string, fn func(string)) func(e *EnvOptionsReader) {
52 return func(e *EnvOptionsReader) {
53 if v, ok := e.GetEnvValue(n); ok {
54 fn(v)
55 }
56 }
57 }
58
59 // WithBool returns a ConfigFn that reads the environment variable n and if it exists passes its parsed bool value to fn.
60 func WithBool(n string, fn func(bool)) ConfigFn {
61 return func(e *EnvOptionsReader) {
62 if v, ok := e.GetEnvValue(n); ok {
63 b := strings.ToLower(v) == "true"
64 fn(b)
65 }
66 }
67 }
68
69 // WithDuration retrieves the specified config and passes it to ConfigFn as a duration.
70 func WithDuration(n string, fn func(time.Duration)) func(e *EnvOptionsReader) {
71 return func(e *EnvOptionsReader) {
72 if v, ok := e.GetEnvValue(n); ok {
73 d, err := strconv.Atoi(v)
74 if err != nil {
75 global.Error(err, "parse duration", "input", v)
76 return
77 }
78 fn(time.Duration(d) * time.Millisecond)
79 }
80 }
81 }
82
83 // WithHeaders retrieves the specified config and passes it to ConfigFn as a map of HTTP headers.
84 func WithHeaders(n string, fn func(map[string]string)) func(e *EnvOptionsReader) {
85 return func(e *EnvOptionsReader) {
86 if v, ok := e.GetEnvValue(n); ok {
87 fn(stringToHeader(v))
88 }
89 }
90 }
91
92 // WithURL retrieves the specified config and passes it to ConfigFn as a net/url.URL.
93 func WithURL(n string, fn func(*url.URL)) func(e *EnvOptionsReader) {
94 return func(e *EnvOptionsReader) {
95 if v, ok := e.GetEnvValue(n); ok {
96 u, err := url.Parse(v)
97 if err != nil {
98 global.Error(err, "parse url", "input", v)
99 return
100 }
101 fn(u)
102 }
103 }
104 }
105
106 // WithCertPool returns a ConfigFn that reads the environment variable n as a filepath to a TLS certificate pool. If it exists, it is parsed as a crypto/x509.CertPool and it is passed to fn.
107 func WithCertPool(n string, fn func(*x509.CertPool)) ConfigFn {
108 return func(e *EnvOptionsReader) {
109 if v, ok := e.GetEnvValue(n); ok {
110 b, err := e.ReadFile(v)
111 if err != nil {
112 global.Error(err, "read tls ca cert file", "file", v)
113 return
114 }
115 c, err := createCertPool(b)
116 if err != nil {
117 global.Error(err, "create tls cert pool")
118 return
119 }
120 fn(c)
121 }
122 }
123 }
124
125 // WithClientCert returns a ConfigFn that reads the environment variable nc and nk as filepaths to a client certificate and key pair. If they exists, they are parsed as a crypto/tls.Certificate and it is passed to fn.
126 func WithClientCert(nc, nk string, fn func(tls.Certificate)) ConfigFn {
127 return func(e *EnvOptionsReader) {
128 vc, okc := e.GetEnvValue(nc)
129 vk, okk := e.GetEnvValue(nk)
130 if !okc || !okk {
131 return
132 }
133 cert, err := e.ReadFile(vc)
134 if err != nil {
135 global.Error(err, "read tls client cert", "file", vc)
136 return
137 }
138 key, err := e.ReadFile(vk)
139 if err != nil {
140 global.Error(err, "read tls client key", "file", vk)
141 return
142 }
143 crt, err := tls.X509KeyPair(cert, key)
144 if err != nil {
145 global.Error(err, "create tls client key pair")
146 return
147 }
148 fn(crt)
149 }
150 }
151
152 func keyWithNamespace(ns, key string) string {
153 if ns == "" {
154 return key
155 }
156 return fmt.Sprintf("%s_%s", ns, key)
157 }
158
159 func stringToHeader(value string) map[string]string {
160 headersPairs := strings.Split(value, ",")
161 headers := make(map[string]string)
162
163 for _, header := range headersPairs {
164 n, v, found := strings.Cut(header, "=")
165 if !found {
166 global.Error(errors.New("missing '="), "parse headers", "input", header)
167 continue
168 }
169
170 trimmedName := strings.TrimSpace(n)
171
172 // Validate the key.
173 if !isValidHeaderKey(trimmedName) {
174 global.Error(errors.New("invalid header key"), "parse headers", "key", trimmedName)
175 continue
176 }
177
178 // Only decode the value.
179 value, err := url.PathUnescape(v)
180 if err != nil {
181 global.Error(err, "escape header value", "value", v)
182 continue
183 }
184 trimmedValue := strings.TrimSpace(value)
185
186 headers[trimmedName] = trimmedValue
187 }
188
189 return headers
190 }
191
192 func createCertPool(certBytes []byte) (*x509.CertPool, error) {
193 cp := x509.NewCertPool()
194 if ok := cp.AppendCertsFromPEM(certBytes); !ok {
195 return nil, errors.New("failed to append certificate to the cert pool")
196 }
197 return cp, nil
198 }
199
200 func isValidHeaderKey(key string) bool {
201 if key == "" {
202 return false
203 }
204 for _, c := range key {
205 if !isTokenChar(c) {
206 return false
207 }
208 }
209 return true
210 }
211
212 func isTokenChar(c rune) bool {
213 return c <= unicode.MaxASCII && (unicode.IsLetter(c) ||
214 unicode.IsDigit(c) ||
215 c == '!' || c == '#' || c == '$' || c == '%' || c == '&' || c == '\'' || c == '*' ||
216 c == '+' || c == '-' || c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~')
217 }