• Stars
    star
    282
  • Rank 141,036 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

"Global" provides accessor methods for your configuration data

Global Runs linter and tests Code Climate

The 'global' gem provides accessor methods for your configuration data and share configuration across backend and frontend.

The data can be stored in YAML files on disk, or in the AWS SSM Parameter Store.

Installation

Add to Gemfile:

gem 'global'

Refer to the documentation on your chosen backend class for other dependencies.

Configuration

Refer to the documentation on your chosen backend class for configuration options.

> Global.backend(:filesystem, environment: "YOUR_ENV_HERE", path: "PATH_TO_DIRECTORY_WITH_FILES")

Or you can use configure block:

Global.configure do |config|
  config.backend :filesystem, environment: "YOUR_ENV_HERE", path: "PATH_TO_DIRECTORY_WITH_FILES"
  # set up multiple backends and have them merged together:
  config.backend :aws_parameter_store, prefix: '/prod/MyApp/'
  config.backend :gcp_secret_manager, prefix: 'prod-myapp-', project_id: 'example'
end

Using multiple backends

Sometimes it is practical to store some configuration data on disk (and perhaps, commit it to source control), but keep some other data in a secure remote location. Which is why you can use more than one backend with Global.

You can declare as many backends as you want; the configuration trees from the backends are deep-merged together, so that the backend declared later overwrites specific keys in the backend declared prior:

Global.configure do |config|
  config.backend :foo # loads tree { credentials: { hostname: 'api.com', username: 'dev', password: 'dev' } }
  config.backend :bar # loads tree { credentials: { username: 'xxx', password: 'yyy' } }
end

Global.credentials.hostname # => 'api.com'
Global.credentials.username # => 'xxx'
Global.credentials.password # => 'yyy'

For Rails, put initialization into config/initializers/global.rb.

There are some sensible defaults, check your backend class for documentation.

Global.configure do |config|
  config.backend :filesystem
end

Filesystem storage

The yaml_whitelist_classes configuration allows you to deserialize other classes from your .yml

AWS Parameter Store

The aws_options configuration allows you to customize the AWS credentials and connection.

Google Cloud Secret Manager

The gcp_options configuration allows you to customize the Google Cloud credentials and timeout.

Usage

Filesystem

For file config/global/hosts.yml:

test:
  web: localhost
  api: api.localhost
development:
  web: localhost
  api: api.localhost
production:
  web: myhost.com
  api: api.myhost.com

In the development environment we now have:

> Global.hosts
=> { "api" => "api.localhost", "web" => "localhost" }
> Global.hosts.api
=> "api.localhost"

Deserialize other classes from .yml

Config file config/global/validations.yml:

default:
  regexp:
    email: !ruby/regexp /.@.+\../

Ensure that Regexp is included in the yaml_whitelist_classes array

Global.validations.regexp.email === '[email protected]'
=> true

Per-environment sections

You can define environment sections at the top level of every individual YAML file

For example, having a config file config/global/web/basic_auth.yml with:

test:
  username: test_user
  password: secret
development:
  username: development_user
  password: secret
production:
  username: production_user
  password: supersecret

You get the correct configuration in development

> Global.web.basic_auth
=> { "username" => "development_user", "password" => "secret" }
> Global.web.basic_auth.username
=> "development_user"

Default section

Config file example:

default:
  web: localhost
  api: api.localhost
production:
  web: myhost.com
  api: api.myhost.com

Data from the default section is used until it's overridden in a specific environment.

Nested configurations

Config file global/nested.yml with:

test:
  group:
    key: "test value"
development:
  group:
    key: "development value"
production:
  group:
    key: "production value"

Nested options can then be accessed as follows:

> Global.nested.group.key
=> "development value"

Environment files

Config file global/aws.yml with:

:default:
  activated: false

staging:
  activated: true
  api_key: 'nothing'

And file global/aws.production.yml with:

:activated: true
:api_key: 'some api key'
:api_secret: 'some secret'

Provide such configuration on Global.environment = 'production' environment:

> Global.aws.activated
=> true
> Global.aws.api_key
=> 'some api key'
> Global.aws.api_secret
=> 'some secret'

Warning: files with dot(s) in name will be skipped by Global (except this env files).

ERB support

Config file global/file_name.yml with:

test:
  key: <%=1+1%>
development:
  key: <%=2+2%>
production:
  key: <%=3+3%>

As a result, in the development environment we have:

> Global.file_name.key
=> 4

AWS Parameter Store

Parameter Store is a secure configuration storage with at-rest encryption. Access is controlled through AWS IAM. You do not need to be hosted on AWS to use Parameter Store.

Refer to the official documentation to set up the store.

Some steps you will need to follow:

  • Allocate an AWS IAM role for your app.
  • Create an IAM user for the role and pass credentials in standard AWS env vars (applications on Fargate get roles automatically).
  • Choose a prefix for the parameters. By default, the prefix is /environment_name/AppClassName/. You can change it with backend parameters (prefer to use '/' as separator).
  • Allow the role to read parameters from AWS SSM. Scope access by the prefix that you're going to use.
  • If you will use encrypted parameters: create a KMS key and allow the role to decrypt using the key.
  • Create parameters in Parameter Store. Use encryption for sensitive data like private keys and API credentials.

Usage with Go

You can reuse the same configuration in your Go services. For this, we developed a Go module that loads the same configuration tree into Go structs.

See github.com/railsware/go-global for further instructions.

Configuration examples

Backend setup:

# in config/environments/development.rb
# you don't need to go to Parameter Store for dev machines
Global.backend(:filesystem)

# in config/environments/production.rb
# enterprise grade protection for your secrets
Global.backend(:aws_parameter_store, app_name: 'my_big_app')

Create parameters:

/production/my_big_app/basic_auth/username => "bill"
/production/my_big_app/basic_auth/password => "secret" # make sure to encrypt this one!
/production/my_big_app/api_endpoint => "https://api.myapp.com"

Get configuration in the app:

# Encrypted parameters are automatically decrypted:
> Global.basic_auth.password
=> "secret"
> Global.api_endpoint
=> "https://api.myapp.com"

Google Cloud Secret Manager

Google Cloud Secret Manager allows you to store, manage, and access secrets as binary blobs or text strings. With the appropriate permissions, you can view the contents of the secret. Google Cloud Secret Manager works well for storing configuration information such as database passwords, API keys, or TLS certificates needed by an application at runtime.

Refer to the official documentation to set up the secret manager.

Some steps you will need to follow:

  • Choose a prefix for the secret key name. By default, the prefix is environment_name-AppClassName-. You can change it with backend parameters (prefer to use '-' as separator).

Configuration examples

Backend setup:

# in config/environments/development.rb
# you don't need to go to Parameter Store for dev machines
Global.backend(:filesystem)

# in config/environments/production.rb
# enterprise grade protection for your secrets
Global.backend(:gcp_secret_manager, prefix: 'prod-myapp-', project_id: 'example')

Create secrets:

prod-myapp-basic_auth-username => "bill"
prod-myapp-basic_auth-password => "secret"
prod-myapp-api_endpoint => "https://api.myapp.com"

Get configuration in the app:

# Encrypted parameters are automatically decrypted:
> Global.basic_auth.password
=> "secret"
> Global.api_endpoint
=> "https://api.myapp.com"

Reload configuration data

> Global.reload!

Using YAML configuration files with Rails Webpacker

If you use the :filesystem backend, you can reuse the same configuration files on the frontend:

Add js-yaml npm package to package.json (use command yarn add js-yaml).

Then create a file at config/webpacker/global/index.js with the following:

const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')

const FILE_ENV_SPLIT = '.'
const YAML_EXT = '.yml'

let globalConfig = {
  environment: null,
  configDirectory: null
}

const globalConfigure = (options = {}) => {
  globalConfig = Object.assign({}, globalConfig, options)
}

const getGlobalConfig = (key) => {
  let config = {}
  const filename = path.join(globalConfig.configDirectory, `${key}${YAML_EXT}`)
  if (fs.existsSync(filename)) {
    const configurations = yaml.safeLoad(fs.readFileSync(filename, 'utf8'))
    config = Object.assign({}, config, configurations['default'] || {})
    config = Object.assign({}, config, configurations[globalConfig.environment] || {})

    const envFilename = path.join(globalConfig.configDirectory, `${key}${FILE_ENV_SPLIT}${globalConfig.environment}${YAML_EXT}`)
    if (fs.existsSync(envFilename)) {
      const envConfigurations = yaml.safeLoad(fs.readFileSync(envFilename, 'utf8'))
      config = Object.assign({}, config, envConfigurations || {})
    }
  }
  return config
}

module.exports = {
  globalConfigure,
  getGlobalConfig
}

After this, modify file config/webpacker/environment.js:

const path = require('path')
const {environment} = require('@rails/webpacker')
const {globalConfigure, getGlobalConfig} = require('./global')

globalConfigure({
  environment: process.env.RAILS_ENV || 'development',
  configDirectory: path.resolve(__dirname, '../global')
})

const sentrySettings = getGlobalConfig('sentry')

environment.plugins.prepend('Environment', new webpack.EnvironmentPlugin({
  GLOBAL_SENTRY_ENABLED: sentrySettings.enabled,
  GLOBAL_SENTRY_JS_KEY: sentrySettings.js,
  ...
}))

...

module.exports = environment

Now you can use these process.env keys in your code:

import {init} from '@sentry/browser'

if (process.env.GLOBAL_SENTRY_ENABLED) {
  init({
    dsn: process.env.GLOBAL_SENTRY_JS_KEY
  })
}

Contributing to global

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Global project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

More Repositories

1

upterm

A terminal emulator for the 21st century.
TypeScript
19,318
star
2

js-routes

Brings Rails named routes to javascript
Ruby
1,592
star
3

Sleipnir

BDD-style framework for Swift
Swift
847
star
4

bozon

🛠 Command line tool for building, testing and publishing modern Electron applications
JavaScript
757
star
5

BloodMagic

BloodMagic is a framework, which gives you a way to create custom property attributes.
Objective-C
317
star
6

applepie

Semantic and Modular CSS Toolkit
CSS
280
star
7

rack_session_access

Rack middleware that provides access to rack.session environment
Ruby
259
star
8

caphub

Generate centralized capistrano skeleton for multiple deployment
Ruby
230
star
9

smt_rails

Shared mustache templates for rails 3.
Ruby
110
star
10

http_logger

Log your http api calls just like SQL queries
Ruby
106
star
11

rspec-example_steps

Given/When/Then steps for RSpec examples
Ruby
85
star
12

sht_rails

Shared handlebars templates for Rails 3
Ruby
76
star
13

actionmailer-balancer

A Ruby gem to send your ActionMailer mail through one of several delivery methods, selected by weight.
Ruby
76
star
14

capistrano-multiconfig

Capistrano extension that allows to use multiple configurations
Ruby
66
star
15

passenger-initscript

Manage multiple passenger instances
Shell
54
star
16

skypekit

Ruby FFI interface to libskypekit C library
Ruby
47
star
17

newrelic_platform_plugins

Ruby
41
star
18

piro

PiRo - it's Rocket for you Pivotal Tracker account
CoffeeScript
40
star
19

zero_deploy

Significantly improves typical deployment speed
Ruby
40
star
20

mailtrap-nodejs

Official mailtrap.io Node.js client
TypeScript
35
star
21

capistrano-calendar

Deployment event creation on (google) calendar service
Ruby
29
star
22

capistrano-patch

Capistrano patch recipes
Ruby
29
star
23

scaffy.railsware.com

Approach for writing and organizing your CSS for large-scale projects
JavaScript
29
star
24

mailtrap-php

The official mailtrap.io PHP client
PHP
20
star
25

libskypekit

Thread-safe C library with synchronous API using asynchronous C++ SkypeKit SDK
C
19
star
26

backfiller

The backfill machine for database records with null columns
Ruby
16
star
27

capistrano-ci

Ruby
16
star
28

indeed

Indeed.com integration plugin
Ruby
16
star
29

mailtrap-ruby

The official mailtrap.io Ruby client
Ruby
15
star
30

RBRouteBuilder

Build routes without strings and headache
C++
15
star
31

sprinkle_recipes

Railsware sprinkle recipes
Ruby
14
star
32

capistrano-uptodate

Capistrano extension that automatically check local repository with remote repository
Ruby
14
star
33

activeresource-persistent

HTTP persistent connection support for ActiveResource
Ruby
11
star
34

haproxy-slow-fast-request-balancer

HAProxy as "Slow-Fast" Request Balancer
10
star
35

generator-electron-app

Yeoman generator to scaffold Electron app
JavaScript
9
star
36

scout-app-plugins

Useful plugins for the Scout Server Monitoring and Reporting Tool
Ruby
9
star
37

mailtrap-python

Official mailtrap.io Python client
Python
8
star
38

web-bundler

WebResourceBundler bundling particular resource (css or js) in one file. Encoding images in base64 and putting then in css directly.
Ruby
8
star
39

gcal4ruby

Author: Mike Reich. GCal4Ruby is a Ruby Gem that can be used to interact with the current version of the Google Calendar API. GCal4Ruby provides the following features: Create and edit calendar events, Add and invite users to events, Set reminders, Make recurring events.
Ruby
8
star
40

db_structure_ext

Extended rails tasks db:structure:dump/load that supports mysql views/triggers/routines
Ruby
7
star
41

github-actions

A collection of GitHub actions used at Railsware
JavaScript
7
star
42

jdt

JSON Driven Templates
JavaScript
7
star
43

shelltoad

Command line interface for airbrake (http://airbrake.io/)
Ruby
7
star
44

i18n_template

18nTemplate is made to extract phrases and translate html/xhtml/xml document or erb templates
Ruby
6
star
45

capistrano-changelog

Chagelog based on Git commits with a Pivotal story tags
Ruby
5
star
46

go-global

Golang configuration reader for AWS Parameter Store and more
Go
4
star
47

chain_flow

Helps to refactor complex data processing
Ruby
4
star
48

fakes3server

Fake AWS S3 server for local development
Go
4
star
49

jOverlay

Overlay jQuery plugin
JavaScript
4
star
50

dev_vagrant_box

Vagrant box for development (ruby, rails)
4
star
51

yaml_settings

YamlSettings is a simple configuration / settings solution that uses an ERB enabled YAML file.
Ruby
3
star
52

multiversion

Use Bundler and RVM to test your library against different gem versions and/or ruby versions.
Ruby
3
star
53

ui-library

UI Library Template
CSS
2
star
54

clicktale

Clicktale rails plugin by Railsware
Ruby
2
star
55

railsware.github.com

CSS
2
star
56

capybara-feature_helpers

Ruby
2
star
57

backbone_showcase

Ruby
2
star
58

em-rest-client

EventMachine::HttpRequest adapter for HTTP REST client
Ruby
2
star
59

newrelic_em_http

New Relic EM::HTTP instrumentation
Ruby
2
star
60

capistrano-strategy-copy-partial

Capistrano deploy strategy to transfer subdirectory of repository
Ruby
2
star
61

hbase-driver

Small and handy Ruby driver for HBase
Ruby
2
star
62

acme-aws-lambda

AWS Lambda function to generate Letsencrypt certificates (need AWS S3 and Route53)
Ruby
2
star
63

showroom

Ruby
2
star
64

email_templates

HTML
2
star
65

rest_facebook

Lightweight ruby facebook client
Ruby
1
star
66

plunger

Code review tool
Python
1
star
67

cat-aws-ssm-param

Go
1
star
68

rw-study-wishlist

Ruby
1
star
69

template

Rails project template
JavaScript
1
star
70

chef-rwci

Ruby
1
star
71

office-iot

Objective-C
1
star
72

simple_on_couch

Example of CouchApp application for RW articles
JavaScript
1
star
73

gdoc_mapreduce

JavaScript
1
star
74

scaffy

Repo moved to scaffy.railsware.com
1
star
75

highrise_assist

Assist for 37signals' highrise
Ruby
1
star
76

sidekiq_unique_retries

Unique Retries for Sidekiq
Ruby
1
star
77

skypekit_pure

Skypekit on pure ruby
Ruby
1
star
78

aws-ecs-tools

Ruby
1
star
79

capybara_mock

CapybaraMock
Ruby
1
star
80

handlebars_assets_i18n

Very simple handlebars_assets internationalization for .hamlbars and .slimbars templates
Ruby
1
star
81

paypal-sdk-http-adapters

HTTP Adapters for PayPal SDK
Ruby
1
star
82

harvest_report

Harvert reports for non-admin harvest users
Ruby
1
star
83

rest-client-adapters

RestClient Adapters
Ruby
1
star
84

versioned_item

Ruby
1
star
85

cordova-baxi-plugin

Java
1
star
86

column_info_reset

Reset ActiveRecord column info when unknown column exception occurs
Ruby
1
star
87

gdoc_pivotal

Gdoc updater for pivotal
JavaScript
1
star
88

mailtrap-examples

Mailtrap repo examples
HTML
1
star
89

mailtrap-elixir

The official mailtrap.io Elixir client
Elixir
1
star