【问题标题】:Rails 6 credentials in AWS Beanstalk: ArgumentError: key must be 16 bytesAWS Beanstalk 中的 Rails 6 凭证:ArgumentError: key must be 16 bytes
【发布时间】:2020-06-12 07:41:48
【问题描述】:

我碰壁了。我正在通过 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_ENVproduction 作为环境属性的值
  6. 我添加了自己的容器 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"

【问题讨论】:

    标签: amazon-elastic-beanstalk ruby-on-rails-6


    【解决方案1】:

    解决了什么问题:

    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 凭据。

    【讨论】:

      【解决方案2】:

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

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

      这只是我升级到 Amazon Linux 2 后的问题

      我的解决方法是从 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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-26
        • 2019-05-22
        • 2020-06-05
        • 2012-12-04
        • 2018-12-23
        • 2014-03-08
        相关资源
        最近更新 更多