3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from flask import Flask, request
19 from flask_restful import Resource, Api
20 from access_management.backend.ambackend import AMBackend
21 from access_management.config.amconfigparser import AMConfigParser
22 import access_management.backend.restlogger as restlog
23 from werkzeug.exceptions import InternalServerError
29 class AuthorizeEndpoint(Resource):
31 backend = AMBackend(config)
32 params = json.loads(request.json['params'])
33 authorized, username = backend.is_authorized(token=params['token'], domain=params['domain'],
34 domain_object=params['domain_object'], method=params['method'])
35 return {'authorized': authorized, 'username': username}
38 class AuthorizeRole(Resource):
40 backend = AMBackend(config)
41 authorized, username = backend.is_authorized(token=request.json['token'], role_name=request.json['role'])
42 return {'authorized': authorized, 'username': username}
45 # class DumpTables(Resource):
47 # backend = AMBackend(config)
48 # results = backend.dump_tables()
52 api.add_resource(AuthorizeEndpoint, '/authorize/endpoint')
53 api.add_resource(AuthorizeRole, '/authorize/role')
54 # api.add_resource(DumpTables, '/dumptables')
59 configparser = AMConfigParser("/etc/access_management/am_backend_config.ini")
60 config = configparser.parse()
61 logger = restlog.get_logger(config)
62 initialize(config,logger)
63 app.run(host=config["Api"]["host"], port=int(config["Api"]["port"]), debug=True)
66 def initialize(config, logger):
67 logger.info("Initializing...")
68 app.register_error_handler(Exception, handle_exp)
69 app.before_request(request_logger)
70 app.after_request(response_logger)
71 app.logger.addHandler(restlog.get_log_handler(config))
72 logger.info("Starting up...")
76 app.logger.info('Request: remote_addr: %s method: %s endpoint: %s', request.remote_addr, request.method,
80 def response_logger(response):
81 app.logger.info('Response: status: %s (Associated Request: remote_addr: %s, method: %s, endpoint: %s)',
82 response.status, request.remote_addr, request.method, request.full_path)
84 app.logger.debug('Response\'s data: %s', response.data)
89 def handle_exp(failure):
90 app.logger.error("Internal error: %s ", failure)
91 raise InternalServerError()
94 if __name__ == '__main__':
97 except Exception as error:# pylint: disable=broad-except
98 print "Failure: %s" % error