Jamie- / openvpn-api

A Python API for the OpenVPN management interface.
MIT License
56 stars 19 forks source link

"list index out of range" when quering for state #17

Closed jakobahlin closed 4 years ago

jakobahlin commented 4 years ago

Sometimes when I'm trying to get the state for a tunnel I get the exception below: .

Exception: list index out of range, Type: <class 'IndexError'>
Traceback (most recent call last):
  File "checkTunnelState.py", line 17, in main
    state = v.state.state_name
  File "/home/ubuntu/.local/lib/python3.7/site-packages/openvpn_api/vpn.py", line 192, in state
    self._state = self._get_state()
  File "/home/ubuntu/.local/lib/python3.7/site-packages/openvpn_api/vpn.py", line 163, in _get_state
    state_name = util.nonify_string(parts[1])
IndexError: list index out of range

It happens perhaps once in 20 attempts. I don't see a pattern when it happens, e.g if it happens with a particular state of the tunnel (however in the example below the tunnel was in state AUTH). What more information do you need?

I'm using version 0.3.0

From the openvpn logs:

Jun 11 10:25:52 server tunnel[29439]: Thu Jun 11 10:25:52 2020 us=998826 VERIFY OK: depth=2, CN=Nordex-Root-CA-2015
Jun 11 10:25:52 server tunnel[29439]: Thu Jun 11 10:25:52 2020 us=999421 VERIFY OK: depth=1, DC=com, DC=NORDEX-AG, CN=Nordex-Issuing-CA-2-2015
Jun 11 10:25:53 server tunnel[29439]: Thu Jun 11 10:25:52 2020 us=999981 VERIFY OK: depth=0, C=DE, ST=Hamburg, L=Hamburg, O=Nordex Energy GmbH, OU=Engineering, CN=kommbox
Jun 11 10:25:53 server tunnel[29439]: Thu Jun 11 10:25:53 2020 us=398446 Need password(s) from management interface, waiting...
Jun 11 10:49:02 server tunnel[29439]: Thu Jun 11 10:49:02 2020 us=281968 MANAGEMENT: Client connected from [AF_INET]127.0.0.1:8896
Jun 11 10:49:02 server tunnel[29439]: Thu Jun 11 10:49:02 2020 us=282115 MANAGEMENT: CMD 'state'
Jun 11 10:49:02 server tunnel[29439]: Thu Jun 11 10:49:02 2020 us=325563 MANAGEMENT: CMD 'quit'
Jun 11 10:49:02 server tunnel[29439]: Thu Jun 11 10:49:02 2020 us=325607 MANAGEMENT: Client disconnected

My code:

#!/usr/bin/env python3.7
import openvpn_api
import sys
import traceback

def main():
    managementPort = sys.argv[1]
    v = openvpn_api.VPN('localhost', managementPort)
    try:
        v.connect()
        state = v.state.state_name
        print(f"{state}")

    except openvpn_api.util.errors.ConnectError as e:
        print("TIMEOUT")
        print(f"Exception: {e}",file=sys.stderr)
    except Exception as e:
        print("ERROR")
        t = type(e)
        print(f"Exception: {e}, Type: {t}",file=sys.stderr)
        traceback.print_exc()
    finally:
        v.disconnect()

if __name__ == "__main__":
    main()
D0han commented 4 years ago

Code for this part is already changed on master, so you may want to check if that is working better for you. If problem persists, can you provide tcpdump of that management connection?

jakobahlin commented 4 years ago

Thanks! I'm not able to reproduce the problem with the code from master, so probably the issue is gone after those changes 👍

jakobahlin commented 4 years ago

Now I'm getting this sometimes:

Exception: invalid literal for int() with base 10: ">NEED-OK:Need 'token-insertion-request' confirmation MSG:Please insert eToken token", Type: <class 'ValueError'>
Traceback (most recent call last):
  File "checkOpenvpnTunnelstate.py", line 19, in main
    state = v.get_state().state_name
  File "/usr/local/lib/python3.7/dist-packages/openvpn_api-1.0.0-py3.7.egg/openvpn_api/vpn.py", line 154, in get_state
    return State.parse_raw(raw)
  File "/usr/local/lib/python3.7/dist-packages/openvpn_api-1.0.0-py3.7.egg/openvpn_api/models/state.py", line 87, in parse_raw
    up_since = datetime.datetime.utcfromtimestamp(int(parts[0])) if parts[0] != "" else None
ValueError: invalid literal for int() with base 10: ">NEED-OK:Need 'token-insertion-request' confirmation MSG:Please insert eToken token"
Jamie- commented 4 years ago

I have a feeling the two issues you've had are related, I think the first one was hiding the second one.

Valid bug, we're only handling notifications starting >INFO, >CLIENT or >STATE at the moment, is this using code from master or 0.3.0?

jakobahlin commented 4 years ago

...is this using code from master or 0.3.0?

From master

Jamie- commented 4 years ago

~Give that a go, it shouldn't blow up there now.~

Oh hang on I forgot to commit a file :man_facepalming:

Jamie- commented 4 years ago

Try that instead