3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from ipsilon.tools.saml2metadata import Metadata
21 from ipsilon.tools.saml2metadata import SAML2_NAMEID_MAP
22 from ipsilon.tools.saml2metadata import SAML2_SERVICE_MAP
23 from ipsilon.tools.certs import Certificate
24 from ipsilon.tools import files
36 HTTPDCONFD = '/etc/httpd/conf.d'
37 SAML2_TEMPLATE = '/usr/share/ipsilon/templates/install/saml2/sp.conf'
38 SAML2_CONFFILE = '/etc/httpd/conf.d/ipsilon-saml.conf'
39 SAML2_HTTPDIR = '/etc/httpd/saml2'
40 SAML2_PROTECTED = '/saml2protected'
42 #Installation arguments
46 logger = logging.getLogger()
50 global logger # pylint: disable=W0603
51 logger = logging.getLogger()
52 lh = logging.StreamHandler(sys.stderr)
57 logger.info('Installing SAML2 Service Provider')
59 if args['saml_idp_metadata'] is None:
60 #TODO: detect via SRV records ?
61 raise ValueError('An IDP metadata file/url is required.')
66 if os.path.exists(args['saml_idp_metadata']):
67 with open(args['saml_idp_metadata']) as f:
69 elif args['saml_idp_metadata'].startswith('file://'):
70 with open(args['saml_idp_metadata'][7:]) as f:
73 r = requests.get(args['saml_idp_metadata'])
76 except Exception, e: # pylint: disable=broad-except
77 logger.error("Failed to retrieve IDP Metadata file!\n" +
78 "Error: [%s]" % repr(e))
82 if not args['saml_no_httpd']:
83 path = os.path.join(SAML2_HTTPDIR, args['hostname'])
84 os.makedirs(path, 0750)
89 if not args['saml_secure_setup']:
91 url = '%s://%s' % (proto, args['hostname'])
92 url_sp = url + args['saml_sp']
93 url_logout = url + args['saml_sp_logout']
94 url_post = url + args['saml_sp_post']
99 c.generate('certificate', args['hostname'])
100 m.set_entity_id(url_sp)
102 m.add_service(SAML2_SERVICE_MAP['logout-redirect'], url_logout)
103 m.add_service(SAML2_SERVICE_MAP['response-post'], url_post, index="0")
104 sp_metafile = os.path.join(path, 'metadata.xml')
105 m.output(sp_metafile)
107 if not args['saml_no_httpd']:
108 idp_metafile = os.path.join(path, 'idp-metadata.xml')
109 with open(idp_metafile, 'w+') as f:
112 saml_protect = 'auth'
114 if args['saml_base'] != args['saml_auth']:
115 saml_protect = 'info'
116 saml_auth = '<Location %s>\n' \
117 ' MellonEnable "auth"\n' \
118 '</Location>\n' % args['saml_auth']
121 if args['saml_auth'] == SAML2_PROTECTED:
122 # default location, enable the default page
126 if args['saml_secure_setup']:
129 samlopts = {'saml_base': args['saml_base'],
130 'saml_protect': saml_protect,
131 'saml_sp_key': c.key,
132 'saml_sp_cert': c.cert,
133 'saml_sp_meta': sp_metafile,
134 'saml_idp_meta': idp_metafile,
135 'saml_sp': args['saml_sp'],
136 'saml_secure_on': saml_secure,
137 'saml_auth': saml_auth,
139 files.write_from_template(SAML2_CONFFILE, SAML2_TEMPLATE, samlopts)
141 files.fix_user_dirs(SAML2_HTTPDIR, args['httpd_user'])
143 logger.info('SAML Service Provider configured.')
144 logger.info('You should be able to restart the HTTPD server and' +
145 ' then access it at %s%s' % (url, args['saml_auth']))
147 logger.info('SAML Service Provider configuration ready.')
148 logger.info('Use the certificate, key and metadata.xml files to' +
149 ' configure your Service Provider')
157 def saml2_uninstall():
159 shutil.rmtree(os.path.join(SAML2_HTTPDIR, args['hostname']))
160 except Exception, e: # pylint: disable=broad-except
163 os.remove(SAML2_CONFFILE)
164 except Exception, e: # pylint: disable=broad-except
169 logger.info('Uninstalling Service Provider')
170 #FXIME: ask confirmation
172 logger.info('Uninstalled SAML2 data')
175 def log_exception(e):
176 if 'debug' in args and args['debug']:
182 def parse_config_profile(args):
183 config = ConfigParser.ConfigParser()
184 files = config.read(args['config_profile'])
186 raise ConfigurationError('Config Profile file %s not found!' %
187 args['config_profile'])
189 if 'globals' in config.sections():
190 G = config.options('globals')
192 val = config.get('globals', g)
200 for k in globals().keys():
201 if k.lower() == g.lower():
205 if 'arguments' in config.sections():
206 A = config.options('arguments')
208 val = config.get('arguments', a)
221 fc = argparse.ArgumentDefaultsHelpFormatter
222 parser = argparse.ArgumentParser(description='Client Install Options',
224 parser.add_argument('--version',
225 action='version', version='%(prog)s 0.1')
226 parser.add_argument('--hostname', default=socket.getfqdn(),
227 help="Machine's fully qualified host name")
228 parser.add_argument('--admin-user', default='admin',
229 help="Account allowed to create a SP")
230 parser.add_argument('--httpd-user', default='apache',
231 help="Web server account used to read certs")
232 parser.add_argument('--saml', action='store_true', default=True,
233 help="Whether to install a saml2 SP")
234 parser.add_argument('--saml-idp-metadata', default=None,
235 help="A URL pointing at the IDP Metadata (FILE or HTTP)")
236 parser.add_argument('--saml-no-httpd', action='store_true', default=False,
237 help="Do not configure httpd")
238 parser.add_argument('--saml-base', default='/',
239 help="Where saml2 authdata is available")
240 parser.add_argument('--saml-auth', default=SAML2_PROTECTED,
241 help="Where saml2 authentication is enforced")
242 parser.add_argument('--saml-sp', default='/saml2',
243 help="Where saml communication happens")
244 parser.add_argument('--saml-sp-logout', default='/saml2/logout',
245 help="Single Logout URL")
246 parser.add_argument('--saml-sp-post', default='/saml2/postResponse',
247 help="Post response URL")
248 parser.add_argument('--saml-secure-setup', action='store_true',
249 default=True, help="Turn on all security checks")
250 parser.add_argument('--debug', action='store_true', default=False,
251 help="Turn on script debugging")
252 parser.add_argument('--config-profile', default=None,
253 help="File containing install options")
254 parser.add_argument('--uninstall', action='store_true',
255 help="Uninstall the server and all data")
257 args = vars(parser.parse_args())
259 if args['config_profile']:
260 args = parse_config_profile(args)
262 if len(args['hostname'].split('.')) < 2:
263 raise ValueError('Hostname: %s is not a FQDN.')
265 # At least one on this list needs to be specified or we do nothing
271 if not present and not args['uninstall']:
272 raise ValueError('Nothing to install, please select a Service type.')
275 if __name__ == '__main__':
281 if 'uninstall' in args and args['uninstall'] is True:
285 except Exception, e: # pylint: disable=broad-except
287 if 'uninstall' in args and args['uninstall'] is True:
288 print 'Uninstallation aborted.'
290 print 'Installation aborted.'
294 if 'uninstall' in args and args['uninstall'] is True:
295 print 'Uninstallation complete.'
297 print 'Installation complete.'