<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manuel Bosi</title>
    <description>The latest articles on DEV Community by Manuel Bosi (@manuelbosi).</description>
    <link>https://dev.to/manuelbosi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F630338%2F2c05b3b9-cdb3-49f4-81e9-113c10d62b03.jpg</url>
      <title>DEV Community: Manuel Bosi</title>
      <link>https://dev.to/manuelbosi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manuelbosi"/>
    <language>en</language>
    <item>
      <title>Bitbucket pipelines - configure SSH Keys</title>
      <dc:creator>Manuel Bosi</dc:creator>
      <pubDate>Mon, 13 Sep 2021 21:17:45 +0000</pubDate>
      <link>https://dev.to/manuelbosi/bitbucket-pipelines-configure-ssh-keys-1l56</link>
      <guid>https://dev.to/manuelbosi/bitbucket-pipelines-configure-ssh-keys-1l56</guid>
      <description>&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Intro&lt;/li&gt;
&lt;li&gt;Why SSH Keys?&lt;/li&gt;
&lt;li&gt;Single Key vs Multiple Keys?&lt;/li&gt;
&lt;li&gt;Single SSH Key&lt;/li&gt;
&lt;li&gt;Multiple SSH Keys&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Intro &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Hi there!&lt;br&gt;
I'm writing this tutorial because I found the bitbucket documentation a little bit confusing explaining the configuration of ssh keys in pipelines. I hope you will find this tutorial useful.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;N.B.: to follow this tutorial you must already have a bitbucket repository where pipeline exists (no needs to be a complex pipeline) or is at least enabled. I assume you have a basic knowledge of pipelines and git&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;LET'S START!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why SSH Keys? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In some of my pipelines I needed to connect to my remote server and run commands/scripts. Bitbucket pipeline allows you to do that. You're on a docker container, based on the image indicated in the pipeline file (tipically ubuntu or another linux distro based) so you can install ssh package on the image, run ssh command to connect to the server and execute commands/scripts directly on the server.&lt;br&gt;
I know you can automate the login passing your password to ssh command but I don't recommend it.&lt;br&gt;
When the pipeline starts, you can't type password or run commands.&lt;br&gt;
SSH Keys avoid you typing password each time you connect to the server and above all are more secure than passwords.&lt;/p&gt;

&lt;p&gt;You can setup ssh keys in your pipeline in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single key&lt;/li&gt;
&lt;li&gt;Multiple keys &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Single Key vs Multiple Keys? &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The difference between choosing single key or multiple keys depends on how you're working.&lt;br&gt;
Usually I associate each branch of the repository with a specific virtual host on my server and I prefer to have multiple keys due to security reasons.&lt;br&gt;
Single key is automatically injected into the docker container when the pipeline starts.&lt;br&gt;
For multiple keys instead, you need to configure the keys manually through little differents steps.&lt;br&gt;
But don't worry and let's start with the single key. &lt;/p&gt;

&lt;h2&gt;
  
  
  Single SSH Keys &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To setup single ssh key we need to generate ssh keys pair. Go to:&lt;br&gt;
&lt;em&gt;Repository Settings&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnsyo7xiwe1nfsp9ygeo6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnsyo7xiwe1nfsp9ygeo6.png" alt="Repository Settings"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;SSH Keys&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyij28am34clxdh9kgoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyij28am34clxdh9kgoh.png" alt="SSH Keys"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Generate Keys&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtxxutdn91qrafya8390.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhtxxutdn91qrafya8390.png" alt="Generate Keys"&gt;&lt;/a&gt;&lt;br&gt;
Now you should have an ssh keys pair (private and public).&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3f5butw6uziv6iyvtez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3f5butw6uziv6iyvtez.png" alt="SSh Keys Pair"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Login into your remote server and cd into the hidden ssh folder in the user's home&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; ~/.ssh/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Take the public key generated previously and copy it into a file called &lt;em&gt;authorized_keys&lt;/em&gt; on the server in the &lt;em&gt;.ssh&lt;/em&gt; folder&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Known Hosts&lt;/strong&gt;&lt;br&gt;
Return to &lt;em&gt;Repository Settings&lt;/em&gt; -&amp;gt; &lt;em&gt;SSH Keys&lt;/em&gt;.&lt;br&gt;
In the &lt;em&gt;Known Hosts&lt;/em&gt; section, type &lt;em&gt;Host address&lt;/em&gt; then click &lt;em&gt;FETCH&lt;/em&gt; and wait for response.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm7ienehcxqqvd2371e4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsm7ienehcxqqvd2371e4.png" alt="Known Hosts"&gt;&lt;/a&gt;&lt;br&gt;
When you got response (the fingerprint of server) click the blue button &lt;em&gt;ADD HOST&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;IMPORTANT&lt;/em&gt;&lt;/strong&gt;: &lt;em&gt;if your server uses a different port than default (22) to connect via ssh you can type SERVERIP:PORT in the Host address field&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Bitbucket automatically injects in the known_host file of docker container the fingerprint of the server and bypass the message below while connecting via ssh for the first time (remember: when pipeline has already started you cannot type or accept anything).&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffv3lgzbfokb89hqw4tng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffv3lgzbfokb89hqw4tng.png" alt="Fingerprint warning"&gt;&lt;/a&gt;&lt;br&gt;
Now in your pipeline file you can use ssh to connect to your server.&lt;br&gt;
Between quotes you can add one or more commands you want to run on your server.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

ssh &lt;span class="nt"&gt;-u&lt;/span&gt; USERNAME &lt;span class="nt"&gt;-h&lt;/span&gt; SERVER &lt;span class="nt"&gt;-p&lt;/span&gt; 22 &lt;span class="s1"&gt;'echo "hello from $USER on remote server"'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Pulling changes&lt;/strong&gt;&lt;br&gt;
Sometimes you may need to pull changes from the repository using &lt;em&gt;git pull&lt;/em&gt;. In order to do this, login into your server through the terminal and run the command below to generate private and public keys for your server.&lt;br&gt;
Press &lt;em&gt;enter&lt;/em&gt; for all questions (filename, passphrase)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

ssh-keygen


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Copy all the content of id_rsa.pub file (public key) and paste it into the &lt;strong&gt;key&lt;/strong&gt; field in &lt;em&gt;Repository Settings&lt;/em&gt; -&amp;gt; &lt;em&gt;Access Keys&lt;/em&gt; -&amp;gt; &lt;em&gt;Add Key&lt;/em&gt;&lt;br&gt;
Finally test the connection running this command on your server.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

ssh &lt;span class="nt"&gt;-T&lt;/span&gt; git@bitbucket.org


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should see an output says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;authenticated via a deploy key.&lt;br&gt;
You can use git to connect to Bitbucket. Shell access is disabled.&lt;br&gt;
This deploy key has read access to the following repositories:&lt;br&gt;
accountName/repositoryName&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Multiple SSH Keys &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You saw how to configure single ssh key, now let's see how to setup multiple ssh keys.&lt;/p&gt;

&lt;p&gt;Login in your server through terminal and generate ssh keys pair by running this command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

ssh-keygen


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Convert the private key (id_rsa) in base64 format, because the bitbucket repository variables don't accept break lines&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;base64&lt;/span&gt; &lt;span class="nt"&gt;-w&lt;/span&gt; 0 &amp;lt; id_rsa


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Take the output, paste it into a repository variable (named SSH_KEY) in &lt;em&gt;Repository Settings&lt;/em&gt; -&amp;gt; &lt;em&gt;Deployments&lt;/em&gt; -&amp;gt; &lt;em&gt;your deployment environment&lt;/em&gt;, let &lt;em&gt;Secured&lt;/em&gt; checked and click &lt;em&gt;ADD&lt;/em&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdepw64qv7nziwej087m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdepw64qv7nziwej087m.png" alt="Base64 String"&gt;&lt;/a&gt;&lt;br&gt;
Repository variables are useful, because in your pipeline file you don't need to write sensitive data like server ip, username, port etc...&lt;/p&gt;

&lt;p&gt;Add 3 variables (keep the secured checkbox checked):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;USER with user&lt;/li&gt;
&lt;li&gt;SERVER with serverIp&lt;/li&gt;
&lt;li&gt;PORT with serverPort&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More informations about repository variables:&lt;br&gt;
&lt;a href="https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Reference-variables-in-your-pipeline" rel="noopener noreferrer"&gt;https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Reference-variables-in-your-pipeline&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Return to &lt;em&gt;Repository Settings&lt;/em&gt; -&amp;gt; &lt;em&gt;SSH Keys&lt;/em&gt;.&lt;br&gt;
In the &lt;em&gt;Known Hosts&lt;/em&gt; section, type &lt;em&gt;Host address&lt;/em&gt; then click &lt;em&gt;FETCH&lt;/em&gt; and after response appears click &lt;em&gt;ADD HOST&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Setup your bitbucket-pipelines.yml like the code below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in &lt;em&gt;deployment&lt;/em&gt; type name of your deployment environment where you previously added the base64 string variable (in my case test)&lt;/li&gt;
&lt;li&gt;in &lt;em&gt;script&lt;/em&gt; add the umask string and the ssh command to test connection.
```yaml
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;image: atlassian/default-image:latest&lt;/p&gt;

&lt;p&gt;pipelines:&lt;br&gt;
  default:&lt;br&gt;
    - step:&lt;br&gt;
        name: Deploy to test&lt;br&gt;
        deployment: test&lt;br&gt;
        script:&lt;br&gt;
          # Decode base64 private ssh key string variable and put content in the private key file on docker container&lt;br&gt;
          - (umask  077 ; echo $SSH_KEY | base64 --decode &amp;gt; ~/.ssh/id_rsa)&lt;br&gt;
          # Test ssh connection&lt;br&gt;
          - ssh $USER@$SERVER -p$PORT 'echo "hello from $USER on remote server"'&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Follow the *single key* procedure about **Pulling changes** if you need to pull changes from repository using *git pull*.

You can repeat multiple times the steps for multiple keys.
You can change deployment environment with different variables and connect to another server (remember: you need to duplicate the code after *default* keyword in pipeline file and change data).
Bitbucket doesn't allow you to use the same deployment environment in a different step.
With bitbucket free plan you can add up to 10 deployment environments.

I'll be happy to help you out if something went wrong :smile:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>devops</category>
      <category>tutorial</category>
      <category>development</category>
      <category>version</category>
    </item>
  </channel>
</rss>
