Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEVCONTAINER] support customization and run as non-root user #3270

Merged
merged 12 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .devcontainer/Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

FROM otel/cpp_format_tools

ARG USER_NAME=devuser
ARG USER_UID=1000
ARG USER_GID=1000
ARG INSTALL_PACKAGES=

ARG GRPC_VERSION=v1.55.0
ARG PROTOBUF_VERSION=23.4
ARG ABSEIL_CPP_VERSION=20240116.1
Expand All @@ -15,7 +20,8 @@ COPY ci /opt/ci
RUN apt update && apt install -y wget \
ninja-build \
libcurl4-openssl-dev \
markdownlint
markdownlint \
shellcheck

RUN cd /opt/ci && bash setup_cmake.sh
RUN cd /opt/ci && bash setup_ci_environment.sh
Expand All @@ -26,3 +32,14 @@ ADD https://github.com/bazelbuild/bazelisk/releases/download/v1.22.1/bazelisk-li

RUN git config --global core.autocrlf input \
&& chmod +x /usr/local/bin/bazelisk-linux-amd64

ENV INSTALL_PACKAGES=${INSTALL_PACKAGES}
ENV USER_NAME=${USER_NAME}
ENV USER_UID=${USER_UID}
ENV USER_GID=${USER_GID}
ENV IS_CONTAINER_BUILD=true
COPY ./.devcontainer/customize_container.sh /tmp/opentelemetry_cpp/devcontainer/customize_container.sh
RUN /tmp/opentelemetry_cpp/devcontainer/customize_container.sh

USER ${USER_NAME}
CMD ["/bin/bash"]
39 changes: 39 additions & 0 deletions .devcontainer/customize_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

set -eu

if [[ $IS_CONTAINER_BUILD != "true" ]]; then
echo "This script should only run inside a Docker container."
exit 1
fi

if [[ -n "$INSTALL_PACKAGES" ]]; then
packages=($INSTALL_PACKAGES)
for package in "${packages[@]}"; do
apt install -y "$package"
done
fi

if [[ $(id "$USER_NAME" 2>/dev/null) ]]; then
echo "User '$USER_NAME' already exists. Removing it."
userdel -rf "$USER_NAME"
elif [[ $(id -u "$USER_UID" 2>/dev/null) ]]; then
OTHER_USER=$(getent passwd "$USER_UID" | cut -d: -f1)
echo "User '$OTHER_USER' exists with UID $USER_UID. Removing it."
userdel -rf "$OTHER_USER"
fi

if [[ ! $(getent group "$USER_GID" 2>/dev/null) ]]; then
echo "Group '$USER_GID' does not exist. Adding it."
groupadd -g "$USER_GID" "$USER_NAME"
fi

useradd -m -u "$USER_UID" -g "$USER_GID" -s /bin/bash "$USER_NAME"
echo "Created user '$USER_NAME' (UID: $USER_UID, GID: $USER_GID)."

echo "$USER_NAME ALL=(ALL) NOPASSWD:ALL" | tee /etc/sudoers.d/"$USER_NAME"

echo "User and group setup complete."
24 changes: 16 additions & 8 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@
"context": "..",
"dockerfile": "Dockerfile.dev",
"args": {
"USER_NAME":"${localEnv:OTEL_CPP_DEVCONTAINER_USER_NAME:devuser}",
"USER_UID": "${localEnv:OTEL_CPP_DEVCONTAINER_USER_UID:1000}",
"USER_GID": "${localEnv:OTEL_CPP_DEVCONTAINER_USER_GID:1000}",
"INSTALL_PACKAGES": "${localEnv:OTEL_CPP_DEVCONTAINER_INSTALL_PACKAGES:}",
"GRPC_VERSION": "v1.55.0",
"PROTOBUF_VERSION": "23.4",
"ABSEIL_CPP_VERSION":"20240116.1"
}
},
"settings": {
"terminal.integrated.shell.linux": "/bin/sh"
"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools",
"ms-azuretools.vscode-docker",
"ms-vscode.cpptools-extension-pack"
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
}
}
},
"extensions": [
"ms-vscode.cpptools",
"ms-azuretools.vscode-docker",
"ms-vscode.cpptools-extension-pack"
],

"remoteUser": "root"
"remoteUser": "${env:USER_NAME}"
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Increment the:
* [SDK] Support OTEL_SDK_DISABLED environment variable
[#3245](https://github.com/open-telemetry/opentelemetry-cpp/pull/3245)

* [DEVCONTAINER] Support customization and run as non-root user
[#3270](https://github.com/open-telemetry/opentelemetry-cpp/pull/3270)

Important changes:

* [SDK] Support OTEL_SDK_DISABLED environment variable
Expand Down
49 changes: 45 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,41 @@ Before getting started, ensure you have the following installed:
files provided (e.g., `.devcontainer/devcontainer.json`). This setup will install
required dependencies, tools, and environment variables needed for the project.

#### Customizing Your Dev Container

Customize your dev container using build arguments (for direct Docker builds) or
environment variables (for evaluation in `devcontainer.json`).

* **Username:**
* Docker ARG:
`USER_NAME` (Default: `devuser`)
* Environment Variable:
`OTEL_CPP_DEVCONTAINER_USER_NAME` (Default: `devuser`)

* **User ID (UID):**
* Docker ARG:
`USER_UID` (Default: `1000`)
* Environment Variable:
`OTEL_CPP_DEVCONTAINER_USER_UID` (Default: `1000`)

* **Group ID (GID):**
* Docker ARG:
`USER_GID` (Default: `1000`)
* Environment Variable:
`OTEL_CPP_DEVCONTAINER_USER_GID` (Default: `1000`)

* **Install Packages:**
* Docker ARG:
`INSTALL_PACKAGES` (Default: ``)
* Environment Variable:
`OTEL_CPP_DEVCONTAINER_INSTALL_PACKAGES` (Default: ``)

##### Examples

* `docker build --build-arg USER_NAME=myuser --build-arg INSTALL_PACKAGES="nano gitk"...`
* `export OTEL_CPP_DEVCONTAINER_USER_NAME=myuser`
* `export OTEL_CPP_DEVCONTAINER_INSTALL_PACKAGES="nano gitk"`

#### Available Commands

Once inside the DevContainer, you can use the following commands to run tests
Expand Down Expand Up @@ -192,7 +227,13 @@ If you made changes to the Markdown documents (`*.md` files), install the latest
[`markdownlint-cli`](https://github.com/igorshubovych/markdownlint-cli) and run:

```sh
markdownlint .
mdl <path to markdown file>.md
```

If you modified shell scripts (`*.sh` files), install `shellcheck` and run:

```sh
shellcheck --severity=error <path to shell script>.sh
```

Open a pull request against the main `opentelemetry-cpp` repo.
Expand Down Expand Up @@ -271,11 +312,11 @@ the C++ repository.

* [OpenTelemetry
Specification](https://github.com/open-telemetry/opentelemetry-specification)
* The OpenTelemetry Specification describes the requirements and expectations
of for all OpenTelemetry implementations.
* The OpenTelemetry Specification describes the requirements and expectations
of for all OpenTelemetry implementations.

* Read through the OpenTelemetry C++ documentation
* The
* The
[API](https://opentelemetry-cpp.readthedocs.io/en/latest/api/api.html)
and
[SDK](https://opentelemetry-cpp.readthedocs.io/en/latest/sdk/sdk.html)
Expand Down
Loading