【问题标题】:AWS DynamoDB Encryption - RubyAWS DynamoDB 加密 - 红宝石
【发布时间】:2018-01-20 17:23:53
【问题描述】:

是否可以使用 Ruby / Ruby on Rails 为 DynamoDB 设置加密?

aws-sdk 提供吗?

我应该自己做吗?

还是不可能?

注意:很少有相同的 Rails 应用程序并行运行并连接到相同的 DynamoDB。

【问题讨论】:

  • 看看下面的答案

标签: ruby-on-rails ruby amazon-web-services amazon-dynamodb


【解决方案1】:

DynamoDB 不会为您加密数据。您必须在 DynamoDB 客户端代码中处理加密和解密。有一个 Java library 来处理这个问题,但我没有看到一个用于 Ruby 的。

请注意,DynamoDB 将无法对您加密的任何字段执行有意义的查询。

我建议使用 AWS KMS 服务来管理您的应用程序在将数据插入 DynamoDB 之前用来加密数据的加密密钥。

【讨论】:

    【解决方案2】:

    使用 AWS KMS 加密关键字段效果很好 - 这是一个如何将其包装在一个方便的模块中的示例。

    需要 gem 'aws-sdk', '~> 2.0'

    config/initializers/aws_kms.rb

    # Helper for AWS KMS encryption / decryption:
    #
    # Example:
    #
    # cipherdata = Aws::KMS.encrypt('boring plain text')
    #
    # => "AQICAkSXnpkzAJrrzNcjlMxRK78jbKHnkPohQdSZ445Xv29z6C2ty433pG2rcs96IujEj4IXAa1rmSzJXfiQqw4LaFcluh3CYsFQOlOfhgh0LhPdiQhnIP7d0BzlIu3uRFzHLrhIpg2JssVsjnCLZstNkzerfiwYtGSNTpltVjaqJblz3kktSFNxnMoVOcJSlvTbuMdiD9yplGMDD3LC0YKjUZtEZIqEjt=="
    #
    # Aws::KMS.decrypt( cipherdata )
    #
    # => "boring plain text"
    #
    #
    # Protects from AWS KMS calls if we are in TEST environment
    #
    module Aws::KMS
    
      def self.encrypt(plaintext)
        if Rails.env.test? || Rails.env.development?
          ['not_encrypted_in_test', plaintext].join('|') # faking encryption so we can test this without AWS
        else
          cipherdata = client.encrypt({
            key_id: ENV['KMS_ARN'],
            plaintext: plaintext,
          }).ciphertext_blob
          Base64.strict_encode64(cipherdata)
        end
      end
    
      def self.decrypt(ciphertext)
        if Rails.env.test? || Rails.env.development?
          ciphertext.split('|').second # faking decryption so we can test this without AWS
        else
          cipherdata = Base64.strict_decode64( ciphertext )
    
          client.decrypt({
            ciphertext_blob: cipherdata
          }).plaintext
        end
      end
    
      # this helper method can be called to check if the ENV variables are configured correctly:
      def self.setup_correct?
        plaintext = 'Hey, it works!'
        cipherdata = Aws::KMS.encrypt( plaintext )
        Aws::KMS.decrypt( cipherdata ) == plaintext
      end
    
      def self.client
        Aws::KMS::Client.new
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 2014-04-11
      • 1970-01-01
      相关资源
      最近更新 更多