class Conjur::WebServer::Server

Launch a web server which serves local files and proxies to the remote Conjur API.

Constants

DEFAULT_PORT

Public Instance Methods

open() click to toggle source
# File lib/conjur/webserver/server.rb, line 80
def open
  require 'launchy'
  url = "http://localhost:#{port}/login?sessionid=#{sessionid}"
  # as launchy sometimes silently fails, we need human-friendly failover
  $stderr.puts "UI should be available now at #{url}" 
  Launchy.open(url)
end
start(root) click to toggle source
# File lib/conjur/webserver/server.rb, line 26
def start(root)
  require 'rack'
  require 'conjur/webserver/login'
  require 'conjur/webserver/authorize'
  require 'conjur/webserver/api_proxy'
  require 'conjur/webserver/home'
  require 'conjur/webserver/conjur_info'
  require 'pry'

  sessionid = self.sessionid
  cookie_options = {
    secret: SecureRandom.hex(32),
    expire_after: 24*60*60
  }

  api_stack = [
    [Rack::Session::Cookie, cookie_options],
    #[Conjur::WebServer::Authorize, sessionid],
    [Conjur::WebServer::ConjurInfo]
  ]

  app = Rack::Builder.app do
    map "/login" do
      use Rack::Session::Cookie, cookie_options
      run Conjur::WebServer::Login.new sessionid
    end
    map "/api" do
      api_stack.each{|args| use *args}
      run Conjur::WebServer::APIProxy.new
    end
    %w(js css fonts images).each do |path|
      map "/#{path}" do
        run Rack::File.new(File.join(root, path), 'Cache-Control' => 'max-age=0')
      end
    end
    map "/ui" do
      run Conjur::WebServer::Home.new(root)
    end
  end
  options = {
    app:  app,
    Port: port,
    Threads: '0:64',
    Verbose: true
  }

  # this vivifies the env for correct url settings
  # otherwise puma sets it to development and
  # confusion ensues
  Conjur.configuration.env

  Rack::Server.start(options)
end

Protected Instance Methods

find_available_port() click to toggle source
# File lib/conjur/webserver/server.rb, line 96
def find_available_port
  begin
    server = TCPServer.new('127.0.0.1', 0)
    server.addr[1]
  ensure
    server.close if server
  end
end
port() click to toggle source
# File lib/conjur/webserver/server.rb, line 90
def port
  @port ||= find_available_port
end
sessionid() click to toggle source
# File lib/conjur/webserver/server.rb, line 105
def sessionid
  require 'securerandom'
  @sessionid ||= SecureRandom.hex(32)
end