Skip to content

Commit 3cfc0c1

Browse files
authored
[exporter/elasticsearch] Fix CloudID parsing to correctly handle Elastic Cloud IDs when sent with multiple dollar sign separators (#44325)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description The CloudID decoder was incorrectly using `strings.Cut()`, which only splits at the first delimiter, resulting in malformed URLs when the decoded CloudID contained multiple `$` separators. Changed to use `strings.Split()` to match the reference implementation from the [go-elasticsearch](https://github.com/elastic/go-elasticsearch/blob/4739f334675880df102135bcca6104c8c63b59db/elasticsearch.go#L474) library. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #44306 <!--Describe what testing was performed and which tests were added.--> #### Testing Added tests to validate this behaviour and the expected output. Signed-off-by: Paulo Dias <[email protected]>
1 parent d92c216 commit 3cfc0c1

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

.chloggen/fix_44306.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: "bug_fix"
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
7+
component: exporter/elasticsearch
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix CloudID parsing to correctly handle Elastic Cloud IDs when sent with multiple dollar sign separators
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [44306]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
The CloudID decoder was incorrectly using `strings.Cut()` which only splits on the first delimiter,
20+
causing malformed URLs when the decoded CloudID contained multiple `$` separators. Changed to use
21+
`strings.Split()` to match the reference implementation from go-elasticsearch library.
22+
23+
# If your change doesn't affect end users or the exported elements of any package,
24+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
25+
# Optional: The change log or logs in which this entry should be included.
26+
# e.g. '[user]' or '[user, api]'
27+
# Include 'user' if the change is relevant to end users.
28+
# Include 'api' if there is a change to a library API.
29+
# Default: '[user]'
30+
change_logs: [user]

exporter/elasticsearchexporter/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,11 @@ func parseCloudID(input string) (*url.URL, error) {
483483
return nil, err
484484
}
485485

486-
before, after, ok := strings.Cut(string(decoded), "$")
487-
if !ok {
486+
parts := strings.Split(string(decoded), "$")
487+
if len(parts) < 2 {
488488
return nil, fmt.Errorf("invalid decoded CloudID %q", string(decoded))
489489
}
490-
return url.Parse(fmt.Sprintf("https://%s.%s", after, before))
490+
return url.Parse(fmt.Sprintf("https://%s.%s", parts[1], parts[0]))
491491
}
492492

493493
func handleDeprecatedConfig(cfg *Config, logger *zap.Logger) {

exporter/elasticsearchexporter/config_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,49 @@ func TestConfig_Validate_Environment(t *testing.T) {
555555
})
556556
}
557557

558+
func TestParseCloudID(t *testing.T) {
559+
tests := map[string]struct {
560+
input string
561+
expectedURL string
562+
expectError bool
563+
}{
564+
"valid cloudid with multiple dollar signs": {
565+
input: "foo:YmFyLmNsb3VkLmVzLmlvJGFiYzEyMyRkZWY0NTY=",
566+
expectedURL: "https://abc123.bar.cloud.es.io",
567+
expectError: false,
568+
},
569+
"valid cloudid with two parts": {
570+
input: "test:ZG9tYWluLmNvbSRlcy1pZA==",
571+
expectedURL: "https://es-id.domain.com",
572+
expectError: false,
573+
},
574+
"missing colon": {
575+
input: "invalid",
576+
expectError: true,
577+
},
578+
"invalid base64": {
579+
input: "test:!!!invalid!!!",
580+
expectError: true,
581+
},
582+
"missing dollar sign": {
583+
input: "test:YWJj",
584+
expectError: true,
585+
},
586+
}
587+
588+
for name, tt := range tests {
589+
t.Run(name, func(t *testing.T) {
590+
url, err := parseCloudID(tt.input)
591+
if tt.expectError {
592+
assert.Error(t, err)
593+
} else {
594+
require.NoError(t, err)
595+
assert.Equal(t, tt.expectedURL, url.String())
596+
}
597+
})
598+
}
599+
}
600+
558601
func withDefaultConfig(fns ...func(*Config)) *Config {
559602
cfg := createDefaultConfig().(*Config)
560603
for _, fn := range fns {

0 commit comments

Comments
 (0)