Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store private key on Heroku?

I have a flask app hosted on Heroku that needs to run commands on an AWS EC2 instance (Amazon Linux AMI) using boto.cmdshell. A couple of questions:

  1. Is using a key pair to access the EC2 instance the best practice? Or is using username/password better?
  2. If using a key pair is the preferred method, what's the best practice on managing/storing private keys on Heroku? Obviously putting the private key in git is not an option.

Thanks.

like image 703
Jason Avatar asked Jan 05 '13 22:01

Jason


People also ask

Where are Heroku secrets stored?

Store your secrets in environment variables. A library like dotenv can seamlessly load and make use of these variables, provided they're accessible in a secure location. Another option is to use a product like Hashicorp Vault, which allows your application to manage secrets through a configurable CLI.

What is security key in Heroku?

Security keys are small physical devices that are easy to use because there's nothing to install and no codes to enter. This is a great option if you are unable to use a mobile device for logging in to Heroku. Options for security keys include Yubikey or Google Titan Key.


1 Answers

What I was looking for was guidance on how to deal with private keys. Both @DrewV and @yfeldblum pointed me to the right direction. I ended up turning my private key into a string and storing it in a Heroku config variables.

If anyone is looking to do something similar, here's a sample code snippit using paramiko:

import paramiko, base64
import StringIO
import os

key = paramiko.RSAKey.from_private_key(StringIO.StringIO(str(os.environ.get("AWS_PRIVATE_KEY"))))
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(str(os.environ.get("EC2_PUBLIC_DNS")), username='ec2-user', pkey=key)
stdin, stdout, stderr = ssh.exec_command('ps')

for line in stdout:
    print '... ' + line.strip('\n')
ssh.close()

Thanks to @DrewV and @yfeldblum for helping (upvote for both).

like image 98
Jason Avatar answered Oct 07 '22 00:10

Jason