vagrant-ec2-metadata 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '08b6abbac16a3fdadb55656647b4acd0952f98e6'
4
+ data.tar.gz: 83b6d82619f2fd7c971ed7cc1a9e8d46ef7072bd
5
+ SHA512:
6
+ metadata.gz: aa220dab78420623cf19fb65a556708f9536fa44004d49db1dd0d4f331cd4b56f0576c0312387c62f691000beae76c37d8d4b6a5b2fd14fbd2a7b84ec4b929f5
7
+ data.tar.gz: bc06e5a696fa6128d351307353e36a4054d34dc27b566d64cd49885327debd9feb4d469e0a121255bfc5edf86133b77ff6407829e2beaa0765a87d406ae11f78
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ C���[G=^8~Уs���9,V��Ş��z`_�2�i�� _0�g�A�����~w�ɂdP-R廗T��W�"����Hb]�DCIE��c7��;�]�<J�P���D�p�s�T�|�} q���ãg̤{_� ��l��q�*�w���>5�p�ꠒ6��?@�މZỴv�|�}�1
2
+ �M�����0/g�gn�y��%M �Ȯ<( ���$���Z�^���Tv��Y�C1l��Y��G�;�Å$�]�_�(���=�
@@ -0,0 +1,71 @@
1
+ require "webrick"
2
+ require "aws-sdk-core"
3
+ require "socket"
4
+
5
+ ENV["AWS_DEFAULT_REGION"] ||= "us-west-2"
6
+
7
+ module VagrantEc2Metadata
8
+ class Server
9
+ def initialize(config, port, options, env)
10
+ @config = config
11
+ @port = port
12
+ @options = options
13
+ @env = env
14
+ end
15
+
16
+ def start
17
+ WEBrick::Daemon.start if @options[:daemonize]
18
+
19
+ host_ip = Socket.ip_address_list.detect(&:ipv4_private?).ip_address
20
+ server = WEBrick::HTTPServer.new(BindAddress: host_ip, Port: @port)
21
+
22
+ trap "INT" do
23
+ server.shutdown
24
+ end
25
+
26
+ server.mount_proc "/" do |req, res|
27
+ # Only allow requests from our own IP, which the VMs will normally share
28
+ if req.peeraddr[-1] != host_ip
29
+ res.status = 403 # Forbidden
30
+ next
31
+ end
32
+
33
+ # This endpoint is all we handle right now
34
+ next if !req.path.start_with?("/latest/meta-data/iam/security-credentials/")
35
+
36
+ if req.path == "/latest/meta-data/iam/security-credentials/"
37
+ res.body = "role"
38
+ else
39
+ sts = ::Aws::STS::Client.new(profile: @config.profile)
40
+ if @config.role_arn
41
+ resp = sts.assume_role({
42
+ duration_seconds: 3600,
43
+ role_arn: @config.role_arn,
44
+ role_session_name: "vagrant",
45
+ })
46
+ creds = resp.credentials
47
+ else
48
+ resp = sts.get_session_token({
49
+ duration_seconds: 3600,
50
+ })
51
+ creds = resp.credentials
52
+ end
53
+
54
+ res.body = <<EOF
55
+ {
56
+ "Code" : "Success",
57
+ "LastUpdated" : "#{Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")}",
58
+ "Type" : "AWS-HMAC",
59
+ "AccessKeyId" : "#{creds.access_key_id}",
60
+ "SecretAccessKey" : "#{creds.secret_access_key}",
61
+ "Token" : "#{creds.session_token}",
62
+ "Expiration" : "#{creds.expiration.strftime("%Y-%m-%dT%H:%M:%SZ")}"
63
+ }
64
+ EOF
65
+ end
66
+ end
67
+
68
+ server.start
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module VagrantEc2Metadata
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,111 @@
1
+ require "vagrant"
2
+ require "socket"
3
+ require "optparse"
4
+
5
+ module VagrantEc2Metadata
6
+ class Config < Vagrant.plugin("2", :config)
7
+ attr_accessor :profile
8
+ attr_accessor :role_arn
9
+ attr_accessor :port
10
+
11
+ def initialize
12
+ @profile = UNSET_VALUE
13
+ end
14
+
15
+ def finalize!
16
+ @profile = "default" if @profile == UNSET_VALUE
17
+ end
18
+
19
+ def self.port(machine)
20
+ return machine.config.ec2_metadata.port if machine.config.ec2_metadata.port
21
+ ec2_metadata_file = machine.data_dir.join("ec2-metadata-port")
22
+ if ec2_metadata_file.file?
23
+ port = ec2_metadata_file.read.chomp.to_i
24
+ else
25
+ # Generate a random port number that hopefully won't interfere with anything
26
+ port = 12000+Random.rand(1000)
27
+ ec2_metadata_file.open("w+") do |f|
28
+ f.write(port.to_s)
29
+ end
30
+ end
31
+ return port
32
+ end
33
+ end
34
+
35
+ class Provisioner < Vagrant.plugin("2", :provisioner)
36
+ def provision
37
+ host_ip = Socket.ip_address_list.detect(&:ipv4_private?).ip_address
38
+ port = Config.port(@machine)
39
+
40
+ # If you are having troubles with the iptables rule, you can flush it with:
41
+ # sudo iptables -t nat -F
42
+
43
+ cmd = <<EOF
44
+ sudo iptables -t nat -A OUTPUT -p tcp -d 169.254.169.254 -j DNAT --to-destination #{host_ip}:#{port} || echo 'Error setting up iptables rule.'
45
+ grep -q -F '169.254.169.254 instance-data' /etc/hosts || echo "# Added by vagrant-ec2-metadata:\n169.254.169.254 instance-data" | sudo tee -a /etc/hosts >/dev/null
46
+ EOF
47
+
48
+ @machine.ui.info("Setting up an iptables rule for the EC2 metadata server (port #{port}).")
49
+ @machine.action(:ssh_run,
50
+ ssh_run_command: cmd,
51
+ ssh_opts: {extra_args: []})
52
+ end
53
+ end
54
+
55
+ class Command < Vagrant.plugin("2", :command)
56
+ def self.synopsis
57
+ "starts the EC2 metadata server"
58
+ end
59
+
60
+ def execute
61
+ options = {}
62
+ opts = OptionParser.new do |o|
63
+ o.banner = "Usage: vagrant ec2-metadata [options] [name|id]"
64
+ o.separator ""
65
+ o.separator "Options:"
66
+ o.separator ""
67
+ o.on("-d", "--daemonize", "Daemonize the servers") do |h|
68
+ options[:daemonize] = h
69
+ end
70
+ end
71
+ argv = parse_options(opts)
72
+ return if !argv
73
+
74
+ if options[:daemonize]
75
+ puts "Daemonizing servers."
76
+ end
77
+
78
+ argv = @env.active_machines.map(&:first).map(&:to_s) if argv.empty?
79
+ require_relative "vagrant-ec2-metadata/server"
80
+ threads = []
81
+ with_target_vms(argv) do |machine|
82
+ port = Config.port(machine)
83
+ config = machine.config.ec2_metadata
84
+ machine.ui.info("Using profile #{machine.config.ec2_metadata.profile}#{config.role_arn ? " with role #{config.role_arn}":""} (port #{port})")
85
+ thread = Thread.new do
86
+ server = VagrantEc2Metadata::Server.new(config, port, options, @env)
87
+ server.start
88
+ end
89
+ threads.push(thread)
90
+ end
91
+ threads.map(&:join)
92
+ end
93
+ end
94
+
95
+ class Plugin < Vagrant.plugin("2")
96
+ name "ec2-metadata"
97
+ description "Easily provide vagrant machines with AWS credentials by faking an EC2 metadata server."
98
+
99
+ config("ec2_metadata") do
100
+ Config
101
+ end
102
+
103
+ provisioner("ec2-metadata") do
104
+ Provisioner
105
+ end
106
+
107
+ command("ec2-metadata") do
108
+ Command
109
+ end
110
+ end
111
+ end
data.tar.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ ���D�*��a�x�8�<�*9�o}�����T4J����f���jS�,�h)���g�ˑ+s��u�<I��Y���8�
2
+ -�g]��MKj.������?��6��P3��v^_����
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-ec2-metadata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Sundin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDjjCCAnagAwIBAgIBATANBgkqhkiG9w0BAQUFADBGMREwDwYDVQQDDAhydWJ5
14
+ Z2VtczEcMBoGCgmSJomT8ixkARkWDHN0ZWZhbnN1bmRpbjETMBEGCgmSJomT8ixk
15
+ ARkWA2NvbTAeFw0xNjEyMjUwNjE1MjVaFw0yNjEyMjMwNjE1MjVaMEYxETAPBgNV
16
+ BAMMCHJ1YnlnZW1zMRwwGgYKCZImiZPyLGQBGRYMc3RlZmFuc3VuZGluMRMwEQYK
17
+ CZImiZPyLGQBGRYDY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
18
+ w1R+aqbSeZjouJP4SvaMtqaJCMnJzpKo4JY6DL/nkqLxfiaTGWx+00mEZJVamdC2
19
+ JqkMIxdWuyyybJjg0X9xHRKEiTwC3GrEIZu9LmWhsul5i7vyOvddvlHROLHoYMS6
20
+ gpILOLkKrVCaDnRnOYDkCGDnu71++HOQHgx0CbnqdNegRmBN8WZRIb6H0jurZhsx
21
+ WepGRF1YjOJ2Q3UL6UNE0IjXTrUTO4QUOIekau53jT5eQYVZAt5x+9GIkPbjnUTU
22
+ D/2LMpfIDldot08FuVFkZ4WX8NiJALWw50R89v8Ua6fOhky87CleVjvxPbZrMHY7
23
+ rXJDhoB1S0l2tFH8vMIpnwIDAQABo4GGMIGDMAkGA1UdEwQCMAAwCwYDVR0PBAQD
24
+ AgSwMB0GA1UdDgQWBBQRpF7HGYIDKCp3AHIksBaEDHzM1zAkBgNVHREEHTAbgRly
25
+ dWJ5Z2Vtc0BzdGVmYW5zdW5kaW4uY29tMCQGA1UdEgQdMBuBGXJ1YnlnZW1zQHN0
26
+ ZWZhbnN1bmRpbi5jb20wDQYJKoZIhvcNAQEFBQADggEBAJWwHS8TyssFdfejrrUq
27
+ kpP0smaCG0hkfD5+xp29HIu4VPyQZIju4DnlnUcj8jCYrJXCwBe6nyx5WAPG3ZIY
28
+ TzwSKVajyJbfgB4NcIE8qSLktx+PgWigqlYQzioqMLNMDpxw558OyGRuEr5hItnN
29
+ SRG/mEUFyjtyl8YS7o5QnSQlR+ZPlOURsKxHsGH0oQtN1EXRpyYWoaCIYT9wfuwY
30
+ shCB2umA9buEFZkDDXDLn+xe8+ZwJHUngtkB6/T8yLUeqpwnVzaPTnhJJstYpxaa
31
+ E04BZKo2WzOTzSDymo97Yu4YFgyc98umMyeaCvPk4YmdNzqSanAXpY2bnsyu0CF5
32
+ Td0=
33
+ -----END CERTIFICATE-----
34
+ date: 2017-10-29 00:00:00.000000000 Z
35
+ dependencies: []
36
+ description: Easily provide vagrant machines with AWS credentials by faking an EC2
37
+ metadata server.
38
+ email:
39
+ - rubygems@stefansundin.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/vagrant-ec2-metadata.rb
45
+ - lib/vagrant-ec2-metadata/server.rb
46
+ - lib/vagrant-ec2-metadata/version.rb
47
+ homepage: https://github.com/stefansundin/vagrant-ec2-metadata
48
+ licenses:
49
+ - GPL-3.0
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.6.13
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Easily provide vagrant machines with AWS credentials.
71
+ test_files: []
metadata.gz.sig ADDED
Binary file