manageiq-password 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2d3819482dabaf189238380c5a9a3f601dc8d5014b0d65c314535c5b8de8c61c
4
+ data.tar.gz: fa0af27e00727164700cf625267a48324187e8a21e5cff34adfaabffef76bee0
5
+ SHA512:
6
+ metadata.gz: be829301516f7d0003daed0247f093d5b70026b0101c9326c4f46ad16a14d774766a7109bfcc0ce3e57db5afd82286ec9211f12ee3710571cad61366c4a2bd4f
7
+ data.tar.gz: bdee9e6f778999408cf4b266005a121ffd72689d790a41eedb5d43aaa6318c002c7c586f75cd4b401b349df53b03e2e12dab59c5d919fdb6a16c2f301245dc4b
@@ -0,0 +1,30 @@
1
+ ---
2
+ version: '2'
3
+ prepare:
4
+ fetch:
5
+ - url: https://raw.githubusercontent.com/ManageIQ/guides/master/.rubocop_base.yml
6
+ path: ".rubocop_base.yml"
7
+ - url: https://raw.githubusercontent.com/ManageIQ/guides/master/.rubocop_cc_base.yml
8
+ path: ".rubocop_cc_base.yml"
9
+ checks:
10
+ argument-count:
11
+ enabled: false
12
+ complex-logic:
13
+ enabled: false
14
+ file-lines:
15
+ enabled: false
16
+ method-complexity:
17
+ config:
18
+ threshold: 11
19
+ method-count:
20
+ enabled: false
21
+ method-lines:
22
+ enabled: false
23
+ nested-control-flow:
24
+ enabled: false
25
+ return-statements:
26
+ enabled: false
27
+ plugins:
28
+ rubocop:
29
+ enabled: true
30
+ config: ".rubocop_cc.yml"
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ /*.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/ManageIQ/guides/master/.rubocop_base.yml
3
+ - .rubocop_local.yml
@@ -0,0 +1,4 @@
1
+ inherit_from:
2
+ - .rubocop_base.yml
3
+ - .rubocop_cc_base.yml
4
+ - .rubocop_local.yml
File without changes
@@ -0,0 +1,15 @@
1
+ ---
2
+ language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ - 2.5.3
8
+ before_install:
9
+ - gem install bundler
10
+ before_script:
11
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
+ - chmod +x ./cc-test-reporter
13
+ - "./cc-test-reporter before-build"
14
+ after_script:
15
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at agrare@redhat.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/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 manageiq-password.gemspec
6
+ gemspec
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,41 @@
1
+ # ManageIQ Password
2
+
3
+ [![Build Status](https://travis-ci.org/ManageIQ/manageiq-password.svg)](https://travis-ci.org/ManageIQ/manageiq-password)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/85064711d083dea96636/maintainability)](https://codeclimate.com/github/ManageIQ/manageiq-password/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/85064711d083dea96636/test_coverage)](https://codeclimate.com/github/ManageIQ/manageiq-password/test_coverage)
6
+
7
+ A simple encryption util for storing passwords in a database.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'manageiq-password'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install manageiq-password
24
+
25
+ ## Development
26
+
27
+ 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
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ManageIQ/manageiq-password. 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.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the ManageIQ Password project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ManageIQ/manageiq-password/blob/master/CODE_OF_CONDUCT.md).
@@ -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,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "manageiq/password"
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,271 @@
1
+ require "manageiq/password/version"
2
+
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'yaml'
6
+
7
+ module ManageIQ
8
+ class Password
9
+ class PasswordError < StandardError; end
10
+
11
+ CURRENT_VERSION = "2"
12
+ REGEXP = /v([0-2]):\{([^}]*)\}/
13
+ REGEXP_PASSWORD = /v[0-2](:\{[^}]*\}|%3A%7B.*?%7D)/ # for "v2:{...}" or its URL encoded string
14
+ REGEXP_START_LINE = /^#{REGEXP}/
15
+ MASK = '********'.freeze
16
+
17
+ attr_reader :encStr
18
+
19
+ def initialize(str = nil)
20
+ return unless str
21
+
22
+ @encStr = encrypt(str)
23
+ end
24
+
25
+ def encrypt(str, ver = "v2", key = self.class.keys[ver])
26
+ value = key.encrypt64(str).delete("\n") unless str.nil? || str.empty?
27
+ "#{ver}:{#{value}}"
28
+ end
29
+
30
+ def decrypt(str, legacy = false)
31
+ if str.nil? || str.empty?
32
+ str
33
+ else
34
+ ver, enc = self.class.split(str)
35
+ return "" if enc.empty?
36
+
37
+ ver ||= "0" # if we don't know what it is, just assume legacy
38
+ key_name = (ver == "2" && legacy) ? "alt" : "v#{ver}"
39
+
40
+ begin
41
+ self.class.keys[key_name].decrypt64(enc).force_encoding('UTF-8')
42
+ rescue
43
+ raise PasswordError, "can not decrypt v#{ver}_key encrypted string"
44
+ end
45
+ end
46
+ end
47
+
48
+ def recrypt(str)
49
+ return str if str.nil? || str.empty?
50
+ decrypted_str =
51
+ begin
52
+ # if a legacy v2 key exists, give decrypt the option to use that
53
+ decrypt(str, self.class.keys["alt"])
54
+ rescue
55
+ source_version = self.class.split(str).first || "0"
56
+ if source_version == "0" # it probably wasn't encrypted
57
+ return str
58
+ elsif source_version == "2" # tried with an alt key, see if regular v2 key works
59
+ decrypt(str)
60
+ else
61
+ raise
62
+ end
63
+ end
64
+ encrypt(decrypted_str)
65
+ end
66
+
67
+ def self.encrypt(str)
68
+ new.encrypt(str) if str
69
+ end
70
+
71
+ def self.decrypt(str)
72
+ new.decrypt(str)
73
+ end
74
+
75
+ def self.encrypted?(str)
76
+ !!split(str).first
77
+ end
78
+
79
+ def self.md5crypt(str)
80
+ cmd = "openssl passwd -1 -salt \"miq\" \"#{try_decrypt(str)}\""
81
+ `#{cmd}`.split("\n").first
82
+ end
83
+
84
+ def self.sysprep_crypt(str)
85
+ Base64.encode64("#{try_decrypt(str)}AdministratorPassword".encode("UTF-16LE")).delete("\n")
86
+ end
87
+
88
+ def self.sanitize_string(s)
89
+ s.gsub(REGEXP_PASSWORD, MASK)
90
+ end
91
+
92
+ def self.sanitize_string!(s)
93
+ s.gsub!(REGEXP_PASSWORD, MASK)
94
+ end
95
+
96
+ def self.try_decrypt(str)
97
+ encrypted?(str) ? decrypt(str) : str
98
+ end
99
+
100
+ def self.try_encrypt(str)
101
+ encrypted?(str) ? str : encrypt(str)
102
+ end
103
+
104
+ # @returns [ver, enc]
105
+ def self.split(encrypted_str)
106
+ if encrypted_str.nil? || encrypted_str.empty?
107
+ [nil, encrypted_str]
108
+ else
109
+ if encrypted_str =~ REGEXP_START_LINE
110
+ [$1, $2]
111
+ elsif legacy = extract_erb_encrypted_value(encrypted_str)
112
+ if legacy =~ REGEXP_START_LINE
113
+ [$1, $2]
114
+ else
115
+ ["0", legacy]
116
+ end
117
+ else
118
+ [nil, encrypted_str]
119
+ end
120
+ end
121
+ end
122
+
123
+ def self.key_root
124
+ @@key_root ||= ENV["KEY_ROOT"]
125
+ end
126
+
127
+ def self.key_root=(key_root)
128
+ clear_keys
129
+ @@key_root = key_root
130
+ end
131
+
132
+ def self.clear_keys
133
+ @@all_keys = nil
134
+ end
135
+
136
+ def self.all_keys
137
+ keys.values
138
+ end
139
+
140
+ def self.keys
141
+ @@all_keys ||= {"v2" => load_v2_key}.delete_if { |_n, v| v.nil? }
142
+ end
143
+
144
+ def self.v2_key
145
+ keys["v2"]
146
+ end
147
+
148
+ def self.load_v2_key
149
+ load_key_file("v2_key") || begin
150
+ key_file = File.expand_path("v2_key", key_root)
151
+ msg = <<-EOS
152
+ #{key_file} doesn't exist!
153
+ On an appliance, it should be generated on boot by evmserverd.
154
+
155
+ If you're a developer, you can copy the #{key_file}.dev to #{key_file}.
156
+
157
+ Caution, using the developer key will allow anyone with the public developer key to decrypt the two-way
158
+ passwords in your database.
159
+ EOS
160
+ Kernel.warn msg
161
+ end
162
+ end
163
+
164
+ def self.add_legacy_key(filename, type = "alt")
165
+ key = load_key_file(filename, type != :v0)
166
+ keys[type.to_s] = key if key
167
+ key
168
+ end
169
+
170
+ # used by tests only
171
+ def self.v2_key=(key)
172
+ (@@all_keys ||= {})["v2"] = key
173
+ end
174
+
175
+ def self.generate_symmetric(filename = nil)
176
+ Key.new.tap { |key| store_key_file(filename, key) if filename }
177
+ end
178
+
179
+ protected
180
+
181
+ def self.store_key_file(filename, key)
182
+ File.write(filename, key.to_h.to_yaml)
183
+ end
184
+
185
+ def self.load_key_file(filename, recent = true)
186
+ return filename if filename.respond_to?(:decrypt64)
187
+
188
+ # if it is an absolute path, or relative to pwd, leave as is
189
+ # otherwise, look in key root for it
190
+ filename = File.expand_path(filename, key_root) unless File.exist?(filename)
191
+ if !File.exist?(filename)
192
+ nil
193
+ elsif recent
194
+ params = YAML.load_file(filename)
195
+ Key.new(*params.values_at(:algorithm, :key, :iv))
196
+ else
197
+ params = YAML.load_file(filename)
198
+ algorithm, key, iv = params.values_at(:algorithm, :key, :iv)
199
+ Key.new(algorithm, key && Base64.encode64(key), iv && Base64.encode64(iv))
200
+ end
201
+ end
202
+
203
+ def self.extract_erb_encrypted_value(value)
204
+ return $1 if value =~ /\A<%= (?:MiqPassword|DB_PASSWORD|ManageIQ::Password)\.decrypt\(['"]([^'"]+)['"]\) %>\Z/
205
+ end
206
+
207
+ class Key
208
+ GENERATED_KEY_SIZE = 32
209
+
210
+ def self.generate_key(password = nil, salt = nil)
211
+ password ||= OpenSSL::Random.random_bytes(GENERATED_KEY_SIZE)
212
+ Base64.strict_encode64(Digest::SHA256.digest("#{password}#{salt}")[0, GENERATED_KEY_SIZE])
213
+ end
214
+
215
+ def initialize(algorithm = nil, key = nil, iv = nil)
216
+ @algorithm = algorithm || "aes-256-cbc"
217
+ @key = key || generate_key
218
+ @raw_key = Base64.decode64(@key)
219
+ @iv = iv
220
+ @raw_iv = iv && Base64.decode64(iv)
221
+ end
222
+
223
+ def encrypt(str)
224
+ apply(:encrypt, str)
225
+ end
226
+
227
+ def encrypt64(str)
228
+ Base64.strict_encode64(encrypt(str))
229
+ end
230
+
231
+ def decrypt(str)
232
+ apply(:decrypt, str)
233
+ end
234
+
235
+ def decrypt64(str)
236
+ decrypt(Base64.decode64(str))
237
+ end
238
+
239
+ def to_s
240
+ @key
241
+ end
242
+
243
+ def to_h
244
+ {
245
+ :algorithm => @algorithm,
246
+ :key => @key
247
+ }.tap do |h|
248
+ h[:iv] = @iv if @iv
249
+ end
250
+ end
251
+
252
+ private
253
+
254
+ def generate_key
255
+ raise "key can only be generated for the aes-256-cbc algorithm" unless @algorithm == "aes-256-cbc"
256
+ self.class.generate_key
257
+ end
258
+
259
+ def apply(mode, str)
260
+ c = OpenSSL::Cipher.new(@algorithm)
261
+ c.public_send(mode)
262
+ c.key = @raw_key
263
+ c.iv = @raw_iv if @raw_iv
264
+ c.update(str) << c.final
265
+ end
266
+ end
267
+ end
268
+ end
269
+
270
+ # Backward compatibility previous class names
271
+ DB_PASSWORD = MiqPassword = ManageIQ::Password
@@ -0,0 +1,5 @@
1
+ module ManageIQ
2
+ class Password
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "manageiq/password/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "manageiq-password"
8
+ spec.version = ManageIQ::Password::VERSION
9
+ spec.authors = "ManageIQ Authors"
10
+
11
+ spec.summary = %q{A simple encryption util for storing passwords in a database.}
12
+ spec.homepage = "https://github.com/ManageIQ/manageiq-password"
13
+ spec.licenses = ["Apache-2.0"]
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "simplecov"
26
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: manageiq-password
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ManageIQ Authors
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-08 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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: simplecov
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
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".codeclimate.yml"
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".rubocop_cc.yml"
80
+ - ".rubocop_local.yml"
81
+ - ".travis.yml"
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/manageiq/password.rb
90
+ - lib/manageiq/password/version.rb
91
+ - manageiq-password.gemspec
92
+ homepage: https://github.com/ManageIQ/manageiq-password
93
+ licenses:
94
+ - Apache-2.0
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.7.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A simple encryption util for storing passwords in a database.
116
+ test_files: []