Search code examples
pythonvolttron

How can I debug a connection issue within VOLTTRON?


I am connecting to an external VOLTTRON instance. I am not getting a response from the connection. What's the issue?

I am writing a simple python script to connect to an external platform and retrieve the peers. If I get the serverkey, clientkey, and/or publickey incorrect I don't know how to determine which is the culprit, from the client side. I just get a gevent timeout. Is there a way to know?

import os

import gevent
from volttron.platform.vip.agent import Agent

secret = "secret"
public = "public"
serverkey = "server"
tcp_address = "tcp://external:22916"

agent = Agent(address=tcp_address, serverkey=serverkey, secretkey=secret,
              publickey=public)
event = gevent.event.Event()
greenlet = gevent.spawn(agent.core.run, event)
event.wait(timeout=30)
print("My id: {}".format(agent.core.identity))
peers = agent.vip.peerlist().get(timeout=5)
for p in peers:
    print(p)
gevent.sleep(3)
greenlet.kill()

Solution

  • The short answer: no, the client cannot determine why its connection to the server failed. The client will attempt to connect until it times out.

    Logs and debug messages on the server side can help troubleshoot a connection problem. There are three distinct messages related to key errors:

    1. CURVE I: cannot open client HELLO -- wrong server key?
      Either the client omit the server key, the client used the wrong server key, or the server omit the secret key.

    2. CURVE I: cannot open client INITIATE vouch
      Either the client omit the public or secret key, or its public and secret keys don't correspond to each other.

    3. authentication failure
      The server key was correct and the secret and public keys are valid, but the server rejected the connection because the client was not authorized to connect (based on the client's public key).

    The first two messages are printed by libzmq. To see the third message volttron must be started with increased verboseness (at least -v).


    Here is a simple ZMQ server-client example you can use to test some of these scenarios:

    Server:

    import zmq 
    
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.curve_server = 1 
    socket.curve_secretkey = "mW4i2O{kmcOXs9q>UP0(no4-Sp1r(p>vK?*NFwV$"
    # The corresponding public key is "krEC0>hsx+o4Jxg2yvitCOVwr2GF85akNIsUdiH5"
    socket.bind("ipc://test123")
    
    while True:
        msg = socket.recv()
        new_msg = "I got the message: {}".format(msg)
        print(new_msg)
        socket.send(new_msg)
    

    Client:

    import zmq 
    
    pub, sec = zmq.curve_keypair()
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.curve_secretkey = sec 
    socket.curve_publickey = pub 
    socket.curve_serverkey = "krEC0>hsx+o4Jxg2yvitCOVwr2GF85akNIsUdiH5"
    socket.connect("ipc://test123")
    
    socket.send(b'Hello')
    msg = socket.recv()
    print("From the server: {}".format(msg))