Class | CASServer::Server |
In: |
lib/casserver/server.rb
|
Parent: | Sinatra::Base |
CONFIG_FILE | = | ENV['CONFIG_FILE'] || "/etc/rubycas-server/config.yml" |
# File lib/casserver/server.rb, line 139 139: def self.handler_options 140: handler_options = { 141: :Host => bind || config[:bind_address], 142: :Port => config[:port] || 443 143: } 144: 145: handler_options.merge(handler_ssl_options).to_hash.symbolize_keys! 146: end
# File lib/casserver/server.rb, line 148 148: def self.handler_ssl_options 149: return {} unless config[:ssl_cert] 150: 151: cert_path = config[:ssl_cert] 152: key_path = config[:ssl_key] || config[:ssl_cert] 153: 154: unless cert_path.nil? && key_path.nil? 155: raise "The ssl_cert and ssl_key options cannot be used with mongrel. You will have to run your " + 156: " server behind a reverse proxy if you want SSL under mongrel." if 157: config[:server] == 'mongrel' 158: 159: raise "The specified certificate file #{cert_path.inspect} does not exist or is not readable. " + 160: " Your 'ssl_cert' configuration setting must be a path to a valid " + 161: " ssl certificate." unless 162: File.exists? cert_path 163: 164: raise "The specified key file #{key_path.inspect} does not exist or is not readable. " + 165: " Your 'ssl_key' configuration setting must be a path to a valid " + 166: " ssl private key." unless 167: File.exists? key_path 168: 169: require 'openssl' 170: require 'webrick/https' 171: 172: cert = OpenSSL::X509::Certificate.new(File.read(cert_path)) 173: key = OpenSSL::PKey::RSA.new(File.read(key_path)) 174: 175: { 176: :SSLEnable => true, 177: :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE, 178: :SSLCertificate => cert, 179: :SSLPrivateKey => key 180: } 181: end 182: end
# File lib/casserver/server.rb, line 184 184: def self.init_authenticators! 185: auth = [] 186: 187: if config[:authenticator].nil? 188: print_cli_message "No authenticators have been configured. Please double-check your config file (#{CONFIG_FILE.inspect}).", :error 189: exit 1 190: end 191: 192: begin 193: # attempt to instantiate the authenticator 194: config[:authenticator] = [config[:authenticator]] unless config[:authenticator].instance_of? Array 195: config[:authenticator].each { |authenticator| auth << authenticator[:class].constantize} 196: rescue NameError 197: if config[:authenticator].instance_of? Array 198: config[:authenticator].each do |authenticator| 199: if !authenticator[:source].nil? 200: # config.yml explicitly names source file 201: require authenticator[:source] 202: else 203: # the authenticator class hasn't yet been loaded, so lets try to load it from the casserver/authenticators directory 204: auth_rb = authenticator[:class].underscore.gsub('cas_server/', '') 205: require 'casserver/'+auth_rb 206: end 207: auth << authenticator[:class].constantize 208: end 209: else 210: if config[:authenticator][:source] 211: # config.yml explicitly names source file 212: require config[:authenticator][:source] 213: else 214: # the authenticator class hasn't yet been loaded, so lets try to load it from the casserver/authenticators directory 215: auth_rb = config[:authenticator][:class].underscore.gsub('cas_server/', '') 216: require 'casserver/'+auth_rb 217: end 218: 219: auth << config[:authenticator][:class].constantize 220: config[:authenticator] = [config[:authenticator]] 221: end 222: end 223: 224: auth.zip(config[:authenticator]).each_with_index{ |auth_conf, index| 225: authenticator, conf = auth_conf 226: $LOG.debug "About to setup #{authenticator} with #{conf.inspect}..." 227: authenticator.setup(conf.merge('auth_index' => index)) if authenticator.respond_to?(:setup) 228: $LOG.debug "Done setting up #{authenticator}." 229: } 230: 231: set :auth, auth 232: end
# File lib/casserver/server.rb, line 254 254: def self.init_database! 255: 256: unless config[:disable_auto_migrations] 257: ActiveRecord::Base.establish_connection(config[:database]) 258: print_cli_message "Running migrations to make sure your database schema is up to date..." 259: prev_db_log = ActiveRecord::Base.logger 260: ActiveRecord::Base.logger = Logger.new(STDOUT) 261: ActiveRecord::Migration.verbose = true 262: ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + "/../../db/migrate") 263: ActiveRecord::Base.logger = prev_db_log 264: print_cli_message "Your database is now up to date." 265: end 266: 267: ActiveRecord::Base.establish_connection(config[:database]) 268: end
# File lib/casserver/server.rb, line 234 234: def self.init_logger! 235: if config[:log] 236: if $LOG && config[:log][:file] 237: print_cli_message "Redirecting RubyCAS-Server log to #{config[:log][:file]}" 238: #$LOG.close 239: $LOG = Logger.new(config[:log][:file]) 240: end 241: $LOG.level = Logger.const_get(config[:log][:level]) if config[:log][:level] 242: end 243: 244: if config[:db_log] 245: if $LOG && config[:db_log][:file] 246: $LOG.debug "Redirecting ActiveRecord log to #{config[:log][:file]}" 247: #$LOG.close 248: ActiveRecord::Base.logger = Logger.new(config[:db_log][:file]) 249: end 250: ActiveRecord::Base.logger.level = Logger.const_get(config[:db_log][:level]) if config[:db_log][:level] 251: end 252: end
# File lib/casserver/server.rb, line 94 94: def self.load_config_file(config_file) 95: begin 96: config_file = File.open(config_file) 97: rescue Errno::ENOENT => e 98: 99: print_cli_message "Config file #{config_file} does not exist!", :error 100: print_cli_message "Would you like the default config file copied to #{config_file.inspect}? [y/N]" 101: if gets.strip.downcase == 'y' 102: require 'fileutils' 103: default_config = File.dirname(__FILE__) + '/../../config/config.example.yml' 104: 105: if !File.exists?(File.dirname(config_file)) 106: print_cli_message "Creating config directory..." 107: FileUtils.mkdir_p(File.dirname(config_file), :verbose => true) 108: end 109: 110: print_cli_message "Copying #{default_config.inspect} to #{config_file.inspect}..." 111: FileUtils.cp(default_config, config_file, :verbose => true) 112: print_cli_message "The default config has been copied. You should now edit it and try starting again." 113: exit 114: else 115: print_cli_message "Cannot start RubyCAS-Server without a valid config file.", :error 116: raise e 117: end 118: rescue Errno::EACCES => e 119: print_cli_message "Config file #{config_file.inspect} is not readable (permission denied)!", :error 120: raise e 121: rescue => e 122: print_cli_message "Config file #{config_file.inspect} could not be read!", :error 123: raise e 124: end 125: 126: config.merge! HashWithIndifferentAccess.new(YAML.load(config_file)) 127: set :server, config[:server] || 'webrick' 128: end
# File lib/casserver/server.rb, line 76 76: def self.print_cli_message(msg, type = :info) 77: if respond_to?(:config) && config && config[:quiet] 78: return 79: end 80: 81: if type == :error 82: io = $stderr 83: prefix = "!!! " 84: else 85: io = $stdout 86: prefix = ">>> " 87: end 88: 89: io.puts 90: io.puts "#{prefix}#{msg}" 91: io.puts 92: end
# File lib/casserver/server.rb, line 70 70: def self.quit!(server, handler_name) 71: ## Use thins' hard #stop! if available, otherwise just #stop 72: server.respond_to?(:stop!) ? server.stop! : server.stop 73: puts "\n== RubyCAS-Server is shutting down" unless handler_name =~/cgi/i 74: end
# File lib/casserver/server.rb, line 130 130: def self.reconfigure!(config) 131: config.each do |key, val| 132: self.config[key] = val 133: end 134: init_database! 135: init_logger! 136: init_authenticators! 137: end
# File lib/casserver/server.rb, line 46 46: def self.run!(options={}) 47: set options 48: 49: handler = detect_rack_handler 50: handler_name = handler.name.gsub(/.*::/, '') 51: 52: puts "== RubyCAS-Server is starting up " + 53: "on port #{config[:port] || port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i 54: 55: begin 56: opts = handler_options 57: rescue Exception => e 58: print_cli_message e, :error 59: raise e 60: end 61: 62: handler.run self, opts do |server| 63: [:INT, :TERM].each { |sig| trap(sig) { quit!(server, handler_name) } } 64: set :running, true 65: end 66: rescue Errno::EADDRINUSE => e 67: puts "== Something is already running on port #{port}!" 68: end
# File lib/casserver/server.rb, line 737 737: def compile_template(engine, data, options, views) 738: super engine, data, options, @custom_views || views 739: rescue Errno::ENOENT 740: raise unless @custom_views 741: super engine, data, options, views 742: end
Helpers
# File lib/casserver/server.rb, line 714 714: def response_status_from_error(error) 715: case error.code.to_s 716: when /^INVALID_/, 'BAD_PGT' 717: 422 718: when 'INTERNAL_ERROR' 719: 500 720: else 721: 500 722: end 723: end
# File lib/casserver/server.rb, line 725 725: def serialize_extra_attribute(builder, key, value) 726: if value.kind_of?(String) 727: builder.tag! key, value 728: elsif value.kind_of?(Numeric) 729: builder.tag! key, value.to_s 730: else 731: builder.tag! key do 732: builder.cdata! value.to_yaml 733: end 734: end 735: end
Strip the config.uri_path from the request.path_info… FIXME: do we really need to override all of Sinatra‘s static! to make this happen?
# File lib/casserver/server.rb, line 34 34: def static! 35: return if (public_dir = settings.public).nil? 36: public_dir = File.expand_path(public_dir) 37: 38: path = File.expand_path(public_dir + unescape(request.path_info.gsub(/^#{settings.config[:uri_path]}/,''))) 39: return if path[0, public_dir.length] != public_dir 40: return unless File.file?(path) 41: 42: env['sinatra.static_file'] = path 43: send_file path, :disposition => nil 44: end