1 | ''' |
---|
2 | Security middleware - set-up configuration items |
---|
3 | |
---|
4 | P J Kershaw 18/03/08 |
---|
5 | ''' |
---|
6 | |
---|
7 | class ndg: |
---|
8 | '''Class structure to define a namespace for SSO Service config attached |
---|
9 | Pylons global variable 'g' |
---|
10 | ''' |
---|
11 | class security: |
---|
12 | class server: |
---|
13 | class ssoservice: |
---|
14 | cfg = None |
---|
15 | |
---|
16 | class SSOMiddleware: |
---|
17 | |
---|
18 | def __init__(self, app, cfgFilePath, appGlobals): |
---|
19 | self.app = app |
---|
20 | ndg.security.server.ssoservice.cfg = SecurityConfig(cfgFilePath) |
---|
21 | ndg.security.server.ssoservice.cfg.read() |
---|
22 | appGlobals.ndg = ndg |
---|
23 | |
---|
24 | def __call__(self, environ, start_response): |
---|
25 | |
---|
26 | # environ['securityConfig'] = self.config |
---|
27 | return self.app(environ, start_response) |
---|
28 | |
---|
29 | |
---|
30 | import sys |
---|
31 | from ConfigParser import SafeConfigParser as ConfigParser |
---|
32 | from ndg.security.common.wssecurity import WSSecurityConfig |
---|
33 | |
---|
34 | class SecurityConfigError(Exception): |
---|
35 | """Handle errors from parsing security config items""" |
---|
36 | |
---|
37 | class SecurityConfig(object): |
---|
38 | """Get Security related parameters from the Pylons NDG config file""" |
---|
39 | |
---|
40 | def __init__(self, cfgFilePath=None): |
---|
41 | '''Get PKI settings for Attribute Authority and Session Manager from |
---|
42 | the configuration file |
---|
43 | |
---|
44 | @type cfgFilePath: pylons config file object |
---|
45 | @param cfgFilePath: reference to NDG configuration file. If omitted |
---|
46 | defaults to request.environ['ndgConfig']''' |
---|
47 | |
---|
48 | self.cfgFilePath = cfgFilePath |
---|
49 | self.gk = None |
---|
50 | self.wss = {} |
---|
51 | |
---|
52 | def read(self): |
---|
53 | '''Read content of config file into object''' |
---|
54 | cfg = ConfigParser() |
---|
55 | cfg.read(self.cfgFilePath) |
---|
56 | |
---|
57 | tracefileExpr = cfg.get('NDG_SECURITY', 'tracefile') |
---|
58 | if tracefileExpr: |
---|
59 | self.tracefile = eval(tracefileExpr) |
---|
60 | |
---|
61 | self.smURI = cfg.get('NDG_SECURITY', 'sessionMgrURI') |
---|
62 | self.aaURI = cfg.get('NDG_SECURITY', 'attAuthorityURI') |
---|
63 | |
---|
64 | # ... for SSL connections to security web services |
---|
65 | try: |
---|
66 | self.sslCACertFilePathList = \ |
---|
67 | cfg.get('NDG_SECURITY', 'sslCACertFilePathList').split() |
---|
68 | |
---|
69 | except AttributeError: |
---|
70 | raise SecurityConfigError, \ |
---|
71 | 'No "sslCACertFilePathList" security setting' |
---|
72 | |
---|
73 | self.sslPeerCertCN = cfg.get('NDG_SECURITY', 'sslPeerCertCN', None) |
---|
74 | |
---|
75 | wssCfgFilePath = cfg.get('NDG_SECURITY', 'wssCfgFilePath', None) |
---|
76 | wss = WSSecurityConfig() |
---|
77 | wss.read(wssCfgFilePath) |
---|
78 | |
---|
79 | # Cast to standard dict because WSSecurityConfig object can't be |
---|
80 | # passed via **kw and dict(wss) doesn't work |
---|
81 | # TODO: check for cleaner solution - dict(wss) |
---|
82 | self.wss = dict(wss.items()) |
---|
83 | |
---|
84 | # # ...and for WS-Security digital signature |
---|
85 | # self.wssCertFilePath = cfg.get('NDG_SECURITY', 'wssCertFilePath') |
---|
86 | # self.wssPriKeyFilePath = cfg.get('NDG_SECURITY', 'wssKeyFilePath') |
---|
87 | # self.wssPriKeyPwd = cfg.get('NDG_SECURITY', 'wssKeyPwd') |
---|
88 | # |
---|
89 | # try: |
---|
90 | # self.wssCACertFilePathList = \ |
---|
91 | # cfg.get('NDG_SECURITY', 'wssCACertFilePathList').split() |
---|
92 | # |
---|
93 | # except AttributeError: |
---|
94 | # raise SecurityConfigError, \ |
---|
95 | # 'No "wssCACertFilePathList" security setting' |
---|
96 | # |
---|
97 | # # Inclusive namespace prefixes for Exclusive C14N |
---|
98 | # try: |
---|
99 | # self.wssRefInclNS = cfg.get('NDG_SECURITY', 'wssRefInclNS').split() |
---|
100 | # |
---|
101 | # except AttributeError: |
---|
102 | # raise SecurityConfigError, 'No "wssRefInclNS" security setting' |
---|
103 | # |
---|
104 | # try: |
---|
105 | # self.wssSignedInfoInclNS = cfg.get('NDG_SECURITY', |
---|
106 | # 'wssSignedInfoInclNS').split() |
---|
107 | # except AttributeError: |
---|
108 | # raise SecurityConfigError, \ |
---|
109 | # 'No "wssSignedInfoInclNS" security setting' |
---|
110 | |
---|
111 | |
---|
112 | # Gatekeeper params |
---|
113 | |
---|
114 | # Attribute Certificate Issuer |
---|
115 | self.acIssuer = cfg.get('NDG_SECURITY', 'acIssuer') |
---|
116 | |
---|
117 | # verification of X.509 cert back to CA |
---|
118 | try: |
---|
119 | self.acCACertFilePathList = cfg.get('NDG_SECURITY', |
---|
120 | 'acCACertFilePathList').split() |
---|
121 | except AttributeError: |
---|
122 | raise SecurityConfigError, \ |
---|
123 | 'No "acCACertFilePathList" security setting' |
---|
124 | |
---|
125 | # Hostname |
---|
126 | self.server=cfg.get('NDG_SECURITY', 'server', '') |
---|
127 | |
---|
128 | # For secure connections |
---|
129 | self.sslServer = cfg.get('NDG_SECURITY', 'sslServer', '') |
---|
130 | |
---|
131 | # These URLs are referred from template files |
---|
132 | self.getCredentials = '%s/getCredentials' % self.sslServer |
---|
133 | self.logout = '%s/logout' % self.server |
---|
134 | |
---|
135 | # Where Are You From URI |
---|
136 | self.wayfuri='%s/wayf' % self.server |
---|
137 | |
---|
138 | self.localLink=cfg.get('layout', 'localLink', None) |
---|
139 | self.localImage=cfg.get('layout', 'localImage', None) |
---|
140 | self.localAlt=cfg.get('layout', 'localAlt', 'Visit Local Site') |
---|
141 | self.ndgLink=cfg.get('layout', 'ndgLink', 'http://ndg.nerc.ac.uk') |
---|
142 | self.ndgImage=cfg.get('layout', 'ndgImage', None) |
---|
143 | self.ndgAlt=cfg.get('layout', 'ndgAlt','Visit NDG') |
---|
144 | self.stfcLink=cfg.get('layout', 'stfcLink') |
---|
145 | self.stfcImage=cfg.get('layout', 'stfcImage') |
---|
146 | self.helpIcon=cfg.get('layout', 'helpIcon') |
---|
147 | self.LeftAlt=cfg.get('layout', 'HdrLeftAlt') |
---|
148 | self.LeftLogo=cfg.get('layout', 'HdrLeftLogo') |
---|
149 | self.pageLogo="bodcHdr" |
---|
150 | self.icons_xml=cfg.get('layout','Xicon') |
---|
151 | self.icons_plot=cfg.get('layout','plot') |
---|
152 | self.icons_prn=cfg.get('layout', 'printer') |
---|
153 | |
---|
154 | self.disclaimer = cfg.get('DEFAULT', 'disclaimer') |
---|
155 | |
---|
156 | |
---|
157 | def __repr__(self): |
---|
158 | return '\n'.join(["%s=%s" % (k,v) for k,v in self.__dict__.items() \ |
---|
159 | if k[:2] != "__"]) |
---|
160 | |
---|