| 1 |
""" |
| 2 |
VOSI renderers. |
| 3 |
|
| 4 |
These are really three different renderers for each service. IVOA wants |
| 5 |
it this way (in effect, since they are supposed to be three different |
| 6 |
capabilities). |
| 7 |
""" |
| 8 |
|
| 9 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 10 |
#c |
| 11 |
#c This program is free software, covered by the GNU GPL. See the |
| 12 |
#c COPYING file in the source distribution. |
| 13 |
|
| 14 |
|
| 15 |
from twisted.internet import threads |
| 16 |
from twisted.web import resource |
| 17 |
from twisted.web import server |
| 18 |
|
| 19 |
from gavo import base |
| 20 |
from gavo import registry |
| 21 |
from gavo import svcs |
| 22 |
from gavo import utils |
| 23 |
from gavo.base import meta |
| 24 |
from gavo.registry import capabilities |
| 25 |
from gavo.registry import tableset |
| 26 |
from gavo.registry.model import VS |
| 27 |
from gavo.protocols import creds |
| 28 |
from gavo.svcs import weberrors |
| 29 |
from gavo.utils.stanxml import Element, registerPrefix, schemaURL, xsiPrefix |
| 30 |
from gavo.web import grend |
| 31 |
|
| 32 |
|
| 33 |
registerPrefix("avl", "http://www.ivoa.net/xml/VOSIAvailability/v1.0", |
| 34 |
schemaURL("VOSIAvailability-v1.0.xsd")) |
| 35 |
registerPrefix("cap", "http://www.ivoa.net/xml/VOSICapabilities/v1.0", |
| 36 |
schemaURL("VOSICapabilities-v1.0.xsd")) |
| 37 |
registerPrefix("vtm", "http://www.ivoa.net/xml/VOSITables/v1.0", |
| 38 |
schemaURL("VOSITables-v1.0.xsd")) |
| 39 |
|
| 40 |
|
| 41 |
class VOSIRenderMixin(object): |
| 42 |
"""A mixin furnishing the basic rendering methods for VOSI resources. |
| 43 |
|
| 44 |
You will have to provide a method _getTree(request) returning some |
| 45 |
xmlstan. |
| 46 |
""" |
| 47 |
def render(self, request): |
| 48 |
request.setHeader("content-type", "text/xml") |
| 49 |
threads.deferToThread(self._getTree, request |
| 50 |
).addCallback(self._shipout, request |
| 51 |
).addCallback(request.finishCallback |
| 52 |
).addErrback(weberrors.renderDCErrorPage, request) |
| 53 |
return server.NOT_DONE_YET |
| 54 |
|
| 55 |
def _shipout(self, response, request): |
| 56 |
utils.xmlwrite(response, request, |
| 57 |
prolog="<?xml-stylesheet href='/static/xsl/vosi.xsl' type='text/xsl'?>") |
| 58 |
|
| 59 |
|
| 60 |
class VOSIRenderer(VOSIRenderMixin, grend.ServiceBasedPage): |
| 61 |
"""An abstract base for renderers handling VOSI requests. |
| 62 |
|
| 63 |
All of these return some sort of XML and are legal on all services. |
| 64 |
|
| 65 |
The actual documents returned are defined in _getTree(request)->deferred |
| 66 |
firing stanxml. |
| 67 |
""" |
| 68 |
checkedRenderer = False |
| 69 |
|
| 70 |
def _getTree(self, request): # pragma: no cover |
| 71 |
raise ValueError("_getTree has not been overridden.") |
| 72 |
|
| 73 |
|
| 74 |
############ The VOSI data models (no better place for it yet) |
| 75 |
|
| 76 |
|
| 77 |
class AVL(object): |
| 78 |
"""The container for elements from the VOSI availability schema. |
| 79 |
""" |
| 80 |
class AVLElement(Element): |
| 81 |
_prefix = "avl" |
| 82 |
|
| 83 |
class availability(AVLElement): |
| 84 |
_additionalPrefixes = xsiPrefix |
| 85 |
class available(AVLElement): pass |
| 86 |
class upSince(AVLElement): pass |
| 87 |
class downAt(AVLElement): pass |
| 88 |
class backAt(AVLElement): pass |
| 89 |
class note(AVLElement): pass |
| 90 |
|
| 91 |
|
| 92 |
class CAP(object): |
| 93 |
"""The container for element from the VOSI capabilities schema. |
| 94 |
""" |
| 95 |
class CAPElement(Element): |
| 96 |
_prefix = "cap" |
| 97 |
|
| 98 |
class capabilities(CAPElement): |
| 99 |
_mayBeEmpty = True |
| 100 |
|
| 101 |
|
| 102 |
class VTM(object): |
| 103 |
"""The container for element from the VOSI tableset schema. |
| 104 |
""" |
| 105 |
class VTMElement(Element): |
| 106 |
_prefix = "vtm" |
| 107 |
|
| 108 |
class tableset(VTMElement): |
| 109 |
_mayBeEmpty = True |
| 110 |
|
| 111 |
class table(VTMElement, VS.table): |
| 112 |
_mayBeEmpty = True |
| 113 |
_local = False |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
SF = meta.stanFactory |
| 118 |
|
| 119 |
_availabilityBuilder = meta.ModelBasedBuilder([ |
| 120 |
('available', SF(AVL.available)), |
| 121 |
('upSince', SF(AVL.upSince)), |
| 122 |
('_scheduledDowntime', SF(AVL.downAt)), |
| 123 |
('backAt', SF(AVL.backAt)), |
| 124 |
('availability_note', SF(AVL.note)), |
| 125 |
('crash_this', lambda: "invalid, for testing"), |
| 126 |
]) |
| 127 |
|
| 128 |
|
| 129 |
############ The actual VOSI renderers |
| 130 |
|
| 131 |
class VOSIAvailabilityRenderer(VOSIRenderer): |
| 132 |
"""A renderer for a VOSI availability endpoint. |
| 133 |
|
| 134 |
An endpoint with this renderer is automatically registered for |
| 135 |
every service. The answers can be configured using the admin |
| 136 |
renderer. |
| 137 |
""" |
| 138 |
name = "availability" |
| 139 |
|
| 140 |
def _getTree(self, request): |
| 141 |
root = AVL.availability[ |
| 142 |
_availabilityBuilder.build(self.service)] |
| 143 |
return root |
| 144 |
|
| 145 |
|
| 146 |
class VOSICapabilityRenderer(VOSIRenderer): |
| 147 |
"""A renderer for a VOSI capability endpoint. |
| 148 |
|
| 149 |
An endpoint with this renderer is automatically registered for |
| 150 |
every service. The responses contain information on what renderers |
| 151 |
("interfaces") are available for a service and what properties they have. |
| 152 |
|
| 153 |
This also doubles as a canary for authentication, which is why there |
| 154 |
are the somewhat complicated things in render; cf. |
| 155 |
https://wiki.ivoa.net/twiki/bin/view/IVOA/SSO_next |
| 156 |
""" |
| 157 |
name = "capabilities" |
| 158 |
|
| 159 |
def _getTree(self, request): |
| 160 |
root = CAP.capabilities() |
| 161 |
|
| 162 |
for pub in self.service.getPublicationsForSet(None): |
| 163 |
try: |
| 164 |
root = root[capabilities.getCapabilityElement(pub)] |
| 165 |
except Exception as msg: |
| 166 |
base.ui.notifyError("Error while creating VOSI capability" |
| 167 |
" for %s: %s"%( |
| 168 |
self.service.getURL(pub.render, absolute=False), |
| 169 |
msg)) |
| 170 |
|
| 171 |
return root |
| 172 |
|
| 173 |
def render(self, request): |
| 174 |
if request.getUser(): |
| 175 |
return self._renderWithUser(request) |
| 176 |
|
| 177 |
else: |
| 178 |
return self._renderWithoutUser(request) |
| 179 |
|
| 180 |
def _addChallenge(self, request): |
| 181 |
"""adds a www-authenticate header to request. |
| 182 |
""" |
| 183 |
# We may have to figure out a way to admit different realms here, |
| 184 |
# but for now just use our global realm |
| 185 |
realm = base.getConfig("web", "realm") |
| 186 |
request.setHeader('WWW-Authenticate', |
| 187 |
'Basic realm="%s"'%realm) |
| 188 |
|
| 189 |
def _renderWithUser(self, request): |
| 190 |
"""creates a response when a request contains some credentials. |
| 191 |
|
| 192 |
Four cases: |
| 193 |
|
| 194 |
* bad credentials: return a 401 with an auth challenge. |
| 195 |
* open service, good credentials: return 200 with the authenticated id |
| 196 |
* closed service, good credentials: return 200 with the authenticated id |
| 197 |
* closed service, good credentials but not authorised: |
| 198 |
return 401 with a challenge. |
| 199 |
""" |
| 200 |
groups = creds.getGroupsForUser(request.getUser(), request.getPassword()) |
| 201 |
|
| 202 |
if self.service.limitTo is None: |
| 203 |
isAuth = bool(groups) |
| 204 |
else: |
| 205 |
isAuth = self.service.limitTo in groups |
| 206 |
|
| 207 |
request.setHeader("content-type", "text/plain") |
| 208 |
if isAuth: |
| 209 |
request.setHeader("x-vo-authenticated", request.getUser()) |
| 210 |
|
| 211 |
else: |
| 212 |
self._addChallenge(request) |
| 213 |
request.setResponseCode(401) |
| 214 |
|
| 215 |
return super().render(request) |
| 216 |
|
| 217 |
def _renderWithoutUser(self, request): |
| 218 |
"""creates a response when a request does not contain credentials. |
| 219 |
|
| 220 |
Two cases: |
| 221 |
|
| 222 |
* open service: return a 200 but announce optional auth for the |
| 223 |
sake of services that may support gratuitous auth. |
| 224 |
* closed service: return 401 and a challenge. |
| 225 |
""" |
| 226 |
self._addChallenge(request) |
| 227 |
if self.service.limitTo is not None: |
| 228 |
request.setResponseCode(401) |
| 229 |
return super().render(request) |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
class VOSITableResponse(VOSIRenderMixin, resource.Resource): |
| 234 |
"""A resource building a VOSI tableset for a single table. |
| 235 |
|
| 236 |
This is returned as a child resource of VOSITablesetRenderer. |
| 237 |
""" |
| 238 |
def __init__(self, tableDef): |
| 239 |
self.tableDef = tableDef |
| 240 |
resource.Resource.__init__(self) |
| 241 |
|
| 242 |
def _getTree(self, request): |
| 243 |
return tableset.getTableForTableDef(self.tableDef, [], |
| 244 |
rootElement=VTM.table) |
| 245 |
|
| 246 |
def getChild(self, request, name): |
| 247 |
raise svcs.UnknownURI("VOSI tables resources have no children") |
| 248 |
|
| 249 |
|
| 250 |
class VOSITablesetRenderer(VOSIRenderer): |
| 251 |
"""A renderer for a VOSI table metadata endpoint. |
| 252 |
|
| 253 |
An endpoint with this renderer is automatically registered for |
| 254 |
every service. The responses contain information on the tables |
| 255 |
exposed by a given service. |
| 256 |
""" |
| 257 |
name = "tableMetadata" |
| 258 |
aliases = frozenset(["tables"]) |
| 259 |
|
| 260 |
def _getTree(self, request): |
| 261 |
detail = request.strargs.get("detail", |
| 262 |
[base.getConfig("ivoa", "vositabledetail")])[0] |
| 263 |
|
| 264 |
root = registry.getTablesetForService(self.service, |
| 265 |
rootElement=VTM.tableset, suppressBodies=detail=="min") |
| 266 |
return root |
| 267 |
|
| 268 |
def getChild(self, name, request): |
| 269 |
if name==b"": |
| 270 |
raise svcs.WebRedirect(self.service.getURL("tableMetadata")) |
| 271 |
|
| 272 |
tableName = name.decode("utf-8").lower() |
| 273 |
|
| 274 |
for td in self.service.getTableSet(): |
| 275 |
if td.getQName().lower()==tableName: |
| 276 |
return VOSITableResponse(td) |
| 277 |
|
| 278 |
raise svcs.UnknownURI("No table %s on this service."%tableName) |