coredns 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.gitlab-ci.yml +9 -0
- data/.rspec +3 -0
- data/.rubocop.yml +28 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +73 -0
- data/README.md +96 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/coredns.gemspec +35 -0
- data/lib/coredns.rb +12 -0
- data/lib/domain.rb +20 -0
- data/lib/etcd/dns_zone.rb +90 -0
- data/lib/etcd/domain.rb +145 -0
- data/lib/etcd.rb +34 -0
- data/lib/helpers/hash_with_indifferent_access_custom.rb +33 -0
- data/lib/helpers/request_helper.rb +15 -0
- data/lib/version.rb +5 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5d7d10d7ef4a2d22ee299c5f5f3e12ac18ef434c3e7447396bc27b6d00539029
|
4
|
+
data.tar.gz: fb11c91b6fd335e6f2a267c0a5fcd1192332a16b52298a5cf35a0392c61d0524
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 620859fdf4174fc5570d0770d2e8337abe4d581ee1fa4c13949d57772625d579ca674f3bb65c13c336d309bebae69397041bdcdaacc4ac6a52f37a5cb36beb5a
|
7
|
+
data.tar.gz: 3e13d1d814feb4cf536f733ab8f48375f70dcd6242c82701b822db63f1b4129a45e23178fffda01db64e641bdbc982c7f7fa084e14c195c6453a1baf70ccad08
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Style/Documentation:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/AbcSize:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Lint/Void:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Layout/LineLength:
|
22
|
+
Max: 120
|
23
|
+
|
24
|
+
Metrics/ClassLength:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/ClassAndModuleChildren:
|
28
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ahrechushkin@ibagoup.eu. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
coredns (1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
domain_name (0.5.20190701)
|
12
|
+
unf (>= 0.0.5, < 1.0.0)
|
13
|
+
http-accept (1.7.0)
|
14
|
+
http-cookie (1.0.5)
|
15
|
+
domain_name (~> 0.5)
|
16
|
+
mime-types (3.4.1)
|
17
|
+
mime-types-data (~> 3.2015)
|
18
|
+
mime-types-data (3.2022.0105)
|
19
|
+
netrc (0.11.0)
|
20
|
+
parallel (1.22.1)
|
21
|
+
parser (3.1.2.0)
|
22
|
+
ast (~> 2.4.1)
|
23
|
+
rainbow (3.1.1)
|
24
|
+
rake (13.0.6)
|
25
|
+
regexp_parser (2.5.0)
|
26
|
+
rest-client (2.1.0)
|
27
|
+
http-accept (>= 1.7.0, < 2.0)
|
28
|
+
http-cookie (>= 1.0.2, < 2.0)
|
29
|
+
mime-types (>= 1.16, < 4.0)
|
30
|
+
netrc (~> 0.8)
|
31
|
+
rexml (3.2.5)
|
32
|
+
rspec (3.11.0)
|
33
|
+
rspec-core (~> 3.11.0)
|
34
|
+
rspec-expectations (~> 3.11.0)
|
35
|
+
rspec-mocks (~> 3.11.0)
|
36
|
+
rspec-core (3.11.0)
|
37
|
+
rspec-support (~> 3.11.0)
|
38
|
+
rspec-expectations (3.11.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.11.0)
|
41
|
+
rspec-mocks (3.11.1)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-support (3.11.0)
|
45
|
+
rubocop (1.30.1)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 3.1.0.0)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
regexp_parser (>= 1.8, < 3.0)
|
50
|
+
rexml (>= 3.2.5, < 4.0)
|
51
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
54
|
+
rubocop-ast (1.18.0)
|
55
|
+
parser (>= 3.1.1.0)
|
56
|
+
ruby-progressbar (1.11.0)
|
57
|
+
unf (0.1.4)
|
58
|
+
unf_ext
|
59
|
+
unf_ext (0.0.8.2)
|
60
|
+
unicode-display_width (2.1.0)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
x86_64-linux
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
coredns!
|
67
|
+
rake (~> 13.0)
|
68
|
+
rest-client (~> 2.0)
|
69
|
+
rspec (~> 3.0)
|
70
|
+
rubocop (~> 1.7)
|
71
|
+
|
72
|
+
BUNDLED WITH
|
73
|
+
2.2.33
|
data/README.md
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# Coredns
|
2
|
+
A simple way to control your dns records managed by [coredns-etcd application](https://coredns.io/plugins/etcd/)
|
3
|
+
|
4
|
+
## Requirements
|
5
|
+
ruby >= 3.0
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'coredns'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install coredns
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Connection object
|
26
|
+
```ruby
|
27
|
+
|
28
|
+
> coredns = CoreDns::Etcd.new('your.etcd.server.url.example')
|
29
|
+
> coredns
|
30
|
+
=>
|
31
|
+
#<CoreDns::Etcd:0x00007f09c2ba7d40
|
32
|
+
@api_url="http://your.etcd.server.url.example:2379/v3",
|
33
|
+
@endpoint="your.etcd.server.url.example",
|
34
|
+
@logger=
|
35
|
+
#<Logger:0x00007f09c2ba7cf0
|
36
|
+
@default_formatter=#<Logger::Formatter:0x00007f09c2ba7c50 @datetime_format=nil>,
|
37
|
+
@formatter=nil,
|
38
|
+
@level=0,
|
39
|
+
@logdev=#<Logger::LogDevice:0x00007f09c2ba7bb0 @binmode=false, @dev=#<IO:<STDOUT>>, @filename=nil, @mon_data=#<Monitor:0x00007f09c2ba7b88>, @mon_data_owner_object_id=49820, @shift_age=nil, @shift_period_suffix=nil, @shift_size=nil>,
|
40
|
+
@progname=nil>,
|
41
|
+
@port=2379,
|
42
|
+
@postfix="x",
|
43
|
+
@prefix="skydns",
|
44
|
+
@version="v3">
|
45
|
+
```
|
46
|
+
|
47
|
+
You can specify custom postfix, prefix, api version and port using enviroment variables:
|
48
|
+
```
|
49
|
+
ENV["COREDNS_PREFIX"], default is "skydns"
|
50
|
+
ENV["COREDNS_POSTFIX"], default is "x"
|
51
|
+
ENV["COREDNS_PORT"], default is 2379
|
52
|
+
ENV["COREDNS_VERSION"], default is "v3" (also tested v3beta)
|
53
|
+
```
|
54
|
+
|
55
|
+
### DNS Zones
|
56
|
+
```ruby
|
57
|
+
# List dns zones
|
58
|
+
> coredns.zone('').list
|
59
|
+
=> []
|
60
|
+
|
61
|
+
# Show information about selected dns zone
|
62
|
+
> coredns.zone('your.zone').show
|
63
|
+
=> {"name"=>"your.zone", "metadata"=>{"zone"=>true, ...additional metadata information... }}
|
64
|
+
|
65
|
+
# List of subzones
|
66
|
+
> coredns.zone('your.zone').subzones
|
67
|
+
|
68
|
+
# Get parent dns zone
|
69
|
+
> coredns.zone('sub1.your.zone').parent_zone
|
70
|
+
|
71
|
+
# Create a new dns zone
|
72
|
+
|
73
|
+
> coredns.zone('new.your.zone').add({metadata: {...additional metadata infromation...}})
|
74
|
+
|
75
|
+
# Delete existing dns zone
|
76
|
+
> coredns.zone('new.your.zone').delete
|
77
|
+
|
78
|
+
# List dns zone records
|
79
|
+
|
80
|
+
> coredns.zone('your.zone').records
|
81
|
+
```
|
82
|
+
|
83
|
+
### DNS domains
|
84
|
+
```ruby
|
85
|
+
# Add a new domain
|
86
|
+
> coredns.domain('domain1.your.zone').add({params})
|
87
|
+
# Whitelist of availalbe params:
|
88
|
+
# host mail port priority text ttl group metadata
|
89
|
+
|
90
|
+
|
91
|
+
# Get information about domain
|
92
|
+
> coredns.domain('domain1.your.zone').show
|
93
|
+
|
94
|
+
# Delete domain
|
95
|
+
> coredns.domain('domain1.your.zone').delete
|
96
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "coredns"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/coredns.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "coredns"
|
7
|
+
spec.version = CoreDns::VERSION
|
8
|
+
spec.authors = ["Aliaksei Hrechushkin"]
|
9
|
+
spec.email = ["ahrechushkin@ibagoup.eu"]
|
10
|
+
|
11
|
+
spec.summary = "Wrapper for coredns-etcd application"
|
12
|
+
spec.description = "Gem which provide simple way to control DNS records."
|
13
|
+
spec.homepage = "https://icdc.io"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 3.0")
|
15
|
+
spec.licenses = ["Apache-2.0"]
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/icdc-io/coredns"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/icdc-io/coredns/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
# Uncomment to register a new dependency of your gem
|
31
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
32
|
+
|
33
|
+
# For more information and examples about making a new gem, checkout our
|
34
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
35
|
+
end
|
data/lib/coredns.rb
ADDED
data/lib/domain.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CoreDns
|
4
|
+
class Domain
|
5
|
+
attr_reader :namespace
|
6
|
+
|
7
|
+
def initialize(client, hostname = "")
|
8
|
+
@namespace = hostname
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def get
|
13
|
+
raise NotImplementedError, ".get must be implemented in a subclass"
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(_values = {})
|
17
|
+
raise NotImplementedError, ".add must be implemented in a subclass"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../helpers/hash_with_indifferent_access_custom"
|
4
|
+
require_relative "../helpers/request_helper"
|
5
|
+
|
6
|
+
# CoreDns::Domain
|
7
|
+
class CoreDns::Etcd::DnsZone < CoreDns::Etcd::Domain
|
8
|
+
VALUES_WHITELIST = %w[metadata].freeze
|
9
|
+
|
10
|
+
def add(data = {})
|
11
|
+
data[:metadata][:zone] = true
|
12
|
+
put(@namespace, HashWithIndifferentAccessCustom.new(data).attributes)
|
13
|
+
end
|
14
|
+
|
15
|
+
def list(level = 1)
|
16
|
+
super.select { |record| record.dig("metadata", "zone") }
|
17
|
+
end
|
18
|
+
|
19
|
+
def list_all
|
20
|
+
super.select { |dns| dns.dig("metadata", "zone") }
|
21
|
+
end
|
22
|
+
|
23
|
+
def subzones
|
24
|
+
@client.domain(@namespace).list_all
|
25
|
+
.select { |domain| domain.dig("metadata", "zone") }
|
26
|
+
.map { |zone_hash| @client.zone("#{zone_hash["name"]}.#{namespace}") }
|
27
|
+
end
|
28
|
+
|
29
|
+
def parent_zone
|
30
|
+
@namespace = @namespace.split(".")[1..].join(".")
|
31
|
+
return nil if @namespace.empty?
|
32
|
+
|
33
|
+
zone_hash = @client.zone(@namespace).show
|
34
|
+
return @client.zone(@namespace) if zone_hash
|
35
|
+
|
36
|
+
parent_zone
|
37
|
+
end
|
38
|
+
|
39
|
+
def fetch_zone_records
|
40
|
+
fetch("").select do |record|
|
41
|
+
record if record["group"]&.end_with?(@namespace.to_s)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def records
|
46
|
+
zone_records = fetch_zone_records
|
47
|
+
|
48
|
+
subzones.collect(&:namespace).map do |subzone_name|
|
49
|
+
zone_records.reject! { |record| record["group"]&.end_with?(subzone_name.to_s) }
|
50
|
+
end
|
51
|
+
|
52
|
+
zone_records.map do |record|
|
53
|
+
hostname = record.delete("hostname")
|
54
|
+
record.merge!({ "name" => hostname.split("/").reverse.reject(&:empty?).join(".").gsub(
|
55
|
+
".#{@namespace}.#{@client.prefix}", ""
|
56
|
+
) })
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def delete
|
61
|
+
to_remove = []
|
62
|
+
results = []
|
63
|
+
records.each { |record| to_remove << get_hostname(record) }
|
64
|
+
zone_name = "#{@namespace}./#{@client.prefix}".split(".").reverse.join("/")
|
65
|
+
to_remove << zone_name
|
66
|
+
to_remove.each do |name|
|
67
|
+
results << remove(name)
|
68
|
+
end
|
69
|
+
results
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def get_hostname(record)
|
75
|
+
"#{record["name"]}.#{@namespace}./#{@client.prefix}".split(".").reverse.join("/")
|
76
|
+
end
|
77
|
+
|
78
|
+
def put(key, value)
|
79
|
+
raise ArgumentError, "Unsupported values keys" unless allowed_values?(value)
|
80
|
+
|
81
|
+
key = "/#{@client.prefix}/#{key.split(".").reverse.join("/")}"
|
82
|
+
payload = { key: Base64.encode64(key), value: Base64.encode64(value.to_json) }.to_json
|
83
|
+
response = CoreDns::Helpers::RequestHelper.request("#{@client.api_url}/kv/put", :post, {}, payload)
|
84
|
+
if response.code == 200
|
85
|
+
payload
|
86
|
+
else
|
87
|
+
response.code
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/etcd/domain.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "logger"
|
4
|
+
require_relative "../helpers/hash_with_indifferent_access_custom"
|
5
|
+
require_relative "../helpers/request_helper"
|
6
|
+
|
7
|
+
module CoreDns
|
8
|
+
class Etcd
|
9
|
+
class Domain < CoreDns::Domain
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
VALUES_WHITELIST = %w[host mail port priority text ttl group].freeze
|
13
|
+
|
14
|
+
def delete(data = {})
|
15
|
+
data = HashWithIndifferentAccessCustom.new(data).attributes
|
16
|
+
hostname = nil
|
17
|
+
if data[:name]
|
18
|
+
hostname = "#{data.delete(:name)}.#{@namespace}./#{@client.prefix}".split(".").reverse.join("/")
|
19
|
+
elsif data[:host]
|
20
|
+
hostname = list.select { |record| record["host"] == data[:host] }[0]["hostname"]
|
21
|
+
end
|
22
|
+
remove(hostname) if hostname
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(hostname)
|
26
|
+
self.class.new(@client, "#{hostname}.#{@namespace}")
|
27
|
+
end
|
28
|
+
|
29
|
+
def add(data = {})
|
30
|
+
put(@namespace, HashWithIndifferentAccessCustom.new(data).attributes)
|
31
|
+
end
|
32
|
+
|
33
|
+
def show
|
34
|
+
self.class.new(@client, "").list_all.select { |x| x["name"] == namespace }[0]
|
35
|
+
end
|
36
|
+
|
37
|
+
def list(level = 1)
|
38
|
+
one_level_records(level).map do |record|
|
39
|
+
hostname = record.delete("hostname")
|
40
|
+
name = format_name hostname.split("/").reverse.reject(&:empty?).join(".")
|
41
|
+
record.merge!({ "name" => name })
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def one_level_records(level)
|
46
|
+
fetch("").each do |record|
|
47
|
+
levels_difference = (record["hostname"].sub("/#{@client.prefix}/#{@namespace.split(".").reverse.join("/")}",
|
48
|
+
"")).split("/")[1..]
|
49
|
+
next unless levels_difference.count == level
|
50
|
+
|
51
|
+
record
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def list_all
|
56
|
+
fetch("").map do |record|
|
57
|
+
hostname = record.delete("hostname")
|
58
|
+
name = format_name hostname.split("/").reverse.reject(&:empty?).join(".")
|
59
|
+
record.merge!({ "name" => name })
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def format_name(name)
|
66
|
+
[@namespace, @client.prefix].reject(&:empty?).each { |part| name.gsub!(".#{part}", "") }
|
67
|
+
name
|
68
|
+
end
|
69
|
+
|
70
|
+
def allowed_values?(data)
|
71
|
+
(data.keys - VALUES_WHITELIST).empty? ? false : true
|
72
|
+
end
|
73
|
+
|
74
|
+
def generate_postfix
|
75
|
+
postfixes = list.collect { |record| record["name"] }.map { |key| key.split("/").last }
|
76
|
+
if postfixes.empty?
|
77
|
+
"#{@client.postfix}1"
|
78
|
+
else
|
79
|
+
((1..postfixes.count + 1).map { |i| "#{@client.postfix}#{i + 1}" } - postfixes).first
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def fetch(hostname)
|
84
|
+
hostname = [hostname, @namespace.split(".")].flatten.compact.join(".")[1..] unless @namespace == hostname
|
85
|
+
payload = {
|
86
|
+
key: Base64.encode64("/#{@client.prefix}/#{hostname.split(".").reverse.join("/")}/"),
|
87
|
+
range_end: Base64.encode64("/#{@client.prefix}/#{hostname.split(".").reverse.join("/")}~")
|
88
|
+
}.to_json
|
89
|
+
response = CoreDns::Helpers::RequestHelper.request("#{@client.api_url}/kv/range", :post, {}, payload)
|
90
|
+
parsed_response(response)
|
91
|
+
rescue StandardError
|
92
|
+
[]
|
93
|
+
end
|
94
|
+
|
95
|
+
def parsed_response(response)
|
96
|
+
JSON.parse(response)["kvs"]
|
97
|
+
.map do |encoded_hash|
|
98
|
+
[{ "hostname" => Base64.decode64(encoded_hash["key"]) }, JSON.parse(Base64.decode64(encoded_hash["value"]))]
|
99
|
+
.reduce(
|
100
|
+
{}, :merge
|
101
|
+
)
|
102
|
+
rescue StandardError
|
103
|
+
next
|
104
|
+
end.compact
|
105
|
+
end
|
106
|
+
|
107
|
+
def put(key, value)
|
108
|
+
raise ArgumentError, "Unsupported values keys" unless allowed_values?(value)
|
109
|
+
|
110
|
+
# [:txt].include?(record_type(value)) ? postfix = nil : postfix = "/#{generate_postfix}"
|
111
|
+
postfix = "/#{generate_postfix}"
|
112
|
+
key = "/#{@client.prefix}/#{key.split(".").reverse.join("/")}#{postfix}"
|
113
|
+
payload = { key: Base64.encode64(key), value: Base64.encode64(value.to_json) }.to_json
|
114
|
+
response = CoreDns::Helpers::RequestHelper.request("#{@client.api_url}/kv/put", :post, {}, payload)
|
115
|
+
if response.code == 200
|
116
|
+
payload
|
117
|
+
else
|
118
|
+
response.code
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def remove(hostname)
|
123
|
+
payload = {
|
124
|
+
key: Base64.encode64(hostname)
|
125
|
+
}.to_json
|
126
|
+
response = CoreDns::Helpers::RequestHelper.request("#{@client.api_url}/kv/deleterange", :post, {}, payload)
|
127
|
+
response.code == 200 ? hostname : response.code
|
128
|
+
rescue StandardError => e
|
129
|
+
@logger.error(e.message)
|
130
|
+
end
|
131
|
+
|
132
|
+
def record_type(record)
|
133
|
+
require "resolv"
|
134
|
+
if record.keys.include?("text") then :txt
|
135
|
+
elsif record.keys.include?("mail") then :mx
|
136
|
+
elsif record.keys.include?("port") then :srv
|
137
|
+
elsif Resolv::IPv4::Regex.match?(record["host"]) then :a
|
138
|
+
elsif Resolv::IPv6::Regex.match?(record["host"]) then :aaaa
|
139
|
+
elsif record["name"].match?(/ns.dns/) then :ns
|
140
|
+
else
|
141
|
+
:cname end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
data/lib/etcd.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
require "json"
|
5
|
+
require "logger"
|
6
|
+
|
7
|
+
class CoreDns::Etcd
|
8
|
+
attr_reader :api_url, :prefix, :postfix
|
9
|
+
|
10
|
+
def initialize(url = nil)
|
11
|
+
@logger = Logger.new($stdout)
|
12
|
+
|
13
|
+
initialize_params
|
14
|
+
@api_url = "http://#{url}:#{@port}/#{@version}"
|
15
|
+
@endpoint = url
|
16
|
+
end
|
17
|
+
|
18
|
+
def domain(hostname)
|
19
|
+
CoreDns::Etcd::Domain.new(self, hostname)
|
20
|
+
end
|
21
|
+
|
22
|
+
def zone(hostname)
|
23
|
+
CoreDns::Etcd::DnsZone.new(self, hostname)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def initialize_params
|
29
|
+
@prefix = ENV["COREDNS_PREFIX"] || "skydns"
|
30
|
+
@postfix = ENV["COREDNS_POSTFIX"] || "x"
|
31
|
+
@port = ENV["COREDNS_PORT"] || 2379
|
32
|
+
@version = ENV["COREDNS_VERSION"] || "v3"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class HashWithIndifferentAccessCustom
|
4
|
+
attr_reader :keys, :values, :attributes
|
5
|
+
|
6
|
+
def initialize(hash = {})
|
7
|
+
@attributes = {}
|
8
|
+
hash.each { |key, val| self[key] = val }
|
9
|
+
@keys = @attributes.keys
|
10
|
+
@values = @attributes.values
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(key, value)
|
14
|
+
@attributes[key.to_sym] = value
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
@attributes[key.to_sym]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.[](*arr)
|
22
|
+
new(Hash[*arr])
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete(key)
|
26
|
+
@attributes.delete(key)
|
27
|
+
self.class.new(@attributes)
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_json(*_args)
|
31
|
+
@attributes.to_json
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rest_client"
|
4
|
+
module CoreDns
|
5
|
+
module Helpers
|
6
|
+
class RequestHelper
|
7
|
+
def self.request(url, method, params = {}, payload = {})
|
8
|
+
params = { url: url, method: method, headers: params }
|
9
|
+
params[:payload] = payload if %i[post put].include?(method)
|
10
|
+
params[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE
|
11
|
+
RestClient::Request.execute(params)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coredns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aliaksei Hrechushkin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-06-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem which provide simple way to control DNS records.
|
14
|
+
email:
|
15
|
+
- ahrechushkin@ibagoup.eu
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".gitlab-ci.yml"
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- CODE_OF_CONDUCT.md
|
26
|
+
- Gemfile
|
27
|
+
- Gemfile.lock
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- coredns.gemspec
|
33
|
+
- lib/coredns.rb
|
34
|
+
- lib/domain.rb
|
35
|
+
- lib/etcd.rb
|
36
|
+
- lib/etcd/dns_zone.rb
|
37
|
+
- lib/etcd/domain.rb
|
38
|
+
- lib/helpers/hash_with_indifferent_access_custom.rb
|
39
|
+
- lib/helpers/request_helper.rb
|
40
|
+
- lib/version.rb
|
41
|
+
homepage: https://icdc.io
|
42
|
+
licenses:
|
43
|
+
- Apache-2.0
|
44
|
+
metadata:
|
45
|
+
homepage_uri: https://icdc.io
|
46
|
+
source_code_uri: https://github.com/icdc-io/coredns
|
47
|
+
changelog_uri: https://github.com/icdc-io/coredns/CHANGELOG.md
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '3.0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.2.33
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Wrapper for coredns-etcd application
|
67
|
+
test_files: []
|