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(headless) click to toggle source
# File lib/conjur/webserver/server.rb, line 87
def open headless
  require 'launchy'
  url = "http://localhost:#{port}/login?sessionid=#{sessionid}"
  # as launchy sometimes silently fails, we need human-friendly failover
  if ENV['DONT_OPEN_IN_BROWSER'] or headless
    puts "Running in headless mode."
    puts "To reach this UI server via SSH tunnel, run the following command:"
    puts "ssh -N -L #{port}:localhost:#{port} user@hostname"
    puts "Then open your browser to the following URL:"
    puts url
  else
    $stderr.puts "If your browser did not opened the UI automatically, point it to #{url}"
    Launchy.open(url)
  end
end
set_port(p) click to toggle source
# File lib/conjur/webserver/server.rb, line 103
def set_port p
  @port = p
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'

  # Pry is optional
  begin
    require 'pry'
  rescue LoadError
  end

  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 maps).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
  # HR: it won't work anymore since API 4.10.2 (because it relies on sticky configuration feature which is removed)
  #     instead we just explicitly set RACK_ENV to "production" on the command call  -- this is also bad, but... good enough for now, until we'll fix API configuration logic
  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 115
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 109
def port
  @port ||= find_available_port
end
sessionid() click to toggle source
# File lib/conjur/webserver/server.rb, line 124
def sessionid
  require 'securerandom'
  @sessionid ||= SecureRandom.hex(32)
end