awssession 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2467e70386d7bd3bd2edebe2f32682a025ff0377
4
- data.tar.gz: 5ae201b950a16f0342e3b21625b32f15103bb932
3
+ metadata.gz: 125bfaca0503b5a1a54324e048fb3bc0bd954493
4
+ data.tar.gz: 233d3a2af38c75b0fda959437fc1819141e247b1
5
5
  SHA512:
6
- metadata.gz: 4c5c1024cb3cb4c100a83ebac1e23cd373b0645f61643a8dc52b023b4e36d3930cbcb2c3443bfb549187ff1f87dc1626e4d8482481d789f8fc40495a4a0c72b9
7
- data.tar.gz: 85cb0a1aa277d5618718944437a17149bbdc6499754051fca13905ed822f43ac60b29f537ea2796fda2eee242ce8a7cd19dcf1053dd2301666232ce8ed9ab0f0
6
+ metadata.gz: 66743d294271c79eed0acc09e79818536789cf71a09d51f82adafa37da31a9b621946a87658ba14cee78d2e42fe55d576e8c8bf3f920c6da5cb4d68858ba47ae
7
+ data.tar.gz: be068b2a3ae0e6d7dec57f82656cbaa914e25890d40e643f15b1dedb20657375db616039406db7880253fc2475cece8d67ec66d9a347a68219922fda2cbbba2f
data/.gitignore CHANGED
@@ -12,4 +12,4 @@
12
12
  # rspec failure tracking
13
13
  .rspec_status
14
14
 
15
- *aws-session.yaml
15
+ *session.yaml
@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  ## [Unreleased]
5
5
 
6
+ ## [0.3.0] - 2017-10-11
7
+ ### Added
8
+ - CLI script `awssession` to wrap commands needing AWS sessions.
9
+ - Create symlink in 'current' directory to role session file.
10
+
6
11
  ## [0.2.5] - 2017-10-06
7
12
  ### Changed
8
13
  - Cleanup + session naming improvement
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'awssession'
4
+ require 'aws_config'
5
+
6
+ profile_name = ARGV.shift
7
+ profile = AWSConfig[profile_name]
8
+ begin
9
+ profile['name'] = profile_name
10
+ rescue NoMethodError
11
+ puts "Requested role '#{profile_name}' not found."
12
+ exit 1
13
+ end
14
+
15
+ awssession = AwsSession.new(profile: profile)
16
+ awssession.start
17
+
18
+ exec(ARGV.join(' '))
19
+
20
+ # vim:set fileencoding=utf8 fileformat=unix filetype=ruby tabstop=2 expandtab:
@@ -4,6 +4,7 @@ require 'aws-sdk-core'
4
4
  require 'yaml'
5
5
  require 'io/console'
6
6
  require 'time'
7
+ require 'fileutils'
7
8
 
8
9
  # AWS Session creation with profile
9
10
  # Structure of options[:profile]
@@ -21,9 +22,9 @@ class AwsSession
21
22
  def initialize(options)
22
23
  @profile = options[:profile]
23
24
  @sts_lifetime = options[:sts_lifetime] || 129_600
24
- @sts_filename = options[:sts_filename] || "#{@profile.name}_aws-sts-session.yaml"
25
+ @sts_filename = options[:sts_filename] || 'aws-sts-session.yaml'
25
26
  @role_lifetime = options[:role_lifetime] || 3_600
26
- @role_filename = options[:role_filename] || "#{@profile.name}_aws-role-session.yaml"
27
+ @role_filename = options[:role_filename] || 'aws-role-session.yaml'
27
28
  @session_save_path = options[:session_save_path] || "#{Dir.home}/.aws/cache"
28
29
  @debug = options[:debug] || 0
29
30
  end
@@ -34,29 +35,30 @@ class AwsSession
34
35
  end
35
36
 
36
37
  def load_session
37
- load_role_session if File.file?("#{@session_save_path}/#{@role_filename}")
38
- load_sts_session if @role_session.nil? && File.file?("#{@session_save_path}/#{@sts_filename}")
38
+ load_role_session if File.file?("#{@session_save_path}/#{@profile.name}_#{@role_filename}")
39
+ load_sts_session if @role_session.nil? && File.file?("#{@session_save_path}/#{@profile.name}_#{@sts_filename}")
39
40
  end
40
41
 
41
42
  def load_role_session
42
- @role_session = YAML.load_file("#{@session_save_path}/#{@role_filename}") # Load
43
+ @role_session = YAML.load_file("#{@session_save_path}/#{@profile.name}_#{@role_filename}") # Load
43
44
  if Time.now > @role_session.credentials.expiration
44
45
  # or soooooooon !
45
46
  puts 'Role session credentials expired. Removing obsolete role session file' if @debug > 0
46
47
  @role_session = nil
47
- File.delete("#{@session_save_path}/#{@role_filename}")
48
- elsif @debug > 0
49
- puts 'Found valid role session credentials.'
48
+ File.delete("#{@session_save_path}/#{@profile.name}_#{@role_filename}")
49
+ else
50
+ FileUtils.ln_s("#{@session_save_path}/#{@profile.name}_#{@role_filename}", "./.#{@role_filename}", force: true)
51
+ puts 'Found valid role session credentials.' if @debug > 0
50
52
  end
51
53
  end
52
54
 
53
55
  def load_sts_session
54
- @sts_session = YAML.load_file("#{@session_save_path}/#{@sts_filename}") # Load
56
+ @sts_session = YAML.load_file("#{@session_save_path}/#{@profile.name}_#{@sts_filename}") # Load
55
57
  if Time.now > @sts_session.credentials.expiration
56
58
  # or soooooooon !
57
59
  puts 'STS session credentials expired. Removing obsolete sts session file' if @debug > 0
58
60
  @sts_session = nil
59
- File.delete("#{@session_save_path}/#{@sts_filename}")
61
+ File.delete("#{@session_save_path}/#{@profile.name}_#{@sts_filename}")
60
62
  elsif @debug > 0
61
63
  puts 'Found valid sts session credentials.'
62
64
  end
@@ -107,7 +109,7 @@ class AwsSession
107
109
 
108
110
  def save_session(file, session)
109
111
  FileUtils.mkdir_p(@session_save_path)
110
- File.open("#{@session_save_path}/#{file}", 'w') { |f| f.write session.to_yaml }
112
+ File.open("#{@session_save_path}/#{@profile.name}_#{file}", 'w') { |f| f.write session.to_yaml }
111
113
  end
112
114
 
113
115
  def credentials
@@ -1,3 +1,3 @@
1
1
  class AwsSession
2
- VERSION = '0.2.5'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awssession
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan - Zipkid - Goethals
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-06 00:00:00.000000000 Z
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,7 +97,8 @@ dependencies:
97
97
  description: AwsSession creates an AWS session
98
98
  email:
99
99
  - stefan.goethals@vrt.be
100
- executables: []
100
+ executables:
101
+ - awssession
101
102
  extensions: []
102
103
  extra_rdoc_files: []
103
104
  files:
@@ -112,6 +113,7 @@ files:
112
113
  - bin/console
113
114
  - bin/setup
114
115
  - changelog.md
116
+ - exe/awssession
115
117
  - lib/awssession.rb
116
118
  - lib/awssession/version.rb
117
119
  - license.txt