acs2aws 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 borisding
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Acs2aws
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/acs2aws`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'acs2aws'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install acs2aws
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/acs2aws. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Acs2aws project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/acs2aws/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,47 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'acs2aws/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'acs2aws'
10
+ spec.version = Acs2aws::VERSION
11
+ spec.authors = ['borisding']
12
+ spec.email = ['lding@sequoiacap.com']
13
+
14
+ spec.summary = 'Login and retrieve AWS STS using a any SAML IDP.'
15
+ spec.description = 'Login and retrieve AWS STS Token using a any SAML IDP. Inspired by OAuth2WebServerFlow.'
16
+ spec.homepage = 'https://github.com/borisding1994/acs2aws'
17
+ spec.license = 'MIT'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+ else
24
+ raise StandardError.new('RubyGems 2.0 or newer is required to protect against public gem pushes.')
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+ spec.bindir = 'exe'
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_dependency 'aws-sdk', '~> 3.0', '>= 3.0.1'
35
+ spec.add_dependency 'clamp', '~> 1.3'
36
+ spec.add_dependency 'colorize', '~> 0.8.1'
37
+ spec.add_dependency 'launchy', '~> 2.4', '>= 2.4.3'
38
+ spec.add_dependency 'mime-types', '~> 3.1'
39
+ spec.add_dependency 'nokogiri', '~> 1.8', '>= 1.8.4'
40
+ spec.add_dependency 'puma', '~> 3.11', '>= 3.11.4'
41
+ spec.add_dependency 'rack', '~> 2.0', '>= 2.0.5'
42
+ spec.add_dependency 'sinatra', '~> 2.0', '>= 2.0.3'
43
+
44
+ spec.add_development_dependency 'bundler', '~> 1.16'
45
+ spec.add_development_dependency 'minitest', '~> 5.0'
46
+ spec.add_development_dependency 'rake', '~> 10.0'
47
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acs2aws"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'acs2aws'
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'acs2aws/version'
4
+ require 'acs2aws/cli/config'
5
+ require 'acs2aws/cli/refresh'
6
+ require 'clamp'
7
+ require 'colorize'
8
+
9
+ module Acs2aws
10
+ CONFIG_PATH = ENV['HOME']+'/.acs2awc.conf'
11
+ SERVER_PORT = 10389
12
+ class MainCommand < Clamp::Command
13
+ option %w(-v --version), :flag, 'print version information' do
14
+ puts "acs2aws: #{Acs2aws::VERSION.colorize(:light_blue)}\n\n"
15
+ exit(0)
16
+ end
17
+
18
+ subcommand 'config', 'configure an IDP settings', Cli::ConfigCommand
19
+ subcommand 'refresh', 'retrieve AWS STS token', Cli::RefreshCommand
20
+ end
21
+
22
+ MainCommand.run
23
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sinatra/base'
4
+ require 'acs2aws/aws_sts'
5
+ require 'uri'
6
+
7
+ module Acs2aws
8
+ class AcsServer
9
+ attr_accessor :instance
10
+
11
+ def initialize
12
+ sp_url = File.read(Acs2aws::CONFIG_PATH) rescue nil
13
+ valid_sp_url = URI.parse(sp_url).is_a?(URI::HTTP) rescue false
14
+ raise StandardError.new('Error: '.colorize(:red) + 'Config not found or invalid. Please re-configure.') unless valid_sp_url
15
+ @instance = Sinatra.new do
16
+ set :port, Acs2aws::SERVER_PORT
17
+ set :title, 'acs2aws'
18
+ get '/' do
19
+ redirect "#{sp_url}?cli_port=#{Acs2aws::SERVER_PORT}"
20
+ end
21
+
22
+ post '/saml_acs' do
23
+ Acs2aws::AwsSts.new(params['SAMLResponse'])
24
+ body 'Successfully. <script>window.opener = self;window.close();</script>'
25
+ Thread.new do
26
+ sleep 1
27
+ Process.kill 'INT', Process.pid
28
+ end
29
+ halt 200
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aws-sdk-iam'
4
+ require 'base64'
5
+ require 'nokogiri'
6
+
7
+ module Acs2aws
8
+ class AwsSts
9
+ attr_accessor :encode_saml, :saml
10
+
11
+ def initialize(encode_saml)
12
+ @encode_saml = encode_saml
13
+ @saml = Nokogiri::XML(Base64.decode64(encode_saml))
14
+ role_entitlement = @saml.xpath('//*[@Name="https://aws.amazon.com/SAML/Attributes/Role"]')
15
+ .children.children.to_s.split(',')
16
+ # TODO: support customized region.
17
+
18
+ client = Aws::STS::Client.new(region: 'ap-southeast-1', credentials: nil)
19
+ resp = client.assume_role_with_saml(
20
+ role_arn: role_entitlement[0],
21
+ principal_arn: role_entitlement[1],
22
+ saml_assertion: @encode_saml,
23
+ # 12 hours
24
+ duration_seconds: 43200
25
+ )
26
+ puts resp
27
+ system "aws configure --profile default set aws_access_key_id #{resp.credentials.access_key_id}"
28
+ system "aws configure --profile default set aws_secret_access_key #{resp.credentials.secret_access_key}"
29
+ system "aws configure --profile default set aws_session_token #{resp.credentials.session_token}"
30
+
31
+ puts "Successfully refresh. Expiration at #{resp.credentials.expiration}".colorize(:green)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'clamp'
4
+ require 'colorize'
5
+ require 'uri'
6
+
7
+
8
+ module Acs2aws
9
+ module Cli
10
+ class ConfigCommand < Clamp::Command
11
+ option %w(-v --version), :flag, 'print version information' do
12
+ puts "acs2aws: version #{Acs2aws::VERSION}\n\n"
13
+ exit(0)
14
+ end
15
+
16
+ def execute
17
+ puts 'SAML SP Start Page URL:'.colorize(:red)
18
+ sp_url = STDIN.gets.chomp
19
+ until URI.parse(sp_url).kind_of?(URI::HTTP)
20
+ puts 'Error: '.colorize(:red) + 'URL must be a valid uri with a scheme matching the http https pattern. Please enter again.'
21
+ sp_url = STDIN.gets.chomp
22
+ end
23
+ File.open(Acs2aws::CONFIG_PATH, 'w') { |f| f.write sp_url }
24
+ puts 'Successfully configure.'.colorize(:green)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'clamp'
4
+ require 'launchy'
5
+ require 'acs2aws/acs_server'
6
+
7
+
8
+ module Acs2aws
9
+ module Cli
10
+ class RefreshCommand < Clamp::Command
11
+ def execute
12
+ srv = Acs2aws::AcsServer.new
13
+ Launchy.open("http://localhost:#{Acs2aws::SERVER_PORT}/")
14
+ srv.instance.run!
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Acs2aws
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,273 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acs2aws
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - borisding
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: clamp
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: colorize
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 0.8.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: launchy
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.4'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.4.3
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '2.4'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.4.3
81
+ - !ruby/object:Gem::Dependency
82
+ name: mime-types
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.1'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.1'
95
+ - !ruby/object:Gem::Dependency
96
+ name: nokogiri
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '1.8'
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 1.8.4
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '1.8'
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 1.8.4
115
+ - !ruby/object:Gem::Dependency
116
+ name: puma
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '3.11'
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 3.11.4
125
+ type: :runtime
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.11'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 3.11.4
135
+ - !ruby/object:Gem::Dependency
136
+ name: rack
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '2.0'
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 2.0.5
145
+ type: :runtime
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '2.0'
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 2.0.5
155
+ - !ruby/object:Gem::Dependency
156
+ name: sinatra
157
+ requirement: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - "~>"
160
+ - !ruby/object:Gem::Version
161
+ version: '2.0'
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.0.3
165
+ type: :runtime
166
+ prerelease: false
167
+ version_requirements: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: '2.0'
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: 2.0.3
175
+ - !ruby/object:Gem::Dependency
176
+ name: bundler
177
+ requirement: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - "~>"
180
+ - !ruby/object:Gem::Version
181
+ version: '1.16'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - "~>"
187
+ - !ruby/object:Gem::Version
188
+ version: '1.16'
189
+ - !ruby/object:Gem::Dependency
190
+ name: minitest
191
+ requirement: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - "~>"
194
+ - !ruby/object:Gem::Version
195
+ version: '5.0'
196
+ type: :development
197
+ prerelease: false
198
+ version_requirements: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - "~>"
201
+ - !ruby/object:Gem::Version
202
+ version: '5.0'
203
+ - !ruby/object:Gem::Dependency
204
+ name: rake
205
+ requirement: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - "~>"
208
+ - !ruby/object:Gem::Version
209
+ version: '10.0'
210
+ type: :development
211
+ prerelease: false
212
+ version_requirements: !ruby/object:Gem::Requirement
213
+ requirements:
214
+ - - "~>"
215
+ - !ruby/object:Gem::Version
216
+ version: '10.0'
217
+ description: Login and retrieve AWS STS Token using a any SAML IDP. Inspired by OAuth2WebServerFlow.
218
+ email:
219
+ - lding@sequoiacap.com
220
+ executables:
221
+ - acs2aws
222
+ extensions: []
223
+ extra_rdoc_files: []
224
+ files:
225
+ - ".gitignore"
226
+ - ".idea/acs2aws.iml"
227
+ - ".idea/misc.xml"
228
+ - ".idea/modules.xml"
229
+ - ".idea/workspace.xml"
230
+ - ".rubocop.yml"
231
+ - ".travis.yml"
232
+ - CODE_OF_CONDUCT.md
233
+ - Gemfile
234
+ - Gemfile.lock
235
+ - LICENSE.txt
236
+ - README.md
237
+ - Rakefile
238
+ - acs2aws.gemspec
239
+ - bin/console
240
+ - bin/setup
241
+ - exe/acs2aws
242
+ - lib/acs2aws.rb
243
+ - lib/acs2aws/acs_server.rb
244
+ - lib/acs2aws/aws_sts.rb
245
+ - lib/acs2aws/cli/config.rb
246
+ - lib/acs2aws/cli/refresh.rb
247
+ - lib/acs2aws/version.rb
248
+ homepage: https://github.com/borisding1994/acs2aws
249
+ licenses:
250
+ - MIT
251
+ metadata:
252
+ allowed_push_host: https://rubygems.org
253
+ post_install_message:
254
+ rdoc_options: []
255
+ require_paths:
256
+ - lib
257
+ required_ruby_version: !ruby/object:Gem::Requirement
258
+ requirements:
259
+ - - ">="
260
+ - !ruby/object:Gem::Version
261
+ version: '0'
262
+ required_rubygems_version: !ruby/object:Gem::Requirement
263
+ requirements:
264
+ - - ">="
265
+ - !ruby/object:Gem::Version
266
+ version: '0'
267
+ requirements: []
268
+ rubyforge_project:
269
+ rubygems_version: 2.7.3
270
+ signing_key:
271
+ specification_version: 4
272
+ summary: Login and retrieve AWS STS using a any SAML IDP.
273
+ test_files: []