rails-creds 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc2e9f9bab0600a6bd80cb4de294fed74e0350dec39a44ece69e2f2f9cf33bb1
4
- data.tar.gz: 122898482b87c4963fe814f87b5121454528ad19f2f1b0fec5add0fac7dba33b
3
+ metadata.gz: cbc1c067d2f4b0a326231eb8c98d1ad5a79f308cc6ef82c2bc9ba2409dd6656d
4
+ data.tar.gz: 35af2f8b5ba9d9488a466dd64f04da29a78065eb94678e774a8e810b669cbccb
5
5
  SHA512:
6
- metadata.gz: 1cdb35a94bbef472049ea5aa9ea6c13d41478187e34360e22d8f15435f8d8cc342616d11861ccac3c1a9ef637ef767cee142b06344449274e680fcdbb8dc39b5
7
- data.tar.gz: 8aaf493870469ed8f6578fa480def8d800214ed8740c6eac06cf6f8d2d9c53e22db161ba4643ccaa55c973607f04fcb29fd182c7cff1c659b136222352e73e04
6
+ metadata.gz: 8b8e7609ef17e6ea9a5b96ba66ccdd3ff2cd21d916ca30f2df1d52a9c1ef318c6f5e81bdbe417faf5fd26fd15a28e22db9753adf17689205bfa010ff4231a97f
7
+ data.tar.gz: 19d31443777ff80b7f4dbea23dd7ccd2e2c888136797377274b3fdd606fe812be96454cedfa35d0fe27296666616ef05d85a4a076a614d82dbf17d4365281146
@@ -0,0 +1,46 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches: [main]
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby:
16
+ - "3.0"
17
+ - "3.1"
18
+ - "3.2"
19
+ - "3.3"
20
+ - "3.4"
21
+
22
+ steps:
23
+ - name: Install packages
24
+ run: sudo apt-get update && sudo apt-get install --no-install-recommends -y build-essential git pkg-config google-chrome-stable
25
+
26
+ - name: Checkout code
27
+ uses: actions/checkout@v4
28
+
29
+ - name: Set up Ruby
30
+ uses: ruby/setup-ruby@v1
31
+ with:
32
+ ruby-version: ruby-3.4.1
33
+ bundler-cache: true
34
+
35
+ - name: Run tests
36
+ env:
37
+ RAILS_ENV: test
38
+ run: bin/rails test
39
+
40
+ - name: Keep screenshots from failed system tests
41
+ uses: actions/upload-artifact@v4
42
+ if: failure()
43
+ with:
44
+ name: screenshots
45
+ path: ${{ github.workspace }}/tmp/screenshots
46
+ if-no-files-found: ignore
data/.rspec CHANGED
@@ -1,3 +1,2 @@
1
- --format documentation
2
1
  --color
3
2
  --require spec_helper
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0
4
+
5
+ **NB:** Probably breaking change.
6
+
7
+ We now rely on YAML for merging credentials. See `bin/rails creds:example` for an example configuration.
8
+
9
+ - New: Missing keys always raise.
10
+ - Added example task.
11
+
3
12
  ## 0.4.0 (2023-07-08)
4
13
 
5
14
  ### Added
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task default: :spec
6
+ task(default: :spec)
data/creds.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.add_dependency("rails", "> 5.2")
25
+ spec.add_dependency("rails", ">= 5.2")
26
26
 
27
27
  spec.add_development_dependency("bundler", "~> 2.0")
28
28
  spec.add_development_dependency("rake", "~> 12.3")
@@ -0,0 +1,7 @@
1
+ module Creds
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ Dir[File.join(__dir__, "../tasks/**/*.rake")].each { |f| load f }
5
+ end
6
+ end
7
+ end
data/lib/creds/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- class Creds
2
- VERSION = "0.4.0".freeze
1
+ module Creds
2
+ VERSION = "0.5.0".freeze
3
3
  end
data/lib/creds.rb CHANGED
@@ -1,84 +1,63 @@
1
1
  require "rails"
2
+ require "active_support/core_ext/string/strip"
2
3
 
3
4
  require "creds/version"
4
- require "creds/errors"
5
+ require "creds/railtie"
5
6
 
6
7
  # The main module of rails-creds
7
- class Creds
8
- include Singleton
8
+ module Creds
9
+ EXAMPLE_CONFIG = <<-YAML
10
+ ---
11
+ secret_key_base: "abc123"
9
12
 
10
- # Credentials that are always nil
11
- class NullCredentials
12
- def respond_to_missing?(_name)
13
- true
14
- end
15
-
16
- def method_missing(*_args)
17
- nil
18
- end
13
+ shared: &shared
14
+ secret: 123
19
15
 
20
- def nil?
21
- true
22
- end
23
- end
24
-
25
- def self.respond_to_missing?(_name)
26
- true
27
- end
28
-
29
- def self.method_missing(name, *_args)
30
- instance.credentials.fetch(name)
31
- rescue KeyError
32
- raise MissingKeyError.new(name, Rails.env)
33
- end
16
+ test:
17
+ <<: *shared
34
18
 
35
- def self.to_h
36
- instance.credentials
37
- end
19
+ development:
20
+ <<: *shared
38
21
 
39
- def credentials
40
- return @credentials if @credentials
22
+ production:
23
+ <<: *shared
24
+ secret: 456
25
+ YAML
26
+ .strip_heredoc
27
+ .freeze
41
28
 
42
- if dummy?
43
- @credentials = NullCredentials.new
44
- return @credentials
45
- end
29
+ class MissingKeyError < StandardError
30
+ MESSAGE = "Key :%<key>s missing from credentials in \"%<env>s\" env".freeze
46
31
 
47
- unless encrypted_credentials_exist?
48
- Rails.logger.warn(MissingCredentialsWarning)
49
- @credentials = NullCredentials.new
50
- return @credentials
32
+ def initialize(key, env)
33
+ super(format(MESSAGE, key: key, env: env))
51
34
  end
52
-
53
- raise MissingMasterKeyError unless master_key_present?
54
-
55
- @credentials = fetch_credentials_for_current_env
56
35
  end
57
36
 
58
- private
37
+ class MissingEnvError < StandardError
38
+ MESSAGE = <<-MSG
39
+ It seems you are missing a scope for the environment "%<env>s".
59
40
 
60
- def fetch_credentials_for_current_env
61
- base = Rails.application.credentials.config
62
- scoped = base.fetch(Rails.env.to_sym)
63
- base.delete(Rails.env.to_sym)
64
- base.merge(scoped)
65
- rescue KeyError
66
- raise MissingEnvError, Rails.env
67
- end
41
+ Here's an example of how your credentials could look:
68
42
 
69
- def encrypted_credentials_exist?
70
- File.exist?(Rails.root.join("config", "credentials.yml.enc"))
43
+ #{Creds::EXAMPLE_CONFIG.gsub(/^([^\n]+)$/m, " \\1")}
44
+ MSG
45
+ .strip_heredoc
46
+ .freeze
47
+
48
+ def initialize(env)
49
+ super(format(MESSAGE, env: env))
50
+ end
71
51
  end
72
52
 
73
- def master_key_present?
74
- return true unless Rails.application.config.require_master_key
75
- return true if ENV["RAILS_MASTER_KEY"]
76
- return true if File.exist?(Rails.root.join("config", "master.key"))
53
+ def self.method_missing(mth, *args, &block)
54
+ @cache ||= Rails.application.credentials[Rails.env].tap do |scoped|
55
+ raise MissingEnvError.new(Rails.env) unless scoped.is_a?(Hash)
56
+ raise MissingKeyError.new(mth, Rails.env) unless scoped.key?(mth.to_sym)
77
57
 
78
- false
79
- end
58
+ scoped
59
+ end
80
60
 
81
- def dummy?
82
- ENV.fetch("SECRET_KEY_BASE_DUMMY", nil) == "1"
61
+ @cache.fetch(mth.to_sym)
83
62
  end
84
63
  end
@@ -0,0 +1,6 @@
1
+ namespace(:creds) do
2
+ desc("Print an example credentials file")
3
+ task(:example => :environment) do
4
+ puts(Creds::EXAMPLE_CONFIG)
5
+ end
6
+ end
metadata CHANGED
@@ -1,27 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-creds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikkel Malmberg
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-07-08 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - ">"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
18
  version: '5.2'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - ">"
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '5.2'
27
26
  - !ruby/object:Gem::Dependency
@@ -73,11 +72,10 @@ executables: []
73
72
  extensions: []
74
73
  extra_rdoc_files: []
75
74
  files:
76
- - ".github/workflows/test.yml"
75
+ - ".github/workflows/ci.yml"
77
76
  - ".gitignore"
78
77
  - ".rspec"
79
78
  - CHANGELOG.md
80
- - CODE_OF_CONDUCT.md
81
79
  - Gemfile
82
80
  - LICENSE.txt
83
81
  - README.md
@@ -85,16 +83,16 @@ files:
85
83
  - config/master.key
86
84
  - creds.gemspec
87
85
  - lib/creds.rb
88
- - lib/creds/errors.rb
86
+ - lib/creds/railtie.rb
89
87
  - lib/creds/version.rb
90
88
  - lib/rails-creds.rb
89
+ - lib/tasks/creds.rake
91
90
  - log/test.log
92
91
  homepage: https://github.com/mikker/rails-creds
93
92
  licenses:
94
93
  - MIT
95
94
  metadata:
96
95
  source_code_uri: https://github.com/mikker/rails-creds
97
- post_install_message:
98
96
  rdoc_options: []
99
97
  require_paths:
100
98
  - lib
@@ -109,8 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
107
  - !ruby/object:Gem::Version
110
108
  version: '0'
111
109
  requirements: []
112
- rubygems_version: 3.4.15
113
- signing_key:
110
+ rubygems_version: 3.6.8
114
111
  specification_version: 4
115
112
  summary: Shorter, env-scoped version of Rails' credentials
116
113
  test_files: []
@@ -1,17 +0,0 @@
1
- name: Tests
2
- on: [push]
3
- jobs:
4
- test:
5
- strategy:
6
- fail-fast: false
7
- matrix:
8
- os: [ubuntu-latest]
9
- ruby: ["3.0", "3.1", "3.2"]
10
- runs-on: ${{ matrix.os }}
11
- steps:
12
- - uses: actions/checkout@v3
13
- - uses: ruby/setup-ruby@v1
14
- with:
15
- ruby-version: ${{ matrix.ruby }}
16
- bundler-cache: true
17
- - run: bundle exec rake
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
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 mikkel@brnbw.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/lib/creds/errors.rb DELETED
@@ -1,58 +0,0 @@
1
- require "active_support/core_ext/string/strip"
2
-
3
- class Creds
4
- class MissingCredentialsWarning
5
- MESSAGE = <<-MSG.strip_heredoc.freeze
6
- You have no encrypted credentials at config/credentials.yml.enc.
7
- Creds will return nil for any key.
8
- Run this to generate your credentials file:
9
- $ bin/rails credentials:edit
10
- MSG
11
- end
12
-
13
- # @api private
14
- class MissingKeyError < StandardError
15
- MESSAGE = "Key :%<key>s missing from credentials in \"%<env>s\" env".freeze
16
-
17
- def initialize(key, env)
18
- super(format(MESSAGE, key: key, env: env))
19
- end
20
- end
21
-
22
- # @api private
23
- class MissingEnvError < StandardError
24
- # rubocop:disable Layout/TrailingWhitespace
25
- MESSAGE = <<-MSG.strip_heredoc.freeze
26
- Creds scopes credentials to the current Rails environment.
27
- It seems you are missing a scope for the environment "%<env>s".
28
-
29
- Here's an example of how your credentials could look:
30
-
31
- ---
32
- aws_key: 'shared between environments'
33
-
34
- production:
35
- <<: *default
36
- aws_key: 'you can override defaults for individual environments'
37
- MSG
38
- # rubocop:enable Layout/TrailingWhitespace
39
-
40
- def initialize(env)
41
- super(format(MESSAGE, env: env))
42
- end
43
- end
44
-
45
- # @api private
46
- class MissingMasterKeyError < StandardError
47
- MESSAGE = <<-MSG.strip_heredoc.freeze
48
- You have encrypted credentials but no master key.
49
-
50
- Either get or recover the file config/master.key
51
- or set the environment variable RAILS_MASTER_KEY
52
- MSG
53
-
54
- def initalize
55
- super(MESSAGE)
56
- end
57
- end
58
- end