Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

klam-ssh support for CoreOS #187

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
27d5e08
Merge remote-tracking branch 'adobe-platform/master'
Apr 27, 2016
32ba74a
Initial check-in of klam-ssh support
Apr 27, 2016
957e3fb
klam-ssh additions
Apr 29, 2016
f80f050
Merge remote-tracking branch 'adobe-platform/master'
Apr 29, 2016
3a1a4b9
Last few refinements
May 3, 2016
dc90b55
Merge remote-tracking branch 'adobe-platform/master'
May 3, 2016
a457ce5
make scripts executable
May 3, 2016
e41b189
Merge remote-tracking branch 'adobe-platform/master'
May 4, 2016
3b26a28
update from master; fixes from comments in PR
May 4, 2016
285bcc8
Modify global prompt for klam-ssh
May 4, 2016
25a6ac4
setting default klam-ssh image value
May 4, 2016
1695dff
Merge remote-tracking branch 'adobe-platform/master'
May 10, 2016
8fd283f
Merge remote-tracking branch 'adobe-platform/master'
May 12, 2016
05ec34d
etcd2 setup for bastion host as well
May 12, 2016
a2dbc4d
Merge remote-tracking branch 'adobe-platform/master'
May 12, 2016
2df21da
Merge remote-tracking branch 'adobe-platform/master'
May 13, 2016
dc94557
Merge remote-tracking branch 'adobe-platform/master'
May 16, 2016
3b5a4d0
Bastion support in mesos-systemd
May 16, 2016
0c9ce40
Fixing klam-ssh installation issues due to reliance on values from se…
May 16, 2016
c0b2691
Merge remote-tracking branch 'adobe-platform/master'
May 16, 2016
0d8aa6f
something's not right...
May 16, 2016
94a8485
Revert "something's not right..."
May 16, 2016
e7843af
Rearranging the script order
May 16, 2016
685f787
update-ssh-keys is causing problems
May 16, 2016
f87bb3d
Merge remote-tracking branch 'adobe-platform/master'
May 17, 2016
2c2917f
Merge remote-tracking branch 'adobe-platform/master'
May 19, 2016
09c83cc
Bastion host ssh helper script
May 19, 2016
947b01a
Merge remote-tracking branch 'adobe-platform/master'
May 24, 2016
2f66d37
Updating the ssh client config script to use the Bastion as a forward…
May 24, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions init
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ else
VERSION="$1"
fi

# Control tier - must form an etcd2 cluster first
if [ "$NODE_ROLE" = "control" ]; then
# Control and Bastion tiers - must form an etcd2 cluster first
if [ "$NODE_ROLE" = "control" ] || [ "$NODE_ROLE" = "bastion" ]; then
sudo ${SCRIPTDIR}/$VERSION/util/etcd2-setup.sh $SCRIPTDIR
fi
if [ "$NODE_ROLE" = "it-hybrid" ]; then
Expand Down Expand Up @@ -49,6 +49,9 @@ ${SCRIPTDIR}/$VERSION/util/seed-etcd.sh || :
# The mesos-credentials.sh script must run after seed-etcd.sh
sudo ${SCRIPTDIR}/$VERSION/util/mesos-credentials.sh || :

# The klam-ssh.sh script must run after seed-etcd.sh
sudo ${SCRIPTDIR}/$VERSION/util/klam-ssh.sh || :

etcdctl get /environment/SCRIPTS-FORK
if [[ $? = 4 ]]; then
# 4 == 404 - key not found
Expand Down
1 change: 1 addition & 0 deletions v3/profile.d/bastion/etcdctl.sh
1 change: 1 addition & 0 deletions v3/profile.d/bastion/fleetctl.sh
2 changes: 1 addition & 1 deletion v3/profile.d/profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fi
# set the prompt for non-root users
if [[ ${EUID} != 0 ]]; then
source /etc/environment
export PS1="\[\\033[01;32m\]\u@\h\[\\033[01;34m\] \[\\033[01;30m\]$NODE_TIER-$NODE_PRODUCT-$ZONE-$NODE_ROLE \[\\033[01;34m\]\w \$\[\\033[00m\] "
export PS1="\[\\033[01;32m\]$(who -m | awk '{print $1}')@\h\[\\033[01;34m\] \[\\033[01;30m\]$NODE_TIER-$NODE_PRODUCT-$ZONE-$NODE_ROLE \[\\033[01;34m\]\w \$\[\\033[00m\] "
fi
12 changes: 12 additions & 0 deletions v3/util/authorizedkeys_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

ROLE_NAME="$(etcdctl get /klam-ssh/config/role-name)"
ENCRYPTION_ID="$(etcdctl get /klam-ssh/config/encryption-id)"
ENCRYPTION_KEY="$(etcdctl get /klam-ssh/config/encryption-key)"
KEY_LOCATION_PREFIX="$(etcdctl get /klam-ssh/config/key-location-prefix)"
IMAGE="$(etcdctl get /images/klam-ssh)"

echo "Running authorizedkeys_command for $1" | systemd-cat -p info -t klam-ssh

docker run --rm -e ROLE_NAME=${ROLE_NAME} -e ENCRYPTION_ID=${ENCRYPTION_ID} -e ENCRYPTION_KEY=${ENCRYPTION_KEY} -e KEY_LOCATION_PREFIX=${KEY_LOCATION_PREFIX} ${IMAGE} /usr/lib/klam/getKeys.py $1
exit 0
10 changes: 10 additions & 0 deletions v3/util/downloadS3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

ROLE_NAME="$(etcdctl get /klam-ssh/config/role-name)"
ENCRYPTION_ID="$(etcdctl get /klam-ssh/config/encryption-id)"
ENCRYPTION_KEY="$(etcdctl get /klam-ssh/config/encryption-key)"
KEY_LOCATION_PREFIX="$(etcdctl get /klam-ssh/config/key-location-prefix)"
IMAGE="$(etcdctl get /images/klam-ssh)"

docker run --rm -e ROLE_NAME=${ROLE_NAME} -e ENCRYPTION_ID=${ENCRYPTION_ID} -e ENCRYPTION_KEY=${ENCRYPTION_KEY} -e KEY_LOCATION_PREFIX=${KEY_LOCATION_PREFIX} ${IMAGE} /usr/lib/klam/downloadS3.py
exit 0
144 changes: 144 additions & 0 deletions v3/util/klam-ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#!/bin/bash -xe

etcdctl set /images/klam-ssh "adobecloudops/klam-ssh:latest"

AZ=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
REGION=${AZ::-1}
ROLE_NAME="$(etcdctl get /klam-ssh/config/role-name)"
ENCRYPTION_ID="$(etcdctl get /klam-ssh/config/encryption-id)"
ENCRYPTION_KEY="$(etcdctl get /klam-ssh/config/encryption-key)"
KEY_LOCATION_PREFIX="$(etcdctl get /klam-ssh/config/key-location-prefix)"
IMAGE="$(etcdctl get /images/klam-ssh)"


case $REGION in
"eu-west-1")
KEY_LOCATION="-ew1" ;;
"ap-northeast-1")
KEY_LOCATION="-an1" ;;
"us-east-1")
KEY_LOCATION="-ue1" ;;
"us-west-1")
KEY_LOCATION="-uw1" ;;
"us-west-2")
KEY_LOCATION="-uw2" ;;
*)
echo "An incorrect region value specified"
exit 1
;;
esac

# create nsswitch.conf
cat << EOT >> /home/core/nsswitch.conf
passwd: files usrfiles ato
shadow: files usrfiles ato
group: files usrfiles ato

hosts: files usrfiles dns
networks: files usrfiles dns

services: files usrfiles
protocols: files usrfiles
rpc: files usrfiles

ethers: files
netmasks: files
netgroup: nisplus
bootparams: files
automount: files nisplus
aliases: files nisplus
EOT

# create klam-ssh.conf
cat << EOT >> /home/core/klam-ssh.conf
{
key_location: ${KEY_LOCATION_PREFIX}${KEY_LOCATION},
role_name: ${ROLE_NAME},
encryption_id: ${ENCRYPTION_ID},
encryption_key: ${ENCRYPTION_KEY},
resource_location: amazon,
time_skew: permissive,
s3_region: ${REGION}
}
EOT

# Create directory structure
mkdir -p /opt/klam/lib /opt/klam/lib64 /etc/ld.so.conf.d

# Docker volume mount
docker create --name klam-ssh ${IMAGE}

# Copy libnss_ato library
docker cp klam-ssh:/tmp/klam-build/coreos/libnss_ato.so.2 /opt/klam/lib64

# Create symlink
ln -sf /opt/klam/lib64/libnss_ato.so.2 /opt/klam/lib64/libnss_ato.so

# Docker remove container
docker rm klam-ssh

# Move the ld.so.conf file to the correct location
echo "/opt/klam/lib64" > /etc/ld.so.conf.d/klam.conf
ldconfig
ldconfig -p | grep klam

# Validate that the files exist in the correct folder
ls -l /opt/klam/lib64/libnss_ato.so*

# Create the klamfed home directory
useradd -p "*" -U -G sudo -u 5000 -m klamfed -s /bin/bash
mkdir -p /home/klamfed
usermod -p "*" klamfed
usermod -U klamfed
update-ssh-keys -u klamfed || :

# Add klamfed to wheel
usermod -a -G wheel klamfed

# Add klamfed to sudo
usermod -a -G sudo klamfed

# Add passwordless sudo to klamfed
echo "klamfed ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/klamfed

# Validate that the klamfed user has the correct uid value (5000) and home directory
id klamfed
ls -ld /home/klamfed

# Re-link nsswitch.conf
mv -f /home/core/nsswitch.conf /etc/nsswitch.conf
cat /etc/nsswitch.conf

# generate the ATO config
grep klamfed /etc/passwd > /opt/klam/lib/klam-ato.conf

# Validate that the contents of /opt/klam/lib/klam-ato.conf
cat /opt/klam/lib/klam-ato.conf

# Move klam-ssh.conf
mv -f /home/core/klam-ssh.conf /opt/klam/lib/klam-ssh.conf
cat /opt/klam/lib/klam-ssh.conf

# update /etc/ssh/sshd_config
cp /etc/ssh/sshd_config sshd_config
echo 'AuthorizedKeysCommand /opt/klam/lib/authorizedkeys_command.sh' >> sshd_config
echo 'AuthorizedKeysCommandUser root' >> sshd_config
mv -f sshd_config /etc/ssh/sshd_config
cat /etc/ssh/sshd_config

# Change ownership of authorizedkeys_command
chown root:root /home/core/mesos-systemd/v3/util/authorizedkeys_command.sh

# Relocate authorizedkeys_command
mv /home/core/mesos-systemd/v3/util/authorizedkeys_command.sh /opt/klam/lib

# Change ownership of downloadS3
chown root:root /home/core/mesos-systemd/v3/util/downloadS3.sh

# Relocate downloadS3.sh
mv /home/core/mesos-systemd/v3/util/downloadS3.sh /opt/klam/lib

# Restart SSHD
systemctl restart sshd.service

echo "KLAM SSH BOOTSTRAP COMPLETE"
42 changes: 42 additions & 0 deletions v3/util/ssh-client-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Use meta-data to determine the public IPv4 of this bastion host and the vpc's cidr block
IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
ETH0_MAC=$(ifconfig eth0 | grep ether | awk '{print tolower($2)}')
VPC_CIDR=$(curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/${ETH0_MAC}/vpc-ipv4-cidr-block)

# Split the network IP from the mask and assign them to separate variables
IFS='/' read -r -a CIDR_ARRAY <<< "${VPC_CIDR}"
NETWORK=${CIDR_ARRAY[0]}
MASK=${CIDR_ARRAY[1]}

# Determine how many /24 networks fit into the given VPC_CIDR. Yes, this is ugly for two reasons.
# One, ssh_config only allows string pattern matching, so for subnets, one can only use a network
# terminated with a wildcard for the last octet. Second, there's a limit to the size of the VPC
# that can be allocated in Adobe private address space. HamCIDR only allows CIDR blocks from /25
# to /22, so any other value is bypassing SOP. A case statement can't do range comparisons, and
# I didn't want to implement a subnet calculator in BASH, so there's this:

if [[ "${MASK}" -ge 24 ]]; then SUBNETS=1
elif [[ "${MASK}" == 23 ]]; then SUBNETS=2
elif [[ "${MASK}" == 22 ]]; then SUBNETS=4
elif [[ "${MASK}" == 21 ]]; then SUBNETS=8
elif [[ "${MASK}" == 20 ]]; then SUBNETS=16
else
echo "Your large CIDR block broke teh internets."
exit 1
fi

# Build the string of /24 networks to use in the ssh_config
HOSTS=$(for ((i = 0; i < ${SUBNETS}; i++)); do echo ${NETWORK} | awk -v x="${i}" -F. '{printf "%d.%d.%d.%s", $1,$2,$3+x,"* "}'; done; echo)


cat << EOF
Host ${IP}
ForwardAgent yes
IdentityFile ~/.ssh/ssh.pem

Host ${HOSTS}
IdentityFile ~/.ssh/ssh.pem
ProxyCommand ssh ${IP} ncat %h %p
EOF