From 5780f5dcc024d4f140fe8f6e8dc3f7c4e905a8ec Mon Sep 17 00:00:00 2001 From: elijah Date: Wed, 29 Jun 2016 16:55:06 -0700 Subject: leap cli: move everything we can from leap_cli to leap_platform --- lib/leap_cli/util/secret.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++ lib/leap_cli/util/x509.rb | 33 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 lib/leap_cli/util/secret.rb create mode 100644 lib/leap_cli/util/x509.rb (limited to 'lib/leap_cli/util') diff --git a/lib/leap_cli/util/secret.rb b/lib/leap_cli/util/secret.rb new file mode 100644 index 00000000..749b9595 --- /dev/null +++ b/lib/leap_cli/util/secret.rb @@ -0,0 +1,55 @@ +# encoding: utf-8 +# +# A simple secret generator +# +# Uses OpenSSL random number generator instead of Ruby's rand function +# +autoload :OpenSSL, 'openssl' + +module LeapCli; module Util + class Secret + CHARS = (('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a) - "i1loO06G".split(//u) + HEX = (0..9).to_a + ('a'..'f').to_a + + # + # generate a secret with with no ambiguous characters. + # + # +length+ is in chars + # + # Only alphanumerics are allowed, in order to make these passwords work + # for REST url calls and to allow you to easily copy and paste them. + # + def self.generate(length = 16) + seed + OpenSSL::Random.random_bytes(length).bytes.to_a.collect { |byte| + CHARS[ byte % CHARS.length ] + }.join + end + + # + # generates a hex secret, instead of an alphanumeric on. + # + # length is in bits + # + def self.generate_hex(length = 128) + seed + OpenSSL::Random.random_bytes(length/4).bytes.to_a.collect { |byte| + HEX[ byte % HEX.length ] + }.join + end + + private + + def self.seed + @pid ||= 0 + pid = $$ + if @pid != pid + now = Time.now + ary = [now.to_i, now.nsec, @pid, pid] + OpenSSL::Random.seed(ary.to_s) + @pid = pid + end + end + + end +end; end diff --git a/lib/leap_cli/util/x509.rb b/lib/leap_cli/util/x509.rb new file mode 100644 index 00000000..787fdfac --- /dev/null +++ b/lib/leap_cli/util/x509.rb @@ -0,0 +1,33 @@ +autoload :OpenSSL, 'openssl' +autoload :CertificateAuthority, 'certificate_authority' + +require 'digest' +require 'digest/md5' +require 'digest/sha1' + +module LeapCli; module X509 + extend self + + # + # returns a fingerprint of a x509 certificate + # + def fingerprint(digest, cert_file) + if cert_file.is_a? String + cert = OpenSSL::X509::Certificate.new(Util.read_file!(cert_file)) + elsif cert_file.is_a? OpenSSL::X509::Certificate + cert = cert_file + elsif cert_file.is_a? CertificateAuthority::Certificate + cert = cert_file.openssl_body + end + digester = case digest + when "MD5" then Digest::MD5.new + when "SHA1" then Digest::SHA1.new + when "SHA256" then Digest::SHA256.new + when "SHA384" then Digest::SHA384.new + when "SHA512" then Digest::SHA512.new + end + digester.hexdigest(cert.to_der) + end + + +end; end -- cgit v1.2.3 From f354e8fa66e49bd989aba196488bfc12f27a92ac Mon Sep 17 00:00:00 2001 From: elijah Date: Fri, 1 Jul 2016 22:38:09 -0700 Subject: fix access to vagrant key file --- lib/leap_cli/util/vagrant.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/leap_cli/util/vagrant.rb (limited to 'lib/leap_cli/util') diff --git a/lib/leap_cli/util/vagrant.rb b/lib/leap_cli/util/vagrant.rb new file mode 100644 index 00000000..c67ea4f1 --- /dev/null +++ b/lib/leap_cli/util/vagrant.rb @@ -0,0 +1,26 @@ +require 'fileutils' + +module LeapCli + module Util + module Vagrant + + # + # returns the path to a vagrant ssh private key file. + # + # if the vagrant.key file is owned by root or ourselves, then + # we need to make sure that it owned by us and not world readable. + # + def self.vagrant_ssh_key_file + file_path = Path.vagrant_ssh_priv_key_file + Util.assert_files_exist! file_path + uid = File.new(file_path).stat.uid + if uid == 0 || uid == Process.euid + FileUtils.install file_path, '/tmp/vagrant.key', :mode => 0600 + file_path = '/tmp/vagrant.key' + end + return file_path + end + + end + end +end \ No newline at end of file -- cgit v1.2.3 From 8c207687e8dfa72f42f25cac7f46b99f895e4f57 Mon Sep 17 00:00:00 2001 From: elijah Date: Sat, 9 Jul 2016 02:47:55 -0700 Subject: refactor the command for ca and node --- lib/leap_cli/util/x509.rb | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 lib/leap_cli/util/x509.rb (limited to 'lib/leap_cli/util') diff --git a/lib/leap_cli/util/x509.rb b/lib/leap_cli/util/x509.rb deleted file mode 100644 index 787fdfac..00000000 --- a/lib/leap_cli/util/x509.rb +++ /dev/null @@ -1,33 +0,0 @@ -autoload :OpenSSL, 'openssl' -autoload :CertificateAuthority, 'certificate_authority' - -require 'digest' -require 'digest/md5' -require 'digest/sha1' - -module LeapCli; module X509 - extend self - - # - # returns a fingerprint of a x509 certificate - # - def fingerprint(digest, cert_file) - if cert_file.is_a? String - cert = OpenSSL::X509::Certificate.new(Util.read_file!(cert_file)) - elsif cert_file.is_a? OpenSSL::X509::Certificate - cert = cert_file - elsif cert_file.is_a? CertificateAuthority::Certificate - cert = cert_file.openssl_body - end - digester = case digest - when "MD5" then Digest::MD5.new - when "SHA1" then Digest::SHA1.new - when "SHA256" then Digest::SHA256.new - when "SHA384" then Digest::SHA384.new - when "SHA512" then Digest::SHA512.new - end - digester.hexdigest(cert.to_der) - end - - -end; end -- cgit v1.2.3 From 760aa6e1b0d7dd1764387c05f638c886745c55e7 Mon Sep 17 00:00:00 2001 From: elijah Date: Sat, 9 Jul 2016 02:48:27 -0700 Subject: move console table into separate file --- lib/leap_cli/util/console_table.rb | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/leap_cli/util/console_table.rb (limited to 'lib/leap_cli/util') diff --git a/lib/leap_cli/util/console_table.rb b/lib/leap_cli/util/console_table.rb new file mode 100644 index 00000000..53c5e18a --- /dev/null +++ b/lib/leap_cli/util/console_table.rb @@ -0,0 +1,55 @@ +module LeapCli; module Util + + class ConsoleTable + def table + @rows = [] + @row_options = [] + @column_widths = [] + @column_options = [] + @current_row = 0 + @current_column = 0 + yield + end + + def row(options=nil) + @current_column = 0 + @row_options[@current_row] ||= options + yield + @current_row += 1 + end + + def column(str, options={}) + str ||= "" + @rows[@current_row] ||= [] + @rows[@current_row][@current_column] = str + @column_widths[@current_column] = [str.length, options[:min_width]||0, @column_widths[@current_column]||0].max + @column_options[@current_column] ||= options + @current_column += 1 + end + + def draw_table + @rows.each_with_index do |row, i| + color = (@row_options[i]||{})[:color] + row.each_with_index do |column, j| + align = (@column_options[j]||{})[:align] || "left" + width = @column_widths[j] + if color + str = LeapCli.logger.colorize(column, color) + extra_width = str.length - column.length + else + str = column + extra_width = 0 + end + if align == "right" + printf " %#{width+extra_width}s" % str + else + printf " %-#{width+extra_width}s" % str + end + end + puts + end + puts + end + end + +end; end \ No newline at end of file -- cgit v1.2.3 From 205b61dfe721e6d88fc06b050a0497eeb35f4e02 Mon Sep 17 00:00:00 2001 From: elijah Date: Thu, 21 Jul 2016 00:55:12 -0700 Subject: added 'leap vm' command --- lib/leap_cli/util/console_table.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'lib/leap_cli/util') diff --git a/lib/leap_cli/util/console_table.rb b/lib/leap_cli/util/console_table.rb index 53c5e18a..ccdcc2ab 100644 --- a/lib/leap_cli/util/console_table.rb +++ b/lib/leap_cli/util/console_table.rb @@ -3,9 +3,12 @@ module LeapCli; module Util class ConsoleTable def table @rows = [] + @cell_options = [] + @row_options = [] @column_widths = [] @column_options = [] + @current_row = 0 @current_column = 0 yield @@ -13,6 +16,8 @@ module LeapCli; module Util def row(options=nil) @current_column = 0 + @rows[@current_row] = [] + @cell_options[@current_row] = [] @row_options[@current_row] ||= options yield @current_row += 1 @@ -20,8 +25,8 @@ module LeapCli; module Util def column(str, options={}) str ||= "" - @rows[@current_row] ||= [] @rows[@current_row][@current_column] = str + @cell_options[@current_row][@current_column] = options @column_widths[@current_column] = [str.length, options[:min_width]||0, @column_widths[@current_column]||0].max @column_options[@current_column] ||= options @current_column += 1 @@ -33,8 +38,10 @@ module LeapCli; module Util row.each_with_index do |column, j| align = (@column_options[j]||{})[:align] || "left" width = @column_widths[j] - if color - str = LeapCli.logger.colorize(column, color) + cell_color = @cell_options[i][j] && @cell_options[i][j][:color] + cell_color ||= color + if cell_color + str = LeapCli.logger.colorize(column, cell_color) extra_width = str.length - column.length else str = column -- cgit v1.2.3