aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/integration/docker/git_server/entrypoint.sh
blob: 03f79e4d3123dee46648c6d6568b249beb98d789 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit lastpipe

script_name="$( basename -- "${BASH_SOURCE[0]}" )"
readonly script_name

dump() {
    local prefix="${FUNCNAME[0]}"
    [ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}"

    local msg
    for msg; do
        echo "$script_name: $prefix: $msg"
    done
}

readonly ssh_dir=/ssh
readonly ssh_client_key="$ssh_dir/client_key"
readonly ssh_client_password='password'

spawn_ssh_agent() {
    if ! command -v ssh-agent > /dev/null 2>&1; then
        dump "could not find ssh-agent" >&2
        return 1
    fi

    local output
    output="$( ssh-agent -s -a "$ssh_dir/agent.sock" )"

    echo "$output"
    eval "$output"

    if [ -z "${SSH_AGENT_PID:+x}" ]; then
        dump "could not start ssh-agent" >&2
        return 1
    fi
}

setup_ssh_agent() {
    echo
    echo ----------------------------------------------------------------------
    echo Setting up ssh-agent
    echo ----------------------------------------------------------------------

    spawn_ssh_agent

    local askpass_path
    askpass_path="$( mktemp )"
    chmod 0700 -- "$askpass_path"

    echo "#!/usr/bin/env sh" >> "$askpass_path"
    printf -- 'echo %q' "$ssh_client_password" >> "$askpass_path"

    SSH_ASKPASS="$askpass_path" SSH_ASKPASS_REQUIRE=force DISPLAY= ssh-add "$ssh_client_key" < /dev/null
}

main() {
    setup_ssh_agent
    exec "$@"
}

main "$@"