Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secret generator #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [1.3.0] - 2022-11-14

### Improvements

- Added quantum secret generator

## [1.2.0] - 2022-04-12

### Improvements
Expand Down
106 changes: 37 additions & 69 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,121 +1,89 @@
PATH
remote: .
specs:
r_creds (1.2.0)
r_creds (1.3.0)

GEM
remote: https://rubygems.org/
specs:
actionpack (7.0.2.3)
actionview (= 7.0.2.3)
activesupport (= 7.0.2.3)
actionpack (7.0.4)
actionview (= 7.0.4)
activesupport (= 7.0.4)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actionview (7.0.2.3)
activesupport (= 7.0.2.3)
actionview (7.0.4)
activesupport (= 7.0.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activesupport (7.0.2.3)
activesupport (7.0.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
builder (3.2.4)
concurrent-ruby (1.1.10)
crass (1.0.6)
diff-lcs (1.5.0)
docile (1.4.0)
erubi (1.10.0)
i18n (1.10.0)
erubi (1.11.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
loofah (2.16.0)
loofah (2.19.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
method_source (1.0.0)
mini_portile2 (2.8.0)
minitest (5.15.0)
nokogiri (1.13.4)
minitest (5.16.3)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
racc (1.6.0)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rack (2.2.4)
rack-test (2.0.2)
rack (>= 1.3)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.4.2)
rails-html-sanitizer (1.4.3)
loofah (~> 2.3)
railties (7.0.2.3)
actionpack (= 7.0.2.3)
activesupport (= 7.0.2.3)
railties (7.0.4)
actionpack (= 7.0.4)
activesupport (= 7.0.4)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.3.0)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.0)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
rubocop (1.27.0)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.16.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.17.0)
parser (>= 3.1.1.0)
rubocop-rspec (2.9.0)
rubocop (~> 1.19)
ruby-progressbar (1.11.0)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
thor (1.2.1)
tzinfo (2.0.4)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unicode-display_width (2.1.0)
zeitwerk (2.5.4)
zeitwerk (2.6.6)

PLATFORMS
ruby

DEPENDENCIES
bundler
bundler (~> 2.3, >= 2.3.25)
r_creds!
railties (~> 7.0)
rake (~> 13.0)
rspec (~> 3.11)
rubocop (~> 1.27)
rubocop-rspec (~> 2.9)
simplecov (~> 0.16)
railties (~> 7.0, >= 7.0.4)
rake (~> 13.0, >= 13.0.6)
rspec (~> 3.12, ~> 3.11)

BUNDLED WITH
2.3.9
2.3.25
65 changes: 51 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# RCreds v1.2.0 - [changelog](https://github.com/coaxsoft/r_creds/blob/master/CHANGELOG.md)
# RCreds v1.3.0 - [changelog](https://github.com/coaxsoft/r_creds/blob/master/CHANGELOG.md)

RCreds makes working with Rails 5.2/6/7 credentials easier

### Tested on second ruby versions:
- 2.7.3
- 3.0.0
## Tested on second ruby versions

- 2.7.3
- 3.0.0
- 3.1.1

## Installation
Expand All @@ -17,58 +18,93 @@ gem 'r_creds'

And then execute:

$ bundle
```ruby
bundle install
```

Or install it yourself as:

$ gem install r_creds
```ruby
gem install r_creds
```

Run the generator:

$ rails generate r_creds:install
```ruby
rails generate r_creds:install
```

## Usage

There are no need to do this:

```ruby
Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]
```

Use RCreds instead:

```ruby
RCreds.fetch(:aws, :access_key_id)
```

RCreds give an ability to read credentials in credentials.yml, ENV or use default value!
RCreds searches for values in following order: credentials.yml > ENV > default and returns nil if nothing match.

```ruby
# value set in credentials.yml
RCreds.fetch(:aws, :access_key_id) # output: secret_key_from_credentials_yaml
# cred > env

# no value in credentials.yml but,
# no value in credentials.yml but
ENV['AWS_ACCESS_KEY_ID'] = 'aws_key_from_env_variable'
RCreds.fetch(:aws, :access_key_id) # output: aws_key_from_env_variable
# cred > env

# value set in credentials.yml and ENV
RCreds.fetch(:aws, :access_key_id, environment_first: true) # output: aws_key_from_env_variable
# env > cred

# no value in credentials.yml and ENV - use default
RCreds.fetch(:aws, :access_key_id, default: 'some_default_test_key') # output: some_default_test_key
# cred > env > default

# no value in credentials.yml and ENV and without default
RCreds.fetch(:aws, :access_key_id, allow_nil_value: true) # output: raise an error
# cred > env > default > error
```

## Generates quantum computed secret

If you want to generate quantum secret use this generator:

```ruby
rails g r_creds:quantum_secret
```
RCreds searches for values in following order: credentials.yml > ENV > default and returns nil if nothing match

# DEPRECATED
`Rails 6 loads only one specific environment credentials and does not allow to cahnge it.`
More info [Quantum random numbers](https://qrng.anu.edu.au)

## DEPRECATED

`Rails 6 loads only one specific environment credentials and does not allow to cahnge it.`

RCreds uses `Rails.env` to determinate environment. But you can set any environment.

```ruby
RCreds.fetch(:aws, :access_key_id, environment: 'production')
```
`Will be removed in next version`

Will be removed in next version

## Development

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.

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).
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](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/coaxsoft/r_creds. 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.
Bug reports and pull requests are welcome on GitHub at this [link](https://github.com/coaxsoft/r_creds). 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.

## License

Expand All @@ -79,4 +115,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
Everyone interacting in the RCreds project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/coaxsoft/r_creds/blob/master/CODE_OF_CONDUCT.md).

## Idea

Initially designed and created by [Orest Falchuk](https://github.com/OrestF)
2 changes: 1 addition & 1 deletion lib/generators/r_creds/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module RCreds
module Generators
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __dir__)
desc 'Creates a RCreds config file.'
desc 'Generates config file.'

def copy_config
template 'r_creds_initializer.rb', "#{Rails.root}/config/initializers/r_creds.rb"
Expand Down
26 changes: 26 additions & 0 deletions lib/generators/r_creds/quantum_secret_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require 'rails/generators'

module RCreds
module Generators
class QuantumSecretGenerator < Rails::Generators::Base
BASE_URL = 'https://qrng.anu.edu.au/API/jsonI.php'

desc 'Generates quantum computed secret.'

URI(BASE_URL).then do |uri|
uri.query = URI.encode_www_form({ length: 1, type: 'hex16', size: 64 })

Net::HTTP.get_response(uri).then do |response|
case response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to if else if only 2 options

when Net::HTTPSuccess
puts JSON.parse(response.body)['data'][0]
else
puts "Service(#{BASE_URL}) is unavailable"
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/r_creds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'r_creds/version'
require_relative 'r_creds/fetcher'
require 'r_creds/fetcher'

module RCreds
class << self
Expand All @@ -18,7 +18,7 @@ def allow_nil_value
end

def config
yield self
yield_self
end

def fetch(*keys,
Expand Down
2 changes: 1 addition & 1 deletion lib/r_creds/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RCreds
VERSION = '1.2.0'
VERSION = '1.3.0'
end
18 changes: 10 additions & 8 deletions r_creds.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
File.expand_path('lib', __dir__).then do |lib|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
end

require 'r_creds/version'

Gem::Specification.new do |spec|
Expand All @@ -10,8 +12,8 @@ Gem::Specification.new do |spec|
spec.authors = ['OrestF']
spec.email = ['falchuko@gmail.com']

spec.summary = 'RCreds makes working with Rails 5.2 credentials easier'
spec.description = 'RCreds makes working with Rails 5.2 credentials easier'
spec.summary = 'RCreds makes working with Rails 5.2/6/7 credentials easier'
spec.description = 'RCreds makes working with Rails 5.2/6/7 credentials easier'
spec.homepage = 'https://github.com/coaxsoft/r_creds'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.5'
Expand All @@ -26,9 +28,9 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.files = Dir['README.md', 'lib/**/*', 'lib/*', 'r_creds.gemspec']

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'railties', '~> 7.0'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.11'
spec.add_development_dependency 'bundler', '~> 2.3', '>= 2.3.25'
spec.add_development_dependency 'railties', '~> 7.0', '>= 7.0.4'
spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6'
spec.add_development_dependency 'rspec', '~> 3.11', '~> 3.12'
spec.metadata['rubygems_mfa_required'] = 'true'
end