-
Notifications
You must be signed in to change notification settings - Fork 18
/
aws-agent
executable file
·75 lines (64 loc) · 1.87 KB
/
aws-agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env ruby
# Copyright (C) 2015-2017 Electronic Arts Inc. All rights reserved.
require 'logger'
require 'socket'
require 'tmpdir'
require 'awsudo'
def usage
warn <<EOS
Usage:
#{File.basename $0}
EOS
exit 1
end
LOG_FILENAME = File.join(ENV['HOME'], ".aws-agent.log")
CONFIG_FILENAME = File.join(ENV['HOME'], '.awsudo')
SUPPORTED_IDPS = AWSUDO::IdentityProviders.constants
logger = Logger.new(LOG_FILENAME, "weekly")
logger.progname = "aws-agent"
logger.level = Logger::WARN
config = AWSUDO.load_config(CONFIG_FILENAME)
idpname = config['IDP'].to_s.capitalize.to_sym
unless SUPPORTED_IDPS.include?(idpname)
warn "`#{config['IDP']}' is not a supported identity provider"
exit 4
end
username, password = AWSUDO.ask_for_credentials
idp = AWSUDO::IdentityProviders.new(idpname, config, username, password)
idp.logger = logger
socket_dir = Dir.mktmpdir("aws-")
socket_name = File.join(socket_dir, "agent")
case ENV.fetch("SHELL", "bash").split("/").last
when 'csh', 'tcsh'
puts "setenv AWS_AUTH_SOCK #{socket_name}"
when 'fish'
puts "set -gx AWS_AUTH_SOCK #{socket_name}"
else
puts "AWS_AUTH_SOCK=#{socket_name}; export AWS_AUTH_SOCK;"
end
Process.daemon
$0 = 'aws-agent'
Process.setrlimit(Process::RLIMIT_CORE, 0, 0)
UNIXServer.open(socket_name) do |socket|
loop do
Thread.new(socket.accept) do |client|
logger.debug "Thread started"
logger.debug {"connection accepted: #{socket.inspect}"}
begin
role_arn = client.gets.strip
logger.debug {"role ARN received: #{role_arn}"}
client.puts idp.assume_role(role_arn).to_json
rescue => e
logger.error e
error = {:error => e}.to_json
client.print error
ensure
logger.debug "Closing connection"
client.close
logger.debug "Connection closed"
end
logger.debug "Thread ending"
end
end
end
FileUtils.rmdir socket_dir