{"action":"created","comment":{"url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/comments/209708006","pull_request_review_id":145774449,"id":209708006,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIwOTcwODAwNg==","diff_hunk":"@@ -0,0 +1,145 @@\n+require 'puppetserver/ca/utils'\n+require 'puppetserver/utils/http_utilities'\n+require 'puppetserver/utils/file_utilities'\n+require 'puppetserver/ca/puppet_config'\n+require 'optparse'\n+require 'json'\n+\n+module Puppetserver\n+  module Ca\n+    class ListAction\n+\n+      include Puppetserver::Utils\n+\n+      SUMMARY = 'List all certificate requests'\n+      BANNER = <<-BANNER\n+Usage:\n+  puppetserver ca list [--help|--version]\n+  puppetserver ca list [--config]\n+  puppetserver ca list [--all]\n+\n+Description:\n+List outstanding certificate requests. If --all is specified, signed and revoked certificates will be listed as well.\n+\n+Options:\n+      BANNER\n+\n+      BODY = JSON.dump({desired_state: 'signed'})\n+\n+      def initialize(logger)\n+        @logger = logger\n+      end\n+\n+      def self.parser(parsed = {})\n+        OptionParser.new do |opts|\n+          opts.banner = BANNER\n+          opts.on('--config CONF', 'Custom path to Puppet\\'s config file') do |conf|\n+            parsed['config'] = conf\n+          end\n+          opts.on('--help', 'Display this command specific help output') do |help|\n+            parsed['help'] = true\n+          end\n+          opts.on('--all', 'List all certificates') do |a|\n+            parsed['all'] = true\n+          end\n+        end\n+      end\n+\n+      def run(input)\n+        config = input['config']\n+\n+        if config\n+          errors = FileUtilities.validate_file_paths(config)\n+          return 1 if Utils.handle_errors(@logger, errors)\n+        end\n+\n+        puppet = PuppetConfig.parse(config)\n+        return 1 if Utils.handle_errors(@logger, puppet.errors)\n+\n+        all_certs = get_all_certs(puppet.settings)\n+        return 1 if all_certs.nil?\n+\n+        requested, signed, revoked = separate_certs(all_certs)\n+        input['all'] ? human_readable(requested, signed, revoked) : human_readable(requested)\n+\n+        return 0\n+      end\n+\n+      def human_readable(requested, signed = [], revoked = [])\n+\n+        if revoked.empty? && signed.empty? && requested.empty?\n+          @logger.inform \"No certificates to list\"\n+          return\n+        end\n+\n+        unless requested.empty?\n+          @logger.inform \"Requested Certificates:\"\n+          print_output(requested)\n+        end\n+\n+        unless signed.empty?\n+          @logger.inform \"Signed Certificates:\"\n+          print_output(signed)\n+        end\n+\n+        unless revoked.empty?\n+          @logger.inform \"Revoked Certificates:\"\n+          print_output(revoked)\n+        end\n+      end\n+\n+      def print_output(certs)\n+        padded = 0\n+        certs.each do |cert|\n+          cert_size = cert[\"name\"].size\n+          padded = cert_size if cert_size > padded\n+        end\n+\n+        certs.each do |cert|\n+          @logger.inform \"    #{cert[\"name\"]}\".ljust(padded + 6) + \" (SHA256) #{cert[\"fingerprints\"][\"SHA256\"]}\" +\n+                             (cert[\"dns_alt_names\"].empty? ? \"\" : \"\\talt names: #{cert[\"dns_alt_names\"]}\")\n+          end\n+      end\n+\n+      def get_certificate_statuses(settings)\n+        url = HttpUtilities.make_ca_url(settings[:ca_server], settings[:ca_port],\n+                                        'certificate_statuses', 'any_key')\n+        HttpUtilities.with_connection(url, settings) do |connection|\n+          connection.get(url)\n+        end\n+      end\n+\n+      def separate_certs(all_certs)\n+        requested = all_certs.find_all { |v| v[\"state\"] == \"requested\" }","path":"lib/puppetserver/ca/list_action.rb","position":113,"original_position":113,"commit_id":"defa78804c90855850b590c38cb6a8f49de46068","original_commit_id":"defa78804c90855850b590c38cb6a8f49de46068","user":{"login":"lazartea","id":22064526,"node_id":"MDQ6VXNlcjIyMDY0NTI2","avatar_url":"https://avatars2.githubusercontent.com/u/22064526?v=4","gravatar_id":"","url":"https://api.github.com/users/lazartea","html_url":"https://github.com/lazartea","followers_url":"https://api.github.com/users/lazartea/followers","following_url":"https://api.github.com/users/lazartea/following{/other_user}","gists_url":"https://api.github.com/users/lazartea/gists{/gist_id}","starred_url":"https://api.github.com/users/lazartea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lazartea/subscriptions","organizations_url":"https://api.github.com/users/lazartea/orgs","repos_url":"https://api.github.com/users/lazartea/repos","events_url":"https://api.github.com/users/lazartea/events{/privacy}","received_events_url":"https://api.github.com/users/lazartea/received_events","type":"User","site_admin":false},"body":"So when I do ``requested = all_certs.find_all{|v| v[\"state\"] == \"requested\"}`` and there aren't any, I get ``requested = []``. But if I do ``certs = all_certs.group_by{|v| v[\"state\"]}`` and then ``requested = certs[\"requested\"]`` if there aren't any, I get ``requested = nil``, which breaks everything. Am I doing something wrong?","created_at":"2018-08-13T18:16:35Z","updated_at":"2018-08-13T18:16:36Z","html_url":"https://github.com/puppetlabs/puppetserver-ca-cli/pull/12#discussion_r209708006","pull_request_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/comments/209708006"},"html":{"href":"https://github.com/puppetlabs/puppetserver-ca-cli/pull/12#discussion_r209708006"},"pull_request":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12"}},"in_reply_to_id":209402687},"pull_request":{"url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12","id":207712140,"node_id":"MDExOlB1bGxSZXF1ZXN0MjA3NzEyMTQw","html_url":"https://github.com/puppetlabs/puppetserver-ca-cli/pull/12","diff_url":"https://github.com/puppetlabs/puppetserver-ca-cli/pull/12.diff","patch_url":"https://github.com/puppetlabs/puppetserver-ca-cli/pull/12.patch","issue_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues/12","number":12,"state":"open","locked":false,"title":"(SERVER-2260) Add list option","user":{"login":"lazartea","id":22064526,"node_id":"MDQ6VXNlcjIyMDY0NTI2","avatar_url":"https://avatars2.githubusercontent.com/u/22064526?v=4","gravatar_id":"","url":"https://api.github.com/users/lazartea","html_url":"https://github.com/lazartea","followers_url":"https://api.github.com/users/lazartea/followers","following_url":"https://api.github.com/users/lazartea/following{/other_user}","gists_url":"https://api.github.com/users/lazartea/gists{/gist_id}","starred_url":"https://api.github.com/users/lazartea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lazartea/subscriptions","organizations_url":"https://api.github.com/users/lazartea/orgs","repos_url":"https://api.github.com/users/lazartea/repos","events_url":"https://api.github.com/users/lazartea/events{/privacy}","received_events_url":"https://api.github.com/users/lazartea/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-08-10T20:16:14Z","updated_at":"2018-08-13T18:16:36Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3e2cf01843001b1f30bcd9f89324b364bd18da35","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12/commits","review_comments_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12/comments","review_comment_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/comments{/number}","comments_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues/12/comments","statuses_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/statuses/defa78804c90855850b590c38cb6a8f49de46068","head":{"label":"lazartea:list_command","ref":"list_command","sha":"defa78804c90855850b590c38cb6a8f49de46068","user":{"login":"lazartea","id":22064526,"node_id":"MDQ6VXNlcjIyMDY0NTI2","avatar_url":"https://avatars2.githubusercontent.com/u/22064526?v=4","gravatar_id":"","url":"https://api.github.com/users/lazartea","html_url":"https://github.com/lazartea","followers_url":"https://api.github.com/users/lazartea/followers","following_url":"https://api.github.com/users/lazartea/following{/other_user}","gists_url":"https://api.github.com/users/lazartea/gists{/gist_id}","starred_url":"https://api.github.com/users/lazartea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lazartea/subscriptions","organizations_url":"https://api.github.com/users/lazartea/orgs","repos_url":"https://api.github.com/users/lazartea/repos","events_url":"https://api.github.com/users/lazartea/events{/privacy}","received_events_url":"https://api.github.com/users/lazartea/received_events","type":"User","site_admin":false},"repo":{"id":142041612,"node_id":"MDEwOlJlcG9zaXRvcnkxNDIwNDE2MTI=","name":"puppetserver-ca-cli","full_name":"lazartea/puppetserver-ca-cli","owner":{"login":"lazartea","id":22064526,"node_id":"MDQ6VXNlcjIyMDY0NTI2","avatar_url":"https://avatars2.githubusercontent.com/u/22064526?v=4","gravatar_id":"","url":"https://api.github.com/users/lazartea","html_url":"https://github.com/lazartea","followers_url":"https://api.github.com/users/lazartea/followers","following_url":"https://api.github.com/users/lazartea/following{/other_user}","gists_url":"https://api.github.com/users/lazartea/gists{/gist_id}","starred_url":"https://api.github.com/users/lazartea/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lazartea/subscriptions","organizations_url":"https://api.github.com/users/lazartea/orgs","repos_url":"https://api.github.com/users/lazartea/repos","events_url":"https://api.github.com/users/lazartea/events{/privacy}","received_events_url":"https://api.github.com/users/lazartea/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lazartea/puppetserver-ca-cli","description":"A simple Ruby CLI tool to interact with the Puppet Server's included Certificate Authority","fork":true,"url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli","forks_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/forks","keys_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/teams","hooks_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/hooks","issue_events_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/issues/events{/number}","events_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/events","assignees_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/assignees{/user}","branches_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/branches{/branch}","tags_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/tags","blobs_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/statuses/{sha}","languages_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/languages","stargazers_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/stargazers","contributors_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/contributors","subscribers_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/subscribers","subscription_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/subscription","commits_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/contents/{+path}","compare_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/merges","archive_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/downloads","issues_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/issues{/number}","pulls_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/pulls{/number}","milestones_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/milestones{/number}","notifications_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/labels{/name}","releases_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/releases{/id}","deployments_url":"https://api.github.com/repos/lazartea/puppetserver-ca-cli/deployments","created_at":"2018-07-23T16:39:06Z","updated_at":"2018-07-23T16:39:08Z","pushed_at":"2018-08-10T22:48:38Z","git_url":"git://github.com/lazartea/puppetserver-ca-cli.git","ssh_url":"git@github.com:lazartea/puppetserver-ca-cli.git","clone_url":"https://github.com/lazartea/puppetserver-ca-cli.git","svn_url":"https://github.com/lazartea/puppetserver-ca-cli","homepage":null,"size":119,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"puppetlabs:master","ref":"master","sha":"3c265ba445f1ad989b337f8aa4c372d48fa4b66b","user":{"login":"puppetlabs","id":234268,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIzNDI2OA==","avatar_url":"https://avatars0.githubusercontent.com/u/234268?v=4","gravatar_id":"","url":"https://api.github.com/users/puppetlabs","html_url":"https://github.com/puppetlabs","followers_url":"https://api.github.com/users/puppetlabs/followers","following_url":"https://api.github.com/users/puppetlabs/following{/other_user}","gists_url":"https://api.github.com/users/puppetlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/puppetlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puppetlabs/subscriptions","organizations_url":"https://api.github.com/users/puppetlabs/orgs","repos_url":"https://api.github.com/users/puppetlabs/repos","events_url":"https://api.github.com/users/puppetlabs/events{/privacy}","received_events_url":"https://api.github.com/users/puppetlabs/received_events","type":"Organization","site_admin":false},"repo":{"id":135350068,"node_id":"MDEwOlJlcG9zaXRvcnkxMzUzNTAwNjg=","name":"puppetserver-ca-cli","full_name":"puppetlabs/puppetserver-ca-cli","owner":{"login":"puppetlabs","id":234268,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIzNDI2OA==","avatar_url":"https://avatars0.githubusercontent.com/u/234268?v=4","gravatar_id":"","url":"https://api.github.com/users/puppetlabs","html_url":"https://github.com/puppetlabs","followers_url":"https://api.github.com/users/puppetlabs/followers","following_url":"https://api.github.com/users/puppetlabs/following{/other_user}","gists_url":"https://api.github.com/users/puppetlabs/gists{/gist_id}","starred_url":"https://api.github.com/users/puppetlabs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/puppetlabs/subscriptions","organizations_url":"https://api.github.com/users/puppetlabs/orgs","repos_url":"https://api.github.com/users/puppetlabs/repos","events_url":"https://api.github.com/users/puppetlabs/events{/privacy}","received_events_url":"https://api.github.com/users/puppetlabs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/puppetlabs/puppetserver-ca-cli","description":"A simple Ruby CLI tool to interact with the Puppet Server's included Certificate Authority","fork":false,"url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli","forks_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/forks","keys_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/teams","hooks_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/hooks","issue_events_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues/events{/number}","events_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/events","assignees_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/assignees{/user}","branches_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/branches{/branch}","tags_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/tags","blobs_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/statuses/{sha}","languages_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/languages","stargazers_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/stargazers","contributors_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/contributors","subscribers_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/subscribers","subscription_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/subscription","commits_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/contents/{+path}","compare_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/merges","archive_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/downloads","issues_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues{/number}","pulls_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls{/number}","milestones_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/milestones{/number}","notifications_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/labels{/name}","releases_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/releases{/id}","deployments_url":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/deployments","created_at":"2018-05-29T20:35:19Z","updated_at":"2018-08-13T17:25:29Z","pushed_at":"2018-08-13T18:05:08Z","git_url":"git://github.com/puppetlabs/puppetserver-ca-cli.git","ssh_url":"git@github.com:puppetlabs/puppetserver-ca-cli.git","clone_url":"https://github.com/puppetlabs/puppetserver-ca-cli.git","svn_url":"https://github.com/puppetlabs/puppetserver-ca-cli","homepage":null,"size":118,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4,"mirror_url":null,"archived":false,"open_issues_count":4,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":4,"open_issues":4,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12"},"html":{"href":"https://github.com/puppetlabs/puppetserver-ca-cli/pull/12"},"issue":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues/12"},"comments":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/issues/12/comments"},"review_comments":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12/comments"},"review_comment":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/pulls/12/commits"},"statuses":{"href":"https://api.github.com/repos/puppetlabs/puppetserver-ca-cli/statuses/defa78804c90855850b590c38cb6a8f49de46068"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"lazartea"}}