[X2Go-Commits] [x2gobroker] 01/01: Allow remote agent calls via hostname or host address when using the format "<hostname> (<hostaddr>)" in the session profile. This can be useful if the <hostname> is a valid address on the local network (broker <-> <server> communication), but the host address is valid for clients (client <-> server communication).

git-admin at x2go.org git-admin at x2go.org
Wed Dec 10 15:41:46 CET 2014


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository x2gobroker.

commit c4c77cbeb42ffd47f6b499ca615b528b0ba385de
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Wed Dec 10 15:41:39 2014 +0100

    Allow remote agent calls via hostname or host address when using the format "<hostname> (<hostaddr>)" in the session profile. This can be useful if the <hostname> is a valid address on the local network (broker <-> <server> communication), but the host address is valid for clients (client <-> server communication).
---
 debian/changelog                      |    5 +++++
 x2gobroker/agent.py                   |   33 ++++++++++++++++++++-------------
 x2gobroker/brokers/base_broker.py     |    6 +++---
 x2gobroker/tests/test_broker_agent.py |   21 ++++++++++++++++++++-
 4 files changed, 48 insertions(+), 17 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index ad3eb5e..935041e 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -217,6 +217,11 @@ x2gobroker (0.0.3.0-0x2go1) UNRELEASED; urgency=low
       left bogus in the session DB (plus unit tests).
     - Fix remote agent detection if one ore more X2Go Servers are offline and
       hostname does not match host address (plus unit test).
+    - Allow remote agent calls via hostname or host address when using the format
+      "<hostname> (<hostaddr>)" in the session profile. This can be useful
+      if the <hostname> is a valid address on the local network (broker <->
+      <server> communication), but the host address is valid for clients
+      (client <-> server communication).
   * debian/control:
     + Provide separate bin:package for SSH brokerage: x2gobroker-ssh.
     + Replace LDAP support with session brokerage support in LONG_DESCRIPTION.
diff --git a/x2gobroker/agent.py b/x2gobroker/agent.py
index 56c66fd..43d8b69 100644
--- a/x2gobroker/agent.py
+++ b/x2gobroker/agent.py
@@ -174,17 +174,24 @@ def _call_remote_broker_agent(username, task, cmdline_args=[], remote_agent=None
     else:
         remote_port = 22
 
-    if x2gobroker.utils.portscan(remote_hostaddr, remote_port):
-        cmd_line = [
-            '{x2gobroker_agent_binary}'.format(x2gobroker_agent_binary=x2gobroker.defaults.X2GOBROKER_AGENT_CMD),
-            '{username}'.format(username=username),
-            '{task}'.format(task=task),
-        ]
+    cmd_line = [
+        '{x2gobroker_agent_binary}'.format(x2gobroker_agent_binary=x2gobroker.defaults.X2GOBROKER_AGENT_CMD),
+        '{username}'.format(username=username),
+        '{task}'.format(task=task),
+    ]
+    for cmdline_arg in cmdline_args:
+        cmd_line.append('"{arg}"'.format(arg=cmdline_arg))
+
+    remote_username = x2gobroker.defaults.X2GOBROKER_AGENT_USER
 
-        for cmdline_arg in cmdline_args:
-            cmd_line.append('"{arg}"'.format(arg=cmdline_arg))
+    # check how we shall connect to the remote agent's SSH host...
+    _remote_sshserver = None
+    if x2gobroker.utils.portscan(remote_hostname, remote_port):
+        _remote_sshserver = remote_hostname
+    elif x2gobroker.utils.portscan(remote_hostaddr, remote_port):
+        _remote_sshserver = remote_hostaddr
 
-        remote_username = x2gobroker.defaults.X2GOBROKER_AGENT_USER
+    if _remote_sshserver:
         # now, connect and use paramiko Client to negotiate SSH2 across the connection
         try:
             client = paramiko.SSHClient()
@@ -192,19 +199,19 @@ def _call_remote_broker_agent(username, task, cmdline_args=[], remote_agent=None
             if os.path.exists(os.path.expanduser("~/.ssh/known_hosts")):
                 client.load_host_keys(os.path.expanduser("~/.ssh/known_hosts"))
             client.set_missing_host_key_policy(remote_agent['host_key_policy'])
-            client.connect(remote_hostaddr, remote_port, remote_username, look_for_keys=True, allow_agent=True)
+            client.connect(_remote_sshserver, remote_port, remote_username, look_for_keys=True, allow_agent=True)
 
             result = []
             ssh_transport = client.get_transport()
             if ssh_transport and ssh_transport.is_authenticated():
                 cmd = ' '.join(cmd_line)
                 cmd = 'sh -c \'{cmd}\''.format(cmd=cmd)
-                logger_broker.info('Executing agent command on remote host ({remote_agent}): {cmd}'.format(remote_agent=remote_agent['hostname'], cmd=cmd))
+                logger_broker.info('Executing agent command on remote host {hostname} ({hostaddr}): {cmd}'.format(hostname=remote_hostname, hostaddr=remote_hostaddr, cmd=cmd))
                 (stdin, stdout, stderr) = client.exec_command(cmd)
                 result = stdout.read().split('\n')
                 err = stderr.read().replace('\n', ' ')
                 if err:
-                    logger_broker.warning('Remote agent command (host: {remote_agent}) reported an error: {err}'.format(remote_agent=remote_agent['hostname'], err=err))
+                    logger_broker.warning('Remote agent command (host: {hostname} ({hostaddr})) reported an error: {err}'.format(hostname=remote_hostname, hostaddr=remote_hostaddr, err=err))
             client.close()
             if result:
                 logger_broker.info('Broker agent answered: {answer}'.format(answer="; ".join(result)))
@@ -233,7 +240,7 @@ def ping(remote_agent=None, **kwargs):
         return _call_local_broker_agent(username)[0]
     else:
         return remote_agent is not None and \
-               (x2gobroker.utils.portscan(remote_agent['hostname'], remote_agent['port']) or x2gobroker.utils.portscan(remote_agent['hostname'], remote_agent['port'])) and \
+               (x2gobroker.utils.portscan(remote_agent['hostaddr'], remote_agent['port']) or x2gobroker.utils.portscan(remote_agent['hostname'], remote_agent['port'])) and \
                _call_remote_broker_agent(username, task='ping', remote_agent=remote_agent)[0]
 tasks['ping'] = ping
 
diff --git a/x2gobroker/brokers/base_broker.py b/x2gobroker/brokers/base_broker.py
index ebcf4af..a8d0ffb 100644
--- a/x2gobroker/brokers/base_broker.py
+++ b/x2gobroker/brokers/base_broker.py
@@ -1092,9 +1092,9 @@ class X2GoBroker(object):
                 try:
                     success, running_sessions, suspended_sessions = x2gobroker.agent.has_sessions(username, remote_agent=remote_agent)
                     if running_sessions:
-                        logger_broker.debug('base_broker.X2GoBroker.list_profiles(): found running sessions on host(s): {hosts}'.format(hosts=', '.join(running_sessions)))
+                        logger_broker.debug('base_broker.X2GoBroker.get_profile_for_user(): found running sessions on host(s): {hosts}'.format(hosts=', '.join(running_sessions)))
                     if suspended_sessions:
-                        logger_broker.debug('base_broker.X2GoBroker.list_profiles(): found running sessions on host(s): {hosts}'.format(hosts=', '.join(suspended_sessions)))
+                        logger_broker.debug('base_broker.X2GoBroker.get_profile_for_user(): found running sessions on host(s): {hosts}'.format(hosts=', '.join(suspended_sessions)))
                     suspended_matching_hostnames = x2gobroker.utils.matching_hostnames(profile['host'], suspended_sessions)
                     running_matching_hostnames = x2gobroker.utils.matching_hostnames(profile['host'], running_sessions)
                     if suspended_matching_hostnames:
@@ -1107,7 +1107,7 @@ class X2GoBroker(object):
                         profile['host'] = [profile['host'][0]]
 
                     if profile.has_key('status') and profile['status']:
-                        logger_broker.debug('base_broker.X2GoBroker.list_profiles(): marking session profile {name} as {status}'.format(name=profile['name'], status=profile['status']))
+                        logger_broker.debug('base_broker.X2GoBroker.get_profile_for_user(): marking session profile {name} as {status}'.format(name=profile['name'], status=profile['status']))
 
                 except x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException:
                     pass
diff --git a/x2gobroker/tests/test_broker_agent.py b/x2gobroker/tests/test_broker_agent.py
index e9ead47..36c7b70 100644
--- a/x2gobroker/tests/test_broker_agent.py
+++ b/x2gobroker/tests/test_broker_agent.py
@@ -449,7 +449,13 @@ broker-autologin = true
         def _fake_portscan(addr, port=22):
             if addr == 'host3.internal':
                 return False
-            if addr.startswith('downhost'):
+            elif addr.startswith('downhost'):
+                return False
+            elif addr == '10.0.2.11':
+                return False
+            elif addr.endswith('.local'):
+                return True
+            elif addr.endswith('.external'):
                 return False
             return True
 
@@ -473,6 +479,12 @@ host = downhost1.internal (10.0.2.11), host2.internal (10.0.2.12), host3.interna
 broker-agent-query-mode = SSH
 broker-portscan-x2goservers = true
 
+[testprofile2]
+name = testprofile2
+host = downhost1.local (downhost1.external), host2.local (host2.external), host3.local (host3.external)
+broker-agent-query-mode = SSH
+broker-portscan-x2goservers = true
+
 """
         tf = tempfile.NamedTemporaryFile()
         print >> tf, _session_profiles
@@ -485,6 +497,13 @@ broker-portscan-x2goservers = true
             self.assertTrue ( remote_agent['hostaddr'] != '10.0.2.11')
             i += 1
 
+        i = 0
+        while i < 50:
+            remote_agent = inifile_backend.get_remote_agent('testprofile2')
+            self.assertTrue ( bool(remote_agent) )
+            self.assertTrue ( remote_agent['hostaddr'] != 'downhost1.external')
+            i += 1
+
 def test_suite():
     from unittest import TestSuite, makeSuite
     suite = TestSuite()

--
Alioth's /srv/git/_hooks_/post-receive-email on /srv/git/code.x2go.org/x2gobroker.git


More information about the x2go-commits mailing list