Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The valid layer stacks are declared in the `chains` dict in `utils/build_chain.p

The `spark` layer installs the official Apache Spark distribution (`spark/scripts/install-spark.sh`)The only thing it adds is the S3A connector, whose jar versions are read from the distribution itself (the `hadoop-client-api` jar it ships, then the matching `hadoop-project` POM on Maven Central), so they are not pinned anywhere.

Tools downloaded at build time (kubectl, helm, AWS CLI, DuckDB CLI, quarto, opencode, Julia, code-server) are pinned in their install script, **not** in Dockerfiles (Dockerfiles only pin the versions the project manages: Python, R, Spark). Each script follows the same model, see `base/scripts/install-kubectl.sh`:
Tools downloaded at build time (kubectl, helm, AWS CLI, DuckDB CLI, quarto, git-credential-oauth, opencode, Julia, code-server) are pinned in their install script, **not** in Dockerfiles (Dockerfiles only pin the versions the project manages: Python, R, Spark). Each script follows the same model, see `base/scripts/install-kubectl.sh`:
- a `# renovate: datasource=<datasource> depName=<name>` comment right above a `<TOOL>_VERSION="x.y.z"` line. One generic regex manager in `renovate.json` picks it up (in `scripts/*.sh` and in `.github/actions/*/action.yml`, where container-structure-test is pinned the same way), and all tool bumps land in a single weekly "Build tools" PR.
- the official release file is downloaded to a `mktemp -d` directory and verified before installing: against the upstream checksum file when there is one (kubectl, helm, quarto, Julia), against the SHA-256 GitHub records for the release asset (read from the GitHub API in the script, with `set -o pipefail`) when there is none (DuckDB, code-server), against the SHA-512 `integrity` the npm registry records for opencode (v2 is only published on npm), and with the PGP signature for the AWS CLI (the public key is embedded in `install-awscli.sh` and expires on 2027-07-01).
- the official release file is downloaded to a `mktemp -d` directory and verified before installing: against the upstream checksum file when there is one (kubectl, helm, quarto, git-credential-oauth, Julia), against the SHA-256 GitHub records for the release asset (read from the GitHub API in the script, with `set -o pipefail`) when there is none (DuckDB, code-server), against the SHA-512 `integrity` the npm registry records for opencode (v2 is only published on npm), and with the PGP signature for the AWS CLI (the public key is embedded in `install-awscli.sh` and expires on 2027-07-01).
- amd64 only.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ If Onyxia support is checked, it means that the Onyxia product could inject auto
| PIP_REPOSITORY | https://some.entreprise.mirror/repository/pypi-proxy/simple | Configure an externally managed pip repository manager | ✔️ | ✔️ |
| R_REPOSITORY | https://some.entreprise.mirror/repository/cran | Configure an externally managed cran repository manager |not yet (*) | not yet (*) |
| PATH_TO_CA_BUNDLE | /etc/ssl/certs/ca-certificates.crt | Configure a path to a ca bundle with autorities to support an auto-signed some.entreprise.mirror | not yet (*) | not yet (*) |
| GIT_PROVIDERS | `[{"id":"gitlab","url":"https://gitlab.example.com","user":{"name":"Jane","email":"jane@example.com"},"credentials":{"type":"token","username":"jane","token":"…"}}]` | Git forges to configure, as JSON: `git.providers` of the Onyxia context. Sets the commit identity and the credentials per forge, with a token (`credentials.type: "token"`) or OAuth through [git-credential-oauth](https://github.com/hickford/git-credential-oauth) (`credentials.type: "oauth"`). Takes over from `GIT_PERSONAL_ACCESS_TOKEN` for the forges it lists | not yet | not yet |
| GIT_REPOSITORIES | `[{"url":"https://gitlab.example.com/jane/project.git","branch":"main"}]` | Git repositories cloned at startup, as JSON: `git.repositories` of the chart values (other keys, such as `providerId`, are ignored). An empty `branch` clones the default branch. The credentials are those of `GIT_PROVIDERS`, no token goes in the URL. Takes over from `GIT_REPOSITORY`/`GIT_BRANCH` | not yet | not yet |

(*) For now you should inject this another way, contact on onyxia slack for some recipes depend of your context.

Expand Down
1 change: 1 addition & 0 deletions base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ RUN /opt/install-system-libs.sh && \
/opt/install-duckdb-cli.sh && \
/opt/install-duckdb-extensions.sh && \
/opt/install-quarto.sh && \
/opt/install-git-credential-oauth.sh && \
# Fix user permissions
/opt/fix-user-permissions.sh && \
# Clean
Expand Down
18 changes: 18 additions & 0 deletions base/scripts/install-git-credential-oauth.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -e

# renovate: datasource=github-releases depName=hickford/git-credential-oauth
GIT_CREDENTIAL_OAUTH_VERSION="0.18.0"

# Download the official archive and verify its checksum
GIT_CREDENTIAL_OAUTH_URL="https://github.com/hickford/git-credential-oauth/releases/download/v${GIT_CREDENTIAL_OAUTH_VERSION}"
GIT_CREDENTIAL_OAUTH_ARCHIVE="git-credential-oauth_${GIT_CREDENTIAL_OAUTH_VERSION}_linux_amd64.tar.gz"
TMP_DIR=$(mktemp -d)
curl -fsSL "${GIT_CREDENTIAL_OAUTH_URL}/${GIT_CREDENTIAL_OAUTH_ARCHIVE}" -o "${TMP_DIR}/${GIT_CREDENTIAL_OAUTH_ARCHIVE}"
curl -fsSL "${GIT_CREDENTIAL_OAUTH_URL}/git-credential-oauth_${GIT_CREDENTIAL_OAUTH_VERSION}_checksums.txt" -o "${TMP_DIR}/checksums.txt"
(cd "${TMP_DIR}" && sha256sum --check --strict --ignore-missing checksums.txt)

# Install git-credential-oauth
tar -xzf "${TMP_DIR}/${GIT_CREDENTIAL_OAUTH_ARCHIVE}" -C "${TMP_DIR}"
install -m 0755 "${TMP_DIR}/git-credential-oauth" /usr/local/bin/git-credential-oauth
rm -rf "${TMP_DIR}"
169 changes: 158 additions & 11 deletions base/scripts/onyxia-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,161 @@ if [[ -z $ROOT_PROJECT_DIRECTORY ]]; then
ROOT_PROJECT_DIRECTORY="$WORKSPACE_DIR"
fi

# Some images start as root (RStudio): what is written in the home directory has to
# belong to the user, and the credential cache daemon has to run as the user.
git_as_user() {
if [[ $(id -u) = 0 ]]; then
sudo -u "$USERNAME" -H git "$@"
else
git "$@"
fi
}

# Scheme, host and port of a URL: what git credentials are matched on.
git_url_origin() {
[[ "$1" =~ ^https?://[^/]+ ]] && echo "${BASH_REMATCH[0]}"
}

# Configures an entry of GIT_PROVIDERS: the commit identity on this forge and the
# credentials that git uses for it.
configure_git_provider() {
local provider="$1"
local id url origin name email identity_file type

id=$(jq -r '.id' <<< "$provider")
url=$(jq -r '.url' <<< "$provider")
origin=$(git_url_origin "$url")
if [[ -z "$origin" ]]; then
echo "skipping Git provider $id: not an http(s) URL"
return
fi
echo "configuration of the Git provider $id"

name=$(jq -r '.user.name // empty' <<< "$provider")
email=$(jq -r '.user.email // empty' <<< "$provider")
identity_file="${HOME}/.gitconfig-onyxia-${id//[^a-zA-Z0-9_-]/_}"
[[ -n "$name" ]] && git_as_user config --file "$identity_file" user.name "$name"
[[ -n "$email" ]] && git_as_user config --file "$identity_file" user.email "$email"
if [[ -f "$identity_file" ]]; then
git_as_user config --global "includeIf.hasconfig:remote.*.url:${url}/**.path" "$identity_file"
fi

type=$(jq -r '.credentials.type // empty' <<< "$provider")
[[ -z "$type" ]] && return

# An empty value drops the helpers configured so far for this forge, the former
# credential.helper with a short timeout would otherwise evict the refresh token.
git_as_user config --global --replace-all "credential.${origin}.helper" ""

case "$type" in
token)
git_as_user config --global --add "credential.${origin}.helper" store
jq -j --arg origin "$origin" \
'"url=\($origin)\nusername=\(.credentials.username)\npassword=\(.credentials.token)\n\n"' <<< "$provider" |
git_as_user credential-store store
;;
oauth)
local client_id scopes authorization_url token_url device_url refresh_token
client_id=$(jq -r '.credentials.clientId' <<< "$provider")
scopes=$(jq -r '.credentials.scopes | join(" ")' <<< "$provider")
authorization_url=$(jq -r '.credentials.authorizationUrl' <<< "$provider")
token_url=$(jq -r '.credentials.tokenUrl' <<< "$provider")
device_url=$(jq -r '.credentials.deviceAuthorizationUrl // empty' <<< "$provider")
refresh_token=$(jq -r '.credentials.refreshToken // empty' <<< "$provider")

git_as_user config --global "credential.${origin}.oauthClientId" "$client_id"
git_as_user config --global "credential.${origin}.oauthScopes" "$scopes"
git_as_user config --global "credential.${origin}.oauthAuthURL" "$authorization_url"
git_as_user config --global "credential.${origin}.oauthTokenURL" "$token_url"

# The cache keeps the access token and, unlike the store helper, the refresh
# token. It only lives in memory: long enough to outlive the service.
git_as_user config --global --add "credential.${origin}.helper" "cache --timeout=$GIT_OAUTH_CACHE_TIMEOUT"
# When it can't refresh, git-credential-oauth falls back on an interactive flow
# and waits for it without any limit: git would hang.
if [[ -n "$device_url" ]]; then
git_as_user config --global "credential.${origin}.oauthDeviceAuthURL" "$device_url"
git_as_user config --global --add "credential.${origin}.helper" "!timeout 300 git credential-oauth -device"
else
# The browser flow can't succeed from a service.
git_as_user config --global --add "credential.${origin}.helper" "!timeout 30 git credential-oauth"
fi

# Seeds the cache with an expired password: git discards it and hands the refresh
# token to git-credential-oauth. This has to go straight to the helper, "git
# credential approve" ignores expired credentials.
if [[ -n "$refresh_token" ]]; then
printf 'url=%s\nusername=oauth2\npassword=expired\npassword_expiry_utc=1\noauth_refresh_token=%s\n\n' "$origin" "$refresh_token" |
git_as_user credential-cache --timeout="$GIT_OAUTH_CACHE_TIMEOUT" store
fi
;;
*)
echo "Git provider $id: unknown credentials type $type"
return
;;
esac

GIT_PROVIDERS_ORIGINS_WITH_CREDENTIALS+=" $origin "
}

if command -v git &>/dev/null; then
if [[ -n "$PATH_TO_CA_BUNDLE" ]]; then
echo "configuration of git to a custom crt"
git config --global http.sslVerify true
git config --global http.sslCAInfo $PATH_TO_CA_BUNDLE
fi
if [[ -n "$GIT_REPOSITORY" ]]; then

# Git config
# NOTE: Before the Git providers, whose identity has to come later in the config to win.
if [ -n "$GIT_USER_NAME" ]; then
git config --global user.name "$GIT_USER_NAME"
fi
if [ -n "$GIT_USER_MAIL" ]; then
git config --global user.email "$GIT_USER_MAIL"
fi
if [ -n "$GIT_CREDENTIALS_CACHE_DURATION" ]; then
git config --global credential.helper "cache --timeout=$GIT_CREDENTIALS_CACHE_DURATION"
fi
# The ownership of the files git_as_user is about to write to, or to read
[ -f ${HOME}/.gitconfig ] && chown ${USERNAME}:${GROUPNAME} ${HOME}/.gitconfig

# GIT_PROVIDERS: git.providers of the Onyxia context, as JSON.
GIT_OAUTH_CACHE_TIMEOUT=31536000
GIT_PROVIDERS_ORIGINS_WITH_CREDENTIALS=""
if [[ -n "$GIT_PROVIDERS" ]] && ! jq -e 'type == "array"' <<< "$GIT_PROVIDERS" &>/dev/null; then
echo "ignoring GIT_PROVIDERS: not a JSON array"
GIT_PROVIDERS=""
fi
if [[ -n "$GIT_PROVIDERS" ]]; then
while IFS= read -r provider; do
configure_git_provider "$provider"
done < <(jq -c '.[] | select((.credentials.type == "oauth" and .credentials.refreshToken == null) | not)' <<< "$GIT_PROVIDERS")
fi

# GIT_REPOSITORIES: git.repositories of the chart values, as JSON ([{url, branch}]).
# Takes over from GIT_REPOSITORY, which then holds the first one.
if [[ -n "$GIT_REPOSITORIES" ]] && ! jq -e 'type == "array"' <<< "$GIT_REPOSITORIES" &>/dev/null; then
echo "ignoring GIT_REPOSITORIES: not a JSON array"
GIT_REPOSITORIES=""
fi
if [[ -n "$GIT_REPOSITORIES" ]] && jq -e 'length > 0' <<< "$GIT_REPOSITORIES" &>/dev/null; then
# Nothing goes in the URL: the credentials are those of the Git providers. The
# token of GIT_PERSONAL_ACCESS_TOKEN belongs to one forge, it would be sent to all.
while IFS=$'\t' read -r url branch; do
[[ -z "$url" ]] && continue
echo "cloning $url"
if [[ -n "$branch" ]]; then
git -C "$ROOT_PROJECT_DIRECTORY" clone "$url" --branch "$branch" || echo "failed to clone $url"
else
git -C "$ROOT_PROJECT_DIRECTORY" clone "$url" || echo "failed to clone $url"
fi
done < <(jq -r '.[] | select(.url | type == "string") | [.url, (.branch // "")] | @tsv' <<< "$GIT_REPOSITORIES")
elif [[ -n "$GIT_REPOSITORY" ]]; then
CLONE_URL="$GIT_REPOSITORY"

if [[ -n "$GIT_PERSONAL_ACCESS_TOKEN" ]]; then
# With the credentials of a Git provider nothing goes in the URL: it would be saved
# in .git/config, in the project directory, and outdated once the token is refreshed.
if [[ -n "$GIT_PERSONAL_ACCESS_TOKEN" && "$GIT_PROVIDERS_ORIGINS_WITH_CREDENTIALS" != *" $(git_url_origin "$GIT_REPOSITORY") "* ]]; then
URL_PREFIX=$(echo "$GIT_REPOSITORY" | grep -oE '^https?://')
URL_NO_PREFIX=$(echo "$GIT_REPOSITORY" | sed 's|^https\?://||')
REPO_DOMAIN=$(awk -F/ '{print $3}' <<< "$GIT_REPOSITORY")
Expand All @@ -115,16 +260,18 @@ if command -v git &>/dev/null; then
fi
fi

# Git config
if [ -n "$GIT_USER_NAME" ]; then
git config --global user.name "$GIT_USER_NAME"
fi
if [ -n "$GIT_USER_MAIL" ]; then
git config --global user.email "$GIT_USER_MAIL"
fi
if [ -n "$GIT_CREDENTIALS_CACHE_DURATION" ]; then
git config --global credential.helper "cache --timeout=$GIT_CREDENTIALS_CACHE_DURATION"
# Providers relying on the device flow only, once the project is cloned: nobody is there
# to enter the code during the startup, the clone would wait for it. Without credentials
# it fails right away on a private repository, the user can clone it from a terminal.
if [[ -n "$GIT_PROVIDERS" ]]; then
while IFS= read -r provider; do
configure_git_provider "$provider"
done < <(jq -c '.[] | select(.credentials.type == "oauth" and .credentials.refreshToken == null)' <<< "$GIT_PROVIDERS")
fi
# The processes of the service have no use of these credentials, they're now in the
# helpers. The refresh token is outdated anyway after its first use.
unset GIT_PROVIDERS

# Default strategy when performing a git pull
# Use Git's former default (fast-forward if possible, else merge) to avoid cryptic error message
git config --global pull.rebase false
Expand Down
Loading
Loading