123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::Messages::RotationCoordinator

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activesupport/lib/active_support/messages/rotation_coordinator.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(&secret_generator) ⇒ RotationCoordinator

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 10

def initialize(&secret_generator)
  raise ArgumentError, "A secret generator block is required" unless secret_generator
  @secret_generator = secret_generator
  @rotate_options = []
  @on_rotation = nil
  @codecs = {}
end

Instance Attribute Details

#transitional (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 8

attr_accessor :transitional

Instance Method Details

#[](salt)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 18

def [](salt)
  @codecs[salt] ||= build_with_rotations(salt)
end

#[]=(salt, codec)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 22

def []=(salt, codec)
  @codecs[salt] = codec
end

#build(salt, secret_generator:, secret_generator_options:, **options) (private)

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 88

def build(salt, secret_generator:, secret_generator_options:, **options)
  raise NotImplementedError
end

#build_with_rotations(salt) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 76

def build_with_rotations(salt)
  rotate_options = @rotate_options.map { |options| options.is_a?(Proc) ? options.(salt) : options }
  transitional = self.transitional && rotate_options.first
  rotate_options.compact!
  rotate_options[0..1] = rotate_options[0..1].reverse if transitional
  rotate_options = rotate_options.map { |options| normalize_options(options) }.uniq

  raise "No options have been configured for #{salt}" if rotate_options.empty?

  rotate_options.map { |options| build(salt.to_s, **options) }.reduce(&:fall_back_to)
end

#changing_configuration! (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 51

def changing_configuration!
  if @codecs.any?
    raise <<~MESSAGE
      Cannot change #{self.class} configuration after it has already been applied.

      The configuration has been applied with the following salts:
      #{@codecs.keys.map { |salt| "- #{salt.inspect}" }.join("\n")}
    MESSAGE
  end
end

#clear_rotations

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 39

def clear_rotations
  changing_configuration!
  @rotate_options.clear
  self
end

#normalize_options(options) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 62

def normalize_options(options)
  options = options.dup

  options[:secret_generator] ||= @secret_generator

  secret_generator_kwargs = options[:secret_generator].parameters.
    filter_map { |type, name| name if type == :key || type == :keyreq }
  options[:secret_generator_options] = options.extract!(*secret_generator_kwargs)

  options[:on_rotation] = @on_rotation

  options
end

#on_rotation(&callback)

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 45

def on_rotation(&callback)
  changing_configuration!
  @on_rotation = callback
end

#rotate(**options, &block)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 26

def rotate(**options, &block)
  raise ArgumentError, "Options cannot be specified when using a block" if block && !options.empty?
  changing_configuration!

  @rotate_options << (block || options)

  self
end

#rotate_defaults

[ GitHub ]

  
# File 'activesupport/lib/active_support/messages/rotation_coordinator.rb', line 35

def rotate_defaults
  rotate()
end