lecli 0.2.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: 032b67c78b3ab17e770faf7583c173faa6cd0333
4
+ data.tar.gz: 4ed9b4cf5b0c03f913953ebc949529e5169e73c6
5
+ SHA512:
6
+ metadata.gz: 4536d4d4f0f041ed7ce8dc89db29e58f04639562ab261b4df60efdb691d4ea5b829ab2b6ebd46b20ea0f5259c947abf4a74a16f758612d0876238e6b30dfbef8
7
+ data.tar.gz: b5dd8a95a6579ef16443df3417a8887c176ea84de25b60bcd77642d96f761a7c21e22d87a67faea9272da0f3a0e63ab3d9c80331bbe7964384f9f8a3b5a6be7c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in lecli.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lecli (0.2.1)
5
+ thor (~> 0.20.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rspec (3.7.0)
13
+ rspec-core (~> 3.7.0)
14
+ rspec-expectations (~> 3.7.0)
15
+ rspec-mocks (~> 3.7.0)
16
+ rspec-core (3.7.1)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-expectations (3.7.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-mocks (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-support (3.7.1)
25
+ thor (0.20.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ lecli!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Fernando Valverde Arredondo
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,39 @@
1
+ # Lecli
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lecli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lecli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lecli
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lecli.
36
+
37
+ ## License
38
+
39
+ 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,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lecli"
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/exe/lecli ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "thor"
4
+ require "yaml"
5
+ require "lecli"
6
+
7
+ class LECLIRunner < Thor
8
+
9
+ YAML_FILENAME = ".lecli.yml"
10
+
11
+ desc "version", "Prints out the gem version"
12
+ def version
13
+ puts "v#{LECLI::VERSION}"
14
+ end
15
+
16
+ desc "yaml", "Generates the #{YAML_FILENAME} file with all default options"
17
+ option :override,
18
+ type: :boolean,
19
+ aliases: [:o],
20
+ desc: "Overrides the existing #{YAML_FILENAME} if any exist."
21
+ def yaml
22
+ if !File.file?(YAML_FILENAME) || options[:override]
23
+ hash = LECLI::CertificateBuilder.default_options_yaml
24
+ File.write(YAML_FILENAME, hash.to_yaml)
25
+ puts YAML_FILENAME
26
+ else
27
+ puts "ERROR: #{YAML_FILENAME} file already exists. Try `lecli help yaml`"
28
+ end
29
+ end
30
+
31
+ desc "generate", "Requests and outputs Let's Encrypt SSL Certificates"
32
+ option :production,
33
+ type: :boolean,
34
+ aliases: [:p],
35
+ desc: "Use Let's Encrypt production API endpoint."
36
+ def generate
37
+ begin
38
+ # Load defaults and merge with the config file data
39
+ hash = LECLI::CertificateBuilder.default_options_yaml
40
+ if File.file?(YAML_FILENAME)
41
+ hash = hash.merge(YAML.load_file(YAML_FILENAME))
42
+ end
43
+ rescue
44
+ puts "ERROR: Invalid format for file #{YAML_FILENAME}"
45
+ return 1
46
+ end
47
+
48
+ hash["production"] = options[:production]
49
+ LECLI::CertificateBuilder.generate_certs(hash)
50
+
51
+ puts "Certificates generated successfully!"
52
+
53
+ script_path = hash["success_callback_script"]
54
+ if File.file?(script_path)
55
+ puts "Execution attempt of success callback script `#{script_path}`"
56
+ `./#{script_path}`
57
+ end
58
+ end
59
+ end
60
+
61
+ LECLIRunner.start(ARGV)
data/lecli.gemspec ADDED
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lecli/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lecli"
8
+ spec.version = LECLI::VERSION
9
+ spec.authors = ["Fernando Valverde Arredondo"]
10
+ spec.email = ["fdov88@gmail.com"]
11
+
12
+ spec.summary = "Let's Encrypt CLI to generate certificates"
13
+ spec.description = "Let's Encrypt CLI to generate certificates"
14
+ spec.homepage = "https://github.com/fdoxyz/lecli"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "thor", "~> 0.20.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+
30
+ end
data/lib/lecli.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "lecli/version"
2
+ require "lecli/certificate_builder"
3
+
4
+ module LECLI
5
+ end
@@ -0,0 +1,85 @@
1
+ require "thor"
2
+
3
+ module LECLI
4
+ class CertificateBuilder
5
+
6
+ def self.default_options_yaml
7
+ {
8
+ "port" => 3333,
9
+ "domains" => [ "example.com" ],
10
+ "common_name" => "Let's Encrypt",
11
+ "account_email" => "test@account.com",
12
+ "request_key" => "request.pem",
13
+ "certificate_key" => "certificate.pem",
14
+ "challenges_relative_path" => "challenges",
15
+ "success_callback_script" => "deploy.sh"
16
+ }
17
+ end
18
+
19
+ def self.generate_certs(options)
20
+ if options["production"]
21
+ endpoint = "https://acme-v02.api.letsencrypt.org/directory"
22
+ else
23
+ endpoint = "https://acme-staging-v02.api.letsencrypt.org/directory"
24
+ end
25
+
26
+ account_pkey = OpenSSL::PKey::RSA.new(4096)
27
+ client = Acme::Client.new(private_key: account_pkey, directory: endpoint)
28
+ client.new_account(
29
+ contact: "mailto:#{options["account_email"]}",
30
+ terms_of_service_agreed: true
31
+ )
32
+ order = client.new_order(identifiers: options["domains"])
33
+
34
+ # Setup if necessary & clear challenges directory
35
+ challenges_dir = File.expand_path(options["challenges_relative_path"])
36
+ FileUtils.mkdir_p(challenges_dir)
37
+ FileUtils.rm(Dir[File.join(challenges_dir, "*")])
38
+
39
+ challenges = []
40
+ order.authorizations.each do |authorization|
41
+ challenge = authorization.http
42
+ token_path = File.join(challenges_dir, challenge.token)
43
+ File.write(token_path, challenge.file_content)
44
+ challenges << challenge
45
+ end
46
+
47
+ sleep(1)
48
+
49
+ wait_time = 5
50
+ pending = true
51
+ while pending
52
+ challenges.each do |challenge|
53
+ challenge.request_validation
54
+ end
55
+
56
+ status = challenges.map(&:status)
57
+ pending = status.include?("pending")
58
+
59
+ if pending
60
+ puts "At least one challenge still pending, waiting #{wait_time}s ..."
61
+ sleep(wait_time)
62
+
63
+ # Gradually increment wait times before retrying (max ~10min)
64
+ wait_time *= 2 if wait_time < 640
65
+ end
66
+ end
67
+ puts "Challenges are all valid now!"
68
+
69
+ request_key = OpenSSL::PKey::RSA.new(4096)
70
+ csr = Acme::Client::CertificateRequest.new(
71
+ private_key: request_key,
72
+ names: domains.values,
73
+ subject: { common_name: options["common_name"] }
74
+ )
75
+ order.finalize(csr: csr)
76
+ sleep(1) while order.status == "processing"
77
+
78
+ certificate_path = File.expand_path(options["certificate_key"])
79
+ File.write(certificate_path, order.certificate)
80
+ request_path = File.expand_path(options["request_key"])
81
+ File.write(request_path, request_key)
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module LECLI
2
+ VERSION = "0.2.1"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lecli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Valverde Arredondo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Let's Encrypt CLI to generate certificates
70
+ email:
71
+ - fdov88@gmail.com
72
+ executables:
73
+ - lecli
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - exe/lecli
88
+ - lecli.gemspec
89
+ - lib/lecli.rb
90
+ - lib/lecli/certificate_builder.rb
91
+ - lib/lecli/version.rb
92
+ homepage: https://github.com/fdoxyz/lecli
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.14
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Let's Encrypt CLI to generate certificates
116
+ test_files: []