|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package e2etest |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "go.opentelemetry.io/collector/config/configopaque" |
| 12 | + "go.opentelemetry.io/collector/confmap" |
| 13 | + "go.opentelemetry.io/collector/confmap/internal" |
| 14 | + "go.opentelemetry.io/collector/featuregate" |
| 15 | +) |
| 16 | + |
| 17 | +type testHeadersConfig struct { |
| 18 | + Headers configopaque.MapList `mapstructure:"headers"` |
| 19 | +} |
| 20 | + |
| 21 | +// TestMapListWithExpandedValue tests that MapList can handle ExpandedValue |
| 22 | +// from environment variable expansion |
| 23 | +func TestMapListWithExpandedValue(t *testing.T) { |
| 24 | + // Simulate what happens when ${env:TOKEN} is expanded |
| 25 | + // The confmap will contain an ExpandedValue instead of a plain string |
| 26 | + data := map[string]any{ |
| 27 | + "headers": []any{ |
| 28 | + map[string]any{ |
| 29 | + "name": "Authorization", |
| 30 | + "value": internal.ExpandedValue{ |
| 31 | + Value: "Bearer secret-token", |
| 32 | + Original: "Bearer secret-token", |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + conf := confmap.NewFromStringMap(data) |
| 39 | + var tc testHeadersConfig |
| 40 | + err := conf.Unmarshal(&tc) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + val, ok := tc.Headers.Get("Authorization") |
| 44 | + require.True(t, ok) |
| 45 | + require.Equal(t, configopaque.String("Bearer secret-token"), val) |
| 46 | +} |
| 47 | + |
| 48 | +// TestMapListWithExpandedValueIntValue tests an ExpandedValue with an integer Value |
| 49 | +func TestMapListWithExpandedValueIntValue(t *testing.T) { |
| 50 | + // Simulate what happens when expanding a value that parses as an int |
| 51 | + data := map[string]any{ |
| 52 | + "headers": []any{ |
| 53 | + map[string]any{ |
| 54 | + "name": "X-Port", |
| 55 | + "value": internal.ExpandedValue{ |
| 56 | + Value: 8080, // Value is parsed as int |
| 57 | + Original: "8080", // Original is string |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + originalState := internal.NewExpandedValueSanitizer.IsEnabled() |
| 64 | + defer func() { |
| 65 | + require.NoError(t, featuregate.GlobalRegistry().Set(internal.NewExpandedValueSanitizer.ID(), originalState)) |
| 66 | + }() |
| 67 | + |
| 68 | + require.NoError(t, featuregate.GlobalRegistry().Set(internal.NewExpandedValueSanitizer.ID(), true)) |
| 69 | + |
| 70 | + conf := confmap.NewFromStringMap(data) |
| 71 | + var tc testHeadersConfig |
| 72 | + err := conf.Unmarshal(&tc) |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + val, ok := tc.Headers.Get("X-Port") |
| 76 | + require.True(t, ok) |
| 77 | + require.Equal(t, configopaque.String("8080"), val) |
| 78 | + |
| 79 | + require.NoError(t, featuregate.GlobalRegistry().Set(internal.NewExpandedValueSanitizer.ID(), false)) |
| 80 | + |
| 81 | + // This will fail because when reverting to old behavior, ExpandedValues get decoded at collection time and doesn't |
| 82 | + // take struct collections into account. |
| 83 | + err = conf.Unmarshal(&tc) |
| 84 | + require.Error(t, err) |
| 85 | +} |
| 86 | + |
| 87 | +// TestDirectConfigopaqueStringWithExpandedValueIntValue tests that direct unmarshaling works |
| 88 | +func TestDirectConfigopaqueStringWithExpandedValueIntValue(t *testing.T) { |
| 89 | + type testConfig struct { |
| 90 | + Value configopaque.String `mapstructure:"value"` |
| 91 | + } |
| 92 | + |
| 93 | + // Direct configopaque.String field (not in a map/slice structure) |
| 94 | + data := map[string]any{ |
| 95 | + "value": internal.ExpandedValue{ |
| 96 | + Value: 8080, |
| 97 | + Original: "8080", |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + conf := confmap.NewFromStringMap(data) |
| 102 | + var tc testConfig |
| 103 | + err := conf.Unmarshal(&tc) |
| 104 | + // This should work because useExpandValue detects the target is a string |
| 105 | + require.NoError(t, err) |
| 106 | + require.Equal(t, configopaque.String("8080"), tc.Value) |
| 107 | +} |
| 108 | + |
| 109 | +// TestStringyStructureWithExpandedValue tests the isStringyStructure path in useExpandValue |
| 110 | +func TestStringyStructureWithExpandedValue(t *testing.T) { |
| 111 | + type testConfig struct { |
| 112 | + Tags []string `mapstructure:"tags"` |
| 113 | + } |
| 114 | + |
| 115 | + data := map[string]any{ |
| 116 | + "tags": []any{ |
| 117 | + internal.ExpandedValue{ |
| 118 | + Value: 8080, |
| 119 | + Original: "8080", |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + originalState := internal.NewExpandedValueSanitizer.IsEnabled() |
| 125 | + defer func() { |
| 126 | + require.NoError(t, featuregate.GlobalRegistry().Set(internal.NewExpandedValueSanitizer.ID(), originalState)) |
| 127 | + }() |
| 128 | + |
| 129 | + // With feature gate disabled, useExpandValue should detect []string as stringy |
| 130 | + require.NoError(t, featuregate.GlobalRegistry().Set(internal.NewExpandedValueSanitizer.ID(), false)) |
| 131 | + |
| 132 | + conf := confmap.NewFromStringMap(data) |
| 133 | + var tc testConfig |
| 134 | + err := conf.Unmarshal(&tc) |
| 135 | + require.NoError(t, err) |
| 136 | + require.Equal(t, []string{"8080"}, tc.Tags) |
| 137 | +} |
0 commit comments