forked from malomalo/sync-accounts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved toward using github as the source for ssh keys
- Loading branch information
Showing
7 changed files
with
132 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
install: | ||
cp ssh-install /usr/local/bin/ | ||
cp ssh-package /usr/local/bin/ | ||
cp ssh-extract /usr/local/bin/ | ||
cp sync-accounts /usr/local/bin/ | ||
|
||
uninstall: | ||
rm /usr/local/bin/ssh-install | ||
rm /usr/local/bin/ssh-package | ||
rm /usr/local/bin/ssh-extract | ||
rm /usr/local/bin/sync-accounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
users: | ||
- github_user_name_1 | ||
- github_user_name_2 | ||
|
||
apps: | ||
app1-staging: | ||
- github_user_name_1 | ||
- github_user_name_2 | ||
app1-production: | ||
- github_user_name_1 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#!/usr/bin/env ruby | ||
require 'uri' | ||
require 'yaml' | ||
require 'net/http' | ||
require 'net/https' | ||
require 'fileutils' | ||
|
||
def github_keys(user) | ||
http = Net::HTTP.new('github.com', 443) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
|
||
response = http.request(Net::HTTP::Get.new("/#{user}.keys")) | ||
raise 'failure' unless response.is_a?(Net::HTTPOK) | ||
response.body.split("\n") | ||
end | ||
|
||
def read_permissions(url) | ||
url = URI.parse(url) | ||
permissions = { 'apps' => {}, 'users' => {} } | ||
|
||
yaml = Net::HTTP.start(url.host) do |http| | ||
resp = http.get(url.path) | ||
YAML.load(resp.body) | ||
end | ||
|
||
# Add keys to users and apps | ||
yaml['users'].each do |user| | ||
permissions['users'][user] = github_keys(user) | ||
end | ||
yaml['apps'].each do |app, users| | ||
permissions['apps'][app] = users.map{|u| permissions['users'][u] }.flatten | ||
end | ||
|
||
permissions | ||
end | ||
|
||
def system_users | ||
users = `cat /etc/passwd | grep -v root | grep '/bin/bash'`.split.map do |u| | ||
u =~ /([^\s]+):.*:.*:.*:.*:([^\s]+):\/bin\/bash$/ | ||
{:name => $1, :home => $2} | ||
end | ||
users = users.select{|u| u[:home] && u[:name] } | ||
users.select{|u| u[:home].index('/home') == 0 } | ||
end | ||
|
||
def system_apps | ||
users = `cat /etc/passwd | grep -v root | grep '/bin/bash'`.split.map do |u| | ||
u =~ /([^\s]+):.*:.*:.*:.*:([^\s]+):\/bin\/bash$/ | ||
{:name => $1, :home => $2} | ||
end | ||
users = users.select{|u| u[:home] && u[:name] } | ||
users.select{|u| u[:home].index('/srv') == 0 } | ||
end | ||
|
||
def add_system_user(name) | ||
raise "invalid user name '#{name}'" unless name =~ /^[a-z][-a-z0-9]*$/ | ||
|
||
`adduser #{name} --gecos "" --disabled-password` | ||
`usermod -a -G sudo #{name}` | ||
end | ||
|
||
def remove_system_user(name) | ||
`deluser --remove-home #{name}` | ||
end | ||
|
||
def write_authorized_keys(user, homedir, keys) | ||
ssh_dir = File.join(homedir, '.ssh') | ||
FileUtils.mkdir_p(ssh_dir) | ||
FileUtils.chown_R(user, user, ssh_dir) | ||
FileUtils.chmod(0700, ssh_dir) | ||
|
||
filename = File.join(ssh_dir, 'authorized_keys') | ||
open(filename, 'w') do |file| | ||
keys.each {|k| file.puts(k) } | ||
end | ||
FileUtils.chown(user, user, filename) | ||
FileUtils.chmod(0600, filename) | ||
end | ||
|
||
|
||
permissions = read_permissions(ARGV[0]) | ||
|
||
# Create missing users | ||
(permissions['users'].keys - system_users.map{|su| su[:name]}).each do |user| | ||
puts "adding user #{user}" | ||
add_system_user(user) | ||
end | ||
|
||
# Removed old users | ||
(system_users.map{|su| su[:name]} - permissions['users'].keys).each do |user| | ||
puts "removing user #{user}" | ||
remove_system_user(user) | ||
end | ||
|
||
# Write user ssh keys | ||
system_users.each do |su| | ||
puts "writing authorized_keys for #{su[:name]}" | ||
write_authorized_keys(su[:name], su[:home], permissions['users'][su[:name]]) | ||
end | ||
|
||
# Write app ssh keys | ||
system_apps.each do |su| | ||
next if !permissions['apps'][su[:name]] | ||
puts "writing authorized_keys for #{su[:name]}" | ||
write_authorized_keys(su[:name], su[:home], permissions['apps'][su[:name]]) | ||
end |