Module: SuperAuth::Editor::CLI

Defined in:
lib/super_auth/editor/cli.rb

Overview

Boots the editor from the command line: connect, optionally migrate and seed, then serve on loopback. See exe/super_auth-editor.

Constant Summary collapse

DEFAULTS =
{ host: "127.0.0.1", port: 4666, migrate: false, seed: false }.freeze
LOOPBACK_BINDS =
%w[127.0.0.1 localhost ::1].freeze
LOOPBACK_HOSTS =

Host headers accepted when bound to loopback (DNS-rebinding defence).

%w[localhost 127.0.0.1 [::1]].freeze
WARNING =
"WARNING: the editor has no authentication. " \
"Anyone who can reach this port can rewrite the authorization graph.".freeze

Class Method Summary collapse

Class Method Details

.parse(argv, out: $stdout) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/super_auth/editor/cli.rb', line 18

def parse(argv, out: $stdout)
  options = DEFAULTS.dup
  parser = OptionParser.new do |o|
    o.banner = "Usage: super_auth-editor [options]"
    o.on("-H", "--host HOST", "bind address (default #{DEFAULTS[:host]})") { |v| options[:host] = v }
    o.on("-p", "--port PORT", Integer, "port (default #{DEFAULTS[:port]})") { |v| options[:port] = v }
    o.on("--migrate", "run the super_auth Sequel migrations before starting") { options[:migrate] = true }
    o.on("--seed", "replace the whole graph with the Acme Cloud sample (destructive)") { options[:seed] = true }
    o.on("-v", "--version", "print the version") do
      out.puts SuperAuth::VERSION
      exit
    end
    o.on("-h", "--help", "show this help") do
      out.puts o
      out.puts "Reads SUPER_AUTH_DATABASE_URL (required)."
      out.puts WARNING
      exit
    end
  end
  parser.parse!(argv.dup)
  options
rescue OptionParser::ParseError => e
  raise SuperAuth::Error, "#{e.message}\n#{parser}"
end

.redact(url) ⇒ Object



86
87
88
# File 'lib/super_auth/editor/cli.rb', line 86

def redact(url)
  url.sub(%r{//([^:/@]+):[^@]*@}, '//\1:***@')
end

.run(argv, env: ENV, err: $stderr) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/super_auth/editor/cli.rb', line 43

def run(argv, env: ENV, err: $stderr)
  options = parse(argv)
  url = env["SUPER_AUTH_DATABASE_URL"].to_s.strip
  if url.empty?
    raise SuperAuth::Error, "SUPER_AUTH_DATABASE_URL is not set. Point it at the database that holds " \
                            "the super_auth tables, e.g. postgres://user:password@localhost/app_development"
  end

  # First Sequel::Database in the process: the models bind to it.
  begin
    SuperAuth.db = Sequel.connect(url)
  rescue Sequel::AdapterNotFound => e
    raise SuperAuth::Error, "#{e.message}. Install the adapter gem for this URL (pg, mysql2 or sqlite3)."
  end

  SuperAuth.install_migrations if options[:migrate]
  begin
    SuperAuth.load
  rescue Sequel::DatabaseError => e
    raise SuperAuth::Error, "super_auth tables not found at #{redact(url)} (#{e.message.lines.first.to_s.strip}). " \
                            "Run with --migrate to create them, or run your application's migrations."
  end

  if options[:seed]
    require "super_auth/editor/seed"
    counts = SuperAuth::Editor::Seed.run!
    err.puts "Seeded the Acme Cloud sample graph: #{counts.map { |k, v| "#{v} #{k}" }.join(', ')}."
  end

  begin
    require "rackup"
  rescue LoadError
    raise SuperAuth::Error, "The editor needs a Rack server: gem install rackup webrick (puma also works)."
  end

  loopback = LOOPBACK_BINDS.include?(options[:host])
  err.puts WARNING
  err.puts "Bound to #{options[:host]}, which other machines can reach." unless loopback
  err.puts "Editor at http://#{options[:host]}:#{options[:port]}"
  app = SuperAuth::Editor.new(hosts: loopback ? LOOPBACK_HOSTS : nil)
  Rackup::Server.start(app: app, Host: options[:host], Port: options[:port], environment: "deployment")
end