github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/containers/ddev-ssh-agent/files/entry.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) Andreas Urbanski, 2018
     3  #
     4  # Permission is hereby granted, free of charge, to any person obtaining a
     5  # copy of this software and associated documentation files (the "Software"),
     6  # to deal in the Software without restriction, including without limitation
     7  # the rights to use, copy, modify, merge, publish, distribute, sublicense,
     8  # and/or sell copies of the Software, and to permit persons to whom the
     9  # Software is furnished to do so, subject to the following conditions:
    10  #
    11  # The above copyright notice and this permission notice shall be included
    12  # in all copies or substantial portions of the Software.
    13  #
    14  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    15  # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    17  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    20  # THE SOFTWARE.
    21  
    22  
    23  set -eo pipefail
    24  rm -f /tmp/healthy
    25  
    26  # Print a debug message if debug mode is on ($DEBUG is not empty)
    27  # @param message
    28  debug_msg ()
    29  {
    30    if [ -n "$DEBUG" ]; then
    31      echo "$@"
    32    fi
    33  }
    34  
    35  mkdir -p $SSH_KEY_DIR && ( chmod 700 $SSH_KEY_DIR || true )
    36  mkdir -p $SOCKET_DIR
    37  
    38  case "$1" in
    39    # Start ssh-agent
    40    ssh-agent)
    41  
    42    # Create proxy-socket for ssh-agent (to give everyone acceess to the ssh-agent socket)
    43    echo "Creating a proxy socket..."
    44    rm -f ${SSH_AUTH_SOCK} ${SSH_AUTH_PROXY_SOCK}
    45    echo "Running socat UNIX-LISTEN:${SSH_AUTH_PROXY_SOCK},perm=0666,fork UNIX-CONNECT:${SSH_AUTH_SOCK}"
    46    socat UNIX-LISTEN:${SSH_AUTH_PROXY_SOCK},perm=0666,fork UNIX-CONNECT:${SSH_AUTH_SOCK} &
    47  
    48    echo "Launching ssh-agent..."
    49    exec /usr/bin/ssh-agent -a ${SSH_AUTH_SOCK} -d
    50    ;;
    51  
    52  	# Manage SSH identities
    53    ssh-add)
    54    shift # remove argument from array
    55  
    56    # Add keys id_rsa and id_dsa from /root/.ssh using cat so it will work regardless of permissions
    57    # docker toolbox mounts files as 0777, which ruins the normal technique.
    58    set +o errexit
    59    keyfiles=$(file ~/.ssh/* | awk -F: '/private key/ {  print $1 }')
    60    set -o errexit
    61    if [ ! -z "$keyfiles" ] ; then
    62        for key in $keyfiles; do
    63          perm=$(stat -c %a "$key")
    64          if [ $perm = "777" ] ; then
    65              echo "Please add password for ${key}..."
    66              cat $key | ssh-add -k -
    67          else
    68              ssh-add $key
    69          fi
    70        done
    71    else
    72      echo "No private keys were found in the directory."
    73    fi
    74  
    75    # Return first command exit code
    76    exit ${PIPESTATUS[0]}
    77    ;;
    78  	*)
    79    exec $@
    80    ;;
    81  esac