如何使用secrets.yml 在Rails 4.1 中动态生成秘密令牌? [英] How to dynamically generate secret tokens in Rails 4.1 with secrets.yml?

查看:73
本文介绍了如何使用secrets.yml 在Rails 4.1 中动态生成秘密令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 新手.按照 Hartl 的教程,他使用此代码为 config/initializers/secret_token.rb 动态生成秘密令牌

New to rails. Followed Hartl's tutorial where he uses this code to dynamically generate secret token for config/initializers/secret_token.rb

require 'securerandom'

def secure_token
  token_file = Rails.root.join('.secret')
  if File.exist?(token_file)
    # Use the existing token.
    File.read(token_file).chomp
  else
    # Generate a new token and store it in token_file.
    token = SecureRandom.hex(64)
    File.write(token_file, token)
    token
  end
end

SampleApp::Application.config.secret_key_base = secure_token

我正在尝试使用 secrets.yml 来遵循新的 Rails 4.1 方式,并删除 secret_token.rb:

I'm trying to follow the new Rails 4.1 way by using secrets.yml, and delete the secret_token.rb:

development:
  secret_key_base: 79c1389c2fadc5a5a1918a5104ab34eb700c

test:
  secret_key_base: fdb4edcde14173d62963705ca4d7876b5307790924

production:
  secret_key_base: 85172605030a8225c083d886d066da2cb4aac1f0

但我认为你不能像在 yml 文件中的 secret_token.rb 中那样运行 ruby​​ 脚本.您将如何让 rails 动态生成秘密令牌.这应该怎么做?什么是最佳实践?

But I think you cannot run ruby script like the one in secret_token.rb in a yml file. How would you have rails dynamically generate the secret tokens in secret. How should this be done? What is best practice?

推荐答案

给定一个函数 secret_token,它的唯一工作是在每次应用程序访问 secrets.yml 文件、cookies 和很可能其他类似会话时生成一个新的令牌字符串行为将无法正常工作,因为秘密令牌会更改对函数的每次调用.

Given a function secret_token whose only job is to generate a new token string each time one's application accesses the secrets.yml file, cookies and most likely other session-like behavior will not work correctly as the secret token changes each call to the function.

首选&安全的方法是在开发和测试环境中使用 secrets.yml 文件中的任何旧密钥(您可以通过在命令行上发出 rake secret 来生成一个秘密字符串),然后使用一个环境变量您的生产服务器知道,所以 secrets.yml 文件看起来像:

The preferred & secure way is to use any old secret key in the secrets.yml file for development and test environments (you can generate a secret string by issuing rake secret on the command line), then use an environment variable that your production server knows, so the secrets.yml file looks like:

production:
 secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

例如,在 Heroku 上,使用 heroku config:set SECRET_KEY_BASE="insert key here" 来设置环境变量,然后就可以了.不要害怕将 secrets.yml 文件检入 scm...只要您没有将生产密钥保存到文件中(而是使用我刚刚描述的环境变量方法),将文件检入 scm不构成威胁.

For example, on Heroku, use heroku config:set SECRET_KEY_BASE="insert key here" to set the environment variable and there you have it. Don't be afraid to check the secrets.yml file into scm...as long as you haven't saved your production key to the file (and are instead using the environment variable method I just described), checking the file into scm poses no threat.

这篇关于如何使用secrets.yml 在Rails 4.1 中动态生成秘密令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆