Class: DjiMqttConnect::Utils::MessageSanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/dji_mqtt_connect/utils/message_sanitizer.rb

Overview

Sanitizes MQTT messages to remove any personally-identifiable information

Constant Summary collapse

DEFAULT_LATITUDE =
"-33.865143"
DEFAULT_LONGITUDE =
"151.209900"
SECRET_KEYS =
%w[
  app_id
  app_key
  app_license
  callsign
  device_secret
  nonce
  access_key_id
  access_key_secret
  security_token
]
SERIAL_NUMBER_KEYS =
%w[device_sn gateway sn]
TOPIC_REGEX =
/\A.+\/.+\/(.+)\/.+/

Instance Method Summary collapse

Constructor Details

#initialize(latitude: DEFAULT_LATITUDE, longitude: DEFAULT_LONGITUDE, secret_keys: SECRET_KEYS, serial_number_keys: SERIAL_NUMBER_KEYS, serial_number_aliases: {}) ⇒ MessageSanitizer

Returns a new instance of MessageSanitizer.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dji_mqtt_connect/utils/message_sanitizer.rb', line 28

def initialize(latitude: DEFAULT_LATITUDE, longitude: DEFAULT_LONGITUDE, secret_keys: SECRET_KEYS, serial_number_keys: SERIAL_NUMBER_KEYS, serial_number_aliases: {})
  @latitude = latitude
  @longitude = longitude

  @secret_keys = secret_keys
  @serial_number_keys = serial_number_keys

  serial_aliases = serial_number_aliases.stringify_keys
  @serial_number_generator = Hash.new do |h, k|
    return "" if k.length == 0
    h[k] = serial_aliases.fetch(k) { sprintf("%s%02d", "SERIAL", h.length + 1) }
  end
  serial_aliases.keys.each { |k| serial_number_generator[k] }
end

Instance Method Details

#sanitize_message(message) ⇒ Object



47
48
49
50
# File 'lib/dji_mqtt_connect/utils/message_sanitizer.rb', line 47

def sanitize_message(message)
  sanitized_message = sanitize_object(message)
  strip_serial_numbers(sanitized_message)
end

#sanitize_topic(topic) ⇒ Object



43
44
45
# File 'lib/dji_mqtt_connect/utils/message_sanitizer.rb', line 43

def sanitize_topic(topic)
  topic.sub(TOPIC_REGEX) { |topic_match| topic_match.sub($1, serial_number_generator[$1]) }
end