# syntax=docker/dockerfile:1

# Pinned by digest: a republished tag would stop matching CI's cached
# dependency build without changing its cache key.
FROM rust:1.94.1-bookworm@sha256:6ae102bdbf528294bc79ad6e1fae682f6f7c2a6e6621506ba959f9685b308a55 AS dependencies

WORKDIR /src
COPY Cargo.toml Cargo.lock build.rs ./
# Compile the locked dependencies against a stub program. This layer changes
# only with the manifest, lock file or build script, so CI can cache it.
RUN mkdir src \
    && printf 'fn main() {}\n' > src/main.rs \
    && cargo build --locked --bin syq \
    && rm -r src

FROM dependencies AS builder

COPY tests/real-ssh/sk-api.h tests/real-ssh/sk-provider.c /tmp/sk-provider/
RUN cc -shared -fPIC -Wno-deprecated-declarations /tmp/sk-provider/sk-provider.c -lcrypto -o /tmp/syq-test-sk.so

# A pre-session-binding OpenSSH for restricted-copy compatibility coverage.
# SHA256 is from https://www.openssh.org/txt/release-8.8.
RUN curl -fsSLo /tmp/openssh.tar.gz https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.8p1.tar.gz \
    && echo '4590890ea9bb9ace4f71ae331785a3a5823232435161960ed5fc86588f331fe9  /tmp/openssh.tar.gz' | sha256sum -c - \
    && tar -C /tmp -xzf /tmp/openssh.tar.gz \
    && cd /tmp/openssh-8.8p1 \
    && ./configure --prefix=/opt/openssh-8.8 --without-pam --without-zlib \
    && make -j4 ssh sshd ssh-agent ssh-add ssh-keygen \
    && mkdir -p /opt/openssh-8.8/bin /opt/openssh-8.8/sbin \
    && cp ssh ssh-agent ssh-add ssh-keygen /opt/openssh-8.8/bin/ \
    && cp sshd /opt/openssh-8.8/sbin/

COPY schemas ./schemas
COPY src ./src
# The second PATH fixture has a real distinct identity and return wire version.
# It must hand off before making anything beyond a stable readiness probe.
# The stub build's artifacts can look newer than these sources. Rebuild this
# package, including its identity build script; keep dependency artifacts.
RUN cargo clean -p syq \
    && cargo build --locked --bin syq \
    && cp target/debug/syq /tmp/syq \
    && printf '%s\n' '{"git":{"sha1":"1234567890abcdef1234567890abcdef12345678"}}' > .cargo_vcs_info.json \
    && sed -i 's/^const VERSION: u16 = 2;/const VERSION: u16 = 65500;/' src/destination.rs \
    && cargo build --locked --bin syq \
    && cp target/debug/syq /tmp/syq-other-build

# PGSTY Silo, a maintained community fork of the MinIO server.
FROM docker.io/pgsty/silo@sha256:b616a0cf8cb281e7e6bb3c9b1fb53875b4016a2878223925541c18f82d6c5ca3 AS storage-fixture

FROM debian:bookworm-slim

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        ca-certificates \
        diffutils \
        dbus \
        libnotify-bin \
        fish \
        git \
        iptables \
        netcat-openbsd \
        openssh-client \
        openssh-server \
        procps \
        perl \
        python3 \
        python3-dbus \
        python3-gi \
        zsh \
        rsync \
        openssl \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 1000 syq \
    && useradd --uid 1000 --gid syq --create-home --shell /bin/sh syq \
    && passwd --delete syq \
    && install -d -m 0700 -o syq -g syq /home/syq/.ssh \
    && install -d -m 0755 /run/sshd

# Its enrollment pathname exceeds Unix socket address limits on both platforms.
RUN useradd --uid 1001 --gid syq --create-home --shell /bin/sh \
        --home-dir /home/syq-real-ssh-long-account-home-for-restricted-worker-paths longhome \
    && passwd --delete longhome

COPY --from=storage-fixture /usr/bin/silo /usr/local/libexec/syq-test-s3-server
COPY tests/object-storage/check.py /usr/local/libexec/syq-storage-check.py
COPY tests/real-ssh/test-storage-authorization.py /usr/local/libexec/syq-test-storage-authorization.py
COPY tests/real-ssh/storage-streams.py /usr/local/libexec/syq-storage-streams.py

COPY --from=builder /opt/openssh-8.8 /opt/openssh-8.8
COPY --from=builder /tmp/syq-test-sk.so /usr/local/lib/syq-test-sk.so
COPY --from=builder /tmp/syq /usr/local/bin/syq
COPY --from=builder /tmp/syq-other-build /usr/local/bin/syq-other-build
COPY --chmod=0755 tests/real-ssh/ssh-wrapper.sh /usr/local/bin/ssh
COPY --chmod=0755 tests/real-ssh/entrypoint.sh /usr/local/libexec/syq-real-ssh-entrypoint
COPY --chmod=0755 tests/real-ssh/scenarios.sh /usr/local/libexec/syq-real-ssh-scenarios
COPY tests/real-ssh/benchmark.sh /usr/local/libexec/syq-real-ssh-benchmark
COPY --chmod=0755 scripts/try-benchmark.sh /usr/local/libexec/syq-try-benchmark
COPY tests/real-ssh/sshd_config /etc/ssh/sshd_config

COPY tests/real-ssh/test-completion-display.py /usr/local/libexec/syq-test-completion-display.py

COPY tests/real-ssh/test-receive-notifications.py /usr/local/libexec/syq-test-receive-notifications.py

COPY tests/real-ssh/test-forward-copy.py /usr/local/libexec/syq-test-forward-copy.py
COPY tests/real-ssh/test-pull-copy.py /usr/local/libexec/syq-test-pull-copy.py
COPY tests/real-ssh/test-send-copy.py /usr/local/libexec/syq-test-send-copy.py

COPY tests/real-ssh/test-return-exec.py /usr/local/libexec/syq-test-return-exec.py
COPY tests/real-ssh/test-return-ssh.py /usr/local/libexec/syq-test-return-ssh.py
COPY tests/real-ssh/test-ssh-tools.py /usr/local/libexec/syq-test-ssh-tools.py
COPY tests/real-ssh/test-provider-ssh.py /usr/local/libexec/syq-test-provider-ssh.py
COPY tests/real-ssh/test-between-servers.py /usr/local/libexec/syq-test-between-servers.py
COPY tests/real-ssh/test-account-switch.py /usr/local/libexec/syq-test-account-switch.py

COPY tests/real-ssh/test-receive-profiles.py /usr/local/libexec/syq-test-receive-profiles.py

COPY tests/real-ssh/test-return-handoff.py /usr/local/libexec/syq-test-return-handoff.py
COPY tests/real-ssh/test-handoff-timing.py /usr/local/libexec/syq-test-handoff-timing.py

COPY tests/real-ssh/test-root-security.py /usr/local/libexec/syq-test-root-security.py

COPY tests/real-ssh/test-receiver-revoke.py /usr/local/libexec/syq-test-receiver-revoke.py
COPY tests/real-ssh/test-requester-loss.py /usr/local/libexec/syq-test-requester-loss.py

COPY tests/real-ssh/test-restricted-mapping.py /usr/local/libexec/syq-test-restricted-mapping.py
COPY tests/real-ssh/test-results-stream.py /usr/local/libexec/syq-test-results-stream.py

COPY tests/real-ssh/test-receiver-identity.py /usr/local/libexec/syq-test-receiver-identity.py

ENTRYPOINT ["/usr/local/libexec/syq-real-ssh-entrypoint"]

COPY tests/real-ssh/test-stream-mappings.py /usr/local/libexec/syq-test-stream-mappings.py

COPY tests/real-ssh/test-map-generation.py /usr/local/libexec/syq-test-map-generation.py
COPY tests/real-ssh/test-metadata.py /usr/local/libexec/syq-test-metadata.py

COPY tests/real-ssh/test-receiver-keys.py /usr/local/libexec/syq-test-receiver-keys.py

# Every python3 in the lab imports this; in the runner it prints the process
# tables of all three containers before a timed-out subprocess.run is killed.
COPY tests/real-ssh/timeout-diagnostics.py /usr/lib/python3/dist-packages/syq_real_ssh_timeouts.py
RUN printf 'import syq_real_ssh_timeouts\n' > /usr/lib/python3/dist-packages/syq-real-ssh-timeouts.pth

COPY tests/real-ssh/test-receiver-old-openssh.py /usr/local/libexec/syq-test-receiver-old-openssh.py
