├── LICENSE ├── bin ├── compile └── detect └── readme.md /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ryan Smith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /bin/compile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # bin/compile 3 | 4 | # Generates an SSH config file for connections if a config var exists. 5 | 6 | ENV_DIR=$3 7 | 8 | if [[ -f $ENV_DIR/CUSTOM_SSH_KEY && -f $ENV_DIR/CUSTOM_SSH_KEY_HOSTS ]]; then 9 | 10 | echo "" >&1 11 | 12 | # Ensure we have an ssh folder 13 | if [ ! -d ~/.ssh ]; then 14 | mkdir -p ~/.ssh 15 | chmod 700 ~/.ssh 16 | fi 17 | 18 | # Load the private key into custom_key file. 19 | base64 --decode $ENV_DIR/CUSTOM_SSH_KEY > ~/.ssh/custom_key 20 | 21 | # Change the permissions on the file to 22 | # be read-only for this user. 23 | chmod 400 ~/.ssh/custom_key 24 | 25 | # Split $CUSTOM_SSH_KEY_HOSTS 26 | IFS=',' ;for element in `cat $ENV_DIR/CUSTOM_SSH_KEY_HOSTS`; 27 | do 28 | echo -e "Host $element\n"\ 29 | " IdentityFile ~/.ssh/custom_key\n"\ 30 | " IdentitiesOnly yes\n"\ 31 | " UserKnownHostsFile=/dev/null\n"\ 32 | " StrictHostKeyChecking no"\ 33 | >> ~/.ssh/config 34 | done 35 | 36 | echo "-----> Successfully added custom SSH key" 37 | 38 | fi 39 | -------------------------------------------------------------------------------- /bin/detect: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # bin/detect 3 | 4 | echo "CustomSSHKey" 5 | exit 0 6 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Heroku Buildpack: Custom SSH key 2 | 3 | Use *Custom SSH key buildpack* if you need to, for example, download a dependency stored in a private repository. 4 | 5 | Based on [http://stackoverflow.com/a/29677091/3303182](http://stackoverflow.com/a/29677091/3303182). 6 | 7 | ## Usage 8 | 9 | - Add the buildpack to your app: 10 | `heroku buildpacks:add --index 1 https://github.com/simon0191/custom-ssh-key-buildpack` 11 | 12 | - Generate a new SSH key (https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/) 13 | 14 | For this example I will assume that you named the key `deploy_key`. 15 | 16 | - Add the ssh key to your private repository account. 17 | 18 | * Github: https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/ 19 | 20 | * Bitbucket: https://confluence.atlassian.com/bitbucket/add-an-ssh-key-to-an-account-302811853.html 21 | 22 | - Encode the private key as a base64 string and add it as the `CUSTOM_SSH_KEY` environment variable of the heroku app. 23 | 24 | - Make a comma separated list of the hosts for which the ssh key should be used and add it as the `CUSTOM_SSH_KEY_HOSTS` environment variable of the heroku app. 25 | 26 | ``` 27 | # OSX 28 | $ heroku config:set CUSTOM_SSH_KEY=$(base64 --input ~/.ssh/deploy_key) CUSTOM_SSH_KEY_HOSTS=bitbucket.org,github.com 29 | 30 | # Linux 31 | $ heroku config:set CUSTOM_SSH_KEY=$(base64 ~/.ssh/deploy_key) CUSTOM_SSH_KEY_HOSTS=bitbucket.org,github.com 32 | ``` 33 | 34 | - Deploy your app and enjoy :) 35 | 36 | ## Motivation 37 | 38 | I needed to install dependencies stored in private repositories but I didn't want to hardcode passwords in the code. 39 | I found a solution in [StackOverflow](http://stackoverflow.com/a/29677091/3303182) but it only worked for the node buildpack 40 | so I decided to create this technology agnostic buildpack. 41 | --------------------------------------------------------------------------------