digicert-cli 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +3 -0
  3. data/.rubocop.yml +637 -0
  4. data/.travis.yml +0 -1
  5. data/Gemfile +0 -2
  6. data/LICENSE.txt +21 -0
  7. data/README.md +218 -21
  8. data/bin/digicert +1 -2
  9. data/digicert-cli.gemspec +3 -12
  10. data/lib/digicert/cli.rb +33 -9
  11. data/lib/digicert/cli/auth.rb +6 -3
  12. data/lib/digicert/cli/base.rb +19 -0
  13. data/lib/digicert/cli/certificate.rb +75 -0
  14. data/lib/digicert/cli/certificate_downloader.rb +51 -0
  15. data/lib/digicert/cli/command.rb +14 -48
  16. data/lib/digicert/cli/commands/certificate.rb +37 -0
  17. data/lib/digicert/cli/commands/csr.rb +31 -0
  18. data/lib/digicert/cli/commands/order.rb +45 -0
  19. data/lib/digicert/cli/csr.rb +48 -0
  20. data/lib/digicert/cli/filter_builder.rb +54 -0
  21. data/lib/digicert/cli/order.rb +9 -23
  22. data/lib/digicert/cli/order_reissuer.rb +60 -11
  23. data/lib/digicert/cli/order_retriever.rb +48 -0
  24. data/lib/digicert/cli/rcfile.rb +48 -0
  25. data/lib/digicert/cli/util.rb +5 -1
  26. data/lib/digicert/cli/version.rb +23 -1
  27. data/spec/acceptance/certificate_spec.rb +66 -0
  28. data/spec/acceptance/config_spec.rb +16 -0
  29. data/spec/acceptance/csr_spec.rb +51 -0
  30. data/spec/acceptance/order_spec.rb +6 -21
  31. data/spec/acceptance/reissuing_order_spec.rb +33 -0
  32. data/spec/digicert/cli/certificate_downloader_spec.rb +30 -0
  33. data/spec/digicert/cli/certificate_spec.rb +104 -0
  34. data/spec/digicert/cli/csr_spec.rb +47 -0
  35. data/spec/digicert/cli/filter_builder_spec.rb +25 -0
  36. data/spec/digicert/cli/order_reissuer_spec.rb +74 -0
  37. data/spec/digicert/cli/order_retriever_spec.rb +23 -0
  38. data/spec/digicert/{order_spec.rb → cli/order_spec.rb} +2 -2
  39. data/spec/digicert/cli/rcfile_spec.rb +18 -0
  40. data/spec/digicert/cli_spec.rb +6 -5
  41. data/spec/fixtures/.digicertrc +2 -0
  42. data/spec/fixtures/rsa4096.csr +51 -0
  43. data/spec/fixtures/rsa4096.key +51 -0
  44. data/spec/spec_helper.rb +8 -1
  45. data/spec/support/disable-logging.rb +12 -0
  46. metadata +70 -22
  47. data/.sample.env +0 -1
  48. data/bin/console +0 -14
  49. data/legacy_cli.sh +0 -301
  50. data/lib/digicert/cli/command/order.rb +0 -50
  51. data/lib/digicert/cli/order_filterer.rb +0 -43
  52. data/spec/digicert/command_spec.rb +0 -16
  53. data/spec/digicert/order_filterer_spec.rb +0 -50
  54. data/spec/digicert/order_reissuer_spec.rb +0 -19
@@ -0,0 +1,48 @@
1
+ require "date"
2
+
3
+ module Digicert
4
+ module CLI
5
+ class OrderRetriever
6
+ def initialize(order_id, attributes)
7
+ @order_id = order_id
8
+ @wait_time = attributes[:wait_time] || 10
9
+ @number_of_times = attributes[:number_of_times] || 5
10
+ end
11
+
12
+ def fetch
13
+ fetch_order_in_interval
14
+ reissued_order
15
+ end
16
+
17
+ def self.fetch(order_id, attributes)
18
+ new(order_id, **attributes).fetch
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :order_id, :number_of_times, :wait_time, :reissued_order
24
+
25
+ def fetch_order_in_interval
26
+ number_of_times.to_i.times do |number|
27
+ sleep wait_time.to_i
28
+ say("Fetch attempt #{number + 1}..")
29
+ order = Digicert::Order.fetch(order_id)
30
+
31
+ if recently_reissued?(order.last_reissued_date)
32
+ break @reissued_order = order
33
+ end
34
+ end
35
+ end
36
+
37
+ def recently_reissued?(datetime)
38
+ if datetime
39
+ ((Time.now - DateTime.parse(datetime).to_time) / 60).ceil < 3
40
+ end
41
+ end
42
+
43
+ def say(message)
44
+ Digicert::CLI::Util.say(message)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ require "yaml"
2
+ require "singleton"
3
+
4
+ module Digicert
5
+ module CLI
6
+ class RCFile
7
+ include Singleton
8
+ attr_reader :path, :data
9
+ FILE_NAME = ".digicertrc".freeze
10
+
11
+ def initialize
12
+ @path = File.join(File.expand_path("~"), FILE_NAME)
13
+ @data = load_configuration
14
+ end
15
+
16
+ def set_key(api_key)
17
+ data[:api_key] = api_key
18
+ write_api_key_to_file
19
+ end
20
+
21
+ def self.api_key
22
+ new.data[:api_key]
23
+ end
24
+
25
+ def self.set_key(api_key)
26
+ new.set_key(api_key)
27
+ end
28
+
29
+ private
30
+
31
+ def load_configuration
32
+ YAML.load_file(path)
33
+ rescue Errno::ENOENT
34
+ default_configuration
35
+ end
36
+
37
+ def default_configuration
38
+ { api_key: nil }
39
+ end
40
+
41
+ def write_api_key_to_file
42
+ File.open(path, File::RDWR | File::TRUNC | File::CREAT, 0o0600) do |rc|
43
+ rc.write(data.to_yaml)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -3,7 +3,7 @@ require "terminal-table"
3
3
  module Digicert
4
4
  module CLI
5
5
  module Util
6
- def self.make_it_pretty(headings:, rows:, table_wdith: 80)
6
+ def self.make_it_pretty(headings:, rows:, table_wdith: 100)
7
7
  Terminal::Table.new do |table|
8
8
  table.headings = headings
9
9
  table.style = { width: table_wdith }
@@ -11,6 +11,10 @@ module Digicert
11
11
  table.rows = rows
12
12
  end
13
13
  end
14
+
15
+ def self.say(message, color = nil)
16
+ Thor::Shell::Basic.new.say(message, color)
17
+ end
14
18
  end
15
19
  end
16
20
  end
@@ -1,5 +1,27 @@
1
+ #--
2
+ # Copyright (c) 2017 Ribose Inc.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ # ++
22
+
1
23
  module Digicert
2
24
  module CLI
3
- VERSION = "0.1.0"
25
+ VERSION = "0.2.0".freeze
4
26
  end
5
27
  end
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Certificate" do
4
+ describe "fetch a certificate" do
5
+ it "returns certificate details for an order" do
6
+ command = %w(certificate fetch 123456 --quiet)
7
+ allow(certificate_klass).to receive_message_chain(:new, :fetch)
8
+
9
+ Digicert::CLI.start(command)
10
+
11
+ expect(certificate_klass.new).to have_received(:fetch)
12
+ expect(certificate_klass).to have_received(:new).with(
13
+ order_id: "123456", quiet: true,
14
+ )
15
+ end
16
+ end
17
+
18
+ describe "downloading a certificate" do
19
+ it "downloads the certificate to provided path" do
20
+ command = %w(certificate fetch 123456 --output /tmp/downloads)
21
+ allow(Digicert::CLI::Certificate).to receive_message_chain(:new, :fetch)
22
+
23
+ Digicert::CLI.start(command)
24
+
25
+ expect(Digicert::CLI::Certificate).to have_received(:new).with(
26
+ order_id: "123456", output: "/tmp/downloads"
27
+ )
28
+ end
29
+ end
30
+
31
+ describe "listing duplicate certificates" do
32
+ it "returns the list of duplicate certificates" do
33
+ command = %w(certificate duplicates 123456)
34
+
35
+ allow(
36
+ Digicert::CLI::Certificate,
37
+ ).to receive_message_chain(:new, :duplicates)
38
+
39
+ Digicert::CLI.start(command)
40
+
41
+ expect(
42
+ Digicert::CLI::Certificate,
43
+ ).to have_received(:new).with(order_id: "123456")
44
+ end
45
+ end
46
+
47
+ describe "downloading a certificate" do
48
+ it "downloads the certificate to output path" do
49
+ command = %w(certificate download --certificate_id 123 --output /tmp)
50
+
51
+ allow(
52
+ Digicert::CLI::Certificate,
53
+ ).to receive_message_chain(:new, :download)
54
+
55
+ Digicert::CLI.start(command)
56
+
57
+ expect(
58
+ Digicert::CLI::Certificate,
59
+ ).to have_received(:new).with(certificate_id: "123", output: "/tmp")
60
+ end
61
+ end
62
+
63
+ def certificate_klass
64
+ Digicert::CLI::Certificate
65
+ end
66
+ end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Config" do
4
+ describe "configuring key" do
5
+ it "stores the provided api key" do
6
+ command = %w(config DIGICERT_SECRET_KEY)
7
+ allow(Digicert::CLI::RCFile).to receive(:set_key)
8
+
9
+ Digicert::CLI.start(command)
10
+
11
+ expect(
12
+ Digicert::CLI::RCFile,
13
+ ).to have_received(:set_key).with("DIGICERT_SECRET_KEY")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "CSR" do
4
+ describe "fetching a CSR" do
5
+ it "fetches the CSR for specified order" do
6
+ command = %w(csr fetch 123456)
7
+ allow(Digicert::CLI::CSR).to receive_message_chain(:new, :fetch)
8
+
9
+ Digicert::CLI.start(command)
10
+
11
+ expect(Digicert::CLI::CSR).to have_received(:new).with(order_id: "123456")
12
+ end
13
+ end
14
+
15
+ describe "generating CSR" do
16
+ context "with existing order" do
17
+ it "generates a new CSR for an existing order" do
18
+ allow(Digicert::CLI::CSR).to receive_message_chain(:new, :generate)
19
+ command = %w(csr generate -o 123456 --key ./spec/fixtures/rsa4096.key)
20
+
21
+ Digicert::CLI.start(command)
22
+
23
+ expect(Digicert::CLI::CSR).to have_received(:new).with(
24
+ order_id: "123456", key: "./spec/fixtures/rsa4096.key",
25
+ )
26
+ end end
27
+
28
+ context "with provided details" do
29
+ it "generates a new CSR with the details" do
30
+ command = %w(
31
+ csr generate
32
+ --order-id 123456
33
+ --common_name ribosetest.com
34
+ --key ./spec/fixtures/rsa4096.key
35
+ --san site1.ribosetest.com site2.ribosetest.com
36
+ )
37
+
38
+ allow(Digicert::CLI::CSR).to receive_message_chain(:new, :generate)
39
+
40
+ Digicert::CLI.start(command)
41
+
42
+ expect(Digicert::CLI::CSR).to have_received(:new).with(
43
+ order_id: "123456",
44
+ common_name: "ribosetest.com",
45
+ key: "./spec/fixtures/rsa4096.key",
46
+ san: ["site1.ribosetest.com", "site2.ribosetest.com"],
47
+ )
48
+ end
49
+ end
50
+ end
51
+ end
@@ -3,38 +3,23 @@ require "spec_helper"
3
3
  RSpec.describe "Order" do
4
4
  describe "listing orders" do
5
5
  it "retrieves the list of the orders" do
6
- command = %w(order list -c *.ribostetest.com -p ssl_wildcard)
6
+ command = %w(order list --filter common_name:*.ribostetest.com)
7
+ allow(Digicert::CLI::Order).to receive_message_chain(:new, :list)
7
8
 
8
- allow($stdout).to receive(:write)
9
- allow(Digicert::Order).to receive(:all).and_return([])
9
+ Digicert::CLI.start(command)
10
10
 
11
- Digicert::CLI.start(*command)
12
-
13
- expect(Digicert::Order).to have_received(:all)
11
+ expect(Digicert::CLI::Order.new).to have_received(:list)
14
12
  end
15
13
  end
16
14
 
17
15
  describe "finding an order" do
18
16
  it "finds a specific order based on the filters params" do
19
- command = %w(order find -c ribosetest.com -p -s expired)
17
+ command = %w(order find --filter common_name:ribosetest.com)
20
18
  allow(Digicert::CLI::Order).to receive_message_chain(:new, :find)
21
19
 
22
- Digicert::CLI.start(*command)
20
+ Digicert::CLI.start(command)
23
21
 
24
22
  expect(Digicert::CLI::Order.new).to have_received(:find)
25
23
  end
26
24
  end
27
-
28
- describe "reissuing an order" do
29
- it "reissues the order and print out the details" do
30
- command = %w(order reissue --order_id 123456)
31
- allow(Digicert::OrderReissuer).to receive(:create)
32
-
33
- Digicert::CLI.start(*command)
34
-
35
- expect(
36
- Digicert::OrderReissuer,
37
- ).to have_received(:create).with(order_id: "123456")
38
- end
39
- end
40
25
  end
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Order reissuing" do
4
+ describe "reissue an order" do
5
+ context "reissue with new csr" do
6
+ it "reissues an order with the provided csr" do
7
+ mock_digicert_order_reissuer_create_message_chain
8
+ command = %w(order reissue 123456 --crt ./spec/fixtures/rsa4096.csr)
9
+
10
+ Digicert::CLI.start(command)
11
+
12
+ expect(Digicert::CLI::OrderReissuer).to have_received(:new).
13
+ with(order_id: "123456", crt: "./spec/fixtures/rsa4096.csr")
14
+ end
15
+ end
16
+
17
+ context "reissue and download certificate" do
18
+ it "reissues an order and download the certificate" do
19
+ mock_digicert_order_reissuer_create_message_chain
20
+ command = %w(order reissue 123456 --output /tmp/downloads)
21
+
22
+ Digicert::CLI.start(command)
23
+
24
+ expect(Digicert::CLI::OrderReissuer).to have_received(:new).
25
+ with(order_id: "123456", output: "/tmp/downloads")
26
+ end
27
+ end
28
+ end
29
+
30
+ def mock_digicert_order_reissuer_create_message_chain
31
+ allow(Digicert::CLI::OrderReissuer).to receive_message_chain(:new, :create)
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+ require "digicert/cli/certificate_downloader"
3
+
4
+ RSpec.describe Digicert::CLI::CertificateDownloader do
5
+ describe ".download" do
6
+ it "downloads the certificate to the specified path" do
7
+ certificate_id = 123_456_789
8
+ allow(File).to receive(:open)
9
+ mock_digicert_certificate_download_api(certificate_id)
10
+
11
+ Digicert::CLI::CertificateDownloader.download(
12
+ print_enable: false,
13
+ path: download_path,
14
+ certificate_id: certificate_id,
15
+ )
16
+
17
+ expect(File).to have_received(:open).thrice
18
+ end
19
+ end
20
+
21
+ def download_path
22
+ File.expand_path("../../tmp", __FILE__).to_s
23
+ end
24
+
25
+ def mock_digicert_certificate_download_api(certificate_id)
26
+ stub_digicert_certificate_download_by_format(
27
+ certificate_id, "pem_all", "pem"
28
+ )
29
+ end
30
+ end
@@ -0,0 +1,104 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Digicert::CLI::Certificate do
4
+ describe "#fetch" do
5
+ context "with only order id" do
6
+ it "returns the details for the certificate" do
7
+ order_id = 123_456_789
8
+ stub_digicert_order_fetch_api(order_id)
9
+
10
+ certificate = Digicert::CLI::Certificate.new(
11
+ order_id: order_id
12
+ ).fetch
13
+
14
+ expect(certificate.id).not_to be_nil
15
+ expect(certificate.organization.id).not_to be_nil
16
+ expect(certificate.common_name).to eq("digicert.com")
17
+ end
18
+ end
19
+
20
+ context "with option attributes" do
21
+ it "returns the option attribute specified details" do
22
+ order_id = 112_358
23
+ stub_digicert_order_fetch_api(order_id)
24
+
25
+ certificate = Digicert::CLI::Certificate.new(
26
+ order_id: order_id, quiet: true
27
+ ).fetch
28
+
29
+ expect(certificate).to eq(order_id)
30
+ end
31
+ end
32
+
33
+ context "output attribute specified" do
34
+ it "sends download message to certificate downloader" do
35
+ order_id = 112_358
36
+
37
+ stub_digicert_order_fetch_api(order_id)
38
+ allow(Digicert::CLI::CertificateDownloader).to receive(:download)
39
+
40
+ Digicert::CLI::Certificate.new(
41
+ order_id: order_id, output: "/tmp/downloads",
42
+ ).fetch
43
+
44
+ expect(Digicert::CLI::CertificateDownloader).
45
+ to have_received(:download).with({
46
+ certificate_id: 112358, filename: order_id, path: "/tmp/downloads",
47
+ })
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#duplicates" do
53
+ it "lists duplicate certificates" do
54
+ order_id = 112_358
55
+ allow(Digicert::DuplicateCertificate).to receive(:all)
56
+
57
+ Digicert::CLI::Certificate.new(order_id: order_id).duplicates
58
+
59
+ expect(
60
+ Digicert::DuplicateCertificate,
61
+ ).to have_received(:all).with(order_id: order_id)
62
+ end
63
+ end
64
+
65
+ describe "#download" do
66
+ context "with certificate_id" do
67
+ it "sends downloader a download message" do
68
+ certificate_id = 123_456_789
69
+ downloader = Digicert::CLI::CertificateDownloader
70
+ allow(downloader).to receive(:download)
71
+
72
+ Digicert::CLI::Certificate.new(
73
+ certificate_id: certificate_id, output: "/tmp/downloads",
74
+ ).download
75
+
76
+ expect(downloader).to have_received(:download).with(
77
+ hash_including(
78
+ certificate_id: certificate_id, filename: certificate_id,
79
+ ),
80
+ )
81
+ end
82
+ end
83
+
84
+ context "with order_id" do
85
+ it "fetch order and sends downloader a download message" do
86
+ order_id = 123_456_789
87
+ downloader = Digicert::CLI::CertificateDownloader
88
+
89
+ allow(downloader).to receive(:download)
90
+ stub_digicert_order_fetch_api(order_id)
91
+
92
+ Digicert::CLI::Certificate.new(
93
+ order_id: order_id, output: "/tmp/downloads",
94
+ ).download
95
+
96
+ expect(downloader).to have_received(:download).with(
97
+ hash_including(
98
+ filename: order_id, path: "/tmp/downloads", certificate_id: 112358,
99
+ ),
100
+ )
101
+ end
102
+ end
103
+ end
104
+ end