Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
296 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Proxy::Dns::Powerdns::Backend | ||
class Dummy < ::Proxy::Dns::Powerdns::Record | ||
|
||
def initialize(a_server = nil, a_ttl = nil) | ||
super(a_server, a_ttl) | ||
end | ||
|
||
def get_zone name | ||
{ | ||
'id' => 1, | ||
'name' => name.partition('.')[2] | ||
} | ||
end | ||
|
||
def create_record domain_id, name, ttl, content, type | ||
false | ||
end | ||
|
||
def delete_record domain_id, name, type | ||
false | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'mysql2' | ||
|
||
module Proxy::Dns::Powerdns::Backend | ||
class Mysql < ::Proxy::Dns::Powerdns::Record | ||
|
||
attr_reader :hostname, :username, :password, :database | ||
|
||
def initialize(a_server = nil, a_ttl = nil) | ||
@hostname = Proxy::Dns::Powerdns::Plugin.settings.powerdns_mysql_hostname || 'localhost' | ||
@username = Proxy::Dns::Powerdns::Plugin.settings.powerdns_mysql_username | ||
@password = Proxy::Dns::Powerdns::Plugin.settings.powerdns_mysql_password | ||
@database = Proxy::Dns::Powerdns::Plugin.settings.powerdns_mysql_database | ||
|
||
super(a_server, a_ttl) | ||
end | ||
|
||
def connection | ||
@connection ||= Mysql2::Client.new(:host => hostname, :username => username, :password => password, :database => database) | ||
end | ||
|
||
def get_zone name | ||
domain = nil | ||
|
||
name = connection.escape(name) | ||
connection.query("SELECT LENGTH(name) domain_length, id, name FROM domains WHERE '#{name}' LIKE CONCAT('%%.', name) ORDER BY domain_length DESC LIMIT 1").each do |row| | ||
domain = row | ||
end | ||
|
||
raise Proxy::Dns::Error, "Unable to determine zone. Zone must exist in PowerDNS." unless domain | ||
|
||
domain | ||
end | ||
|
||
def create_record domain_id, name, ttl, content, type | ||
name = connection.escape(name) | ||
content = connection.escape(content) | ||
type = connection.escape(type) | ||
connection.query("INSERT INTO records (domain_id, name, ttl, content, type) VALUES (#{domain_id}, '#{name}', #{ttl.to_i}, '#{content}', '#{type}')") | ||
true | ||
end | ||
|
||
def delete_record domain_id, name, type | ||
name = connection.escape(name) | ||
type = connection.escape(type) | ||
connection.query("DELETE FROM records WHERE domain_id=#{domain_id} AND name='#{name}' AND type='#{type}'") | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'dns_common/dependency_injection/dependencies' | ||
|
||
class Proxy::Dns::DependencyInjection::Dependencies | ||
case Proxy::Dns::Powerdns::Plugin.settings.powerdns_backend | ||
when 'mysql' | ||
require 'smart_proxy_dns_powerdns/backend/mysql' | ||
dependency :dns_provider, Proxy::Dns::Powerdns::Backend::Mysql | ||
when 'dummy' | ||
require 'smart_proxy_dns_powerdns/backend/dummy' | ||
dependency :dns_provider, Proxy::Dns::Powerdns::Backend::Dummy | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
lib/smart_proxy_dns_powerdns/dns_powerdns_configuration_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'smart_proxy_dns_powerdns/dns_powerdns_plugin' | ||
|
||
module Proxy::Dns::Powerdns | ||
class ConfigurationValidator | ||
def validate_settings!(settings) | ||
validate_choice(settings, :powerdns_backend, ['mysql', 'dummy']) | ||
|
||
case settings[:powerdns_backend] | ||
when 'mysql' | ||
validate_presence(settings, [:powerdns_mysql_username, :powerdns_mysql_password, :powerdns_mysql_database]) | ||
end | ||
end | ||
|
||
def validate_choice(settings, setting, choices) | ||
value = settings[setting] | ||
unless choices.include?(value) | ||
raise ::Proxy::Error::ConfigurationError, "Parameter '#{setting}' is expected to be one of #{choices.join(",")}" | ||
end | ||
true | ||
end | ||
|
||
def validate_presence(settings, names) | ||
names.each do |name| | ||
value = settings[name] | ||
raise ::Proxy::Error::ConfigurationError, "Parameter '#{name}' is expected to have a non-empty value" if value.nil? || value.to_s.empty? | ||
end | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'test_helper' | ||
|
||
require 'smart_proxy_dns_powerdns/dns_powerdns_plugin' | ||
require 'smart_proxy_dns_powerdns/dns_powerdns_configuration_validator' | ||
|
||
class DnsPowerdnsConfigurationValidatorTest < Test::Unit::TestCase | ||
def setup | ||
@config_validator = Proxy::Dns::Powerdns::ConfigurationValidator.new | ||
end | ||
|
||
def test_initialize_missing_backend | ||
settings = {:dns_provider => 'powerdns', :powerdns_backend => nil} | ||
|
||
assert_raise Proxy::Error::ConfigurationError do | ||
@config_validator.validate_settings!(settings) | ||
end | ||
end | ||
|
||
def test_initialize_invalid_backend | ||
settings = {:dns_provider => 'powerdns', :powerdns_backend => 'invalid'} | ||
|
||
assert_raise Proxy::Error::ConfigurationError do | ||
@config_validator.validate_settings!(settings) | ||
end | ||
end | ||
|
||
def test_initialize_dummy_with_settings | ||
settings = {:dns_provider => 'powerdns', :powerdns_backend => 'dummy'} | ||
|
||
assert_nothing_raised do | ||
@config_validator.validate_settings!(settings) | ||
end | ||
end | ||
|
||
def test_initialize_mysql_without_settings | ||
settings = {:dns_provider => 'powerdns', :powerdns_backend => 'mysql'} | ||
|
||
assert_raise Proxy::Error::ConfigurationError do | ||
@config_validator.validate_settings!(settings) | ||
end | ||
end | ||
|
||
def test_initialize_mysql_with_settings | ||
settings = { | ||
:dns_provider => 'powerdns', | ||
:powerdns_backend => 'mysql', | ||
:powerdns_mysql_hostname => 'localhost', | ||
:powerdns_mysql_username => 'username', | ||
:powerdns_mysql_password => 'password', | ||
:powerdns_mysql_database => 'powerdns' | ||
} | ||
|
||
assert_nothing_raised do | ||
@config_validator.validate_settings!(settings) | ||
end | ||
end | ||
end |
Oops, something went wrong.