Skip to content

Commit

Permalink
Fixes #34667 - Add SSL support when connecting to mqtt broker (#75)
Browse files Browse the repository at this point in the history
Use of SSL can be forced either way by explicitly setting mqtt_tls
setting. If unset, it gets used if certificate, private key and CA
certificate are available. Currently it reuses the foreman_ssl_* set
of certs the smart proxy has.
  • Loading branch information
adamruzicka authored Apr 6, 2022
1 parent 6f4d13b commit fc0b785
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/smart_proxy_remote_execution_ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def validate_mqtt_settings!

raise 'mqtt_broker has to be set when pull-mqtt mode is used' if Plugin.settings.mqtt_broker.nil?
raise 'mqtt_port has to be set when pull-mqtt mode is used' if Plugin.settings.mqtt_port.nil?

if Plugin.settings.mqtt_tls.nil?
Plugin.settings.mqtt_tls = [:foreman_ssl_cert, :foreman_ssl_key, :foreman_ssl_ca].all? { |key| ::Proxy::SETTINGS[key] }
end
end

def validate_ssh_log_level!
Expand Down
11 changes: 10 additions & 1 deletion lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,20 @@ def mqtt_cancel
end

def mqtt_notify(payload)
MQTT::Client.connect(settings.mqtt_broker, settings.mqtt_port) do |c|
with_mqtt_client do |c|
c.publish(mqtt_topic, JSON.dump(payload), false, 1)
end
end

def with_mqtt_client(&block)
MQTT::Client.connect(settings.mqtt_broker, settings.mqtt_port,
:ssl => settings.mqtt_tls,
:cert_file => ::Proxy::SETTINGS.foreman_ssl_cert,
:key_file => ::Proxy::SETTINGS.foreman_ssl_key,
:ca_file => ::Proxy::SETTINGS.foreman_ssl_ca,
&block)
end

def host_name
alternative_names = input.fetch(:alternative_names, {})

Expand Down
1 change: 1 addition & 0 deletions lib/smart_proxy_remote_execution_ssh/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Plugin < Proxy::Plugin
:cleanup_working_dirs => true,
# :mqtt_broker => nil,
# :mqtt_port => nil,
# :mqtt_tls => nil,
:mode => :ssh

plugin :ssh, Proxy::RemoteExecution::Ssh::VERSION
Expand Down
5 changes: 5 additions & 0 deletions settings.d/remote_execution_ssh.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@
# MQTT configuration, need to be set if mode is set to pull-mqtt
# :mqtt_broker: localhost
# :mqtt_port: 1883

# Use of SSL can be forced either way by explicitly setting mqtt_tls setting. If
# unset, SSL gets used if smart-proxy's foreman_ssl_cert, foreman_ssl_key and
# foreman_ssl_ca settings are set available.
# :mqtt_tls:

0 comments on commit fc0b785

Please sign in to comment.