CI di GitLab: SSH fallito, impossibile autenticare la chiave privata

Aug 18 2020

Ho seguito questo collegamento per provare ad accedere in SSH al mio server in Gitlab-CI. Per le chiavi SSH, sono entrato nel server e ho generato le chiavi pubblica e privata. La chiave privata viene estratta nelle variabili env CI / CD di GitLab.

Il modello YAML è il seguente, copiato principalmente dal collegamento.

    image: docker:19.03.8
      services:
        - docker:19.03.8-dind

    deployment:
      variables:
        ip: <ip-address>
      script:
        - apk add --update openssh-client sshpass
        - eval $(ssh-agent -s) - echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - export SSHPASS=$AWS_PASSWORD - sshpass -e ssh -o StrictHostKeyChecking=no -vvv ubuntu@$ip echo testing

Tuttavia, ho riscontrato un errore durante il tentativo di accedere alla chiave privata.

    debug1: Authentications that can continue: publickey,password
    debug1: Trying private key: /root/.ssh/id_rsa
    debug3: no such identity: /root/.ssh/id_rsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_dsa
    debug3: no such identity: /root/.ssh/id_dsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_ecdsa
    debug3: no such identity: /root/.ssh/id_ecdsa: No such file or directory
    debug1: Trying private key: /root/.ssh/id_ed25519
    debug3: no such identity: /root/.ssh/id_ed25519: No such file or directory
    debug1: Trying private key: /root/.ssh/id_xmss
    debug3: no such identity: /root/.ssh/id_xmss: No such file or directory
    debug2: we did not send a packet, disable method
    debug3: authmethod_lookup password
    debug3: remaining preferred: ,password
    debug3: authmethod_is_enabled password
    debug1: Next authentication method: password
    debug3: send packet: type 50
    debug2: we sent a password packet, wait for reply
    debug3: receive packet: type 51
    debug1: Authentications that can continue: publickey,password
    Permission denied, please try again.

Sto usando i corridori condivisi di gitlab, se questo aiuta.

[Aggiornare]

Ho dimenticato di aggiungere che nel server che voglio connettere, ho aggiunto le chiavi pubbliche che ho generato id_rsa.pubnei authorized_keysfile.

[Modifica 1]

Come suggerito, ho aggiunto gli host conosciuti utilizzando ssh-keyscan per copiare l'output come variabile $ SSH_KNOWN_HOSTS. Di seguito il file yaml aggiornato. Tuttavia ho riscontrato lo stesso errore.

    deployment:
      variables:
        ip: <ip-address>
      script:
        - apk add --update openssh-client sshpass
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | ssh-add - > /dev/null - mkdir -p ~/.ssh - chmod 700 ~/.ssh - touch ~/.ssh/known_hosts - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - export SSHPASS=$AWS_PASSWORD - sshpass -e ssh -o StrictHostKeyChecking=no -vvv ubuntu@$ip echo testing

Risposte

2 DV82XL Aug 18 2020 at 12:45

Non ne sono sicuro sshpass, dato che di solito uso chiavi pubbliche / private. Ecco un esempio di un lavoro che configurerei per eseguire SCP/ SSHcomandi su server remoti:

deploy:
  stage: deploy
variables:
  hostname: app-dev
before_script:
  # optional step if you decide to use a hostname instead of IP address
  - cp -f ./network/etc/hosts /etc/hosts
  # Setup SSH
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - eval $(ssh-agent -s) - ssh-add <(cat $SSH_PRIVATE_KEY)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan $HOSTNAME >> ~/.ssh/known_hosts - chmod 644 ~/.ssh/known_hosts script: # Copy files and execute commands - scp ./scripts/install_package.sh root@$HOSTNAME:/tmp/deploy
  - ssh root@$HOSTNAME "/tmp/deploy/install_package.sh && exit"

Prima di eseguire la pipeline, è necessario eseguire le seguenti operazioni:

  1. Genera coppie di chiavi ssh usando ssh-keygen. Non utilizzare una passphrase. La chiave pubblica termina con .pub, la chiave privata non ha estensione.
  2. SSH sul server remoto, copia il contenuto della chiave pubblica in~/.ssh/authorized_keys
  3. Copia il contenuto della tua chiave privata in un file Environment Variables di GitLab chiamatoSSH_PRIVATE_KEY
  4. Se utilizzi una $HOSTNAMEvariabile di ambiente, definisci la variabile nella pipeline e aggiungi l'IP / nome host al /etc/hostsfile nel contenitore della pipeline. Altrimenti, usa semplicemente un indirizzo IP.