| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package configurablehttpprovider // import "go.opentelemetry.io/collector/confmap/provider/internal/configurablehttpprovider" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"crypto/tls" |
| 9 |
"crypto/x509" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"net/http" |
| 13 |
"net/url" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/confmap" |
| 19 |
) |
| 20 |
|
| 21 |
type SchemeType string |
| 22 |
|
| 23 |
const ( |
| 24 |
HTTPScheme SchemeType = "http" |
| 25 |
HTTPSScheme SchemeType = "https" |
| 26 |
) |
| 27 |
|
| 28 |
type provider struct { |
| 29 |
scheme SchemeType |
| 30 |
caCertPath string // Used for tests |
| 31 |
insecureSkipVerify bool // Used for tests |
| 32 |
} |
| 33 |
|
| 34 |
// New returns a new provider that reads the configuration from http server using the configured transport mechanism |
| 35 |
// depending on the selected scheme. |
| 36 |
// There are two types of transport supported: PlainText (HTTPScheme) and TLS (HTTPSScheme). |
| 37 |
// |
| 38 |
// One example for http-uri: http://localhost:3333/getConfig |
| 39 |
// One example for https-uri: https://localhost:3333/getConfig |
| 40 |
// This is used by the http and https external implementations. |
| 41 |
func New(scheme SchemeType, _ confmap.ProviderSettings) confmap.Provider { |
| 42 |
return &provider{scheme: scheme} |
| 43 |
} |
| 44 |
|
| 45 |
// Create the client based on the type of scheme that was selected. |
| 46 |
func (fmp *provider) createClient() (*http.Client, error) { |
| 47 |
switch fmp.scheme { |
| 48 |
case HTTPScheme: |
| 49 |
return &http.Client{}, nil |
| 50 |
case HTTPSScheme: |
| 51 |
pool, err := x509.SystemCertPool() |
| 52 |
if err != nil { |
| 53 |
return nil, fmt.Errorf("unable to create a cert pool: %w", err) |
| 54 |
} |
| 55 |
|
| 56 |
if fmp.caCertPath != "" { |
| 57 |
cert, err := os.ReadFile(filepath.Clean(fmp.caCertPath)) |
| 58 |
if err != nil { |
| 59 |
return nil, fmt.Errorf("unable to read CA from %q URI: %w", fmp.caCertPath, err) |
| 60 |
} |
| 61 |
|
| 62 |
if ok := pool.AppendCertsFromPEM(cert); !ok { |
| 63 |
return nil, fmt.Errorf("unable to add CA from uri: %s into the cert pool", fmp.caCertPath) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return &http.Client{ |
| 68 |
Transport: &http.Transport{ |
| 69 |
TLSClientConfig: &tls.Config{ |
| 70 |
InsecureSkipVerify: fmp.insecureSkipVerify, |
| 71 |
RootCAs: pool, |
| 72 |
}, |
| 73 |
}, |
| 74 |
}, nil |
| 75 |
default: |
| 76 |
return nil, fmt.Errorf("invalid scheme type: %s", fmp.scheme) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func (fmp *provider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 81 |
if !strings.HasPrefix(uri, string(fmp.scheme)+":") { |
| 82 |
return nil, fmt.Errorf("%q uri is not supported by %q provider", uri, string(fmp.scheme)) |
| 83 |
} |
| 84 |
|
| 85 |
if _, err := url.ParseRequestURI(uri); err != nil { |
| 86 |
return nil, fmt.Errorf("invalid uri %q: %w", uri, err) |
| 87 |
} |
| 88 |
|
| 89 |
client, err := fmp.createClient() |
| 90 |
if err != nil { |
| 91 |
return nil, fmt.Errorf("unable to configure http transport layer: %w", err) |
| 92 |
} |
| 93 |
|
| 94 |
// send a HTTP GET request |
| 95 |
resp, err := client.Get(uri) |
| 96 |
if err != nil { |
| 97 |
return nil, fmt.Errorf("unable to download the file via HTTP GET for uri %q: %w ", uri, err) |
| 98 |
} |
| 99 |
defer resp.Body.Close() |
| 100 |
|
| 101 |
// check the HTTP status code |
| 102 |
if resp.StatusCode != http.StatusOK { |
| 103 |
return nil, fmt.Errorf("failed to load resource from uri %q. status code: %d", uri, resp.StatusCode) |
| 104 |
} |
| 105 |
|
| 106 |
// read the response body |
| 107 |
body, err := io.ReadAll(resp.Body) |
| 108 |
if err != nil { |
| 109 |
return nil, fmt.Errorf("fail to read the response body from uri %q: %w", uri, err) |
| 110 |
} |
| 111 |
|
| 112 |
return confmap.NewRetrievedFromYAML(body) |
| 113 |
} |
| 114 |
|
| 115 |
func (fmp *provider) Scheme() string { |
| 116 |
return string(fmp.scheme) |
| 117 |
} |
| 118 |
|
| 119 |
func (*provider) Shutdown(context.Context) error { |
| 120 |
return nil |
| 121 |
} |