Module: Radd

Defined in:
lib/radd.rb,
lib/radd/ip.rb,
lib/radd/config.rb,
lib/radd/errors.rb,
lib/radd/schema.rb,
lib/radd/update.rb,
lib/radd/version.rb,
lib/radd/middleware.rb

Defined Under Namespace

Modules: Cli, Schema Classes: ConfigurationError, DNS, DNSService, Forbidden, HTTP, HTTPService, InvalidRequest, Middleware, Nameserver, RaddError, Record, Update, UpdateError, Webserver

Constant Summary collapse

IP =

IP address query responder

Proc.new do |env|
  [200, {"Content-Type" => "text/plain"}, ["#{env['rack.request'].ip}\n"]]
end
App =
Rack::Builder.app do
  use Radd::Middleware

  map '/ip' do
    run Radd::IP
  end

  map '/update' do
    use Rack::Auth::Basic, 'Authorization required' do |user, password|
      Radd.authorized?(user, password)
    end
    run Radd::Update
  end
end
VERSION =
'1.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/radd/config.rb', line 4

def db
  @db
end

.dns_hostObject (readonly)

Returns the value of attribute dns_host.



4
5
6
# File 'lib/radd/config.rb', line 4

def dns_host
  @dns_host
end

.dns_portObject (readonly)

Returns the value of attribute dns_port.



4
5
6
# File 'lib/radd/config.rb', line 4

def dns_port
  @dns_port
end

.domainObject (readonly)

Returns the value of attribute domain.



4
5
6
# File 'lib/radd/config.rb', line 4

def domain
  @domain
end

.http_hostObject (readonly)

Returns the value of attribute http_host.



4
5
6
# File 'lib/radd/config.rb', line 4

def http_host
  @http_host
end

.http_portObject (readonly)

Returns the value of attribute http_port.



4
5
6
# File 'lib/radd/config.rb', line 4

def http_port
  @http_port
end

.ipObject (readonly)

Returns the value of attribute ip.



4
5
6
# File 'lib/radd/config.rb', line 4

def ip
  @ip
end

.ttlObject (readonly)

Returns the value of attribute ttl.



4
5
6
# File 'lib/radd/config.rb', line 4

def ttl
  @ttl
end

Class Method Details

.authorized?(name, password) ⇒ Boolean

Check whether name is authorized with password

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/radd.rb', line 42

def authorized?(name, password)
  return false unless record = Record.where(name: name).first
  BCrypt::Password.new(record.password_hash) == password
end

.configure!(file, skip_models: false, skip_db: false) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/radd/config.rb', line 6

def configure!(file, skip_models: false, skip_db: false)
  file_path = Pathname.new(file || 'radd.yml')
  file_path = Radd.root + file_path unless file_path.absolute?
  raise ConfigurationError, "could not open config file #{file_path}" unless file_path.file?
  config = YAML.load(file_path.read)
  raise ConfigurationError, 'domain missing' unless config['domain']
  @domain = config['domain']
  raise ConfigurationError, 'invalid IP' unless Radd.valid_ip?(config['ip'])
  @ip = config['ip']
  uri = URI.parse("http://#{config['http']}")
  @http_host = uri.host || '127.0.0.1'
  @http_port = uri.port || 3003
  uri = URI.parse("dns://#{config['dns']}")
  @dns_host = uri.host || '0.0.0.0'
  @dns_port = uri.port || 53
  raise ConfigurationError, 'invalid TTL' if config['ttl'] && config['ttl'] < 1
  @ttl = config['ttl'] || 300
  db_path = Pathname.new(config['db'] || 'radd.sqlite3')
  db_path = Radd.root + db_path unless db_path.absolute?
  @db = db_path
  raise ConfigurationError, 'invalid database' if !skip_db && !Radd.db.file?
  #
  # Late loading required by Sequel architecture
  #
  require_relative 'db'
  require_relative 'record' unless skip_models
end

.loggerObject



37
38
39
# File 'lib/radd.rb', line 37

def logger
  @logger = Logger.new(STDOUT)
end

.query(fqdn) ⇒ Object

Query the database for fqdn



48
49
50
51
52
53
# File 'lib/radd.rb', line 48

def query(fqdn)
  return unless fqdn
  return unless name = fqdn2name(fqdn)
  return unless record = Record.active.where(name: name).first
  record.ip
end

.rootObject



33
34
35
# File 'lib/radd.rb', line 33

def root
  @root ||= Pathname.new(Dir.pwd)
end

.root=(path) ⇒ Object



29
30
31
# File 'lib/radd.rb', line 29

def root=(path)
  @root = Pathname.new(path)
end

.valid_ip?(ip) ⇒ Boolean

Check whether ip is a valid IP address string

Returns:

  • (Boolean)


35
36
37
# File 'lib/radd/config.rb', line 35

def valid_ip?(ip)
  !!(ip && ip.match(Resolv::IPv4::Regex))
end