Rails 6 credentials in AWS Beanstalk: ArgumentError: key must be 16 bytes

Rails 6 credentials in AWS Beanstalk: ArgumentError: key must be 16 bytes

我碰壁了。我正在通过 Elastic Beanstalk 将 Rails 6 应用程序部署到 AWS。部署是通过 eb cli 完成的,我正在使用 git 来完成。

无论我尝试什么,我遇到的错误是: ArgumentError: key must be 16 bytes

每当我尝试访问加密凭据时,就会发生这种情况,例如 Rails.application.credentials.sendgrid[:api_key],这些凭据是使用带有 EDITOR="mvim -f" rails credentials:edit --environment production

的环境密钥设置的

我看到的所有内容都使用 Rails 5.2,而且所有内容似乎都使用 master.key 而不是特定于环境的 yml 文件进行存储。

我尝试过的:

  1. 在 EB Web 控制台的环境属性中设置 RAILS_MASTER_KEY
  2. 我可以 eb printenv 而且我确实看到了这个密钥
  3. config/production.rb中我设置了config.require_master_key = true
  4. 我试过将 RAILS_PRODUCTION_KEY 设置为与主密钥相同的东西,还是不行
  5. 我将 RAILS_ENV 添加为 production 作为环境值 属性
  6. 我为 运行ning 迁移和预编译添加了自己的容器 ebextensions,但每当我尝试检索凭据时它们仍然以相同的方式出错。

总的来说,它似乎无法正确获取主密钥,并且它抱怨密钥不是正确的 16 字节。

当我在本地 运行 RAILS_ENV=production bundle exec rails c 时,它工作正常,我可以获得所有凭据。

这是我的 .ebextensions/config 文件:

# Beanstalk ain't ready for Rails 6. This fix is courtesy of https://austingwalters.com/rails-6-on-elastic-beanstalk/
# Additional node 6 cleanup courtesy of https://github.com/nodesource/distributions/issues/486

commands:
  00_remove_node_6_if_present:
    command: "/bin/rm -rf /var/cache/yum && /usr/bin/yum remove -y nodejs && /bin/rm /etc/yum.repos.d/nodesource* && /usr/bin/yum clean all"
    ignoreErrors: true
  01_download_nodejs:
    command: "curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -"
  02_install_nodejs:
    command: "yum -y install nodejs"
  03_install_yarn:
    command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && sudo yum install yarn -y"
  04_mkdir_webapp_dir:
    command: "mkdir /home/webapp"
    ignoreErrors: true
  05_chown_webapp_dir:
    command: "chown webapp:webapp /home/webapp"
    ignoreErrors: true
  06_chmod_webapp_dir:
    command: "chmod 0744 /home/webapp"
    ignoreErrors: true
  07_chmod_logs:
    command: "chown webapp:webapp -R /var/app/current/log/"
    ignoreErrors: true
  08_create_log_file:
    command: "touch /var/app/current/log/production.log"
    ignoreErrors: true
  09_chown_log_production:
    command: "chown webapp:webapp /var/app/current/log/production.log"
    ignoreErrors: true
  10_chmod_log_dir:
    command: "chmod 0664 -R /var/app/current/log/"
    ignoreErrors: true
  11_update_bundler:
    command: "gem update bundler"
    ignoreErrors: true
  12_config_for_update_nokogiri:
    command: "bundle config build.nokogiri --use-system-libraries"
  13_chown_current:
    command: "chown webapp:webapp -R /var/app/current/"
    ignoreErrors: true
  14_chmod_current:
    command: "chmod 0755 -R /var/app/current/"
    ignoreErrors: true
  15_chown_current:
    command: "chown webapp:webapp -R /var/app/ondeck/"
    ignoreErrors: true
  16_chown_current:
    command: "chmod 0644 -R /var/app/ondeck/"
    ignoreErrors: true

container_commands:

  17_install_webpack:
    command: "npm install --save-dev webpack"
  18_config_for_update_nokogiri:
    command: "bundle config build.nokogiri --use-system-libraries"
  19_precompile:
    command: "RAILS_ENV=production bundle exec rake assets:precompile"
  20_database_migration:
    leader_only: true
    command: "RAILS_ENV=production bundle exec rake db:migrate"

解决方法:

eb setenv RAILS_MASTER_KEY=XXXXXXXX

尽管我在 EB Web 控制台中设置了这个环境 属性,但由于某种原因它没有被使用。在控制台设置后,它会给我一个更新成功的信息。

Environment update completed successfully.

但是一旦我这样做 $eb deploy 我就会在事件中看到这个:

Environment update is starting.

这让我相信它覆盖了我在控制台中设置的所有环境变量,或者至少它以某种方式设置了我想要的不同子集。我一尝试 $eb setenv RAILS_MASTER_KEY=XXX,就可以找到 rails 凭据。

在尝试将 ENV 属性传递给 rails 控制台时遇到同样的问题

在 AWS Web 控制台中设置它们,puma 找到它们,但 rails 控制台没有

这是我升级到 Amazon 后才出现的问题 Linux 2

我的解决方法是 re-create 来自 get-config

的 .env
files:
  "/home/ec2-user/railsc":
    mode: "000777"
    owner: root
    group: root
    content: |
      sudo su - -c "cd /var/app/current; /opt/elasticbeanstalk/bin/get-config --output YAML environment | sed 's/: /=/g' > .env; bundle exec rails c"