host($run); // Idempotent: already trusted (host key pinned) and password already scrubbed. if (filled($host->ssh_host_key) && blank($run->context('root_password'))) { return StepResult::advance(); } $publicKey = trim(ProvisioningSettings::sshPublicKey()); $password = Crypt::decryptString((string) $run->context('root_password')); $this->shell->connectWithPassword($host->public_ip, 'root', $password); $this->shell->run('mkdir -p /root/.ssh && chmod 700 /root/.ssh'); $this->shell->run( 'grep -qxF '.escapeshellarg($publicKey).' /root/.ssh/authorized_keys 2>/dev/null '. '|| echo '.escapeshellarg($publicKey).' >> /root/.ssh/authorized_keys' ); $host->update(['ssh_host_key' => $this->shell->hostKeyFingerprint()]); // Verify the key works, then remove the password from state. $this->keyLogin($this->shell, $host); $run->forgetContext('root_password'); return StepResult::advance(); } }