跳至内容 跳至搜索
方法
#
C
I
O
R

属性

[RW] transitional

如果为真,则在构建消息加密器时交换前两个旋转选项集。例如,使用以下配置,消息加密器将使用serializer: Marshal, url_safe: true加密消息,并且能够解密使用任何三个选项集加密的消息

encryptors = ActiveSupport::MessageEncryptors.new { ... }
encryptors.rotate(serializer: JSON, url_safe: true)
encryptors.rotate(serializer: Marshal, url_safe: true)
encryptors.rotate(serializer: Marshal, url_safe: false)
encryptors.transitional = true

这在执行应用程序的滚动部署时很有用,在滚动部署中,尚未更新的服务器仍然必须能够解密来自更新服务器的消息。在这种情况下,首先使用新的旋转(例如serializer: JSON, url_safe: true)作为第一个旋转和transitional = true执行滚动部署。然后,在所有服务器更新后,使用transitional = false执行第二次滚动部署。

实例公共方法

[](salt)

返回一个使用从给定salt派生的密钥和来自rotate的选项配置的MessageEncryptorMessageEncryptor实例将被记忆,因此相同的salt将返回相同的实例。

# File activesupport/lib/active_support/message_encryptors.rb, line 48
    

[]=(salt, encryptor)

覆盖与给定salt关联的MessageEncryptor实例。

# File activesupport/lib/active_support/message_encryptors.rb, line 56
    

clear_rotations

清除选项集列表。

# File activesupport/lib/active_support/message_encryptors.rb, line 117
    

initialize(&secret_generator)

初始化一个新实例。secret_generator 必须接受一个盐值和一个 secret_length 关键字参数,并返回一个合适的密钥(字符串)或密钥集合(字符串数组)。secret_generator 也可能接受其他任意关键字参数。如果调用 rotate 时,任何与这些关键字参数匹配的选项将被传递给 secret_generator 而不是消息加密器。

encryptors = ActiveSupport::MessageEncryptors.new do |salt, secret_length:, base:|
  MySecretGenerator.new(base).generate(salt, secret_length)
end

encryptors.rotate(base: "...")
# File activesupport/lib/active_support/message_encryptors.rb, line 31
    

on_rotation(&callback)

设置一个回调函数,当使用除第一个以外的选项集解密消息时调用。

例如,此回调函数可以记录每次调用,从而指示旧的选项集是否仍在使用,或者是否可以从轮换中删除。

# File activesupport/lib/active_support/message_encryptors.rb, line 123
    

rotate(**options)
rotate(&block)

options 添加到选项集列表中。 Messages 将使用列表中的第一个集合进行加密。但是,在解密时,将按顺序尝试每个集合,直到成功为止。

值得注意的是,:secret_generator 选项可以指定与最初指定的不同的密钥生成器。密钥生成器必须响应 call,接受一个盐值和一个 secret_length 关键字参数,并返回一个合适的密钥(字符串)或密钥集合(字符串数组)。密钥生成器也可以接受其他任意关键字参数。

如果任何选项与有效密钥生成器的关键字参数匹配,则这些选项将被传递给密钥生成器,而不是消息加密器。

为了实现细粒度的按盐值轮换,支持块形式。块将接收盐值,并应返回一个适当的选项 Hash。块也可以返回 nil 来指示轮换不适用于给定的盐值。例如

encryptors = ActiveSupport::MessageEncryptors.new { ... }

encryptors.rotate do |salt|
  case salt
  when :foo
    { serializer: JSON, url_safe: true }
  when :bar
    { serializer: Marshal, url_safe: true }
  end
end

encryptors.rotate(serializer: Marshal, url_safe: false)

# Uses `serializer: JSON, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: false`.
encryptors[:foo]

# Uses `serializer: Marshal, url_safe: true`.
# Falls back to `serializer: Marshal, url_safe: false`.
encryptors[:bar]

# Uses `serializer: Marshal, url_safe: false`.
encryptors[:baz]
# File activesupport/lib/active_support/message_encryptors.rb, line 62
    

rotate_defaults

使用默认选项调用 rotate

# File activesupport/lib/active_support/message_encryptors.rb, line 111