Skip to content

Commit

Permalink
Add recipe lyraphase_workstation::loopback_alias_ip for re-creating I…
Browse files Browse the repository at this point in the history
…P alias on lo0 every reboot (For use with Docker for Mac)

Use case: Docker container == socks5h:// ==> Alias IP of macOS Host running SSH Tunnel => Bastion Host

See diagram: https://gist.github.com/trinitronx/6427d6454fb3b121fc2ab5ca7ac766bc

References:

 - [user-defined networks](https://docs.docker.com/network/bridge/##differences-between-user-defined-bridges-and-the-default-bridge)

 - [Docker forum thread: Connecting docker container to macOS Host](https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/13)

 - [Terraform enhancement request for SOCKS5h support and Explanation of Use Case](hashicorp/terraform#17754 (comment))
  • Loading branch information
trinitronx committed Apr 26, 2018
1 parent 298037c commit c980c68
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions attributes/loopback_alias_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
default['lyraphase_workstation']['loopback_alias_ip'] = {}
default['lyraphase_workstation']['loopback_alias_ip']['alias_ip'] = '172.16.222.111'
37 changes: 37 additions & 0 deletions recipes/loopback_alias_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 James Cuzella
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


loopback_alias_ip = nil
if ! node['lyraphase_workstation']['loopback_alias_ip'].nil? && ! node['lyraphase_workstation']['loopback_alias_ip']['alias_ip'].nil?
loopback_alias_ip = node['lyraphase_workstation']['loopback_alias_ip']['alias_ip']
end

template "/Library/LaunchDaemons/com.runlevel1.lo0.alias.plist" do
source "com.runlevel1.lo0.alias.plist.erb"
user "root"
group "wheel"
mode "0644"
variables({ loopback_alias_ip: loopback_alias_ip })
notifies :run, 'execute[load the com.runlevel1.lo0.alias plist into launchd]'
end

execute "load the com.runlevel1.lo0.alias plist into launchd" do
command "launchctl load -w /Library/LaunchDaemons/com.runlevel1.lo0.alias.plist"
user node['lyraphase_workstation']['user']
not_if 'launchctl list com.runlevel1.lo0.alias'
end
61 changes: 61 additions & 0 deletions spec/unit/recipes/loopback_alias_ip_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 James Cuzella
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

require 'spec_helper'

describe 'lyraphase_workstation::loopback_alias_ip' do

let(:launchd_plist) { "/Library/LaunchDaemons/com.runlevel1.lo0.alias.plist" }
let(:loopback_alias_ip) { '172.16.222.111' }

let(:chef_run) {
klass = ChefSpec.constants.include?(:SoloRunner) ? ChefSpec::SoloRunner : ChefSpec::Runner
klass.new(platform: 'mac_os_x', version: '10.11.1') do |node|
create_singleton_struct "EtcPasswd", [ :name, :passwd, :uid, :gid, :gecos, :dir, :shell, :change, :uclass, :expire ]
node.normal['etc']['passwd']['brubble'] = Struct::EtcPasswd.new('brubble', '********', 501, 20, 'Barney Rubble', '/Users/brubble', '/bin/bash', 0, '', 0)
node.normal['lyraphase_workstation']['user'] = 'brubble'
node.normal['lyraphase_workstation']['home'] = '/Users/brubble'

stub_command("which git").and_return('/usr/local/bin/git')

stub_command('launchctl list com.runlevel1.lo0.alias').and_return(true)
end.converge(described_recipe)
}

it 'installs launchd plist for adding IP alias to loopback network interface lo0' do
expect(chef_run).to create_template(launchd_plist).with(
user: 'root',
group: 'wheel',
mode: '0644'
)
expect(chef_run).to render_file(launchd_plist).with_content(Regexp.new("^\\s+<string>/sbin/ifconfig</string>$"))
expect(chef_run).to render_file(launchd_plist).with_content(Regexp.new("^\\s+<string>#{loopback_alias_ip}</string>$"))

expect(chef_run.template(launchd_plist)).to notify('execute[load the com.runlevel1.lo0.alias plist into launchd]').to(:run)
end

context "when launchd plist is already loaded" do
before(:all) do
stub_command('launchctl list com.runlevel1.lo0.alias').and_return(true)
end

it "skips loading com.runlevel1.lo0.alias launchd plist file" do
expect(chef_run.execute('load the com.runlevel1.lo0.alias plist into launchd')).to do_nothing
end
end
end

24 changes: 24 additions & 0 deletions templates/default/com.runlevel1.lo0.alias.plist.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.runlevel1.lo0.alias</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/sbin/ifconfig</string>
<string>lo0</string>
<string>alias</string>
<string><%= @loopback_alias_ip %></string>
<string>up</string>
</array>
<key>StandardErrorPath</key>
<string>/var/log/loopback-alias.log</string>
<key>StandardOutPath</key>
<string>/var/log/loopback-alias.log</string>
<key>UserName</key>
<string>root</string>
</dict>
</plist>

0 comments on commit c980c68

Please sign in to comment.