Self-Hosting Bitwarden On AWS
Published: 2023-01-03 21:52:29

<!-- wp:image {"align":"center","id":763,"width":593,"height":593,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full is-resized"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/bitwardenaws.png" alt="" class="wp-image-763" width="593" height="593"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><a href="https://github.com/linuxtek-canada/Projects/tree/main/bitwarden-aws"><strong>Quick Start - Github Project</strong></a></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Introduction</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I've used LastPass for quite a few years now as my primary password storage service.  I randomly generate most of the passwords for websites, and I don't even know what most of them are, so I depend on my LastPass Vault quite heavily.  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I am using Multi-Factor Authentication, and even paid for LastPass Premium for a while so I could <a href="https://www.lastpass.com/yubico">use a Yubikey</a> for access.  After doing this for a while, I switched to using Google Authenticator for MFA.  It concerns me that while MFA can be added as a layer to access the LastPass vault using the browser plugin or app, there is no extra encryption on the Vault itself.  That means that if the data ever got stolen, the MFA layers would be useless, and only the master password would need to be cracked to access the passwords and other Vault data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In December 2022, LastPass <a href="https://blog.lastpass.com/2022/12/notice-of-recent-security-incident/">released details</a> around a recent security incident from August 2022, where an unauthorized third party gained access to backups of production data, including vault data.  While the vaults are encrypted and secured with 256-bit AES encryption, the master password can be brute-forced to decrypt the vault.  This <a href="https://en.wikipedia.org/wiki/LastPass#Security_incidents">isn't the first security incident</a> suffered by LastPass, but it is very concerning to me in this case that vault data was stolen.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://www.linkedin.com/in/lawrencesystems/">Tom Lawrence</a> from <a href="https://lawrencesystems.com/">Lawrence Systems</a> posted a number of YouTube videos on the topic, which drew my attention to the urgency of the problem.  I would highly recommend watching this video for more details:</p>
<!-- /wp:paragraph -->

<!-- wp:embed {"url":"https://youtu.be/x2K2h6W4pTw","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://youtu.be/x2K2h6W4pTw
</div></figure>
<!-- /wp:embed -->

<!-- wp:heading -->
<h2>Considering Alternate Password Managers</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I had been considering moving to a self-hosted password manager for a while, but this latest security incident prompted me to take more urgent action to do so, and rotate all of my site passwords in the process.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I've used <a href="https://1password.com/">1Password</a> for work, and it didn't seem too bad, but it isn't an open source platform, and doesn't look like it can be self hosted.  There is a <a href="https://survey.1password.com/self-host/">survey you can submit</a> to them on the topic, if this is something you'd like them to do.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I also looked at <a href="https://keepass.info/" data-type="URL" data-id="https://keepass.info/">KeePass Password Safe</a> and <a href="https://keepassxc.org/">KeePassXC</a>, which are open source.  KeePassXC does have a browser plugin, but the password database is stored as a file, which makes it trickier to maintain as it would need to be manually synchronized between computers.  Other products like Bitwarden and VaultWarden run as a full server, and have browser plugins and phone applications that can talk to it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Tom also did a separate video on this topic:</p>
<!-- /wp:paragraph -->

<!-- wp:embed {"url":"https://youtu.be/46aiqnEHOVU","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://youtu.be/46aiqnEHOVU
</div></figure>
<!-- /wp:embed -->

<!-- wp:paragraph -->
<p>I considered using <a href="https://github.com/dani-garcia/vaultwarden">VaultWarden</a>, which is a free, alternative implementation of Bitwarden written in Rust, but the fact that Bitwarden is run through a number of Docker containers appeals to me, and I can dedicate the resources in my homelab, or in AWS, to run it.  Bitwarden also has a number of other features that appeal to me, and supporting the original project also seemed to make sense.  Using Bitwarden also would maintain the best compatibility with browser plugins and mobile applications.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Setting Up The AWS Environment</h2>
<!-- /wp:heading -->

<!-- wp:image {"align":"center","id":767,"width":788,"height":472,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large is-resized"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/terraformaws-1024x614.png" alt="" class="wp-image-767" width="788" height="472"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now that I had decided on setting up Bitwarden, I had to build an AWS environment to run it on.  I decided to do this in Terraform, as it lets me document Infrastructure-as-Code, and makes it easy to set up and tear down.  I've put all of the code needed with instructions and minimal tweaks in this <a href="https://github.com/linuxtek-canada/Projects/tree/main/bitwarden-aws">Github project folder</a> for others to use as well, if it is helpful.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Most of the AWS environment setup is pretty generic.  The real configuration is in the <a href="https://github.com/linuxtek-canada/Projects/blob/main/bitwarden-aws/terraform/userdata.sh">userdata.sh</a> file which provisions the EC2 instance after Terraform brings it up.  I considered doing some of this work with Ansible using a <a href="https://developer.hashicorp.com/terraform/language/resources/provisioners/remote-exec">remote-exec provisioner</a>, but this is really only as a last resort and using the <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html">User Data to run Linux commands</a> after launch is supported by, and recommended for Amazon EC2.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I chose not to automate the Bitwarden installation itself, as there is a bit of a chicken and egg situation regarding the prerequisites for installing Bitwarden.  More on that later.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Bitwarden Prerequisites</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>One of the <a href="https://bitwarden.com/help/install-on-premise-linux/">requirements for installing</a> Bitwarden is a domain name with DNS records.  This could be created in <a href="https://aws.amazon.com/route53/">Amazon Route 53</a> if you are creating a new domain or subdomain already managed in AWS.  I am using <a href="https://www.cloudflare.com">CloudFlare</a> for DNS for this domain, so I had to create an A record for Bitwarden to use, pointing to the public IP address of the EC2 instance created by Terraform:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":768,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/cloudflare-ar-1024x225.png" alt="" class="wp-image-768"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>I also needed an SMTP Server for Bitwarden to use to send email verification links.  For this purpose, using an external SMTP server is easier than setting up your own mail server.  <a href="https://support.google.com/a/answer/176600?hl=en">GMail has options</a> to use their SMTP servers with authentication, but for this test, I decided to use <a href="https://aws.amazon.com/ses/">Amazon Simple Email Service</a> (SES).  In order to send email from the linuxtek.ca domain, I needed to add the domain as a <a href="https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html">verified identity</a>.  I used the <a href="https://docs.aws.amazon.com/ses/latest/dg/send-email-authentication-dkim-easy.html">Easy DomainKeys Identified Mail</a> (DKIM) wizard, and added the required CNAME, MX, and TXT DNS records in CloudFlare:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":769,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/cloudflare-dkim-1024x154.png" alt="" class="wp-image-769"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Shortly after this was done, SES was able to verify the identity successfully.  I also added my personal email address I would be testing with under verified identities, as by default, the Amazon SES account <a href="https://docs.aws.amazon.com/ses/latest/dg/request-production-access.html">is in a sandbox</a>, and is only able to send email to verified addresses.  If you will be using this in production, you will need to submit a support case to Amazon in order to move to production.  For the purposes of this test, this was not necessary.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In order to authenticate with Amazon's SMTP server, I also needed to create an SMTP username and password.  This can be done under <strong>Amazon SES &gt; SMTP Settings</strong>, and basically creates an IAM user which only has access to write to SES.  Make sure to write down the username and password as they are not available after creation.  Also pro tip - the IAM username is different from the SMTP username, so make sure to record both.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Installing Bitwarden</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><a href="https://www.linkedin.com/in/lawrencesystems/">Tom Lawrence</a> has done a great video on installing Self-Hosted Bitwarden, and if you want a good walkthrough I'd recommend giving it a view:</p>
<!-- /wp:paragraph -->

<!-- wp:embed {"url":"https://youtu.be/SSLGa0LjTrA","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://youtu.be/SSLGa0LjTrA
</div></figure>
<!-- /wp:embed -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The <a href="https://bitwarden.com/help/install-on-premise-linux/">Bitwarden installation documentation</a> for on-premise Linux installation is also very easy to follow, and a recommended read.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I will go through the steps I performed, as some of the installation steps like creating the bitwarden user are taken care of by Terraform.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I SSH'd into the AWS EC2 instance as the <code>admin</code> user, using the keypair that was inserted into the machine as part of the setup.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once logged in, I switched to the Bitwarden user using <code>sudo su bitwarden</code>.  If you want you can specify a password for this user to log in directy, but I prefer to use my keypair and only use the <code>admin</code> user.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the <code>/opt/bitwarden</code> directory, the bitwarden.sh file is already present, so I ran that using <code>./bitwarden.sh install</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I entered the domain name that I had created in CloudFlare:  <code>bitwarden.linuxtek.ca</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For this test, I wanted to use a Let's Encrypt SSL certificate, so I said "yes" on the next prompt, and entered my personal email address to send expiration reminders to.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":770,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/bitwarden-install1.png" alt="" class="wp-image-770"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>When prompted, I entered <code>linuxtek-vault</code> for the name of the Bitwarden database.  After this, the installer pulled down all of the required Docker containers.  I then entered my Bitwarden Installation ID and Key, which I generated at <a href="https://bitwarden.com/host/">this link</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":771,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/bitwarden-install2.png" alt="" class="wp-image-771"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After entering this, the installer generated some encryption keys, and completed the install.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I then modified the <code>global.override.env</code> file in <code>/opt/bitwarden/bwdata/env</code> to configure these lines:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>globalSettings__mail__replyToEmail=no-reply@bitwarden.linuxtek.ca
globalSettings__mail__smtp__host=REPLACE
globalSettings__mail__smtp__port=587
globalSettings__mail__smtp__ssl=false
globalSettings__mail__smtp__username=REPLACE
globalSettings__mail__smtp__password=REPLACE
globalSettings__disableUserRegistration=false
globalSettings__hibpApiKey=REPLACE
adminSettings__admins=</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In my case, I added the SMTP endpoint address for Amazon SES, from the <strong>Amazon SES &gt; SMTP Settings</strong> page.  I used SMTP port 587, and specified to use SSL.  I entered in the SMTP username and password I generated and recorded when generating my SMTP credentials.  For this test, I left <code>disableUserRegistration=false</code> to allow registration, but once I was set up, I would disable that.  For <code>adminSettings__admins</code>, I put in my personal email address.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once I finished editing the file, as per the installation guide, I switched back to <code>/opt/bitwarden</code>, and  ran an <code>./bitwarden.sh rebuild</code> and <code>./bitwarden.sh restart</code> to ensure the settings took effect.  Many of the Docker containers were pulled down, extracted, and started; so it took a few minutes to completely start up.  I also ran <code>./bitwarden.sh updateself</code> and <code>./bitwarden.sh update</code> to make sure I had the latest builds.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once complete, I was able to run <code>docker ps</code> to see all of the containers running:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":784,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/bitwarden-install3-1-1024x231.png" alt="" class="wp-image-784"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>And by visiting the domain name I had set, I was presented with the Bitwarden login screen:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":774,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/Screenshot_2023-01-03_16-15-12.png" alt="" class="wp-image-774"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After creating an account with my personal email address, I logged into the Vault and saw the account would need to be verified via email address:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":775,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-full"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/Screenshot_2023-01-03_16-16-46.png" alt="" class="wp-image-775"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Clicking the "Send email" button successfully sent a verification email via Amazon SES to my personal email.  Once I clicked the link, I entered my credentials again, and the account was verified, and I had full access to all features:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":777,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://www.linuxtek.ca/wp-content/uploads/2023/01/Screenshot_2023-01-03_16-23-56-1024x634.png" alt="" class="wp-image-777"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After testing, I ran a <code>terraform destroy</code> on the environment, and removed all of the DNS records from CloudFlare, to prevent any abuse of my account (just a heads up for anyone looking to exploit the system).</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Troubleshooting</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I did run into a few issues while setting this up initially, mostly around the Amazon SES configuration.  Mainly this was due to using the IAM username as the SMTP username, and they are not the same.  Definitely make sure you record both once you set up the credentials.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To troubleshoot the verification email, use the logs under <code>/opt/bitwarden/bwdata/logs/api/Api</code>.  The errors were fairly clear when there I had an SMTP configuration issue.  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, be sure to do a <code>./bitwarden.sh rebuild</code> and <code>./bitwarden.sh restart</code> any time you make changes to the Bitwarden configuration, to ensure the settings are applied.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, when using SES, keep in mind that in Sandbox mode, you won't be able to send to any emails to addresses not in your verification list.  Initially I thought I would need to switch to a Production mode to test, but all I needed to do was verify that email address ahead of time.  Thanks to <strong>Traian Ciobanu</strong> for the help troubleshooting this.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When I initially decided to set Bitwarden up to self host in AWS, I had ideas to share the service with my family.  Eventually I decided it was too much of a risk to keep the server open on the internet, and locking it down would be costly and complex.  Instead, I will be hosting a Bitwarden server in my homelab behind my firewall, and access it using VPN if needed, to avoid having to open ports.  </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Hopefully the resources and information in this article can help others who are looking to self host, and you can decide if hosting in AWS makes sense for you.  </p>
<!-- /wp:paragraph -->