chambermaid 0.0.1 → 0.3.2

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
  SHA256:
3
- metadata.gz: 07456ca91b66f62c0c8184a33622d02cf18075e56f75872b5ffa79ef243074fb
4
- data.tar.gz: 120815ce7e083a74b30ff62e4679e66e9b3217ab7022bec3b43eb971dd1a92f5
3
+ metadata.gz: fbcb610044b8dd8ff4487cb6729889f18dde9a32bd09b25620b53b6c6336a9e9
4
+ data.tar.gz: 80abde8476c848335de1f9557f49557322a1463f36419fe8c986a53ee18cb816
5
5
  SHA512:
6
- metadata.gz: 73ea6ab93523e9e4e8e472140a314c5fd6d0661ed95b0591f53db345e2bffa5aec455cfa426e873e1102ec2c7881bed0170d6d66c2f6032f2fde9fbb5cdafb55
7
- data.tar.gz: 1c0010e8ef0c9005a46a7831ec343dcaaf17a78dce974250225777dabd2f9be49ce41c8716b5b59c3b82cc77b46f79b7fa99b96b1ca7b125a83f29b00a14ea14
6
+ metadata.gz: beb4b9d61c45e48bd48497e2cf27fec7cd5055447aac237324b298ba343b3d4bed49a46151aff364a90049ac31ae78b2c9dcf51b42cfa3cf000c3adde06a9552
7
+ data.tar.gz: 4228d741b603f187e69d3c900efae0241808eccabd4e3227b745cfd4e75e9c570949071c5d46c079d3ecd34992704c0390cea0f6b174c8560c2ce8ba3fe361f9
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in chambermaid.gemspec
4
4
  gemspec
5
-
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chambermaid (0.0.1)
4
+ chambermaid (0.3.2)
5
5
  aws-sdk-ssm (~> 1.85)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  aws-eventstream (1.1.0)
11
- aws-partitions (1.348.0)
11
+ aws-partitions (1.349.0)
12
12
  aws-sdk-core (3.104.3)
13
13
  aws-eventstream (~> 1, >= 1.0.2)
14
14
  aws-partitions (~> 1, >= 1.239.0)
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Chambermaid
1
+ # Chambermaid [![Gem Version](https://badge.fury.io/rb/chambermaid.svg)](https://badge.fury.io/rb/chambermaid) [![Build Status](https://travis-ci.com/mileszim/chambermaid.svg?branch=master)](https://travis-ci.com/mileszim/chambermaid)
2
2
 
3
3
  Companion RubyGem for [chamber](https://github.com/segmentio/chamber).
4
4
 
@@ -22,18 +22,39 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ **Standalone**
26
+
27
+ ```ruby
28
+ Chambermaid.add_namespace("/my/param/namespace")
29
+ Chambermaid.add_service("my-chamber-service")
30
+ ```
31
+
32
+ **Configuration Block**
33
+
25
34
  ```ruby
26
35
  # config/initializers/chambermaid.rb
27
36
 
28
37
  Chambermaid.configure do |config|
29
38
  config.add_namespace("/my/param/namespace")
39
+ config.add_service("my-chamber-service")
30
40
 
31
- # Set `priority: true` to choose these params over existing
41
+ # Set `overload: true` to choose these params over existing
32
42
  # ones in ENV when they are merged together
33
- config.add_namespace("/my/important/namespace", priority: true)
43
+ config.add_namespace("/my/important/namespace", overload: true)
34
44
  end
35
45
  ```
36
46
 
47
+ **Reload SSM into ENV**
48
+ ```ruby
49
+ Chambermaid.reload!
50
+ ```
51
+
52
+ **Restore ENV to original state**
53
+ ```ruby
54
+ Chambermaid.restore!
55
+ Chambermaid.reset! # alias of .restore!
56
+ ```
57
+
37
58
  ## Development
38
59
 
39
60
  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.
@@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  # Gems
30
30
  spec.add_dependency("aws-sdk-ssm", "~> 1.85")
31
+ spec.add_development_dependency("rspec", "~> 3.0")
32
+ spec.add_development_dependency("rake", "~> 12.0")
31
33
  end
@@ -1,6 +1,8 @@
1
+ require "chambermaid/base"
1
2
  require "chambermaid/version"
2
3
 
3
4
  module Chambermaid
4
5
  class Error < StandardError; end
5
- # Your code goes here...
6
+
7
+ extend Base
6
8
  end
@@ -0,0 +1,113 @@
1
+ require "chambermaid/environment"
2
+ require "chambermaid/namespace"
3
+ require "chambermaid/parameter_store"
4
+
5
+ module Chambermaid
6
+ module Base
7
+ def self.extended(base)
8
+ # Make a copy of ENV before we mess it all up
9
+ @@_original_env = ENV.to_h.dup.freeze
10
+ end
11
+
12
+ extend self
13
+
14
+ def configure
15
+ yield self
16
+ load!
17
+ end
18
+
19
+ # @todo
20
+ def configuration
21
+ raise "Namespaces must be defined" unless @namespaces
22
+ end
23
+
24
+ # Load SSM into ENV
25
+ def load!
26
+ @namespaces.each(&:load!)
27
+ end
28
+
29
+ # @todo
30
+ def reload!
31
+ @namespaces.each(&:reload!)
32
+ end
33
+
34
+ def unload!
35
+ @namespaces.each(&:unload!)
36
+ end
37
+
38
+ # Restore ENV to its original state
39
+ def restore!
40
+ ENV.replace(@@_original_env)
41
+ end
42
+ alias :reset! :restore!
43
+
44
+ # Add an AWS SSM parameter namespace to ENV
45
+ #
46
+ # @param [String] path
47
+ # @param [Boolean] overload
48
+ # true - replace any duplicate ENV keys with new params
49
+ # false - keep any existing duplicate ENV key values
50
+ #
51
+ # @raise [ArgumentError]
52
+ # when `path` is not a string
53
+ #
54
+ # @example
55
+ # Chambermaid.add_namespace("/my/param/namespace")
56
+ #
57
+ # @example overload duplicate ENV vars
58
+ # Chambermaid.add_namespace("/my/param/namespace", overload: true)
59
+ def add_namespace(path, overload: false)
60
+ raise ArgumentError.new("`path` must be a string") unless path.is_a?(String)
61
+ raise ArgumentError.new("`overload` must be a boolean") unless [true, false].include?(overload)
62
+
63
+ namespaces << Namespace.new(path: path, overload: overload)
64
+ end
65
+
66
+ # Immediately load an AWS SSM parameter namespace to ENV
67
+ #
68
+ # @param [String] path
69
+ # @param [Boolean] overload
70
+ # true - replace any duplicate ENV keys with new params
71
+ # false - keep any existing duplicate ENV key values
72
+ #
73
+ # @raise [ArgumentError]
74
+ # when `path` is not a string
75
+ #
76
+ # @example
77
+ # Chambermaid.add_namespace!("/my/param/namespace")
78
+ #
79
+ # @example overload duplicate ENV vars
80
+ # Chambermaid.add_namespace("/my/param/namespace", overload: true)
81
+ def add_namespace!(path, overload: false)
82
+ raise ArgumentError.new("`path` must be a string") unless path.is_a?(String)
83
+ raise ArgumentError.new("`overload` must be a boolean") unless [true, false].include?(overload)
84
+
85
+ namespaces << Namespace.load!(path: path, overload: overload)
86
+ end
87
+
88
+ # Add all secrets from Chamber service to ENV
89
+ #
90
+ # @param [String] service
91
+ # @param [Boolean] overload
92
+ # true - replace any duplicate ENV keys with new params
93
+ # false - keep any existing duplicate ENV key values
94
+ #
95
+ # @example
96
+ # Chambermaid.add_service("my-chamber-service")
97
+ #
98
+ # @example overload duplicate ENV vars
99
+ # Chambermaid.add_service("my-chamber-service", overload: true)
100
+ #
101
+ # @see {Chambermaid::Base.add_namespace}
102
+ def add_service(service, overload: false)
103
+ service = "/#{service}" unless service[0] == "/"
104
+ add_namespace(service)
105
+ end
106
+
107
+ private
108
+
109
+ def namespaces
110
+ @namespaces ||= []
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,68 @@
1
+ module Chambermaid
2
+ class Environment < Hash
3
+ attr_reader :params
4
+
5
+ # Create new Chambermaid::Environment
6
+ #
7
+ # @param [Hash] params
8
+ #
9
+ # @raise [ArgumentError]
10
+ # if params is not type Hash
11
+ #
12
+ # @return [Chambermaid::Environment]
13
+ def initialize(params)
14
+ validate_params!(params)
15
+ stash_current_env!
16
+ update(format_env(params))
17
+ end
18
+
19
+ # Generate a dotenv (.env) compatible string
20
+ #
21
+ # @return [String] dotenv compatible string
22
+ def to_dotenv
23
+ to_h.inject("") do |env_str, param|
24
+ env_str + "#{param[0]}=#{param[1]}\n"
25
+ end
26
+ end
27
+
28
+ # Write a .env file
29
+ #
30
+ # @param [String] file_path
31
+ def to_file!(file_path)
32
+ File.open(file_path, "wb") do |f|
33
+ f.write(to_dotenv)
34
+ end
35
+ end
36
+
37
+ # Inject into ENV without overwriting duplicates
38
+ def load!
39
+ each { |k, v| ENV[k] ||= v }
40
+ end
41
+
42
+ # Inject into ENV and overwrite duplicates
43
+ def overload!
44
+ each { |k, v| ENV[k] = v }
45
+ end
46
+
47
+ # Restore to original ENV
48
+ def unload!
49
+ ENV.replace(@_original_env)
50
+ end
51
+
52
+ private
53
+
54
+ def stash_current_env!
55
+ @_original_env ||= ENV.to_h.dup.freeze
56
+ end
57
+
58
+ def format_env(params)
59
+ params.map{|k,v| [k.upcase, v]}.to_h
60
+ end
61
+
62
+ def validate_params!(params)
63
+ unless params.is_a?(Hash)
64
+ raise ArgumentError.new("`params` must be a hash")
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,42 @@
1
+ module Chambermaid
2
+ class Namespace
3
+ # @param [String] path
4
+ # @param [Boolean] overload
5
+ def initialize(path:, overload: false)
6
+ @path = path
7
+ @overload = overload
8
+
9
+ @store = ParameterStore.new(path: path)
10
+ @env = Environment.new({})
11
+ end
12
+
13
+ def self.load!(path:, overload: false)
14
+ namespace = new(path: path, overload: overload)
15
+ namespace.load!
16
+ namespace
17
+ end
18
+
19
+ def load!
20
+ @store.load!
21
+ load_env!
22
+ end
23
+
24
+ def reload!
25
+ @env.unload!
26
+ @store.reload!
27
+ load_env!
28
+ end
29
+
30
+ def unload!
31
+ @env.unload!
32
+ end
33
+
34
+ private
35
+
36
+ # Inject into ENV
37
+ def load_env!
38
+ @env.replace(@store.params)
39
+ @overload ? @env.overload! : @env.load!
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,67 @@
1
+ require "aws-sdk-ssm"
2
+
3
+ module Chambermaid
4
+ class ParameterStore
5
+ def initialize(path:)
6
+ @path = path
7
+ end
8
+
9
+ def load!
10
+ fetch_ssm_params!
11
+ end
12
+
13
+ def reload!
14
+ clear_params!
15
+ fetch_ssm_params!
16
+ end
17
+
18
+ def loaded?
19
+ !@params_list.empty?
20
+ end
21
+
22
+ def self.load!(path:)
23
+ store = new(path: path)
24
+ store.load!
25
+ store
26
+ end
27
+
28
+ def params
29
+ @params ||= @param_list.map { |p|
30
+ [p.name.split("/").last.upcase, p.value]
31
+ }.to_h
32
+ end
33
+
34
+ alias :to_h :params
35
+
36
+ private
37
+
38
+ def client
39
+ @client ||= Aws::SSM::Client.new
40
+ end
41
+
42
+ def fetch_ssm_params!
43
+ @param_list = []
44
+ response = nil
45
+ loop do
46
+ response = fetch_ssm_param_batch!(response&.next_token)
47
+ @param_list.concat(response.parameters)
48
+
49
+ break unless response.next_token
50
+ end
51
+ end
52
+
53
+ def fetch_ssm_param_batch!(next_token = nil)
54
+ client.get_parameters_by_path(
55
+ path: @path,
56
+ with_decryption: true,
57
+ recursive: true,
58
+ next_token: next_token
59
+ )
60
+ end
61
+
62
+ def clear_params!
63
+ @params = nil
64
+ @params_list = []
65
+ end
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module Chambermaid
2
- VERSION = "0.0.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chambermaid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Zimmerman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-31 00:00:00.000000000 Z
11
+ date: 2020-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-ssm
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.85'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
27
55
  description:
28
56
  email:
29
57
  - miles@asktrim.com
@@ -44,6 +72,10 @@ files:
44
72
  - bin/setup
45
73
  - chambermaid.gemspec
46
74
  - lib/chambermaid.rb
75
+ - lib/chambermaid/base.rb
76
+ - lib/chambermaid/environment.rb
77
+ - lib/chambermaid/namespace.rb
78
+ - lib/chambermaid/parameter_store.rb
47
79
  - lib/chambermaid/version.rb
48
80
  homepage: https://github.com/mileszim/chambermaid
49
81
  licenses: