Where should I store api key in rails3? - ruby-on-rails-3

What is the best practice for storing/retrieving API keys in rails3?
Should I create my own application yaml and access it through there? If so, how?
Sorry for the noob question...

I use the settingslogic plugin for things like this. Very easy to use.
Add settingslogic to your Gemfile and bundle install:
gem 'settingslogic'
Create a directory for your settings and place the settingslogic yaml in there:
/my_app/config/settings/my_settings.yml
You can include default settings and per environment settings. The file looks like this:
defaults: &defaults
api_key: abc123
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
Add this file: app/models/my_settings.rb, start up your app and you are good to go
class MySettings < Settingslogic
source "#{Rails.root}/config/settings/my_settings.yml"
namespace Rails.env
end
Now you can use call these settings from anywhere in the app like so:
MySettings.api_key

Related

Spread all dotenv keys and values to process.env without having to define it one by one in serverless.yml

I am using node & expressjs as an app. I have problem in deploying it to serverless with dotenv (without using any dotenv plugin).
My serverless.yml is this:
functions:
server:
handler: index.server
events:
- http: ANY /
- http: "ANY /{proxy+}"
environment:
ENV_VAR1: ${env:ENV_VAR1}
and my .env.staging is this:
ENV_VAR1='ENV_VAR1 using .env.staging'
ENV_VAR2='ENV_VAR2 using .env.staging'
I use AWS Lambda and deploy it using sls deploy --stage staging. So far I can read the ENV_VAR1from .env.staging by calling process.env.ENV_VAR1 because I explicitly define it in the serverless.yml file. But that's not the case with ENV_VAR2.
My question is, how to load all the .env.{stage} file without defining the key one by one?
I hope to load it similar to something like this:
environment: ${file(.env.${self:stage})}
Thank you in advance.
As I know, there is no way, you'll have to do it one by one (you have to have an object with key-value pairs inside environment, but you are trying to give it a string - even if it is going to be interpreted as a file containing something, it is still a string when you are referencing it).
I'm not an expert, so there still might be some tricks.

Can I use a %variable% in module section of codeception environment file?

I tried to have the server address injected to the test with environment variable ABSOLUTE_URL so PhpBrowser would test against it. The config I wanted to do is something like this:
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: "%ABSOLUTE_URL%"
But I simply could not get it to work. Is there anyway I can do it?
Add params section to codeception.yaml file:
params:
- env
Documented at https://codeception.com/docs/06-ModulesAndHelpers#Dynamic-Configuration-With-Parameters

spring cloud config with native file

when config files changed,the config server did not reload the changes.
It can't monitor the native file changes or something wrong?
spring:
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/conf/, classpath:/conf/licensingservice/
enter image description here
Since that your configuration are packed in your jar if you change a configuration since that it is saved in your classpath the server do not see the modification.
A better option is save these config in an other place outside the classpath.
you can use a configuration like below:
spring:
application:
name: configserver
cloud:
config:
server:
native:
searchLocations: file://${LOCAL_REPO}
in this way you can pilot the place with the environment variable LOCAL_REPO.
Of course I suggest you for the production to move the configuration repository to a git repository that it is a more suitable production ready choice.
I hope that this can help you

set rails fog gem application.yml keys for development environment

I'm using paperclip, fog, figaro gems to upload images to amazon S3. In production works fine as I introduced the env variables in heroku by hand.
But in development, it is suposed to use the env from the aplication.yml file (added to .gitignore). It is not using this file, as in development the ENV[] placed in application.yml do not exist.
I followed several tutorial for this, and in all of them I understand that the ENV[] introduced in applicaction.yml should be used. Am I wrong? Am I missing something? Do I need something else? I read that I can introduce the ENV[] by hand typing export, but I need to know if it is not enough to write them down at application.yml
#config/application.yml
MAPS_API_KEY: 1234googlemapapikey56788
SECRET_KEY_BASE: ab1234567longkeyy2979127401411124
AWS_ACCES_KEY_ID: ACCESSKEYID1234567
AWS_SECRET_ACCESS_KEY: SECRETKEY123456788/jaa\12345
AWS_REGION: eu-west-1
S3_HOST_NAME: s3-eu-west-1.amazonaws.com
development:
FOG_DIRECTORY: example-dev
S3_BUCKET_NAME: example-dev
production:
FOG_DIRECTORY: example-production
S3_BUCKET_NAME: example-production
in development and production I use the same code:
#config/environments/development.rb && config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
In production works fine, and in development I get errors like:
development.rb:54:in `fetch': key not found: "AWS_ACCESS_KEY_ID" (KeyError)
# config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
The config/application.yml you list above has a typo.
The key you want is AWS_ACCESS_KEY_ID, but the copied file uses AWS_ACCES_KEY_ID (where access is missing the second s).
I suspect that should get things working for you again.

thinking sphinx yml file not being read

Thinking Sphinx works on my Rails app (Rails 3.2.1, TS 3.0.1) but when I set up a sphinx.yml file in the config folder, it doesn't seem to read the morphology instructions.
The file is called sphinx.yml and is in the config folder. Here is the yml format:
development:
morphology: stem_en
production:
morphology: stem_en
I copy/pasted this so the indentation matches wha'ts in my code. I don't use a libstemmer or haven't installed any other wordforms.
Please, I keep my fingers crossed someone can help me on this one as I'm stumped!
And the answer is: read ALL the doco on:
https://github.com/pat/thinking-sphinx/blob/master/README.textile
The new name for the .yml is thinking_sphinx.yml