smps 0.3.4 → 0.5.4

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
- SHA1:
3
- metadata.gz: 8543dab65697a71c1ed88ab0891d42ba172f8f19
4
- data.tar.gz: d782a5b1800eb519bc82ede934de3c4d3e909a83
2
+ SHA256:
3
+ metadata.gz: d28b459006110169081af4ffa0bbf3602cd87ec18bec0c3090f61a83031e4118
4
+ data.tar.gz: 516d0b9783c8ea2a5c4685be3240cf0526d69e9a88b79bb8121c59786e1319d6
5
5
  SHA512:
6
- metadata.gz: 75f384cbfa95f4036fca82be71f0220938eacb66116d029ed9220afe0d996f440384c83f999b778e18e5404c36db1d38475928c8b0a548e7c71b9cbe44cc2051
7
- data.tar.gz: facdd18b450b38ebf35a311d95a60cd3051f1653349225178163b24a031c1f9a20abebef2aead8c2d0f0d961cd7d32021c966dce696fd32c6a8caeaecfb39f7b
6
+ metadata.gz: ac4b56d53277286c7ee4e5662bbc21f58ada8330263acfa9fdd574c443b37fb6825d662e4daa6c3d3e8dfdd2e2b4757aab16a91f233cc366a07687ccd41fe16c
7
+ data.tar.gz: 679c488a2b926beea73ffb4c6e58c76eff6ca36f14b4994a879f66f66e69ceb0385fd6e221eccba7f354cf93a7a3da3068bb676108d7828dac87f507f342e661
data/README.md CHANGED
@@ -1,12 +1,16 @@
1
- # Smps
1
+ # SMPS - System Manager Parameter Store
2
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/smps`. 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
3
+ A command line tool and library to interact with the Amazon System Manager Parameter Store
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
7
+ $ gem install smps
8
+
9
+ ## Usage
10
+
11
+ ### Library
12
+
13
+ To use this gem in your own tools, add this line to your application's Gemfile:
10
14
 
11
15
  ```ruby
12
16
  gem 'smps'
@@ -16,20 +20,124 @@ And then execute:
16
20
 
17
21
  $ bundle
18
22
 
19
- Or install it yourself as:
23
+ ### CLI
20
24
 
21
- $ gem install smps
25
+ After installation, 2 executable scripts will be installed: `smps` & `smps-cli`:
22
26
 
23
- ## Usage
27
+ - smps: the simple version of this utility.
28
+ - smps-cli: The thor cli executable offers a different interface.
29
+
30
+ #### smps
31
+
32
+ Run the command with `--help` for more information.
33
+
34
+ ```
35
+ smps [OPTION]
36
+
37
+ -h, --help:
38
+ show help
39
+
40
+ -r, --role <rolename>
41
+ IAM role to use. From ~/.aws/config
42
+ Use this for interactive use on your workstation.
43
+
44
+ -p, --param <param_name>
45
+ Parameter name.
46
+
47
+ -b --by_path <path>
48
+ Path name
49
+
50
+ -v, --value <new_value>
51
+ Value to assign
52
+
53
+ -t, --type <type>
54
+ Parameter type.
55
+ One of [String StringList SecureString]
56
+
57
+ -k, --key <key_id>
58
+ Key for SecureString encrypting.
24
59
 
25
- TODO: Write usage instructions here
60
+ -d, --debug [level]:
61
+ Debug level.
62
+ ```
63
+
64
+ #### smps-cli
65
+
66
+ The smps-cli command is a new cli with more straightforward actions.
67
+
68
+ Run the command with `help` for more information: `smps-cli help`. For each individual action,
69
+ you can also use `smps-cli help <action>`
70
+
71
+ ##### help
72
+
73
+ ```
74
+ Commands:
75
+ smps-cli get NAME # Get path or path indicated by the name
76
+ smps-cli help [COMMAND] # Describe available commands or one s...
77
+ smps-cli set NAME VALUE --type=TYPE # Set the parameter to this value
78
+
79
+ Options:
80
+ [--role=PROFILE] # IAM profile/role to use. From ~/.aws/config
81
+ [--region=REGION] # Override the aws region
82
+ [--debug=N] # AwsSession debug level.
83
+ # Default: 0
84
+ [--user-data], [--no-user-data] # Retrieve the name or path, and the key values from the userdata
85
+ ```
86
+
87
+ ##### get NAME
88
+
89
+ ```
90
+ Usage:
91
+ smps-cli get NAME
92
+
93
+ Options:
94
+ [--role=PROFILE] # IAM profile/role to use. From ~/.aws/config
95
+ [--region=REGION] # Override the aws region
96
+ [--debug=N] # AwsSession debug level.
97
+ # Default: 0
98
+ [--user-data], [--no-user-data] # Retrieve the name or path, and the key values from the userdata
99
+
100
+ Description:
101
+ Gets a value from the parameter store.
102
+ ```
103
+
104
+ ##### set NAME VALUE
105
+
106
+ ```
107
+ Usage:
108
+ smps-cli set NAME VALUE --type=TYPE
109
+
110
+ Options:
111
+ --type=TYPE
112
+ # Default: String
113
+ [--key=ARN] # KMS key for SecureString encryption/decryption
114
+ [--role=PROFILE] # IAM profile/role to use. From ~/.aws/config
115
+ [--region=REGION] # Override the aws region
116
+ [--debug=N] # AwsSession debug level.
117
+ # Default: 0
118
+ [--user-data], [--no-user-data] # Retrieve the name or path, and the key values from the userdata
119
+
120
+ Set the parameter to this value
121
+ ```
26
122
 
27
123
  ## Development
28
124
 
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.
125
+ After checking out the repo, run `./bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
126
 
31
127
  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
128
 
129
+ To test the `smps` utility on your local machine, run `bundle exec smps --help` to see command-line options.
130
+
33
131
  ## Contributing
34
132
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smps.
133
+ We use git flow in this project. To create your pull request, you can either use the git-flow helper or
134
+ manually create a new feature branch and pull request.
135
+
136
+ 1. Fork it ( https://github.com/vrtdev/smps/fork )
137
+ 2. Clone your forked repository.
138
+ 3. Create your feature branch (`git checkout -b feature/my-new-feature`)
139
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
140
+ 5. Push to the branch (`git push origin feature/my-new-feature`)
141
+ 6. Create a new Pull Request. Remember to make the pull request against the `develop` branch.
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vrtdev/smps.
data/changelog.md CHANGED
@@ -5,49 +5,114 @@ Version format based on http://semver.org/
5
5
 
6
6
  ## [Unreleased]
7
7
 
8
+ ## [0.5.4] - 2021-09-17
9
+
10
+ ### Changed
11
+
12
+ - Added automatic client side throttling to avoid Aws::SSM::Errors::ThrottlingException.
13
+
14
+ ## [0.5.3] - 2020-04-01
15
+
16
+ ### Changed
17
+
18
+ - Don't use .to_h as it is not available on jruby (puppetserver)
19
+
20
+ ## [0.5.2] - 2020-03-11
21
+
22
+ ### Changed
23
+
24
+ - Make return of parameters_by_path a hash with key : value and print it cleanly on cli
25
+
26
+ ## [0.5.1] - 2019-04-29
27
+
28
+ ### Changed
29
+
30
+ - Updated doc.
31
+ - Lower required Ruby version for smps gem.
32
+
33
+ ## [0.5.0.pre]
34
+
35
+ ### Changed
36
+
37
+ - Split in 2 separate gems: smps and smps-cli.
38
+
39
+ ## [0.4.1] - 2018-09-25
40
+
41
+ ### Changed
42
+
43
+ - Change dependency on thor to 0.19.* to work with available native debian packages
44
+
45
+ ## [0.4.0] - 2018-09-18
46
+
47
+ ### Added
48
+
49
+ - New cli executable based on thor
50
+
8
51
  ## [0.3.4] - 2017-12-20
52
+
9
53
  ### Changed
54
+
10
55
  - Add http timeout to region get in helper script
11
56
 
12
57
  ## [0.3.3] - 2017-11-17
58
+
13
59
  ### Changed
60
+
14
61
  - Add next_token handling for get_parameters_by_path
15
62
 
16
63
  ## [0.3.2] - 2017-11-16
64
+
17
65
  ### Changed
66
+
18
67
  - Set \@decrypt param to true by default
19
68
 
20
69
  ## [0.3.1] - 2017-11-16
70
+
21
71
  ### Added
72
+
22
73
  - Test script param for fetch by_path
23
74
 
24
75
  ## [0.3.0] - 2017-10-13
76
+
25
77
  ### Added
78
+
26
79
  - parameters_by_path method
27
80
 
28
81
  ## [0.2.3] - 2017-10-06
82
+
29
83
  ### Changed
84
+
30
85
  - Update gem dependency specification
31
86
 
32
87
  ## [0.2.2] - 2017-10-06
88
+
33
89
  ### Added
90
+
34
91
  - Parameters for creating / writing SecureString
35
92
 
36
93
  ## [0.2.1] - 2017-10-05
94
+
37
95
  ### Changed
96
+
38
97
  - Class structure improvement
39
98
  - move optional requires into if block
40
99
 
41
100
  ## [0.2.0] - 2017-10-05
101
+
42
102
  ### Added
103
+
43
104
  - smps cli query script
44
105
 
45
106
  ## [0.1.3] - 2017-10-05
107
+
46
108
  ### Added
109
+
47
110
  - ...
48
111
 
49
112
  ### Changed
113
+
50
114
  - ...
51
115
 
52
116
  ### Removed
117
+
53
118
  - ...
data/exe/smps CHANGED
@@ -1,128 +1,119 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  require 'getoptlong'
3
5
  require 'smps'
4
6
  require 'net/http'
5
7
  require 'pp'
6
8
 
7
- def clidoc
8
- puts <<-CLIDOC
9
-
10
- #{$PROGRAM_NAME} [OPTION]
11
-
12
- -h, --help:
13
- show help
9
+ # Dummy class to initialize our 'program' and make
10
+ # use of the AWsHelpers we have defined.
11
+ class SmpsCLI
12
+ include SmPs::AwsHelpers
14
13
 
15
- -r, --role <rolename>
16
- IAM role to use. From ~/.aws/config
17
- Use this for interactive use on your workstation.
14
+ def clidoc
15
+ puts <<~CLIDOC
16
+ #{$PROGRAM_NAME} [OPTION]
18
17
 
19
- -p, --param <param_name>
20
- Parameter name.
18
+ -h, --help:
19
+ show help
21
20
 
22
- -b --by_path <path>
23
- Path name
21
+ -r, --role <rolename>
22
+ IAM role to use. From ~/.aws/config
23
+ Use this for interactive use on your workstation.
24
24
 
25
- -v, --value <new_value>
26
- Value to assign
25
+ -p, --param <param_name>
26
+ Parameter name.
27
27
 
28
- -t, --type <type>
29
- Parameter type.
30
- One of [String StringList SecureString]
28
+ -b --by_path <path>
29
+ Path name
31
30
 
32
- -k, --key <key_id>
33
- Key for SecureString encrypting.
31
+ -v, --value <new_value>
32
+ Value to assign
34
33
 
35
- -d, --debug [level]:
36
- Debug level.
37
-
38
- CLIDOC
39
- end
34
+ -t, --type <type>
35
+ Parameter type.
36
+ One of [String StringList SecureString]
40
37
 
41
- def region
42
- require 'net/http'
43
- require 'uri'
44
- require 'json'
45
-
46
- uri = URI.parse('http://169.254.169.254/latest/dynamic/instance-identity/document')
47
- http = Net::HTTP.new(uri.host, uri.port)
48
-
49
- http.read_timeout = 2
50
- http.open_timeout = 2
51
- response = http.start { |h| h.get(uri.path) }
52
-
53
- JSON.parse(response.body)['region']
54
- end
38
+ -k, --key <key_id>
39
+ Key for SecureString encrypting.
55
40
 
56
- opts = GetoptLong.new(
57
- ['--help', '-h', GetoptLong::NO_ARGUMENT],
58
- ['--role', '-r', GetoptLong::REQUIRED_ARGUMENT],
59
- ['--param', '-p', GetoptLong::REQUIRED_ARGUMENT],
60
- ['--by_path', '-b', GetoptLong::REQUIRED_ARGUMENT],
61
- ['--value', '-v', GetoptLong::REQUIRED_ARGUMENT],
62
- ['--type', '-t', GetoptLong::REQUIRED_ARGUMENT],
63
- ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
64
- ['--debug', '-d', GetoptLong::OPTIONAL_ARGUMENT]
65
- )
66
-
67
- role = nil
68
- param = nil
69
- by_path = nil
70
- value = nil
71
- type = nil
72
- key_id = nil
73
- debug = 0
74
-
75
- opts.each do |opt, arg|
76
- case opt
77
- when '--help'
78
- clidoc
79
- exit
80
- when '--role'
81
- role = arg
82
- when '--param'
83
- param = arg
84
- when '--by_path'
85
- by_path = arg
86
- when '--value'
87
- value = arg
88
- when '--type'
89
- type = arg
90
- when '--key'
91
- key_id = arg
92
- when '--debug'
93
- debug = if arg == ''
94
- 1
95
- else
96
- arg.to_i
97
- end
41
+ -d, --debug [level]:
42
+ Debug level.
43
+ CLIDOC
98
44
  end
99
- end
100
-
101
- if role
102
- require 'awssession'
103
- require 'aws_config'
104
-
105
- profile_name = role
106
- profile = AWSConfig[profile_name]
107
- profile['name'] = profile_name
108
-
109
- awssession = AwsSession.new(profile: profile, debug: debug)
110
- awssession.start
111
45
 
112
- smps = SmPs.new(credentials: awssession.credentials)
113
- else
114
- Aws.config.update(region: region)
115
- smps = SmPs.new
116
- end
117
-
118
- if param
119
- parameter = smps.parameter(name: param, type: type, key_id: key_id)
120
- parameter.write!(value) if value
121
- puts parameter.to_s
46
+ def run
47
+ opts = GetoptLong.new(
48
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
49
+ ['--role', '-r', GetoptLong::REQUIRED_ARGUMENT],
50
+ ['--param', '-p', GetoptLong::REQUIRED_ARGUMENT],
51
+ ['--by_path', '-b', GetoptLong::REQUIRED_ARGUMENT],
52
+ ['--value', '-v', GetoptLong::REQUIRED_ARGUMENT],
53
+ ['--type', '-t', GetoptLong::REQUIRED_ARGUMENT],
54
+ ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
55
+ ['--region', GetoptLong::OPTIONAL_ARGUMENT],
56
+ ['--debug', '-d', GetoptLong::OPTIONAL_ARGUMENT]
57
+ )
58
+
59
+ role = nil
60
+ param = nil
61
+ by_path = nil
62
+ value = nil
63
+ type = nil
64
+ key_id = nil
65
+ region = nil
66
+ debug = 0
67
+
68
+ opts.each do |opt, arg|
69
+ case opt
70
+ when '--help'
71
+ clidoc
72
+ exit
73
+ when '--role'
74
+ role = arg
75
+ when '--param'
76
+ param = arg
77
+ when '--by_path'
78
+ by_path = arg
79
+ when '--value'
80
+ value = arg
81
+ when '--type'
82
+ type = arg
83
+ when '--key'
84
+ key_id = arg
85
+ when '--region'
86
+ region = arg
87
+ when '--debug'
88
+ debug = if arg == ''
89
+ 1
90
+ else
91
+ arg.to_i
92
+ end
93
+ end
94
+ end
95
+
96
+ if role
97
+ credentials = credentials_from_role(role)
98
+ smps = SmPs::Client.new(credentials: credentials)
99
+ else
100
+ configure_aws_region(region)
101
+ smps = SmPs::Client.new
102
+ end
103
+
104
+ if param
105
+ parameter = smps.parameter(name: param, type: type, key_id: key_id)
106
+ parameter.write!(value) if value
107
+ puts parameter.to_s
108
+ end
109
+
110
+ return unless by_path
111
+ parameters = smps.parameters_by_path(path: by_path)
112
+ parameters.each do |k, v|
113
+ puts "key: #{k}\nvalue: #{v}"
114
+ end
115
+ end
122
116
  end
123
117
 
124
- if by_path
125
- parameters = smps.parameters_by_path(path: by_path)
126
- pp parameters
127
- end
118
+ SmpsCLI.new.run
128
119
  # vim:set fileencoding=utf8 fileformat=unix filetype=ruby tabstop=2 expandtab:
data/lib/smps/aws.rb ADDED
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'yaml'
7
+
8
+ module SmPs
9
+ # Some helpers to setup and interact with various aws services.
10
+ module AwsHelpers
11
+ SSM_PARAMETER_TYPES = %w[String StringList SecureString].freeze
12
+ DEFAULT_USERDATA_URI = 'http://169.254.169.254/latest/user-data'
13
+
14
+ def aws_region
15
+ @aws_region ||= initialize_aws_region
16
+ end
17
+
18
+ def retrieve_from_userdata(key, userdata_type = :auto, source = DEFAULT_USERDATA_URI)
19
+ userdata = user_data(source, userdata_type)
20
+ userdata[key]
21
+ end
22
+
23
+ def user_data(source, type = :auto)
24
+ @user_data ||= initialize_user_data(source, type)
25
+ end
26
+
27
+ def credentials_from_role(role, debug = false)
28
+ @aws_session ||= initialize_aws_session_from_role(role, debug)
29
+ @aws_session.credentials
30
+ end
31
+
32
+ def configure_aws_region(region = nil)
33
+ if region
34
+ ::Aws.config.update(region: region)
35
+ else
36
+ ::Aws.config.update(region: aws_region)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def initialize_aws_session_from_role(role, debug)
43
+ require 'awssession'
44
+ require 'aws_config'
45
+ profile = AWSConfig[role]
46
+ profile['name'] = role
47
+ session = AwsSession.new(profile: profile, debug: debug)
48
+ session.start
49
+ session
50
+ end
51
+
52
+ def initialize_user_data(source, type)
53
+ data = get_resource(source)
54
+ case type
55
+ when 'yaml', :yaml
56
+ parse_yaml(data, true)
57
+ when 'json', :json
58
+ parse_json(data, true)
59
+ else
60
+ parse_auto(data)
61
+ end
62
+ end
63
+
64
+ def parse_auto(data)
65
+ parsed = parse_yaml(data)
66
+ parsed = parse_json(data) if parsed.nil?
67
+ raise ArgumentError, 'Could not determine the format of the user-data' if parsed.nil?
68
+ parsed
69
+ end
70
+
71
+ def parse_json(json, raise = false)
72
+ JSON.parse(json)
73
+ rescue JSON::ParserError => e
74
+ raise e if raise
75
+ nil
76
+ end
77
+
78
+ def parse_yaml(yaml, raise = false)
79
+ YAML.safe_load(yaml)
80
+ rescue YAML::ParseError => e
81
+ raise e if raise
82
+ nil
83
+ end
84
+
85
+ def initialize_aws_region
86
+ url = 'http://169.254.169.254/latest/dynamic/instance-identity/document'
87
+ JSON.parse(get_resource(url))['region']
88
+ end
89
+
90
+ def get_resource(url)
91
+ uri = URI.parse(url)
92
+ return File.read(url) unless %w[http https].include?(uri.scheme)
93
+ http = Net::HTTP.new(uri.host, uri.port)
94
+ http.read_timeout = 2
95
+ http.open_timeout = 2
96
+ response = http.start { |h| h.get(uri.path) }
97
+ response.body
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'smps/version'
4
+ require 'smps/parameter'
5
+ require 'aws-sdk-ssm'
6
+
7
+ module SmPs
8
+ # Presents a client interface with parameter parsing to aws ssm
9
+ # Allows querying and writing Paramstore parameters.
10
+ class Client
11
+ def initialize(options = {})
12
+ @options = options
13
+ @parameters = {}
14
+ end
15
+
16
+ # Creates (if needed) and returns the ssm_client.
17
+ def ssm_client
18
+ @ssm_client ||= initialize_ssm_client
19
+ end
20
+
21
+ # Creates a new SmPs::Parameter from the given options hash.
22
+ def parameter(options)
23
+ name = options.fetch(:name)
24
+ type = options[:type]
25
+ key_id = options[:key_id]
26
+ unless @parameters.key?(name)
27
+ @parameters[name] = SmPs::Parameter.new(
28
+ ssm: ssm_client,
29
+ name: name, type: type, key_id: key_id
30
+ )
31
+ end
32
+ @parameters[name]
33
+ end
34
+
35
+ # Creates a list of all parameters filtered by path.
36
+ def parameters_by_path(options)
37
+ @parameters_by_path_list = []
38
+ next_token = nil
39
+ while (params = get_parameters_by_path_with_token(options, next_token))
40
+ store_parameters params
41
+ next_token = params.next_token
42
+ break if next_token.nil? || next_token.empty?
43
+ end
44
+ parameters_result_hash options.fetch(:path), @parameters_by_path_list
45
+ end
46
+
47
+ protected
48
+
49
+ def parameters_result_hash(path, list)
50
+ path = "#{path}/" unless path.end_with?('/')
51
+ # .to_h is not available in jruby (the puppetserver ruby version)
52
+ # list.map { |p| [p.name.gsub(/#{Regexp.escape(path)}/, ''), p.value] }.to_h
53
+ arr = list.map { |p| [p.name.gsub(/#{Regexp.escape(path)}/, ''), p.value] }
54
+ Hash[arr]
55
+ end
56
+
57
+ # Get a parameter list by path using the next_token (if provided)
58
+ def get_parameters_by_path_with_token(options, next_token = nil)
59
+ ssm_client.get_parameters_by_path(
60
+ path: options.fetch(:path),
61
+ recursive: options[:recursive],
62
+ with_decryption: options.fetch(:decrypt, true),
63
+ next_token: next_token
64
+ )
65
+ end
66
+
67
+ private
68
+
69
+ def store_parameters(params)
70
+ return if params.nil?
71
+ params.parameters.each do |parameter|
72
+ @parameters[parameter.name] = SmPs::Parameter.new(
73
+ ssm: ssm_client, fetch: false,
74
+ name: parameter.name, value: parameter.value, type: parameter.type
75
+ )
76
+ @parameters_by_path_list << @parameters[parameter.name]
77
+ end
78
+ end
79
+
80
+ # def info
81
+ # describe_parameters
82
+ # end
83
+ #
84
+ # def parameter_list
85
+ # get_parameters
86
+ # end
87
+ #
88
+ # def by_path
89
+ # get_parameters_by_path
90
+ # end
91
+
92
+ def initialize_ssm_client
93
+ # see https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/SSM/Client.html#initialize-instance_method
94
+ all_options = @options.merge(max_attempts: 10,
95
+ retry_mode: 'adaptive')
96
+ # puts all_options
97
+ Aws::SSM::Client.new(all_options)
98
+ end
99
+ end
100
+ end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
 
2
- class SmPs
3
+ module SmPs
3
4
  # SmPs Parameter management
4
5
  class Parameter
5
6
  attr_accessor :name, :type, :key_id, :decrypt, :description
@@ -11,11 +12,11 @@ class SmPs
11
12
  @type = options[:type]
12
13
  @key_id = options[:key_id]
13
14
  @decrypt = options[:decrypt] || true
14
- fetch = options[:fetch]
15
- fetch = true if options[:fetch].nil?
15
+ fetch = options[:fetch] || true
16
16
  parameter if fetch
17
17
  end
18
18
 
19
+ # Gets a parameter from Aws SSM.
19
20
  def parameter
20
21
  resp = @ssm.get_parameter(
21
22
  name: @name, with_decryption: @decrypt
@@ -27,15 +28,18 @@ class SmPs
27
28
  @exists = false
28
29
  end
29
30
 
31
+ # Prints the current value.
30
32
  def to_s
31
33
  @value
32
34
  end
33
35
 
36
+ # Returns the value (String) or array if the `type` is StringList
34
37
  def value
35
38
  return @value.split(',') if @type == 'StringList'
36
39
  @value
37
40
  end
38
41
 
42
+ # Set the value and mark the parameter as changed.
39
43
  def value=(value)
40
44
  @changed = true if value != @value
41
45
  @value = value
@@ -45,6 +49,7 @@ class SmPs
45
49
  @exists
46
50
  end
47
51
 
52
+ # Updates the parameter in the parameter store (remote).
48
53
  def write!(value = nil)
49
54
  @changed = true if value != @value
50
55
  @value = value if value
@@ -56,10 +61,12 @@ class SmPs
56
61
  @value
57
62
  end
58
63
 
64
+ # Not implemented yet.
59
65
  def history
60
66
  # get_parameter_history
61
67
  end
62
68
 
69
+ # Not implemented yet.
63
70
  def tag
64
71
  # add_tags_to_resource
65
72
  # remove_tags_from_resource
data/lib/smps/version.rb CHANGED
@@ -1,3 +1,6 @@
1
- class SmPs
2
- VERSION = '0.3.4'.freeze
1
+ # frozen_string_literal: true
2
+
3
+ module SmPs
4
+ # Current version.
5
+ VERSION = '0.5.4'
3
6
  end
data/lib/smps.rb CHANGED
@@ -1,80 +1,12 @@
1
- require 'smps/version'
2
- require 'smps/parameter'
3
- require 'aws-sdk-ssm'
4
-
5
- # SmPs class queries and writes Paramstore parameters
6
- class SmPs
7
- def initialize(options = {})
8
- @credentials = options[:credentials]
9
- @parameters = {}
10
- end
11
-
12
- def ssm_client
13
- @ssm || @ssm = if @credentials.nil?
14
- Aws::SSM::Client.new
15
- else
16
- Aws::SSM::Client.new(credentials: @credentials)
17
- end
18
- end
1
+ # frozen_string_literal: true
19
2
 
20
- def parameter(options)
21
- name = options.fetch(:name)
22
- type = options[:type]
23
- key_id = options[:key_id]
24
- unless @parameters.key?(name)
25
- @parameters[name] = SmPs::Parameter.new(
26
- ssm: ssm_client,
27
- name: name, type: type, key_id: key_id
28
- )
29
- end
30
- @parameters[name]
31
- end
32
-
33
- def parameters_by_path(options)
34
- path = options.fetch(:path)
35
- recursive = options[:recursive]
36
- decrypt = options[:decrypt] || true
37
- @parameters_by_path_list = []
38
- # while result has 'next_token'
39
- fetch_more = true
40
- next_token = nil
41
- while fetch_more
42
- params = ssm_client.get_parameters_by_path(
43
- path: path,
44
- recursive: recursive,
45
- with_decryption: decrypt,
46
- next_token: next_token
47
- )
48
- if params.next_token
49
- next_token = params.next_token
50
- else
51
- fetch_more = false
52
- end
53
- store_parameters params
54
- end
55
- @parameters_by_path_list
56
- end
3
+ require 'smps/version'
4
+ require 'smps/client'
5
+ require 'smps/aws'
57
6
 
58
- def store_parameters(params)
59
- return if params.nil?
60
- params.parameters.each do |parameter|
61
- @parameters[parameter.name] = SmPs::Parameter.new(
62
- ssm: ssm_client, fetch: false,
63
- name: parameter.name, value: parameter.value, type: parameter.type
64
- )
65
- @parameters_by_path_list << @parameters[parameter.name]
66
- end
7
+ # Dummy module. Includes all required classes/libs
8
+ module SmPs
9
+ def self.new(*args)
10
+ Client.new(*args)
67
11
  end
68
-
69
- # def info
70
- # describe_parameters
71
- # end
72
- #
73
- # def parameter_list
74
- # get_parameters
75
- # end
76
- #
77
- # def by_path
78
- # get_parameters_by_path
79
- # end
80
12
  end
data/license.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2017 VRT.be
1
+ Copyright 2018 Vlaamse Radio- en Televisieomroeporganisatie
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of
4
4
  this software and associated documentation files (the "Software"), to deal in
metadata CHANGED
@@ -1,128 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan - Zipkid - Goethals
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-20 00:00:00.000000000 Z
11
+ date: 2024-01-03 00:00:00.000000000 Z
12
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_config
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: awssession
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: awesome_print
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
13
  - !ruby/object:Gem::Dependency
98
14
  name: aws-sdk-core
99
15
  requirement: !ruby/object:Gem::Requirement
100
16
  requirements:
101
- - - "~>"
17
+ - - ">"
102
18
  - !ruby/object:Gem::Version
103
19
  version: '3.0'
104
20
  type: :runtime
105
21
  prerelease: false
106
22
  version_requirements: !ruby/object:Gem::Requirement
107
23
  requirements:
108
- - - "~>"
24
+ - - ">"
109
25
  - !ruby/object:Gem::Version
110
26
  version: '3.0'
111
27
  - !ruby/object:Gem::Dependency
112
28
  name: aws-sdk-ssm
113
29
  requirement: !ruby/object:Gem::Requirement
114
30
  requirements:
115
- - - "~>"
31
+ - - ">"
116
32
  - !ruby/object:Gem::Version
117
33
  version: '1.0'
118
34
  type: :runtime
119
35
  prerelease: false
120
36
  version_requirements: !ruby/object:Gem::Requirement
121
37
  requirements:
122
- - - "~>"
38
+ - - ">"
123
39
  - !ruby/object:Gem::Version
124
40
  version: '1.0'
125
- description: SMPS - Systems Manager Parameter Store
41
+ description: SMPS - Systems Manager Parameter Store.
126
42
  email:
127
43
  - stefan.goethals@vrt.be
128
44
  executables:
@@ -130,29 +46,21 @@ executables:
130
46
  extensions: []
131
47
  extra_rdoc_files: []
132
48
  files:
133
- - ".gitignore"
134
- - ".rspec"
135
- - ".travis.yml"
136
- - Gemfile
137
49
  - README.md
138
- - Rakefile
139
- - bin/console
140
- - bin/setup
141
- - bin/smps-use-ec2.rb
142
- - bin/smps-use.rb
143
50
  - changelog.md
144
51
  - exe/smps
145
52
  - lib/smps.rb
53
+ - lib/smps/aws.rb
54
+ - lib/smps/client.rb
146
55
  - lib/smps/parameter.rb
147
56
  - lib/smps/version.rb
148
57
  - license.txt
149
- - smps.gemspec
150
58
  homepage: http://github.com/vrtdev/smps
151
59
  licenses:
152
60
  - MIT
153
61
  metadata:
154
62
  allowed_push_host: https://rubygems.org
155
- post_install_message:
63
+ post_install_message:
156
64
  rdoc_options: []
157
65
  require_paths:
158
66
  - lib
@@ -160,16 +68,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
68
  requirements:
161
69
  - - ">="
162
70
  - !ruby/object:Gem::Version
163
- version: '0'
71
+ version: '2.3'
164
72
  required_rubygems_version: !ruby/object:Gem::Requirement
165
73
  requirements:
166
74
  - - ">="
167
75
  - !ruby/object:Gem::Version
168
76
  version: '0'
169
77
  requirements: []
170
- rubyforge_project:
171
- rubygems_version: 2.6.13
172
- signing_key:
78
+ rubygems_version: 3.1.6
79
+ signing_key:
173
80
  specification_version: 4
174
81
  summary: SMPS - Systems Manager Parameter Store
175
82
  test_files: []
data/.gitignore DELETED
@@ -1,15 +0,0 @@
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
- *-session.yaml
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.4.2
5
- before_install: gem install bundler -v 1.15.4
data/Gemfile DELETED
@@ -1,6 +0,0 @@
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 smps.gemspec
6
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "smps"
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__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
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
data/bin/smps-use-ec2.rb DELETED
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'smps'
4
-
5
- smps = SmPs.new
6
- param_abc = smps.parameter(name: 'abc')
7
- puts param_abc.to_s
8
-
9
- param_abc.write!('xyz')
10
- puts param_abc.to_s
11
-
12
- param_abc.write!('Another value.')
13
- puts param_abc.to_s
14
-
15
- param_z = smps.parameter(name: '/Zipkid/test1')
16
- puts param_z.to_s
data/bin/smps-use.rb DELETED
@@ -1,44 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'awssession'
4
- require 'smps'
5
-
6
- require 'aws_config'
7
- require 'awesome_print'
8
-
9
- profile_name = ARGV[0]
10
- profile = AWSConfig[profile_name]
11
- profile['name'] = profile_name
12
-
13
- awssession = AwsSession.new(profile: profile)
14
- awssession.start
15
-
16
- smps = SmPs.new(credentials: awssession.credentials)
17
-
18
- puts '1'
19
- pl = smps.parameters_by_path(path: '/aem/dev/aem--author/packages')
20
- puts '2'
21
- # ap pl
22
- pl.each do |p|
23
- puts ' ---- '
24
- ap p
25
- puts p.name
26
- ap p.value
27
- end
28
-
29
- # exit
30
-
31
- # param = smps.parameter(name: 'abc')
32
- param_abc = smps.parameter(name: 'abc')
33
- puts param_abc.to_s
34
- # puts "#{param_abc}"
35
-
36
- param_abc.write!('xyz')
37
- puts param_abc.to_s
38
- # OR
39
- param_abc.value = 'def'
40
- param_abc.write!
41
- puts param_abc.to_s
42
-
43
- param_z = smps.parameter(name: '/Zipkid/test1')
44
- puts param_z.to_s
data/smps.gemspec DELETED
@@ -1,42 +0,0 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
-
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'smps/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'smps'
8
- spec.version = SmPs::VERSION
9
- spec.licenses = ['MIT']
10
- spec.authors = ['Stefan - Zipkid - Goethals']
11
- spec.email = ['stefan.goethals@vrt.be']
12
-
13
- spec.summary = 'SMPS - Systems Manager Parameter Store'
14
- spec.description = 'SMPS - Systems Manager Parameter Store'
15
- spec.homepage = 'http://github.com/vrtdev/smps'
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_config'
37
- spec.add_development_dependency 'awssession'
38
- spec.add_development_dependency 'awesome_print'
39
-
40
- spec.add_runtime_dependency 'aws-sdk-core', '~> 3.0'
41
- spec.add_runtime_dependency 'aws-sdk-ssm', '~> 1.0'
42
- end