njh / ruby-mqtt

Pure Ruby gem that implements the MQTT protocol, a lightweight protocol for publish/subscribe messaging.
http://www.rubydoc.info/gems/mqtt
MIT License
538 stars 135 forks source link

ruby-mqtt-271ee631f128/lib/mqtt/packet.rb:223:in `read_byte': Failed to read byte from socket (MQTT::ProtocolException) #161

Closed sumskyi closed 2 months ago

sumskyi commented 7 months ago

mqtt_client.connect exception

As I understood, MQTT protocol doesn't support reasonable error message from Ruby, so..

Please, point me, what I am doing wrong.

The code below mostly taken from the https://til.magmalabs.io/posts/37aa95d040-connecting-ruby-to-aws-iot-core-using-mqtt-client


region = ENV['IOT_AWS_REGION']
access_key_id = ENV['IOT_AWS_ACCESS_KEY']
secret_access_key = ENV['IOT_AWS_SECRET_ACCESS_KEY']
host = ENV['IOT_AWS_ENDPOINT']
port = ENV['IOT_AWS_PORT']

secrets_manager = Aws::SecretsManager::Client.new(
  region:, access_key_id:, secret_access_key:
)

client = Aws::IoT::Client.new(
  region:, access_key_id:, secret_access_key:
)

things = []

1.times do |i|
  logger = Logger.new("log/subscriber#{i}.log", 'daily')

  # Creates new ssl certificate
  cert = client.create_keys_and_certificate(set_as_active: true)
  ap cert

  thing = client.create_thing(thing_name: "Subscriber#{i}")
  ap thing
  #<struct Aws::IoT::Types::CreateThingResponse thing_name="Subscriber0", thing_arn="arn:aws:iot:us-west-2:607446338782:thing/Subscriber0", thing_id="a647b47c-f45f-4af4-927b-1fd03871b89f">

  things << { thing:, cert: }

  ap things

  client.attach_policy(policy_name: 'new_policy', target: cert.certificate_arn)
  client.attach_thing_principal(
    thing_name: thing.thing_name, # required
    principal: cert.certificate_arn, # required)
  )

  # Stores the certificate in aws secrets manager
  # secrets_manager.create_secret(name: 'iot_cert_pem', secret_string: cert.certificate_pem)
  # secrets_manager.create_secret(name: 'iot_private_key', secret_string: cert.key_pair.private_key)
  #
  # The operation failed because the secret iot_cert_pem already exists. (Aws::SecretsManager::Errors::ResourceExistsException)

  # Reads the certificate from aws secrets manager
  cert_pem = secrets_manager.get_secret_value(secret_id: 'iot_cert_pem').secret_string
  private_key = secrets_manager.get_secret_value(secret_id: 'iot_private_key').secret_string

  # Connects to aws iot core endpoint using mqtts
  mqtt_client = MQTT::Client.new
  mqtt_client.ssl = true
  mqtt_client.host = host
  mqtt_client.port = port
  # mqtt_client.client_id = 'basicPubSub1'
  mqtt_client.cert = cert_pem
  mqtt_client.key = private_key

  # TODO: unknown reason exception
  #   * Failed to read byte from socket (MQTT::ProtocolException)
  #   - probably some issues with thing-policy-permissions-etc combination fails
  mqtt_client.connect(MQTT::Client.generate_client_id('my-awesome-app-'))
MikeJakuszewski commented 2 months ago

@sumskyi were you able to figure this out?

sumskyi commented 2 months ago

@sumskyi were you able to figure this out?

@MikeJakuszewski Yes, sorry for not respond ASAP after I figured out the reason.

The problem was because I've committed out the storing keys to the secret_manager for some reason, so then I set mqtt_client.cert= and mqtt_client.key= as nil's