I CANNOT CONNECT TO AWS IOT USING PAHO GOLANG

0

I have the following code made using golang:

` package main

import ( "crypto/tls" "crypto/x509" "fmt" "log" "os" "os/signal" "path/filepath"

MQTT "github.com/eclipse/paho.mqtt.golang"

)

func main() { MQTT.DEBUG = log.New(os.Stdout, "", 0) MQTT.ERROR = log.New(os.Stdout, "", 0)

// Get working dir path
dir, err := os.Getwd()
if err != nil {
	panic(err)
}

// Archivos
KeyPath := filepath.Join(dir, "assets", "otra_prueba.private.key")
CertPath := filepath.Join(dir, "assets", "otra_prueba.cert.pem")
CAPath1 := filepath.Join(dir, "assets", "root-CA.crt")
ClientId := "basicPubSub"
Endpoint := "a2fqp5ya964aj9-ats.iot.us-east-1.amazonaws.com"

// Carga de certificados
tlsCert, err := tls.LoadX509KeyPair(CertPath, KeyPath)
if err != nil {
	panic(err)
}

certs := x509.NewCertPool()
caPem1, err := os.ReadFile(CAPath1)
if err != nil {
	panic(err)
}

certs.AppendCertsFromPEM(caPem1)

tlsConfig := &tls.Config{
	Certificates: []tls.Certificate{tlsCert},
	RootCAs:      certs,
}

// Carga de opciones
options := MQTT.NewClientOptions()
options.AddBroker(fmt.Sprintf("tcps://%s:8883/mqtt", Endpoint))
options.SetClientID(ClientId)
options.SetTLSConfig(tlsConfig)

// Conexion
mqttClient := MQTT.NewClient(options)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
	panic(token.Error())
}

fmt.Println("[MQTT] Connected")

quit := make(chan struct{})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
	<-c
	mqttClient.Disconnect(250)
	fmt.Println("[MQTT] Disconnected")

	quit <- struct{}{}
}()
<-quit

} ` //--------------------------------------------------------------------------------

Output the following response:

  • $ go1.20.2 run cmd/main.go
  • [client] Connect()
  • [store] memorystore initialized
  • [client] about to write new connect msg
  • [client] socket connected to broker
  • [client] Using MQTT 3.1.1 protocol
  • [net] connect started
  • [net] connect got error EOF
  • [client] Trying reconnect using MQTT 3.1 protocol
  • [client] socket connected to broker
  • [client] Using MQTT 3.1 protocol
  • [net] connect started
  • [net] connect got error EOF
  • [client] Failed to connect to a broker
  • [store] memorystore closed
  • panic: network Error : EOF

// --------------------------------------------------------------------------------- I think I'm using the certificates wrong. How can I connect to the mqtt broker

gefragt vor einem Jahr545 Aufrufe
1 Antwort
1

Hi Guillermo. I take it that the certificate is registered in IoT Core? Do you have an IoT policy attached to that certificate? Does that policy allow Connect?

profile pictureAWS
EXPERTE
Greg_B
beantwortet vor einem Jahr
  • I have the following policies:

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:Publish", "iot:Receive" ], "Resource": [ "arn:aws:iot:[Region]:[account]:topic/sdk/test/java", "arn:aws:iot:[Region]:[account]:topic/sdk/test/python", "arn:aws:iot:[Region]:[account]:topic/sdk/test/js" ] }, { "Effect": "Allow", "Action": [ "iot:Subscribe" ], "Resource": [ "arn:aws:iot:[Region]:[account]:topicfilter/sdk/test/java", "arn:aws:iot:[Region]:[account]:topicfilter/sdk/test/python", "arn:aws:iot:[Region]:[account]:topicfilter/sdk/test/js" ] }, { "Effect": "Allow", "Action": [ "iot:Connect" ], "Resource": [ "arn:aws:iot:[Region]:[account]:client/sdk-java", "arn:aws:iot:[Region]:[account]:client/basicPubSub", "arn:aws:iot:[Region]:[account]:client/sdk-nodejs-*" ] } ] }

  • I think the policy looks OK. Is it attached to the certificate?

  • yes, i attached that policy to the certificate

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen