rails_key_rotator 0.1.3 → 0.2.1

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: 1185099ebdd0aa95fe346dd3903a6a0cb56540a0e5b40acf8b917342ca33912a
4
- data.tar.gz: 6bec30ca3c50f0870d05b9377a94f0f7afe69ec8eb36d0a21dbde210b73c6d3b
3
+ metadata.gz: 29eb6c4fb0ee94eb94483058e009c91b40bc017ff13da75d90f60249c51df5c7
4
+ data.tar.gz: cb203507ac300b69adac536d0aee9685c42a09c78b0b3a9d391bfbc4a0ba77c2
5
5
  SHA512:
6
- metadata.gz: 20d3d663fe20d4cd2d08a7d5e5e6c7f8c55837f222f52f1a237f667ec7760b25744f1f8c4e90cb3c22f47c21c423cc28c6a1c7e4826029631dff45eb4ef855f9
7
- data.tar.gz: 8a1c6af613656d15ad0380aa1d4e43a69289e1dd4bd7ccfbe9e242d7c8df7c82c7e0bcc86c727c179786085fbf85eb06ccba8bcdb9ceae2cf8978bb9b3e6d868
6
+ metadata.gz: 87a8d7106191f090426e9d9d6a7d997fa5b972c1a840c76fef0250adab92e56a309bc0a9ca1a127e7ceeecf9195e10a57006adee2748e0d5e9e30e14315162cf
7
+ data.tar.gz: 74cb11e1eb92733a54fa3e2051ade59b108578b1b41e02e7e8105297cec4fd96978bbec962214a6b4e8606f358630f333028c7fb0bc1c226b18413811ff84007
data/.projections.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "lib/*.rb": { "alternate": "spec/{}_spec.rb" }
3
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "recommendations": [
3
+ "testdouble.vscode-alternate-alternate-file"
4
+ ]
5
+ }
data/README.md CHANGED
@@ -12,33 +12,22 @@ If bundler is not being used to manage dependencies, install the gem by executin
12
12
 
13
13
  ## Usage
14
14
 
15
- > _*⚠️ !!! WARNING !!! ⚠️*_
16
- > _*⚠️ DON'T FORGET TO HANDOUT THE NEW KEY TO YOUR COLLEAGUES! ⚠️*_
17
-
18
- 1. First create a new key w/ `dip rails runner "puts ActiveSupport::EncryptedConfiguration.generate_key"` and deploy this in `RAILS_MASTER_KEY_NEW` on the targeted infrastructure.
19
-
20
- 2. While waiting on deploying this variable, create a new encrypted file:
21
-
22
- ```shell
23
- # Copy the output current credentials
24
- dip credentials show -e development
25
- # Backup current credentials
26
- mv -i config/credentials/development.yml.enc config/credentials/development.yml.enc.bak-$(date "+%Y-%m-%d-%H%M")
27
- # Backup current key
28
- mv -i config/credentials/development.key config/credentials/development.key.bak-$(date "+%Y-%m-%d-%H%M")
29
- # Save the new key into file
30
- echo d92599b046b58ab2d4158212e6d27162 > config/credentials/development.key
31
- # Create new credentials file w/
32
- dip credentials -e development
33
- # Verify content
34
- dip credentials show -e development
35
- ```
36
-
37
- 3. Commit to Github and deploy new encrypted file.
15
+ > **Warning**
16
+ > **DON'T FORGET TO HANDOUT THE NEW KEY TO YOUR COLLEAGUES!**
17
+
18
+ 1. Run the rake taks
19
+
20
+ bundle rake key_rotator:rotate
21
+
22
+ This will backup current key / credentials, create a new key and saves encrypts the credentails w/ this new key
23
+
24
+ 2. Deploying this variable as an env `RAILS_MASTER_KEY_NEW`
25
+
26
+ 3. Commit and deploy new encrypted file.
38
27
 
39
28
  4. After a while when everything is back in sync replace `RAILS_MASTER_KEY` w/ the new key and delete `RAILS_MASTER_KEY_NEW`
40
29
 
41
- ### Process
30
+ ## Process
42
31
 
43
32
  When we've defined `RAILS_MASTER_KEY_NEW` it means we are rotating the encryption key for our credentials. What we want to do then is:
44
33
 
@@ -53,13 +42,20 @@ See: https://www.reddit.com/r/rails/comments/x4sujc/deploying_a_rotated_credenti
53
42
 
54
43
  ## Development
55
44
 
56
- 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.
45
+ This project uses docker and [dip](https://github.com/bibendi/dip), a.k.a. the _Docker Interaction Program._
46
+
47
+ To use it:
48
+ ```shell
49
+ gem install dip
50
+ dip provision
51
+ dip guard # run specs
52
+ ```
57
53
 
58
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+ 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 `dip bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
55
 
60
56
  ## Contributing
61
57
 
62
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_key_rotator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rails_key_rotator/blob/master/CODE_OF_CONDUCT.md).
58
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/LeipeLeon/rails_key_rotator>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/LeipeLeon/rails_key_rotator/blob/master/CODE_OF_CONDUCT.md).
63
59
 
64
60
  ## License
65
61
 
@@ -67,4 +63,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
67
63
 
68
64
  ## Code of Conduct
69
65
 
70
- Everyone interacting in the RailsKeyRotator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails_key_rotator/blob/master/CODE_OF_CONDUCT.md).
66
+ Everyone interacting in the RailsKeyRotator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/LeipeLeon/rails_key_rotator/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -8,3 +8,10 @@ RSpec::Core::RakeTask.new(:spec)
8
8
  require "standard/rake"
9
9
 
10
10
  task default: %i[spec standard]
11
+
12
+ desc "Show RailsKeyRotator version"
13
+ task :version do
14
+ puts RailsKeyRotator::VERSION
15
+ end
16
+
17
+ Dir.glob("lib/tasks/*.rake").each { |r| import r }
@@ -5,7 +5,10 @@ require "rails"
5
5
  module RailsKeyRotator
6
6
  class Railtie < Rails::Railtie
7
7
  config.before_initialize do
8
- KeyRotator.call
8
+ KeyRotator.rotated?
9
+ end
10
+ rake_tasks do
11
+ load "tasks/key_rotator.rake"
9
12
  end
10
13
  end
11
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsKeyRotator
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -9,9 +9,9 @@ require "rails_key_rotator/railtie" if defined?(Rails)
9
9
 
10
10
  module RailsKeyRotator
11
11
  class Error < StandardError; end
12
- # Your code goes here...
12
+
13
13
  class << self
14
- def call
14
+ def rotated?
15
15
  return if ENV["RAILS_MASTER_KEY"].blank?
16
16
 
17
17
  if ENV.fetch("RAILS_MASTER_KEY_NEW", false)
@@ -24,23 +24,39 @@ module RailsKeyRotator
24
24
  end
25
25
  end
26
26
 
27
+ def rotate
28
+ decrypted = read(credentials_path) # Decrypt current credentials
29
+ backup_file(key_path) # Backup key
30
+ backup_file(credentials_path) # Backup credentials
31
+ File.write(key_path, new_key) # Save new key
32
+ write(decrypted) # Save new credentials
33
+ end
34
+
35
+ def credentials_path
36
+ File.join(root, "config", "credentials", "#{env}.yml.enc")
37
+ end
38
+
39
+ def key_path
40
+ File.join(root, "config", "credentials", "#{env}.key")
41
+ end
42
+
27
43
  private
28
44
 
45
+ def root
46
+ defined?(Rails) ? Rails.root : Dir.pwd
47
+ end
48
+
29
49
  def can_read_credentials!
30
50
  ActiveSupport::EncryptedConfiguration.new(
31
- config_path: credential_path,
51
+ config_path: credentials_path,
32
52
  env_key: "RAILS_MASTER_KEY_NEW",
33
- key_path: "",
53
+ key_path: key_path,
34
54
  raise_if_missing_key: true
35
55
  ).read
36
56
  rescue ActiveSupport::MessageEncryptor::InvalidMessage
37
57
  false
38
58
  end
39
59
 
40
- def credential_path
41
- Rails.root.join("config/credentials/#{env}.yml.enc")
42
- end
43
-
44
60
  def say(message)
45
61
  warn "\e[41;37;1m\n\n\tKeyRotator: Using #{message} for #{env} env\n\e[0m"
46
62
  end
@@ -48,5 +64,35 @@ module RailsKeyRotator
48
64
  def env
49
65
  defined?(Rails) ? Rails.env : (ENV["RAILS_ENV"] || "test")
50
66
  end
67
+
68
+ def date
69
+ @date ||= Time.new.strftime("%Y-%m-%d-%H%M%S")
70
+ end
71
+
72
+ def new_key
73
+ @new_key ||= ActiveSupport::EncryptedConfiguration.generate_key
74
+ end
75
+
76
+ def backup_file(original)
77
+ FileUtils.mv(original, "#{original}.bak-#{date}")
78
+ end
79
+
80
+ def read(credentials_path) # the old configuration
81
+ ActiveSupport::EncryptedConfiguration.new(
82
+ config_path: credentials_path,
83
+ key_path: key_path,
84
+ env_key: "",
85
+ raise_if_missing_key: true
86
+ ).read
87
+ end
88
+
89
+ def write(contents) # the new configuration
90
+ ActiveSupport::EncryptedConfiguration.new(
91
+ config_path: credentials_path,
92
+ key_path: key_path,
93
+ env_key: "",
94
+ raise_if_missing_key: true
95
+ ).write(contents)
96
+ end
51
97
  end
52
98
  end
@@ -0,0 +1,11 @@
1
+ namespace :key_rotator do
2
+ require "rails_key_rotator"
3
+ require "fileutils"
4
+
5
+ desc "Start rotation"
6
+ task rotate: [
7
+ # environment
8
+ ] do
9
+ RailsKeyRotator.rotate
10
+ end
11
+ end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Rotate your RAILS_MASTER_KEY with ease"
12
12
  # spec.description = "TODO: Write a longer description or delete this line."
13
- spec.homepage = "https://wendbaar.nl"
13
+ spec.homepage = "https://www.wendbaar.nl"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.6.0"
16
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_key_rotator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leon Berenschot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-13 00:00:00.000000000 Z
11
+ date: 2023-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -37,8 +37,10 @@ executables: []
37
37
  extensions: []
38
38
  extra_rdoc_files: []
39
39
  files:
40
+ - ".projections.json"
40
41
  - ".rspec"
41
42
  - ".rubocop.yml"
43
+ - ".vscode/extensions.json"
42
44
  - Appraisals
43
45
  - CHANGELOG.md
44
46
  - CODE_OF_CONDUCT.md
@@ -56,14 +58,15 @@ files:
56
58
  - lib/rails_key_rotator.rb
57
59
  - lib/rails_key_rotator/railtie.rb
58
60
  - lib/rails_key_rotator/version.rb
61
+ - lib/tasks/key_rotator.rake
59
62
  - rails_key_rotator.gemspec
60
63
  - sig/rails_key_rotator.rbs
61
- homepage: https://wendbaar.nl
64
+ homepage: https://www.wendbaar.nl
62
65
  licenses:
63
66
  - MIT
64
67
  metadata:
65
68
  rubygems_mfa_required: 'true'
66
- homepage_uri: https://wendbaar.nl
69
+ homepage_uri: https://www.wendbaar.nl
67
70
  source_code_uri: https://github.com/LeipeLeon/rails_key_rotator
68
71
  changelog_uri: https://github.com/LeipeLeon/rails_key_rotator/CHANGELOG.md
69
72
  post_install_message: