1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # How to build an echo server using the extended code generation |
---|
4 | # |
---|
5 | import sys, os |
---|
6 | from ConfigParser import SafeConfigParser |
---|
7 | |
---|
8 | # Import the ZSI stuff you'd need no matter what |
---|
9 | from ZSI.ServiceContainer import ServiceContainer |
---|
10 | |
---|
11 | # This is a new method imported to show it's value |
---|
12 | from ZSI.ServiceContainer import GetSOAPContext |
---|
13 | |
---|
14 | from EchoService_services_server import EchoService as _EchoService |
---|
15 | |
---|
16 | from ndg.security.common.wssecurity.dom import SignatureHandler |
---|
17 | |
---|
18 | from os.path import expandvars as xpdVars |
---|
19 | from os.path import join, dirname, abspath |
---|
20 | mkPath = lambda file: join(os.environ['NDGSEC_WSSESRV_UNITTEST_DIR'], file) |
---|
21 | |
---|
22 | import logging |
---|
23 | log = logging.getLogger(__name__) |
---|
24 | logging.basicConfig(level=logging.DEBUG, |
---|
25 | format='%(asctime)s %(filename)s:%(lineno)d ' |
---|
26 | '%(levelname)s %(message)s') |
---|
27 | |
---|
28 | |
---|
29 | from ndg.security.test import BaseTestCase |
---|
30 | |
---|
31 | # Initialize environment for unit tests |
---|
32 | if BaseTestCase.configDirEnvVarName not in os.environ: |
---|
33 | os.environ[BaseTestCase.configDirEnvVarName] = \ |
---|
34 | join(dirname(dirname(abspath(dirname(__file__)))), 'config') |
---|
35 | |
---|
36 | class EchoService(_EchoService): |
---|
37 | |
---|
38 | def __init__(self, **kw): |
---|
39 | |
---|
40 | # Stop in debugger at beginning of SOAP stub if environment variable |
---|
41 | # is set |
---|
42 | self.__debug = bool(os.environ.get('NDGSEC_INT_DEBUG')) |
---|
43 | if self.__debug: |
---|
44 | import pdb |
---|
45 | pdb.set_trace() |
---|
46 | |
---|
47 | _EchoService.__init__(self, **kw) |
---|
48 | |
---|
49 | def sign(self, sw): |
---|
50 | '''\ |
---|
51 | Overrides ServiceInterface class method to allow digital signature''' |
---|
52 | self.signatureHandler.sign(sw) |
---|
53 | |
---|
54 | def verify(self, ps): |
---|
55 | '''\ |
---|
56 | Overrides ServiceInterface class method to allow signature |
---|
57 | verification''' |
---|
58 | self.signatureHandler.verify(ps) |
---|
59 | |
---|
60 | def soap_Echo(self, ps, **kw): |
---|
61 | '''Simple echo method to test WS-Security DSIG |
---|
62 | |
---|
63 | @type ps: ZSI ParsedSoap |
---|
64 | @param ps: client SOAP message |
---|
65 | @rtype: tuple |
---|
66 | @return: response objects''' |
---|
67 | log.info("Server received an Echo service request...") |
---|
68 | if self.__debug: |
---|
69 | import pdb |
---|
70 | pdb.set_trace() |
---|
71 | |
---|
72 | response = _EchoService.soap_Echo(self, ps) |
---|
73 | response.EchoResult = "Received message from client: " + \ |
---|
74 | self.request.EchoIn |
---|
75 | return response |
---|
76 | |
---|
77 | |
---|
78 | def authorize(self, auth_info, post, action): |
---|
79 | '''Override default simply to display client request info''' |
---|
80 | ctx = GetSOAPContext() |
---|
81 | print "-"*80 |
---|
82 | print dir(ctx) |
---|
83 | print "Container: ", ctx.connection |
---|
84 | print "Parsed SOAP: ", ctx.parsedsoap |
---|
85 | print "Container: ", ctx.container |
---|
86 | print "HTTP Headers:\n", ctx.httpheaders |
---|
87 | print "-"*80 |
---|
88 | print "Client Request:\n", ctx.xmldata |
---|
89 | return 1 |
---|
90 | |
---|
91 | |
---|
92 | if __name__ == "__main__": |
---|
93 | # Here we set up the server |
---|
94 | |
---|
95 | if 'NDGSEC_WSSESRV_UNITTEST_DIR' not in os.environ: |
---|
96 | os.environ['NDGSEC_WSSESRV_UNITTEST_DIR'] = \ |
---|
97 | os.path.abspath(os.path.dirname(__file__)) |
---|
98 | |
---|
99 | configFilePath = mkPath('echoServer.cfg') |
---|
100 | |
---|
101 | cfg = SafeConfigParser() |
---|
102 | cfg.read(configFilePath) |
---|
103 | |
---|
104 | hostname = cfg.get('setUp', 'hostname') |
---|
105 | port = cfg.getint('setUp', 'port') |
---|
106 | path = cfg.get('setUp', 'path') |
---|
107 | |
---|
108 | wsseCfgFilePath = xpdVars(cfg.get('setUp', 'wsseCfgFilePath')) |
---|
109 | |
---|
110 | serviceContainer = ServiceContainer((hostname, port)) |
---|
111 | |
---|
112 | # Create the Inherited version of the server |
---|
113 | echo = EchoService() |
---|
114 | echo.signatureHandler = SignatureHandler(cfg=wsseCfgFilePath) |
---|
115 | |
---|
116 | serviceContainer.setNode(echo, url=path) |
---|
117 | |
---|
118 | try: |
---|
119 | # Run the service container |
---|
120 | print "listening at http://%s:%s%s" % (hostname, port, path) |
---|
121 | serviceContainer.serve_forever() |
---|
122 | except KeyboardInterrupt: |
---|
123 | sys.exit(0) |
---|