awssession 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30907688da26f18c8385f5c7c6eb0602f94083bb
4
+ data.tar.gz: 61a930defac12fbe5d27590141ac0c5dc43bae7f
5
+ SHA512:
6
+ metadata.gz: 68167bb19ec5522b2d4829d954b48cbc63a619cf697486a84d1c3381547ac042def0dd4df2b54c5eb142b7d985d0b6a031d35454a3ca30c89de6e2dbec1525f2
7
+ data.tar.gz: 1f54c617e44f9fb9d33e1cc44bd1219818334b1ecf2cb840b5a0ca1fed6ae4e7b46d51213cfd815b2473f8544805d754d7527b114b3bc80d0cdf5b64b19fe942
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ *aws-session.yaml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in awssession.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ # Awssession
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/awssession`. 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 'awssession'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install awssession
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 spec` 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]/awssession.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,40 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'awssession/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'awssession'
8
+ spec.version = AwsSession::VERSION
9
+ spec.licenses = ['MIT']
10
+ spec.authors = ['Stefan - Zipkid - Goethals']
11
+ spec.email = ['stefan.goethals@vrt.be']
12
+
13
+ spec.summary = 'AwsSession creates an AWS session'
14
+ spec.description = 'AwsSession creates an AWS session'
15
+ spec.homepage = 'http://github.com/vrtdev/awssession'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.15'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'aws-sdk-ssm'
37
+ spec.add_development_dependency 'aws_config'
38
+
39
+ spec.add_runtime_dependency 'aws-sdk-core'
40
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "awssession"
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,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'awssession'
4
+
5
+ require 'aws-sdk-ssm'
6
+ require 'aws_config'
7
+ require 'pp'
8
+
9
+ profile_name = 'vrt-dpc-sandbox-admin'
10
+ profile = AWSConfig[profile_name]
11
+ profile['name'] = profile_name
12
+
13
+ awssession = AwsSession.new(profile: profile)
14
+ awssession.start
15
+
16
+ ssm = Aws::SSM::Client.new(credentials: awssession.credentials)
17
+
18
+ ssm.describe_parameters.parameters.each do |p|
19
+ puts "Parameter Name: #{p.name}."
20
+ end
21
+
22
+ puts 'Get Parameter by Path, 2 results'
23
+ pp ssm.get_parameters_by_path(
24
+ path: '/',
25
+ recursive: true,
26
+ max_results: 2
27
+ )
@@ -0,0 +1,122 @@
1
+ require 'awssession/version'
2
+
3
+ require 'aws-sdk-core'
4
+ require 'yaml'
5
+ require 'io/console'
6
+
7
+ # AWS Session creation with profile
8
+ # Structure of options[:profile]
9
+ # {
10
+ # 'name' => <name>,
11
+ # 'region' => <region>
12
+ # 'role_arn' => <role_arn>
13
+ # 'aws_access_key_id' => <aws_access_key_id>
14
+ # 'aws_secret_access_key' => <aws_secret_access_key>
15
+ # 'mfa_serial' => <mfa_serial>
16
+ # }
17
+ # Can be fetched with AWSConfig[profile_name] if .aws/config
18
+ #
19
+ class AwsSession
20
+ def initialize(options)
21
+ @profile = options[:profile]
22
+ @sts_lifetime = options[:sts_lifetime] || 129_600
23
+ @sts_filename = options[:sts_filename] || "#{@profile.name}_aws-sts-session.yaml"
24
+ @role_lifetime = options[:role_lifetime] || 3_600
25
+ @role_filename = options[:role_filename] || "#{@profile.name}_aws-role-session.yaml"
26
+ @session_save_path = options[:session_save_path] || "#{Dir.home}/.aws/cache"
27
+ end
28
+
29
+ def start
30
+ load_session
31
+ create_session
32
+ end
33
+
34
+ def load_session
35
+ load_role_session if File.file?("#{@session_save_path}/#{@role_filename}")
36
+ load_sts_session if @role_session.nil? && File.file?("#{@session_save_path}/#{@sts_filename}")
37
+ end
38
+
39
+ def load_role_session
40
+ @role_session = YAML.load_file("#{@session_save_path}/#{@role_filename}") # Load
41
+ if Time.now > @role_session.credentials.expiration
42
+ # or soooooooon !
43
+ puts 'Role session credentials expired. Removing obsolete role session file'
44
+ @role_session = nil
45
+ File.delete("#{@session_save_path}/#{@role_filename}")
46
+ else
47
+ puts 'Found valid role session credentials.'
48
+ end
49
+ end
50
+
51
+ def load_sts_session
52
+ @sts_session = YAML.load_file("#{@session_save_path}/#{@sts_filename}") # Load
53
+ if Time.now > @sts_session.credentials.expiration
54
+ # or soooooooon !
55
+ puts 'STS session credentials expired. Removing obsolete sts session file'
56
+ @sts_session = nil
57
+ File.delete("#{@session_save_path}/#{@sts_filename}")
58
+ else
59
+ puts 'Found valid sts session credentials.'
60
+ end
61
+ end
62
+
63
+ def create_session
64
+ if @role_session.nil? && @sts_session.nil?
65
+ read_token_input
66
+ sts_session_token
67
+ save_session @sts_filename, @sts_session
68
+ end
69
+ return unless @role_session.nil?
70
+ assume_role
71
+ save_session @role_filename, @role_session
72
+ end
73
+
74
+ def read_token_input
75
+ print 'Enter AWS MFA token: '
76
+ @token_code = STDIN.noecho(&:gets)
77
+ @token_code.chomp!
78
+ puts ''
79
+ end
80
+
81
+ def sts_session_token
82
+ sts_client = Aws::STS::Client.new(
83
+ access_key_id: @profile.aws_access_key_id,
84
+ secret_access_key: @profile.aws_secret_access_key
85
+ )
86
+ @sts_session = sts_client.get_session_token(
87
+ duration_seconds: @sts_lifetime,
88
+ serial_number: @profile.mfa_serial,
89
+ token_code: @token_code
90
+ )
91
+ end
92
+
93
+ def assume_role
94
+ sts_client = Aws::STS::Client.new(
95
+ access_key_id: @sts_session.credentials.access_key_id,
96
+ secret_access_key: @sts_session.credentials.secret_access_key,
97
+ session_token: @sts_session.credentials.session_token
98
+ )
99
+ @role_session = sts_client.assume_role(
100
+ duration_seconds: @role_lifetime,
101
+ role_arn: @profile.role_arn,
102
+ role_session_name: 'mysession'
103
+ )
104
+ end
105
+
106
+ def save_session(file, session)
107
+ FileUtils.mkdir_p(@session_save_path)
108
+ File.open("#{@session_save_path}/#{file}", 'w') { |f| f.write session.to_yaml }
109
+ end
110
+
111
+ def credentials
112
+ Aws::Credentials.new(*session_credentials)
113
+ end
114
+
115
+ def session_credentials
116
+ [
117
+ @role_session.credentials.access_key_id,
118
+ @role_session.credentials.secret_access_key,
119
+ @role_session.credentials.session_token
120
+ ]
121
+ end
122
+ end
@@ -0,0 +1,3 @@
1
+ class AwsSession
2
+ VERSION = '0.2.0'.freeze
3
+ end
@@ -0,0 +1,19 @@
1
+ Copyright 2017 VRT.be
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awssession
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefan - Zipkid - Goethals
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk-ssm
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws_config
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: aws-sdk-core
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: AwsSession creates an AWS session
98
+ email:
99
+ - stefan.goethals@vrt.be
100
+ executables:
101
+ - awssession-use.rb
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - README.md
110
+ - Rakefile
111
+ - awssession.gemspec
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/awssession-use.rb
115
+ - lib/awssession.rb
116
+ - lib/awssession/version.rb
117
+ - license.txt
118
+ homepage: http://github.com/vrtdev/awssession
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ allowed_push_host: https://rubygems.org
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.6.13
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: AwsSession creates an AWS session
143
+ test_files: []