Skip to content

elasticsearch exporter: Cloud ID is decoded wrongly #44306

@lroellin

Description

@lroellin

Component(s)

exporter/elasticsearch

What happened?

Description

The Cloud ID is decoded wrongly, it uses the wrong algorithm for decoding.

The following code snippet demonstrates the problem. It contains code copied from config.go, and the code copied from go-elasticsearch which the own code is based on.

Steps to Reproduce

Go Playground Link

Reproduction code
package main

import (
	"encoding/base64"
	"fmt"
	"net/url"
	"strings"
)

// copied from: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/a2ceaf015cecfd9c8559f876bb6ca5e32a00d10a/exporter/elasticsearchexporter/config.go#L475
// original comments:
// addrFromCloudID extracts the Elasticsearch URL from CloudID.
// See: https://www.elastic.co/guide/en/cloud/current/ec-cloud-id.html
func parseCloudID(input string) (*url.URL, error) {
	_, after, ok := strings.Cut(input, ":")
	if !ok {
		return nil, fmt.Errorf("invalid CloudID %q", input)
	}

	decoded, err := base64.StdEncoding.DecodeString(after)
	if err != nil {
		return nil, err
	}

	before, after, ok := strings.Cut(string(decoded), "$")
	if !ok {
		return nil, fmt.Errorf("invalid decoded CloudID %q", string(decoded))
	}
	return url.Parse(fmt.Sprintf("https://%s.%s", after, before))
}

// real addrFromCloudID
// https://github.com/elastic/go-elasticsearch/blob/4739f334675880df102135bcca6104c8c63b59db/elasticsearch.go#L474
func addrFromCloudID(input string) (string, error) {
	var scheme = "https://"

	values := strings.Split(input, ":")
	if len(values) != 2 {
		return "", fmt.Errorf("unexpected format: %q", input)
	}
	data, err := base64.StdEncoding.DecodeString(values[1])
	if err != nil {
		return "", err
	}
	parts := strings.Split(string(data), "$")

	if len(parts) < 2 {
		return "", fmt.Errorf("invalid encoded value: %s", parts)
	}

	return fmt.Sprintf("%s%s.%s", scheme, parts[1], parts[0]), nil
}

func main() {
	// value taken from config example, https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/elasticsearchexporter#configuration-options$
	const cloudid = "foo:YmFyLmNsb3VkLmVzLmlvJGFiYzEyMyRkZWY0NTY="

	fmt.Println(parseCloudID(cloudid))
	fmt.Println(addrFromCloudID(cloudid))
}

Expected Result

https://abc123.bar.cloud.es.io

Actual Result

https://abc123$def456.bar.cloud.es.io

Collector version

0.138.0

Environment information

Environment

OS: Linux

OpenTelemetry Collector configuration

Log output

Additional context

No response

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions