zerossl 0.1.0

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
+ SHA256:
3
+ metadata.gz: 297b976a0e485d71411c80a4b0fc1fe64cc5731f325bdbb9ebef2ed5ee2b4297
4
+ data.tar.gz: c93377a58be0f2529c559263caa368a356edcf0c1c96ffc884c5c582c9c732cf
5
+ SHA512:
6
+ metadata.gz: d451828123c8be153fa4a63f47c56e2687673283abd4deb576d1878cb4f24e3b18c8dc11516e1fe57de902905d9480ec423753ce37f71e8424e0699a5d33d759
7
+ data.tar.gz: 1d6a9a8b5a14dc262e74b5abd9c24218069fbddce69b205550c329c88d261c911f0e1e9e0b0ded9e45b83a7c4cfedfa3d6cdb372c7dd6328f99a80529b32d5fd
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in zerossl.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zerossl (0.1.0)
5
+ dry-configurable (~> 0.11)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ concurrent-ruby (1.1.9)
11
+ dry-configurable (0.12.1)
12
+ concurrent-ruby (~> 1.0)
13
+ dry-core (~> 0.5, >= 0.5.0)
14
+ dry-core (0.7.1)
15
+ concurrent-ruby (~> 1.0)
16
+ minitest (5.14.4)
17
+ rake (12.3.3)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ minitest (~> 5.0)
24
+ rake (~> 12.0)
25
+ zerossl!
26
+
27
+ BUNDLED WITH
28
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 creadone
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # ZeroSSL
2
+
3
+ Ruby client to obtain SSL certificate from [ZeroSSL](https://zerossl.com) via [REST API](https://zerossl.com/documentation/api/)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'zerossl'
11
+ ```
12
+
13
+ And then execute:
14
+ ```ruby
15
+ $ bundle install
16
+ ```
17
+ Or install it yourself as:
18
+ ```ruby
19
+ $ gem install zerossl
20
+ ```
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'zerossl'
25
+
26
+ # Setup gem
27
+ ZeroSSL::Setup.config.access_key = '123456789'
28
+ client = ZeroSSL::Client.new
29
+
30
+ # Define domain and server html path
31
+ domain_name = 'umbrella.llc'
32
+ html_directory = '/var/www/html'
33
+
34
+ # Set Certificate Signing Request options
35
+ csr_opts = {
36
+ common_name: domain_name,
37
+ organization: 'Umbrella',
38
+ country: 'RU',
39
+ state_name: 'Moscow',
40
+ locality: 'Moscow'
41
+ }
42
+
43
+ # Build CSR
44
+ csr, key = ZeroSSL::CSR.new(csr_opts).call
45
+
46
+ # Build certificate request
47
+ request = {
48
+ certificate_domains: [domain_name],
49
+ certificate_validity_days: ZeroSSL::VALIDITY_DAYS::DAY90,
50
+ certificate_csr: csr
51
+ }
52
+
53
+ # Receive request, extract certificate id and validation details
54
+ response = client.create(request).body
55
+ certificate_id = response['id']
56
+ other_methods = response['validation']['other_methods']
57
+
58
+ # Write validation content into file to the server directory
59
+ other_methods.each do |domain_name, validation_types|
60
+ validation_uri = URI(validation_types['file_validation_url_http'])
61
+ validation_content = validation_types['file_validation_content']
62
+
63
+ File.open(File.join(html_directory, validation_uri.path), 'w') do |io|
64
+ io << validation_content.join("\n")
65
+ end
66
+ end
67
+
68
+ # Tell ZeroSSL domain are ready for validation
69
+ client.verify(certificate_id, ZeroSSL::VALIDATION_TYPE::HTTP)
70
+ #=> { [...] validation => { other_methods => { <domain> => file_validation_url_http }}}
71
+
72
+ # Check validation status
73
+ client.status(certificate_id)
74
+ #=> true
75
+
76
+ # Download Certificate (inline)
77
+ client.download(certificate_id)
78
+ #=> {
79
+ # "certificate.crt": "---BEGIN CERTIFICATE---{primary_certificate}---END CERTIFICATE---",
80
+ # "ca_bundle.crt": "---BEGIN CERTIFICATE---{certificate_bundle}---END CERTIFICATE---"
81
+ # }
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/creadone/zerossl.
93
+
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zerossl"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/zerossl.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'zerossl/version'
2
+ require 'zerossl/config'
3
+ require 'zerossl/constants'
4
+ require 'zerossl/http'
5
+ require 'zerossl/client'
6
+ require 'zerossl/csr'
7
+
8
+ # require 'zerossl/transport'
9
+
@@ -0,0 +1,42 @@
1
+ module ZeroSSL
2
+ class Client
3
+ def initialize
4
+ @config = ZeroSSL::Setup.config
5
+ @http = HTTP.new
6
+ end
7
+
8
+ def certificates
9
+ @http.get('certificates')
10
+ end
11
+
12
+ def create(opts = {})
13
+ attributes = %w[
14
+ certificate_domains
15
+ certificate_validity_days
16
+ certificate_csr
17
+ ]
18
+
19
+ options = opts.transform_keys(&:to_s).slice(*attributes)
20
+ domains = options['certificate_domains']
21
+
22
+ if domains.is_a?(Array)
23
+ options['certificate_domains'] = domains.join(',')
24
+ end
25
+ @http.post('certificates', options)
26
+ end
27
+
28
+ def verify(id, validation_method)
29
+ opts = { validation_method: validation_method }
30
+ @http.post("certificates/#{id}/challenges", opts)
31
+ end
32
+
33
+ def status(id)
34
+ !!@http.get("certificates/#{id}/status")&.body['validation_completed']
35
+ end
36
+
37
+ def download(id)
38
+ @http.get("certificates/#{id}/download/return")&.body
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+ require 'dry-configurable'
3
+
4
+ module ZeroSSL
5
+ class Setup
6
+ extend Dry::Configurable
7
+
8
+ setting :api_uri, 'https://api.zerossl.com'
9
+ setting :access_key, nil
10
+ setting :logger, Logger.new(STDOUT)
11
+ setting :user_agent, "ZeroSSL Ruby Client / #{ZeroSSL::VERSION}"
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module ZeroSSL
2
+ module VALIDATION_TYPE
3
+ EMAIL = 'EMAIL'
4
+ CNAME = 'CNAME_CSR_HASH'
5
+ HTTP = 'HTTP_CSR_HASH'
6
+ HTTPS = 'HTTPS_CSR_HASH'
7
+ end
8
+
9
+ module VALIDITY_DAYS
10
+ DAY90 = 90
11
+ DAY360 = 365
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ require 'openssl'
2
+
3
+ module ZeroSSL
4
+ class CSR
5
+ def initialize opts = {}
6
+ opts.transform_keys!(&:to_s)
7
+
8
+ @common_name = opts.dig('common_name')
9
+ @organization = opts.dig('organization')
10
+ @country = opts.dig('country')
11
+ @state_name = opts.dig('state_name')
12
+ @locality = opts.dig('locality')
13
+
14
+ @signing_key = signing_key
15
+ @subject = subject
16
+ end
17
+
18
+ def call
19
+ csr = OpenSSL::X509::Request.new
20
+ csr.version = 0
21
+ csr.subject = subject
22
+ csr.public_key = signing_key.public_key
23
+ csr.sign signing_key, OpenSSL::Digest::SHA256.new
24
+
25
+ [csr.to_s, signing_key.to_s]
26
+ end
27
+
28
+ private
29
+
30
+ def signing_key
31
+ @signing_key ||= OpenSSL::PKey::RSA.new 2048
32
+ end
33
+
34
+ def subject
35
+ @subject ||= OpenSSL::X509::Name.new [
36
+ ['CN', @common_name],
37
+ ['O', @organization],
38
+ ['C', @country],
39
+ ['ST', @state_name],
40
+ ['L', @locality]
41
+ ]
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,70 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module ZeroSSL
6
+ class HTTP
7
+
8
+ VERBS = {
9
+ get: Net::HTTP::Get,
10
+ post: Net::HTTP::Post
11
+ }
12
+
13
+ Response = Struct.new(:code, :success, :body)
14
+
15
+ def initialize
16
+ @api_uri = Setup.config.api_uri
17
+ @access_key = Setup.config.access_key
18
+ @logger = Setup.config.logger
19
+ @user_agent = Setup.config.user_agent
20
+ end
21
+
22
+ def default_headers(opts = {})
23
+ {
24
+ 'User-Agent' => @user_agent,
25
+ 'Content-Type' => 'application/json',
26
+ }.merge!(opts)
27
+ end
28
+
29
+ def default_params(opts = {})
30
+ {
31
+ 'access_key' => @access_key
32
+ }.merge!(opts)
33
+ end
34
+
35
+ def get(path, options = {})
36
+ execute(path, :get, options)
37
+ end
38
+
39
+ def post(path, options = {})
40
+ execute(path, :post, options)
41
+ end
42
+
43
+ private
44
+ def execute (path, method, options = {})
45
+ # Build URL
46
+ url = URI.join(@api_uri, path)
47
+ url.query = URI.encode_www_form(default_params.to_a)
48
+
49
+ # Build request
50
+ req = VERBS[method].new(url)
51
+ options.transform_keys!(&:to_s) unless options.empty?
52
+ default_headers.each{ |k,v| req[k] = v }
53
+ req.set_form_data(options.dig('body') || options || {})
54
+
55
+ # Execute request
56
+ ssl_conf = { :use_ssl => url.scheme == 'https' }
57
+ resp = Net::HTTP.start(url.hostname, url.port, ssl_conf) do |http|
58
+ http.request(req)
59
+ end
60
+
61
+ # Parse response
62
+ body = resp.body.empty? ? {} : JSON.parse(resp.body)
63
+ Response.new(resp.code.to_i, !body.dig('error'), body)
64
+
65
+ rescue => e
66
+ @logger.error(e.message) if @logger
67
+ raise e
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module ZeroSSL
2
+ VERSION = "0.1.0"
3
+ end
data/zerossl.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/zerossl/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "zerossl"
5
+ spec.version = ZeroSSL::VERSION
6
+ spec.authors = ["creadone"]
7
+ spec.email = ["creadone@gmail.com"]
8
+
9
+ spec.summary = %q{Ruby client to obtain SSL certificate from ZeroSSL via REST API}
10
+ spec.description = %q{Ruby client to obtain SSL certificate from ZeroSSL via REST API}
11
+ spec.homepage = "https://github.com/creadone/zerossl"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/creadone/zerossl"
17
+
18
+ spec.add_runtime_dependency 'dry-configurable', '~> 0.11'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zerossl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - creadone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.11'
27
+ description: Ruby client to obtain SSL certificate from ZeroSSL via REST API
28
+ email:
29
+ - creadone@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - lib/zerossl.rb
44
+ - lib/zerossl/client.rb
45
+ - lib/zerossl/config.rb
46
+ - lib/zerossl/constants.rb
47
+ - lib/zerossl/csr.rb
48
+ - lib/zerossl/http.rb
49
+ - lib/zerossl/version.rb
50
+ - zerossl.gemspec
51
+ homepage: https://github.com/creadone/zerossl
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/creadone/zerossl
56
+ source_code_uri: https://github.com/creadone/zerossl
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.3.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.4
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Ruby client to obtain SSL certificate from ZeroSSL via REST API
76
+ test_files: []