rails-creds 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +5 -5
- data/README.md +2 -0
- data/lib/creds.rb +44 -41
- data/lib/creds/errors.rb +69 -0
- data/lib/creds/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 869c29c1e36634cdcf8a8ebb845adefec90b04e01ca50f4009dc2b5c994a8d95
|
4
|
+
data.tar.gz: e7df049073d9d67617ab65eebc893734ca29942848483b0afd28e9aa87ecab84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '01209c664af0dfc8040b7376461a6850f23296ef289ef09fb7a465d06531bde89f46bc33b1380b714e0bb439d43f6c599ed6dc2a6d066499b75fd83d9f77f8dc'
|
7
|
+
data.tar.gz: a06fe617ba442d248fc1c07217b8fdb0f3cdbf88e3efc378660d498dd13cde67637a36e674a20a8eb1a51a7de778af6b5121c1c55eb31fa3d99f607b6b751cdd
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# 0.2.0
|
2
|
+
|
3
|
+
Changed:
|
4
|
+
|
5
|
+
- Creds will now warn about missing credentials when the encrypted file isn't
|
6
|
+
found. It will afterwards be a Null Object and return `nil` on every key.
|
7
|
+
- When encrypted credentials are found but the master key file AND env
|
8
|
+
variable is missing, Creds will return a special error with explanation.
|
9
|
+
|
10
|
+
# 0.1.0
|
11
|
+
|
12
|
+
Initial version
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
creds (0.
|
4
|
+
rails-creds (0.2.0)
|
5
5
|
rails (~> 5.2)
|
6
6
|
|
7
7
|
GEM
|
@@ -57,7 +57,7 @@ GEM
|
|
57
57
|
erubi (1.7.1)
|
58
58
|
globalid (0.4.1)
|
59
59
|
activesupport (>= 4.2.0)
|
60
|
-
i18n (1.0.
|
60
|
+
i18n (1.0.1)
|
61
61
|
concurrent-ruby (~> 1.0)
|
62
62
|
loofah (2.2.2)
|
63
63
|
crass (~> 1.0.2)
|
@@ -71,14 +71,14 @@ GEM
|
|
71
71
|
mini_mime (1.0.0)
|
72
72
|
mini_portile2 (2.3.0)
|
73
73
|
minitest (5.11.3)
|
74
|
-
nio4r (2.3.
|
74
|
+
nio4r (2.3.1)
|
75
75
|
nokogiri (1.8.2)
|
76
76
|
mini_portile2 (~> 2.3.0)
|
77
77
|
parallel (1.12.1)
|
78
78
|
parser (2.5.0.5)
|
79
79
|
ast (~> 2.4.0)
|
80
80
|
powerpack (0.1.1)
|
81
|
-
rack (2.0.
|
81
|
+
rack (2.0.5)
|
82
82
|
rack-test (1.0.0)
|
83
83
|
rack (>= 1.0, < 3)
|
84
84
|
rails (5.2.0)
|
@@ -149,7 +149,7 @@ PLATFORMS
|
|
149
149
|
|
150
150
|
DEPENDENCIES
|
151
151
|
bundler (~> 1.16)
|
152
|
-
creds!
|
152
|
+
rails-creds!
|
153
153
|
rake (~> 10.0)
|
154
154
|
rspec (~> 3.0)
|
155
155
|
rubocop (~> 0.54)
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
<h1><img src='https://s3.brnbw.com/logo-2x-SiurkO6hTL.png' alt='' width='220' /></h1>
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/mikker/rails-creds.svg?branch=master)](https://travis-ci.org/mikker/rails-creds) [![Gem version](https://img.shields.io/gem/v/rails-creds.svg)](https://rubygems.org/gems/rails-creds)
|
4
|
+
|
3
5
|
`Creds` is both **a)** a shortcut for the dreadfully long `Rails.application.credentials` and **b)** bringing environment scoping back (from when it was called _secrets_).
|
4
6
|
|
5
7
|
## Usage
|
data/lib/creds.rb
CHANGED
@@ -1,66 +1,69 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'creds/version'
|
4
|
-
require 'ostruct'
|
5
|
-
require 'singleton'
|
6
|
-
|
7
3
|
require 'rails'
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
class MissingKeyError < StandardError
|
12
|
-
MESSAGE = 'Key :%<key>s missing from credentials in "%<env>s" env'.freeze
|
13
|
-
end
|
14
|
-
|
15
|
-
class MissingEnvError < StandardError
|
16
|
-
# rubocop:disable Layout/TrailingWhitespace
|
17
|
-
MESSAGE = <<-MSG
|
18
|
-
Creds scopes credentials to the current Rails environment.
|
19
|
-
It seems you are missing a scope for the environment "%<env>s".
|
20
|
-
|
21
|
-
Here's an example of how your credentials could look:
|
5
|
+
require 'creds/version'
|
6
|
+
require 'creds/errors'
|
22
7
|
|
23
|
-
|
24
|
-
|
25
|
-
|
8
|
+
# The main module of rails-creds
|
9
|
+
class Creds
|
10
|
+
include Singleton
|
26
11
|
|
27
|
-
|
28
|
-
|
12
|
+
# Credentials that are always nil
|
13
|
+
class NullCredentials
|
14
|
+
def respond_to_missing?(_name)
|
15
|
+
true
|
16
|
+
end
|
29
17
|
|
30
|
-
|
31
|
-
|
18
|
+
def method_missing(*_args)
|
19
|
+
nil
|
20
|
+
end
|
32
21
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
MSG
|
37
|
-
.strip_heredoc.freeze
|
38
|
-
# rubocop:enable Layout/TrailingWhitespace
|
22
|
+
def nil?
|
23
|
+
true
|
24
|
+
end
|
39
25
|
end
|
40
26
|
|
41
|
-
|
27
|
+
def self.respond_to_missing?(_name)
|
28
|
+
true
|
29
|
+
end
|
42
30
|
|
43
|
-
# rubocop:disable Style/MethodMissing
|
44
31
|
def self.method_missing(name, *_args)
|
45
32
|
instance.credentials.fetch(name)
|
46
33
|
rescue KeyError
|
47
|
-
raise(
|
48
|
-
MissingKeyError,
|
49
|
-
format(MissingKeyError::MESSAGE, key: name, env: Rails.env)
|
50
|
-
)
|
34
|
+
raise MissingKeyError.new(name, Rails.env)
|
51
35
|
end
|
52
|
-
# rubocop:enable Style/MethodMissing
|
53
36
|
|
54
37
|
def self.to_h
|
55
38
|
instance.credentials
|
56
39
|
end
|
57
40
|
|
58
41
|
def credentials
|
42
|
+
unless encrypted_credentials_exist?
|
43
|
+
Rails.logger.warn MissingCredentialsWarning
|
44
|
+
return NullCredentials.new
|
45
|
+
end
|
46
|
+
|
47
|
+
raise MissingMasterKeyError unless master_key_present?
|
48
|
+
|
49
|
+
fetch_credentials_for_current_env
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def fetch_credentials_for_current_env
|
59
55
|
Rails.application.credentials.fetch(Rails.env.to_sym)
|
60
56
|
rescue KeyError
|
61
|
-
raise
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
raise MissingEnvError, Rails.env
|
58
|
+
end
|
59
|
+
|
60
|
+
def encrypted_credentials_exist?
|
61
|
+
File.exist? Rails.root.join('config', 'credentials.yml.enc')
|
62
|
+
end
|
63
|
+
|
64
|
+
def master_key_present?
|
65
|
+
return true if ENV['RAILS_MASTER_KEY']
|
66
|
+
return true if File.exist?(Rails.root.join('config', 'master.key'))
|
67
|
+
false
|
65
68
|
end
|
66
69
|
end
|
data/lib/creds/errors.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
class Creds
|
2
|
+
class MissingCredentialsWarning
|
3
|
+
MESSAGE =
|
4
|
+
<<-MSG
|
5
|
+
You have no encrypted credentials at config/credentials.yml.enc.
|
6
|
+
Creds will return nil for any key.
|
7
|
+
Run this to generate your credentials file:
|
8
|
+
$ bin/rails credentials:edit
|
9
|
+
MSG
|
10
|
+
.strip_heredoc.freeze
|
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 =
|
26
|
+
<<-MSG
|
27
|
+
Creds scopes credentials to the current Rails environment.
|
28
|
+
It seems you are missing a scope for the environment "%<env>s".
|
29
|
+
|
30
|
+
Here's an example of how your credentials could look:
|
31
|
+
|
32
|
+
---
|
33
|
+
default: &default
|
34
|
+
aws_key: 'shared between environments'
|
35
|
+
|
36
|
+
development:
|
37
|
+
<<: *default
|
38
|
+
|
39
|
+
test:
|
40
|
+
<<: *default
|
41
|
+
|
42
|
+
production:
|
43
|
+
<<: *default
|
44
|
+
aws_key: 'you can override defaults for individual environments'
|
45
|
+
MSG
|
46
|
+
.strip_heredoc.freeze
|
47
|
+
# rubocop:enable Layout/TrailingWhitespace
|
48
|
+
|
49
|
+
def initialize(env)
|
50
|
+
super format(MESSAGE, env: env)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# @api private
|
55
|
+
class MissingMasterKeyError < StandardError
|
56
|
+
MESSAGE =
|
57
|
+
<<-MSG
|
58
|
+
You have encrypted credentials but no master key.
|
59
|
+
|
60
|
+
Either get or recover the file config/master.key
|
61
|
+
or set the environment variable RAILS_MASTER_KEY
|
62
|
+
MSG
|
63
|
+
.strip_heredoc.freeze
|
64
|
+
|
65
|
+
def initalize
|
66
|
+
super(MESSAGE)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/creds/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-creds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikkel Malmberg
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- ".rspec"
|
92
92
|
- ".rubocop.yml"
|
93
93
|
- ".travis.yml"
|
94
|
+
- CHANGELOG.md
|
94
95
|
- CODE_OF_CONDUCT.md
|
95
96
|
- Gemfile
|
96
97
|
- Gemfile.lock
|
@@ -100,6 +101,7 @@ files:
|
|
100
101
|
- config/master.key
|
101
102
|
- creds.gemspec
|
102
103
|
- lib/creds.rb
|
104
|
+
- lib/creds/errors.rb
|
103
105
|
- lib/creds/version.rb
|
104
106
|
- lib/rails-creds.rb
|
105
107
|
homepage: https://github.com/mikker/rails-creds
|