dns_adapter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d8e3fd1654dd5ce7799fa3211d210ba4b633c67
4
+ data.tar.gz: f72a3cabad09a65366f89b55fc6fca4111aae82c
5
+ SHA512:
6
+ metadata.gz: 1630678f66915f648dceee32bc418021c3de846954824b03ef3bf0670b720f30d0944b1d9bc66457f706ea285a448418bc605597085e91b43165681993e773c9
7
+ data.tar.gz: 2420893ee8679b6b13c3c8b21f62d35af148d474ed5f06dcdeb6ab7a8fd03d509f85255a4580213cb262186273e3c0c8530adb77eb79c13671e853de37738af0
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ env:
3
+ - CODECLIMATE_REPO_TOKEN=9881d1116b64b55a16845046a4a2dc43f12203d7201a33ce92fec4b7732e3c93
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.2
7
+ - jruby-19mode
8
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'simplecov', group: :test, require: false
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2014 Peter M. Goldstein
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # DNSAdapter
2
+
3
+ [![Build Status](https://travis-ci.org/petergoldstein/dns_adapter.svg?branch=master)](https://travis-ci.org/petergoldstein/dns_adapter)
4
+ [![Test Coverage](https://codeclimate.com/github/petergoldstein/dns_adapter/badges/coverage.svg)](https://codeclimate.com/github/petergoldstein/dns_adapter)
5
+ [![Code Climate](https://codeclimate.com/github/petergoldstein/dns_adapter/badges/gpa.svg)](https://codeclimate.com/github/petergoldstein/dns_adapter)
6
+
7
+ An adapter layer for DNS queries that makes it simple to swap in different DNS providers.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'dns_adapter'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install dns_adapter
24
+
25
+ ## Usage
26
+
27
+ DNSAdapter contains a set of useful adapter classes that present a common set of return types and errors for DNS services. To use the gem, simply instantiate an instance of the desired adapter class.
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/[my-github-username]/dns_adapter/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :spec
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = ['--color', '--format doc']
8
+ end
9
+
10
+ require 'rubocop/rake_task'
11
+
12
+ desc 'Run RuboCop on the lib directory'
13
+ RuboCop::RakeTask.new(:rubocop) do |task|
14
+ task.patterns = ['lib/**/*.rb', 'spec/**/*.rb']
15
+ # don't abort rake on failure
16
+ task.fail_on_error = false
17
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dns_adapter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dns_adapter'
8
+ spec.version = DNSAdapter::VERSION
9
+ spec.authors = ['Peter M. Goldstein']
10
+ spec.email = ['peter.m.goldstein@gmail.com']
11
+ spec.summary = 'An adapter layer for DNS queries.'
12
+ spec.description = 'An adapter layer for DNS queries.'
13
+ spec.homepage = 'https://github.com/petergoldstein/dns_adapter'
14
+ spec.license = 'Apache'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '>= 3.0'
24
+ spec.add_development_dependency 'rubocop'
25
+ end
@@ -0,0 +1,5 @@
1
+ module DNSAdapter
2
+ class Error < ::StandardError; end
3
+ class TimeoutError < Error; end
4
+ class NXDomainError < Error; end
5
+ end
@@ -0,0 +1,104 @@
1
+ require 'dns_adapter/error'
2
+
3
+ module DNSAdapter
4
+ # A mock client for use in tests.
5
+ class MockClient
6
+ def initialize(zone_data)
7
+ @zone_data = {}
8
+ zone_data.each do |k, v|
9
+ @zone_data[k.downcase] = v.dup
10
+ end
11
+ end
12
+
13
+ def fetch_a_records(domain)
14
+ fetch_records(domain, 'A')
15
+ end
16
+
17
+ def fetch_aaaa_records(domain)
18
+ fetch_records(domain, 'AAAA')
19
+ end
20
+
21
+ def fetch_mx_records(domain)
22
+ fetch_records(domain, 'MX')
23
+ end
24
+
25
+ def fetch_ptr_records(arpa_address)
26
+ fetch_records(arpa_address, 'PTR')
27
+ end
28
+
29
+ def fetch_txt_records(domain)
30
+ fetch_records(domain, 'TXT')
31
+ end
32
+
33
+ def fetch_spf_records(domain)
34
+ fetch_records(domain, 'SPF')
35
+ end
36
+
37
+ def fetch_records(domain, type)
38
+ record_set = find_records_for_domain(domain)
39
+ return [] if record_set.empty?
40
+ records = records_for_type(record_set, type)
41
+ if records.empty?
42
+ check_for_timeout(record_set)
43
+ else
44
+ formatted_records(records, type)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def normalize_domain(domain)
51
+ return if domain.blank?
52
+ domain = domain[0...-1] if domain[domain.length - 1] == '.'
53
+ domain.downcase
54
+ end
55
+
56
+ def find_records_for_domain(domain)
57
+ return [] if domain.blank?
58
+ @zone_data[normalize_domain(domain)] || []
59
+ end
60
+
61
+ def records_for_type(record_set, type)
62
+ record_set.select do |r|
63
+ r.is_a?(Hash) && r[type] && r[type] != 'NONE'
64
+ end
65
+ end
66
+
67
+ TIMEOUT = 'TIMEOUT'
68
+ def check_for_timeout(record_set)
69
+ return [] if record_set.select { |r| r == TIMEOUT }.empty?
70
+ fail DNSAdapter::TimeoutError
71
+ end
72
+
73
+ RECORD_TYPE_TO_ATTR_NAME_MAP = {
74
+ 'A' => :address,
75
+ 'AAAA' => :address,
76
+ 'MX' => :exchange,
77
+ 'PTR' => :name,
78
+ 'SPF' => :text,
79
+ 'TXT' => :text
80
+ }
81
+
82
+ def formatted_records(records, type)
83
+ records.map do |r|
84
+ val = r[type]
85
+ fail DNSAdapter::TimeoutError if val == TIMEOUT
86
+ val = normalize_value(val, type)
87
+ {
88
+ type: type,
89
+ RECORD_TYPE_TO_ATTR_NAME_MAP[type] => val
90
+ }
91
+ end
92
+ end
93
+
94
+ def normalize_value(value, type)
95
+ if type == 'MX' && value.is_a?(Array)
96
+ value.last
97
+ elsif (type == 'TXT' || type == 'SPF') && value.is_a?(Array)
98
+ value.join('')
99
+ else
100
+ value
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,108 @@
1
+ require 'resolv'
2
+ require 'resolv/dns/resource/in/spf'
3
+
4
+ module DNSAdapter
5
+ # An adapter client for the internal Resolv DNS client.
6
+ class ResolvClient
7
+ def fetch_a_records(domain)
8
+ fetch_a_type_records(domain, 'A')
9
+ end
10
+
11
+ def fetch_aaaa_records(domain)
12
+ fetch_a_type_records(domain, 'AAAA')
13
+ end
14
+
15
+ def fetch_mx_records(domain)
16
+ fetch_records(domain, 'MX') do |record|
17
+ {
18
+ type: 'MX',
19
+ exchange: record.exchange.to_s
20
+ }
21
+ end
22
+ end
23
+
24
+ def fetch_ptr_records(arpa_address)
25
+ fetch_records(arpa_address, 'PTR') do |record|
26
+ {
27
+ type: 'PTR',
28
+ name: record.name.to_s
29
+ }
30
+ end
31
+ end
32
+
33
+ def fetch_txt_records(domain)
34
+ fetch_txt_type_records(domain, 'TXT')
35
+ end
36
+
37
+ def fetch_spf_records(domain)
38
+ fetch_txt_type_records(domain, 'SPF')
39
+ end
40
+
41
+ private
42
+
43
+ def fetch_a_type_records(domain, type)
44
+ fetch_records(domain, type) do |record|
45
+ {
46
+ type: type,
47
+ address: record.address.to_s
48
+ }
49
+ end
50
+ end
51
+
52
+ def fetch_txt_type_records(domain, type)
53
+ fetch_records(domain, type) do |record|
54
+ {
55
+ type: type,
56
+ # Use strings.join('') to avoid JRuby issue where
57
+ # data only returns the first string
58
+ text: record.strings.join('')
59
+ }
60
+ end
61
+ end
62
+
63
+ def fetch_records(domain, type, &block)
64
+ records = dns_lookup(domain, type)
65
+ records.map(&block)
66
+ end
67
+
68
+ TRAILING_DOT_REGEXP = /\.\z/
69
+ def normalize_domain(domain)
70
+ (domain.sub(TRAILING_DOT_REGEXP, '') || domain).downcase
71
+ end
72
+
73
+ def dns_lookup(domain, rr_type)
74
+ domain = normalize_domain(domain)
75
+ resources = getresources(domain, rr_type)
76
+
77
+ unless resources
78
+ fail DNSAdapter::Error,
79
+ "Unknown error on DNS '#{rr_type}' lookup of '#{domain}'"
80
+ end
81
+
82
+ resources
83
+ end
84
+
85
+ def getresources(domain, rr_type)
86
+ rr_class = self.class.type_class(rr_type)
87
+ dns_resolver.getresources(domain, rr_class)
88
+ rescue Resolv::ResolvTimeout
89
+ raise DNSAdapter::TimeoutError,
90
+ "Time-out on DNS '#{rr_type}' lookup of '#{domain}'"
91
+ rescue Resolv::ResolvError
92
+ raise DNSAdapter::Error, "Error on DNS lookup of '#{domain}'"
93
+ end
94
+
95
+ SUPPORTED_RR_TYPES = %w(A AAAA MX PTR TXT SPF)
96
+ def self.type_class(rr_type)
97
+ if SUPPORTED_RR_TYPES.include?(rr_type)
98
+ Resolv::DNS::Resource::IN.const_get(rr_type)
99
+ else
100
+ fail ArgumentError, "Unknown RR type: #{rr_type}"
101
+ end
102
+ end
103
+
104
+ def dns_resolver
105
+ @dns_resolver ||= Resolv::DNS.new
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module DNSAdapter
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'dns_adapter/version'
2
+ require 'dns_adapter/error'
3
+ require 'dns_adapter/mock_client'
4
+ require 'dns_adapter/resolv_client'
5
+
6
+ # A useful adapter interface for hiding DNS implementations
7
+ module DNSAdapter
8
+ end
@@ -0,0 +1,15 @@
1
+ require 'resolv'
2
+
3
+ class Resolv
4
+ class DNS
5
+ class Resource
6
+ module IN
7
+ # DNS record type for SPF records
8
+ class SPF < Resolv::DNS::Resource::IN::TXT
9
+ # resolv.rb doesn't define an SPF resource type.
10
+ TypeValue = 99 # rubocop:disable Style/ConstantName
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,307 @@
1
+ require 'spec_helper'
2
+
3
+ describe DNSAdapter::ResolvClient do
4
+ subject { DNSAdapter::ResolvClient.new }
5
+ let(:mock_resolver) { double(:resolver) }
6
+
7
+ context '#fetch_a_records' do
8
+ let(:first_a_addr) { Resolv::IPv4.new([127, 0, 0, 1].pack('CCCC')) }
9
+ let(:first_a_record) { Resolv::DNS::Resource::IN::A.new(first_a_addr) }
10
+ let(:second_a_addr) { Resolv::IPv4.new([192, 168, 8, 14].pack('CCCC')) }
11
+ let(:second_a_record) { Resolv::DNS::Resource::IN::A.new(second_a_addr) }
12
+ let(:record_list) { [first_a_record, second_a_record] }
13
+ let(:domain) { 'example.com' }
14
+ let(:domain_with_trailing) { "#{domain}." }
15
+
16
+ it 'should map the Resolv classes to a set of hashes' do
17
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
18
+ expect(mock_resolver).to receive(:getresources)
19
+ .with(domain, Resolv::DNS::Resource::IN::A).and_return(record_list)
20
+ results = subject.fetch_a_records(domain)
21
+ expect(results.size).to eq(record_list.length)
22
+ expect(results.map { |x| x[:type] })
23
+ .to eq(record_list.length.times.map { 'A' })
24
+ expect(results.map { |x| x[:address] })
25
+ .to eq(record_list.map(&:address).map(&:to_s))
26
+ end
27
+
28
+ it 'should map when the domain has a trailing dot' do
29
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
30
+ expect(mock_resolver).to receive(:getresources)
31
+ .with(domain, Resolv::DNS::Resource::IN::A).and_return(record_list)
32
+ results = subject.fetch_a_records(domain_with_trailing)
33
+ expect(results.size).to eq(record_list.length)
34
+ expect(results.map { |x| x[:type] })
35
+ .to eq(record_list.length.times.map { 'A' })
36
+ expect(results.map { |x| x[:address] })
37
+ .to eq(record_list.map(&:address).map(&:to_s))
38
+ end
39
+
40
+ it 'should map the Resolv errors to Coppertone errors' do
41
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
42
+ expect(mock_resolver).to receive(:getresources)
43
+ .with(domain, Resolv::DNS::Resource::IN::A)
44
+ .and_raise(Resolv::ResolvError)
45
+ expect do
46
+ subject.fetch_a_records(domain_with_trailing)
47
+ end.to raise_error(DNSAdapter::Error)
48
+ end
49
+
50
+ it 'should map the Resolv timeout errors to Coppertone errors' do
51
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
52
+ expect(mock_resolver).to receive(:getresources)
53
+ .with(domain, Resolv::DNS::Resource::IN::A)
54
+ .and_raise(Resolv::ResolvTimeout)
55
+ expect do
56
+ subject.fetch_a_records(domain_with_trailing)
57
+ end.to raise_error(DNSAdapter::TimeoutError)
58
+ end
59
+ end
60
+
61
+ context '#fetch_aaaa_records' do
62
+ let(:first_aaaa_addr) do
63
+ Resolv::IPv6.create('FE80:10:1:1:202:B3FF:FE1E:8329')
64
+ end
65
+ let(:first_aaaa_record) do
66
+ Resolv::DNS::Resource::IN::AAAA.new(first_aaaa_addr)
67
+ end
68
+ let(:second_aaaa_addr) do
69
+ Resolv::IPv6.create('AB61:10:111:891:4202:B3FF:FE1E:7329')
70
+ end
71
+ let(:second_aaaa_record) do
72
+ Resolv::DNS::Resource::IN::AAAA.new(second_aaaa_addr)
73
+ end
74
+ let(:record_list) { [first_aaaa_record, second_aaaa_record] }
75
+ let(:domain) { 'example.com' }
76
+ let(:domain_with_trailing) { "#{domain}." }
77
+
78
+ it 'should map the Resolv classes to a set of hashes' do
79
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
80
+ expect(mock_resolver).to receive(:getresources)
81
+ .with(domain, Resolv::DNS::Resource::IN::AAAA).and_return(record_list)
82
+ results = subject.fetch_aaaa_records(domain)
83
+ expect(results.size).to eq(record_list.length)
84
+ expect(results.map { |x| x[:type] })
85
+ .to eq(record_list.length.times.map { 'AAAA' })
86
+ expect(results.map { |x| x[:address] })
87
+ .to eq(record_list.map(&:address).map(&:to_s))
88
+ end
89
+
90
+ it 'should map when the domain has a trailing dot' do
91
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
92
+ expect(mock_resolver).to receive(:getresources)
93
+ .with(domain, Resolv::DNS::Resource::IN::AAAA).and_return(record_list)
94
+ results = subject.fetch_aaaa_records(domain_with_trailing)
95
+ expect(results.size).to eq(record_list.length)
96
+ expect(results.map { |x| x[:type] })
97
+ .to eq(record_list.length.times.map { 'AAAA' })
98
+ expect(results.map { |x| x[:address] })
99
+ .to eq(record_list.map(&:address).map(&:to_s))
100
+ end
101
+
102
+ it 'should map the Resolv errors to Coppertone errors' do
103
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
104
+ expect(mock_resolver).to receive(:getresources)
105
+ .with(domain, Resolv::DNS::Resource::IN::AAAA)
106
+ .and_raise(Resolv::ResolvError)
107
+ expect { subject.fetch_aaaa_records(domain_with_trailing) }
108
+ .to raise_error(DNSAdapter::Error)
109
+ end
110
+
111
+ it 'should map the Resolv timeout errors to Coppertone errors' do
112
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
113
+ expect(mock_resolver).to receive(:getresources)
114
+ .with(domain, Resolv::DNS::Resource::IN::AAAA)
115
+ .and_raise(Resolv::ResolvTimeout)
116
+ expect { subject.fetch_aaaa_records(domain_with_trailing) }
117
+ .to raise_error(DNSAdapter::TimeoutError)
118
+ end
119
+ end
120
+
121
+ context '#fetch_mx_records' do
122
+ let(:first_mx_name) do
123
+ Resolv::DNS::Name.create('alt1.aspmx.l.google.com.')
124
+ end
125
+ let(:first_mx_record) do
126
+ Resolv::DNS::Resource::IN::MX.new(20, first_mx_name)
127
+ end
128
+ let(:second_mx_name) do
129
+ Resolv::DNS::Name.create('aspmx.l.google.com')
130
+ end
131
+ let(:second_mx_record) do
132
+ Resolv::DNS::Resource::IN::MX.new(10, second_mx_name)
133
+ end
134
+ let(:record_list) { [first_mx_record, second_mx_record] }
135
+ let(:domain) { 'example.com' }
136
+ let(:domain_with_trailing) { "#{domain}." }
137
+
138
+ it 'should map the Resolv classes to a set of hashes' do
139
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
140
+ expect(mock_resolver).to receive(:getresources)
141
+ .with(domain, Resolv::DNS::Resource::IN::MX).and_return(record_list)
142
+ results = subject.fetch_mx_records(domain)
143
+ expect(results.size).to eq(record_list.length)
144
+ expect(results.map { |x| x[:type] })
145
+ .to eq(record_list.length.times.map { 'MX' })
146
+ expect(results.map { |x| x[:exchange] })
147
+ .to eq(record_list.map(&:exchange).map(&:to_s))
148
+ end
149
+
150
+ it 'should map when the domain has a trailing dot' do
151
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
152
+ expect(mock_resolver).to receive(:getresources)
153
+ .with(domain, Resolv::DNS::Resource::IN::MX).and_return(record_list)
154
+ results = subject.fetch_mx_records(domain_with_trailing)
155
+ expect(results.size).to eq(record_list.length)
156
+ expect(results.map { |x| x[:type] })
157
+ .to eq(record_list.length.times.map { 'MX' })
158
+ expect(results.map { |x| x[:exchange] })
159
+ .to eq(record_list.map(&:exchange).map(&:to_s))
160
+ end
161
+
162
+ it 'should map the Resolv errors to Coppertone errors' do
163
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
164
+ expect(mock_resolver).to receive(:getresources)
165
+ .with(domain, Resolv::DNS::Resource::IN::MX).and_raise(Resolv::ResolvError)
166
+ expect { subject.fetch_mx_records(domain_with_trailing) }
167
+ .to raise_error(DNSAdapter::Error)
168
+ end
169
+
170
+ it 'should map the Resolv timeout errors to Coppertone errors' do
171
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
172
+ expect(mock_resolver).to receive(:getresources)
173
+ .with(domain, Resolv::DNS::Resource::IN::MX)
174
+ .and_raise(Resolv::ResolvTimeout)
175
+ expect { subject.fetch_mx_records(domain_with_trailing) }
176
+ .to raise_error(DNSAdapter::TimeoutError)
177
+ end
178
+ end
179
+
180
+ context '#fetch_txt_records' do
181
+ let(:first_txt_string) { SecureRandom.hex(10) }
182
+ let(:first_txt_record) do
183
+ Resolv::DNS::Resource::IN::TXT.new(first_txt_string)
184
+ end
185
+ let(:second_txt_string) { SecureRandom.hex(10) }
186
+ let(:second_txt_string_array) do
187
+ [SecureRandom.hex(10), SecureRandom.hex(10)]
188
+ end
189
+ let(:second_txt_record) do
190
+ Resolv::DNS::Resource::IN::TXT.new(second_txt_string,
191
+ second_txt_string_array)
192
+ end
193
+ let(:record_list) { [first_txt_record, second_txt_record] }
194
+ let(:domain) { 'example.com' }
195
+ let(:domain_with_trailing) { "#{domain}." }
196
+
197
+ it 'should map the Resolv classes to a set of hashes' do
198
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
199
+ expect(mock_resolver).to receive(:getresources)
200
+ .with(domain, Resolv::DNS::Resource::IN::TXT)
201
+ .and_return(record_list)
202
+ results = subject.fetch_txt_records(domain)
203
+ expect(results.size).to eq(record_list.length)
204
+ expect(results.map { |x| x[:type] })
205
+ .to eq(record_list.length.times.map { 'TXT' })
206
+ expect(results.map { |x| x[:text] }).to eq(
207
+ [first_txt_string,
208
+ ([second_txt_string] + second_txt_string_array).join('')])
209
+ end
210
+
211
+ it 'should map when the domain has a trailing dot' do
212
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
213
+ expect(mock_resolver).to receive(:getresources)
214
+ .with(domain, Resolv::DNS::Resource::IN::TXT)
215
+ .and_return(record_list)
216
+ results = subject.fetch_txt_records(domain_with_trailing)
217
+ expect(results.size).to eq(record_list.length)
218
+ expect(results.map { |x| x[:type] })
219
+ .to eq(record_list.length.times.map { 'TXT' })
220
+ expect(results.map { |x| x[:text] }).to eq(
221
+ [first_txt_string,
222
+ ([second_txt_string] + second_txt_string_array).join('')])
223
+ end
224
+
225
+ it 'should map the Resolv errors to Coppertone errors' do
226
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
227
+ expect(mock_resolver).to receive(:getresources)
228
+ .with(domain, Resolv::DNS::Resource::IN::TXT)
229
+ .and_raise(Resolv::ResolvError)
230
+ expect { subject.fetch_txt_records(domain_with_trailing) }
231
+ .to raise_error(DNSAdapter::Error)
232
+ end
233
+
234
+ it 'should map the Resolv timeout errors to Coppertone errors' do
235
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
236
+ expect(mock_resolver).to receive(:getresources)
237
+ .with(domain, Resolv::DNS::Resource::IN::TXT)
238
+ .and_raise(Resolv::ResolvTimeout)
239
+ expect { subject.fetch_txt_records(domain_with_trailing) }
240
+ .to raise_error(DNSAdapter::TimeoutError)
241
+ end
242
+ end
243
+
244
+ context '#fetch_spf_records' do
245
+ let(:first_spf_string) { SecureRandom.hex(10) }
246
+ let(:first_spf_record) do
247
+ Resolv::DNS::Resource::IN::TXT.new(first_spf_string)
248
+ end
249
+ let(:second_spf_string) { SecureRandom.hex(10) }
250
+ let(:second_spf_string_array) do
251
+ [SecureRandom.hex(10), SecureRandom.hex(10)]
252
+ end
253
+ let(:second_spf_record) do
254
+ Resolv::DNS::Resource::IN::SPF.new(second_spf_string,
255
+ second_spf_string_array)
256
+ end
257
+ let(:record_list) { [first_spf_record, second_spf_record] }
258
+ let(:domain) { 'example.com' }
259
+ let(:domain_with_trailing) { "#{domain}." }
260
+
261
+ it 'should map the Resolv classes to a set of hashes' do
262
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
263
+ expect(mock_resolver).to receive(:getresources)
264
+ .with(domain, Resolv::DNS::Resource::IN::SPF)
265
+ .and_return(record_list)
266
+ results = subject.fetch_spf_records(domain)
267
+ expect(results.size).to eq(record_list.length)
268
+ expect(results.map { |x| x[:type] })
269
+ .to eq(record_list.length.times.map { 'SPF' })
270
+ expect(results.map { |x| x[:text] }).to eq(
271
+ [first_spf_string,
272
+ ([second_spf_string] + second_spf_string_array).join('')])
273
+ end
274
+
275
+ it 'should map when the domain has a trailing dot' do
276
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
277
+ expect(mock_resolver).to receive(:getresources)
278
+ .with(domain, Resolv::DNS::Resource::IN::SPF)
279
+ .and_return(record_list)
280
+ results = subject.fetch_spf_records(domain_with_trailing)
281
+ expect(results.size).to eq(record_list.length)
282
+ expect(results.map { |x| x[:type] })
283
+ .to eq(record_list.length.times.map { 'SPF' })
284
+ expect(results.map { |x| x[:text] }).to eq(
285
+ [first_spf_string,
286
+ ([second_spf_string] + second_spf_string_array).join('')])
287
+ end
288
+
289
+ it 'should map the Resolv errors to Coppertone errors' do
290
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
291
+ expect(mock_resolver).to receive(:getresources)
292
+ .with(domain, Resolv::DNS::Resource::IN::SPF)
293
+ .and_raise(Resolv::ResolvError)
294
+ expect { subject.fetch_spf_records(domain_with_trailing) }
295
+ .to raise_error(DNSAdapter::Error)
296
+ end
297
+
298
+ it 'should map the Resolv timeout errors to Coppertone errors' do
299
+ expect(Resolv::DNS).to receive(:new).and_return(mock_resolver)
300
+ expect(mock_resolver).to receive(:getresources)
301
+ .with(domain, Resolv::DNS::Resource::IN::SPF)
302
+ .and_raise(Resolv::ResolvTimeout)
303
+ expect { subject.fetch_spf_records(domain_with_trailing) }
304
+ .to raise_error(DNSAdapter::TimeoutError)
305
+ end
306
+ end
307
+ end
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ require 'codeclimate-test-reporter'
3
+ CodeClimate::TestReporter.start || SimpleCov.start
4
+
5
+ require 'dns_adapter'
6
+
7
+ # Support files
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dns_adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter M. Goldstein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: An adapter layer for DNS queries.
70
+ email:
71
+ - peter.m.goldstein@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - dns_adapter.gemspec
83
+ - lib/dns_adapter.rb
84
+ - lib/dns_adapter/error.rb
85
+ - lib/dns_adapter/mock_client.rb
86
+ - lib/dns_adapter/resolv_client.rb
87
+ - lib/dns_adapter/version.rb
88
+ - lib/resolv/dns/resource/in/spf.rb
89
+ - spec/resolv_client_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/petergoldstein/dns_adapter
92
+ licenses:
93
+ - Apache
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: An adapter layer for DNS queries.
115
+ test_files:
116
+ - spec/resolv_client_spec.rb
117
+ - spec/spec_helper.rb