Decode URL-encoded headers in environment vars#2312
Merged
lzchen merged 10 commits intoopen-telemetry:mainfrom Jan 4, 2022
Merged
Decode URL-encoded headers in environment vars#2312lzchen merged 10 commits intoopen-telemetry:mainfrom
lzchen merged 10 commits intoopen-telemetry:mainfrom
Conversation
|
The [opentelemetry-specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables) wants `OTEL_EXPORTER_OTLP_HEADERS` to conform to the [Baggage HTTP Header Format](https://github.com/w3c/baggage/blob/main/baggage/HTTP_HEADER_FORMAT.md). To effectively use the headers from `OTEL_EXPORTER_OTLP_HEADERS` for things like authentication (a common use case) the values need to be decoded. Given a header like this: ``` export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic base64" ``` The code prior to this commit would reject the header for containing whitespace mid-value. If the header is modified to this: ``` export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Basic%20base64" ``` The code prior to this commit would send the header as `Authorization: Basic%20base64`, which will be rejected upstream. This change applies `urllib.parse.unquote` to header **names** and **values**, based on precedent set by the following libraries: - [opentelemetry-go](https://github.com/open-telemetry/opentelemetry-go) - [opentelemetry-js](https://github.com/open-telemetry/opentelemetry-js) - [opentelemetry-ruby](https://github.com/open-telemetry/opentelemetry-ruby)
85a1288 to
17c5f2a
Compare
Unquoting header keys and values **after** calling `str.strip()` opened
the possibility for user encoding errors where a header contained
invalid whitespace. Unquoting **before** `str.strip()` aligns
`opentelemetry-python` with other `opentelemetry-{lang}` libraries that
implemenent URL decoding.
The HTTP exporter allows headers to be provided as `Dict[str, str]`. The gRPC exporter required headers to be `Sequence[Tuple[str, str]]`. This commit enables the `Dict[str, str]` format for gRPC.
lzchen
reviewed
Dec 10, 2021
...pentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py
Outdated
Show resolved
Hide resolved
lzchen
reviewed
Dec 10, 2021
lzchen
reviewed
Dec 10, 2021
...pentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py
Outdated
Show resolved
Hide resolved
owais
approved these changes
Dec 25, 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The opentelemetry-specification wants
OTEL_EXPORTER_OTLP_HEADERSto conform to the Baggage HTTP Header Format.To effectively use the headers from
OTEL_EXPORTER_OTLP_HEADERSfor things like authentication (a common use case) the values need to be decoded.Given a header like this:
The code prior to this commit would reject the header for containing white space mid-value.
If the header is modified to this:
The code prior to this commit would send the header as
Authorization: Basic%20base64, which will be rejected upstream.This change applies
urllib.parse.unquoteto header names and values, based on precedent set by the following libraries:Fixes #2248 1
1 This PR does not restore previous functionality (where the header
strcould be provided with spaces in the values), but it does provide the ability to provide a headerstrwith encoded spaces. I also added the ability to passheadersasDict[str, str]to address both issues raised in #2248. These can be split into individual PRs if needed.Type of change
Please delete options that are not relevant.
How Has This Been Tested?
re.pyDoes This PR Require a Contrib Repo Change?
Checklist: