123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Encryption::Properties

Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/encryption/properties.rb

Overview

This is a wrapper for a hash of encryption properties. It is used by Key (public tags) and Message (headers).

Since properties are serialized in messages, it is important for storage efficiency to keep their keys as short as possible. It defines accessors for common properties that will keep these keys very short while exposing a readable name.

message.headers.encrypted_data_key # instead of message.headers[:k]

See DEFAULT_PROPERTIES, Key, Message

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(initial_properties = {}) ⇒ Properties

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 42

def initialize(initial_properties = {})
  @data = {}
  add(initial_properties)
end

Instance Attribute Details

#data (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 73

attr_reader :data

#each (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 20

delegate :==, :[], :each, :key?, to: :data

Instance Method Details

#==

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 20

delegate :==, :[], :each, :key?, to: :data

#[]

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 20

delegate :==, :[], :each, :key?, to: :data

#[]=(key, value)

Set a value for a given key

It will raise an EncryptedContentIntegrity if the value exists

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 50

def []=(key, value)
  raise Errors::EncryptedContentIntegrity, "Properties can't be overridden: #{key}" if key?(key)
  validate_value_type(value)
  data[key] = value
end

#add(other_properties)

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 62

def add(other_properties)
  other_properties.each do |key, value|
    self[key.to_sym] = value
  end
end

#key?Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 20

delegate :==, :[], :each, :key?, to: :data

#to_h

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 68

def to_h
  data
end

#validate_value_type(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/encryption/properties.rb', line 56

def validate_value_type(value)
  unless ALLOWED_VALUE_CLASSES.include?(value.class) || ALLOWED_VALUE_CLASSES.any? { |klass| value.is_a?(klass) }
    raise ActiveRecord::Encryption::Errors::ForbiddenClass, "Can't store a #{value.class}, only properties of type #{ALLOWED_VALUE_CLASSES.inspect} are allowed"
  end
end