| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package confmaptest // import "go.opentelemetry.io/collector/confmap/confmaptest" |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"regexp" |
| 11 |
|
| 12 |
yaml "go.yaml.in/yaml/v3" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/confmap" |
| 15 |
) |
| 16 |
|
| 17 |
// LoadConf loads a confmap.Conf from file, and does NOT validate the configuration. |
| 18 |
func LoadConf(fileName string) (*confmap.Conf, error) { |
| 19 |
// Clean the path before using it. |
| 20 |
content, err := os.ReadFile(filepath.Clean(fileName)) |
| 21 |
if err != nil { |
| 22 |
return nil, fmt.Errorf("unable to read the file %v: %w", fileName, err) |
| 23 |
} |
| 24 |
|
| 25 |
var rawConf map[string]any |
| 26 |
if err := yaml.Unmarshal(content, &rawConf); err != nil { |
| 27 |
return nil, err |
| 28 |
} |
| 29 |
|
| 30 |
return confmap.NewFromStringMap(rawConf), nil |
| 31 |
} |
| 32 |
|
| 33 |
var schemeValidator = regexp.MustCompile("^[A-Za-z][A-Za-z0-9+.-]+$") |
| 34 |
|
| 35 |
// ValidateProviderScheme enforces that given confmap.Provider.Scheme() object is following the restriction defined by the collector: |
| 36 |
// - Checks that the scheme name follows the restrictions defined https://datatracker.ietf.org/doc/html/rfc3986#section-3.1 |
| 37 |
// - Checks that the scheme name has at leas two characters per the confmap.Provider.Scheme() comment. |
| 38 |
func ValidateProviderScheme(p confmap.Provider) error { |
| 39 |
scheme := p.Scheme() |
| 40 |
if len(scheme) < 2 { |
| 41 |
return fmt.Errorf("scheme must be at least 2 characters long: %q", scheme) |
| 42 |
} |
| 43 |
|
| 44 |
if !schemeValidator.MatchString(scheme) { |
| 45 |
return fmt.Errorf("scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, \"+\", \".\", or \"-\": %q", scheme) |
| 46 |
} |
| 47 |
|
| 48 |
return nil |
| 49 |
} |