Add Ansible-based SSH bastion host environment

Add files to show how to set up an SSH bastion host test environment using Ansible instead of the Vagrant file/shell provisioners.
This commit is contained in:
Scott S. Lowe 2016-10-25 21:32:18 -06:00
parent 4538551e92
commit f0f6e92b19
10 changed files with 270 additions and 0 deletions

View file

@ -0,0 +1,45 @@
# SSH Bastion Hosts
These files were created to allow users to use Vagrant ([http://www.vagrantup.com](http://www.vagrantup.com)) to quickly and easily spin up an environment for learning and working with SSH bastion hosts and proxy commands. This environment was created and tested using VMware Fusion 7.1.2, Vagrant 1.7.4, and the Vagrant VMware plugin.
## Contents
* **bastion\_rsa** and **bastion\_rsa.pub**: Private/public key pair for accessing the SSH bastion host. The password for this key pair is `password` (all lowercase). These files are automatically installed by Vagrant into the correct VMs.
* **bastion-hosts**: This snippet of an `/etc/hosts` file contains IP addresses for the remote SSH destinations. It is added to `/etc/hosts` on the bastion host automatically by Vagrant during provisioning.
* **client-ssh-config**: This is an SSH configuration file that sets up the SSH bastion host configuration. This file is installed automatically by Vagrant into the client VM, but must be edited to properly reflect the IP address assigned to the bastion host (see the instructions below).
* **README.md**: This file you're currently reading.
* **remote\_rsa** and **remote\_rsa.pub**: Private/public key pair for accessing the remote SSH nodes behind the bastion host. The password for this key pair is `secure` (all lowercase). The `Vagrantfile` will automatically place these files in the correct locations on the appropriate VMs.
* **servers.yml**: This YAML file contains a list of VM definitions and associated configuration data. It is referenced by `Vagrantfile` when Vagrant instantiates the VMs. Generally, this is the _only_ file that requires any edits; you'll edit this file to specify the correct Vagrant box installed on your system.
* **ssh-bastion-diagram.png**: This PNG diagram provides a graphical overview of the different VMs in this environment and how they are connected.
* **Vagrantfile**: This file is used by Vagrant to spin up the virtual machines. This file is fairly extensively commented to help explain what's happening. You should be able to use this file unchanged; all the VM configuration options are stored outside this file.
## Instructions
These instructions assume you've already installed VMware Fusion, Vagrant, and the Vagrant VMware plugin. Please refer to the documentation for those products for more information on installation or configuration.
1. Use `vagrant box add` to add a 64-bit Ubuntu 14.04 ("Trusty Tahr") base box to be used by this `Vagrantfile`. You'll need to specify a box that provides support for the `vmware_desktop` provider (it should work with either VMware Workstation or VMware Fusion). If you'd like, you can use my base box ("slowe/ubuntu-trusty-x64").
2. Edit the `servers.yml` file to ensure the box you downloaded in step 1 is specified on the "box:" line of this file for each VM. (By default, there are four VMs, so make sure to specify the correct box name for all four VMs.)
3. Run `vagrant up`, and when the VMs are finished provisioning run `vagrant ssh-config bastion`. Make note of the IP address provided for this VM; you'll need it in the next step.
4. Run `vagrant ssh client` to access the SSH client VM.
5. Use the editor of your choice to edit `~/.ssh/config` on the client VM to specify the correct address for the bastion host (look for the `Hostname` line). Save the changes to this file.
6. In the client VM, load the SSH agent via this command:
eval `ssh-agent -s`
7. Use `ssh-add` to add the bastion\_rsa and remote\_rsa keys. (The passphrase for `bastion_rsa` is "password"; for `remote_rsa` the passphrase is "secure".)
8. Use `ssh remote1` or `ssh remote2` to establish an SSH session _through_ the bastion host, as specified by the `ProxyCommand` in the SSH configuration file.
Enjoy!

71
ssh-bastion-ansible/Vagrantfile vendored Normal file
View file

@ -0,0 +1,71 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
# Require 'yaml' module
require 'yaml'
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Edit machines.yml to change VM configuration details
machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml'))
# Create and configure the VMs
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Always use Vagrant's default insecure key
config.ssh.insert_key = false
# Iterate through entries in YAML file to create VMs
machines.each do |machine|
# Configure the VMs per details in machines.yml
config.vm.define machine['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
# Specify the hostname of the VM
srv.vm.hostname = machine['name']
# Specify the Vagrant box to use (use VMware box by default)
srv.vm.box = machine['box']['vmw']
# Configure default synced folder (disable by default)
if machine['sync_disabled'] != nil
srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled']
else
srv.vm.synced_folder '.', '/vagrant', disabled: true
end #if machine['sync_disabled']
# Iterate through networks as per settings in machines.yml
machine['nics'].each do |net|
srv.vm.network net['type'], ip: net['ip_addr']
end # machine['nics'].each
# Configure CPU & RAM per settings in machines.yml (Fusion)
srv.vm.provider 'vmware_fusion' do |vmw|
vmw.vmx['memsize'] = machine['ram']
vmw.vmx['numvcpus'] = machine['vcpu']
if machine['nested'] == true
vmw.vmx['vhv.enable'] = 'TRUE'
end #if machine['nested']
end # srv.vm.provider 'vmware_fusion'
# Configure CPU & RAM per settings in machines.yml (VirtualBox)
srv.vm.provider 'virtualbox' do |vb, override|
vb.memory = machine['ram']
vb.cpus = machine['vcpu']
override.vm.box = machine['box']['vb']
vb.linked_clone = true if Vagrant::VERSION =~ /^1.8/
end # srv.vm.provider 'virtualbox'
end # config.vm.define
end # machines.each
# Provision all systems using Ansible
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'provision.yml'
end # config.vm.provision
end # Vagrant.configure

View file

@ -0,0 +1,30 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,7FE49F27BE773D4FFC45681AAFBE2100
D1WCSSy7EhsNjDNw+2bI0P2T3WmriJjHHY7UVwOUShD+yw2JU0Q5Z/6YHiOm9h92
iU2ZHZzR8HurQ6rDD0KBQI++nmDerkKSdxjTu+3dSHRPG51WniwqmNEriOvSW0RO
cdq1NdIYCDN8YE/r+htJAfhgVU7d+2yOJ9DGKEsm0PQrdEY1tlFfVPJbCNyZkI7M
GPg9RRYdM4qgE5v3wu2KWPbuMkjqltIUOGeJfEl6D4ER8y5sHTxvSXA/gNJUsQuX
9ZUb0fLAwRtfRiRPBIbNbv2HXkGdugo8srqF+7rWxmCk4f14ONekIgtlxxDUIdYK
G7Wx2j9kf2ES8PBdZ3Udl/U404F5LEoIqBLo23zfDmuSK8LF47u5VS5enW68CyIi
mzFw6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vkpbIL7
p74yHn4aW2jGMM1Sr68pUZGj8w9dOboARsHACsAnzsf5Sa+3ruegiu+KS1YidZDq
TNMxZ7tyB5x4A39KY7Enz69c9WHQMlEv5q4PJwEqkth1pCAOVFEFoGtlP6ZboqOZ
-----END RSA PRIVATE KEY-----

View file

@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8ED3jLmtB0o4NCqT4XirE2lGNt4M+WKd5PjIZj1/dgb4Be+IntdrWPAfkbgUoZaAGxp9Apj3asZJKVjFXqHIthu2AUoRGpBI75CQJ/8qXGtkazEhyE9kccIRKsTrngPBw7/uB7o6qXUWVkCjIJ6+QBOF5oTn5LB/FgB4FTxgezyVoRsakuFsuGRpTfndJE/YxK05/w+2Q21znfccORblLPASNnJ6H0vsX5glbF5RqhCLHHlIRLNMGNiUoleVojG60Z5m65fiDJvMSdexGcCZuk9eKxQ0sVwLmEabSZ3muW9SmuyJM1DjKD2/l2Ia+2RKrez1JLJFVmZ6GAsfvAzGX vagrant@outer

View file

@ -0,0 +1,8 @@
Host bastion
Hostname 192.168.100.102
IdentityFile ~/.ssh/bastion_rsa
User vagrant
Host remote*
IdentityFile ~/.ssh/remote_rsa
ProxyCommand ssh bastion -W %h:%p

View file

@ -0,0 +1,39 @@
---
- name: "client"
box:
vmw: "slowe/ubuntu-trusty-x64"
vb: "ubuntu/trusty64"
ram: 512
vcpu: 1
nics:
- type: "private_network"
ip_addr: "192.168.100.101"
- name: "bastion"
box:
vmw: "slowe/ubuntu-trusty-x64"
vb: "ubuntu/trusty64"
ram: 512
vcpu: 1
nics:
- type: "private_network"
ip_addr: "192.168.100.102"
- type: "private_network"
ip_addr: "10.100.60.10"
- name: "remote1"
box:
vmw: "slowe/ubuntu-trusty-x64"
vb: "ubuntu/trusty64"
ram: 512
vcpu: 1
nics:
- type: "private_network"
ip_addr: "10.100.60.11"
- name: "remote2"
box:
vmw: "slowe/ubuntu-trusty-x64"
vb: "ubuntu/trusty64"
ram: 512
vcpu: 1
nics:
- type: "private_network"
ip_addr: "10.100.60.12"

View file

@ -0,0 +1,45 @@
---
- hosts: "all"
become: "yes"
remote_user: "vagrant"
tasks:
- name: "Copy files for client system"
copy:
src: "{{ item }}"
dest: "/home/vagrant/.ssh/"
owner: "vagrant"
group: "vagrant"
mode: "0600"
with_items:
- "bastion_rsa"
- "bastion_rsa.pub"
- "remote_rsa"
- "remote_rsa.pub"
- "config"
when: ansible_hostname == "client"
- name: "Set up authorized keys on bastion host"
authorized_key:
user: "vagrant"
key: "{{ item }}"
with_file:
- "bastion_rsa.pub"
when: ansible_hostname == "bastion"
- name: "Ensure entries are in /etc/hosts on appropriate systems"
lineinfile:
dest: "/etc/hosts"
line: "{{ item }}"
with_items:
- "10.100.60.11 remote1"
- "10.100.60.12 remote2"
when: ansible_hostname == "bastion" or ansible_hostname == "client"
- name: "Set up authorized keys on remote systems"
authorized_key:
user: "vagrant"
key: "{{ item }}"
with_file:
- "remote_rsa.pub"
when: ansible_hostname == "remote1" or ansible_hostname == "remote2"

View file

@ -0,0 +1,30 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,059D08D45581C2138081DD04CAED14E2
jTTPJzsuMpgS9AjsQ3ebLT17PfB23pzzYE2rSuCdIbz+Zp/tG8dpUlH1CNzPw32F
7A7PrGGE9neGCHAdQa18G+hW7hB5nwjfxnDOY5V+/IwMj+FTmyU5GH+KHT2ffTvp
Cdb4xnsRDSPPsfJsaON78TrSbmmoVjwuRo/8ERt8HK7GixlE9/Ht65NrQFe3CfUc
mSpLDhnjuRA54sLaCIaUJl0iKbUAGB0qPV8+W2RXGkz/x/CBSRrP7GB5FrZDWUlI
Mu6RhYyt74VHX7hPlUExIxKuKqjKXwx4X8gV7cn5qLZuULg8EyPGTgfCziOfNjZF
OdLAMRfYhwyjmhvEVqQsE8dmZTB7/w9wEcN8NIKLG6ECSKsOJ1a/UvnqUMFAIIx6
cB655+Y57E93LQ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rteePxvURYKlNHOPlzt
1i/BC3f1LaPDig2avp4JmWBV6OqWor2cEdTWF+azfOYH1g4u14U0E176G+G1SIRm
pC3NK3V526S6isGnyng3kuzhLF7x/y/NKiaW4Gpk6m1rIrkyHPCNM7xRxGmR5SJp
-----END RSA PRIVATE KEY-----

View file

@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCp0CZCDAEBX1QPzo3dV2IkSWaAgDuqLBjQc4r8TDFN8epTXauo8IzDJ4L7UuRDdw64X54wSqwK+Z+WzvITHujWtIvznXUTf4M67odDbvWt5T8gT/7bi9+Y2DMFsKQI2CjrQ+lEeiGAZYt/XGGQW9nIhKpSO6m8Vw4JUTfFxrZFkbQ6RyU4kvwOyHlbNYktmfxXTrWp3NHyfWRQC3ywSX19QvVFMeRIu9mmzZWtDaj2k44uwUkBlFjb6ijXzBuQNBtjWlDfwge1XVPfTVVyXSqDZJerHqDfFhqwPB8ZHRvg4YcnOz1ryjpkESJGl3BM3ZFBZHsrjfHhkG2kypn5Xefr vagrant@outer

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB