Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Implement "dynamic_auth" for Tango #938

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/taurus/core/tango/tangodatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from taurus.core.taurusauthority import TaurusAuthority
from taurus.core.util.containers import CaselessDict
from taurus.core.util.log import taurus4_deprecation
from taurus.core.tango.tangovalidator import TangoAuthorityNameValidator


__all__ = ["TangoInfo", "TangoAttrInfo", "TangoDevInfo", "TangoServInfo",
Expand Down Expand Up @@ -673,19 +674,31 @@ def __init__(self, host=None, port=None, parent=None):
if host is None or port is None:
try:
_hp = TangoAuthority.get_default_tango_host()
host, port = _hp.rsplit(':', 1)
v = TangoAuthorityNameValidator()
g = v.getUriGroups('tango://{}'.format(_hp))
host = g.get('host', None)
port = g.get('port', None)
if host is None:
complete_name = "tango://.dynamic_auth."
else:
host, port = _hp.rsplit(':', 1)
except Exception:
from taurus import warning
warning("Error getting default Tango host")

# Set host to fqdn
host = socket.getfqdn(host)
if host is not None:
host = socket.getfqdn(host)
db = Database(host, port)
complete_name = "tango://%s:%s" % (host, port)
else:
db = Database()

self.dbObj = Database(host, port)
self.dbObj = db
self._dbProxy = None
self._dbCache = None

complete_name = "tango://%s:%s" % (host, port)

self.call__init__(TaurusAuthority, complete_name, parent)

try:
Expand Down
8 changes: 7 additions & 1 deletion lib/taurus/core/tango/tangodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from taurus.core.taurusbasetypes import (TaurusDevState, TaurusLockInfo,
LockStatus, TaurusEventType)
from taurus.core.util.log import taurus4_deprecation
from taurus.core.tango.tangovalidator import TangoDeviceNameValidator


__all__ = ["TangoDevice"]
Expand Down Expand Up @@ -206,7 +207,12 @@ def getDisplayValue(self, cache=True):

def _createHWObject(self):
try:
return DeviceProxy(self.getFullName())
v = TangoDeviceNameValidator()
g = v.getUriGroups(self.getFullName())
if g['authority'] != '//.dynamic_auth.':
return DeviceProxy(self.getFullName())
else:
return DeviceProxy(self.getSimpleName())
except DevFailed as e:
self.warning('Could not create HW object: %s' % (e.args[0].desc))
self.traceback()
Expand Down
3 changes: 2 additions & 1 deletion lib/taurus/core/tango/tangofactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ def getAuthority(self, name=None):
if name is None:
if self.dft_db is None:
try:
if self._default_tango_host is None:
if (self._default_tango_host is None
or self._default_tango_host == '.dynamic_auth.'):
self.dft_db = _Authority()
else:
name = self._default_tango_host
Expand Down
27 changes: 17 additions & 10 deletions lib/taurus/core/tango/tangovalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class TangoAuthorityNameValidator(TaurusAuthorityNameValidator):
'''

scheme = 'tango'
authority = '//(?P<host>([\w\-_]+\.)*[\w\-_]+):(?P<port>\d{1,5})'
authority = ('//((?P<host>([\w\-_]+\.)*[\w\-_]+):(?P<port>\d{1,5})'
+ '|.dynamic_auth.'
+ '|([\w\-_]+\.)*[\w\-_]+:\d{1,5}'
+ '(,([\w\-_]+\.)*[\w\-_]+:\d{1,5})+)')
path = '(?!)'
query = '(?!)'
fragment = '(?!)'
Expand All @@ -62,7 +65,7 @@ def getUriGroups(self, name, strict=None):
name using fully qualified domain name for the host.
'''
ret = TaurusAuthorityNameValidator.getUriGroups(self, name, strict)
if ret is not None:
if ret is not None and ret.get("host", None) is not None:
fqdn = socket.getfqdn(ret["host"])
ret["host"] = fqdn
ret["authority"] = "//{host}:{port}".format(**ret)
Expand Down Expand Up @@ -120,10 +123,10 @@ def getNames(self, fullname, factory=None, queryAuth=True):

if default_authority is None:
import PyTango
host, port = PyTango.ApiUtil.get_env_var('TANGO_HOST').split(":")
# Get the fully qualified domain name
host = socket.getfqdn(host)
default_authority = "//{0}:{1}".format(host, port)
tango_host = PyTango.ApiUtil.get_env_var('TANGO_HOST')
v = TangoAuthorityNameValidator()
g = v.getUriGroups('tango://{}'.format(tango_host))
cpascual marked this conversation as resolved.
Show resolved Hide resolved
default_authority = g.get('authority')

authority = groups.get('authority')
if authority is None:
Expand Down Expand Up @@ -221,10 +224,14 @@ def getUriGroups(self, name, strict=None):
name using fully qualified domain name for the host.
'''
ret = TaurusAttributeNameValidator.getUriGroups(self, name, strict)
if ret is not None and ret.get("host", None) is not None:
fqdn = socket.getfqdn(ret["host"])
ret["host"] = fqdn
ret["authority"] = "//{host}:{port}".format(**ret)
if ret is not None:
if ret.get("host", None) is not None:
fqdn = socket.getfqdn(ret["host"])
ret["host"] = fqdn
auth = "//{host}:{port}".format(**ret)
else:
auth = "//.dynamic_auth."
ret["authority"] = auth
return ret

def getNames(self, fullname, factory=None, queryAuth=True, fragment=False):
Expand Down
5 changes: 5 additions & 0 deletions lib/taurus/core/tango/test/test_tangovalidator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@
# Tests for Tango Authority name validation
#=========================================================================
@valid(name='tango://foo:10000')
@valid(name='tango://foo:10000,foo:20000')
@valid(name='tango://.dynamic_auth.')
@invalid(name='tango:foo')
@invalid(name='tango:foo,')
@invalid(name='tango:foo:10000')
@invalid(name='tango://foo:10000,')
@invalid(name='tango://foo:10000,bar')
@invalid(name='tango://foo:10000/')
@invalid(name='tango://foo:10000/?')
@invalid(name='tango://foo:bar')
Expand Down