You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fixture.rb 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. require 'openssl'
  2. class String
  3. alias :colorize_old :colorize
  4. COLORS = {
  5. critical: { color: :white, background: :red },
  6. error: :red,
  7. warning: :light_red,
  8. good: :green,
  9. perfect: :blue,
  10. best: :magenta,
  11. unknown: { background: :black }
  12. }
  13. def colorize(state)
  14. color = COLORS[state] || state
  15. self.colorize_old color
  16. end
  17. end
  18. class Exception
  19. BACKTRACE_REGEXP = /^(.*):(\d+):in `(.*)'$/
  20. def colorize
  21. $stderr.puts self.message.colorize(:red)
  22. self.backtrace.each do |line|
  23. line = BACKTRACE_REGEXP.match line
  24. line = '%s:%s:in `%s\'' % [
  25. line[1].colorize(:yellow),
  26. line[2].colorize(:blue),
  27. line[3].colorize(:magenta)
  28. ]
  29. $stderr.puts line
  30. end
  31. end
  32. end
  33. class Integer
  34. def humanize
  35. secs = self
  36. [[60, :second],
  37. [60, :minute],
  38. [24, :hour],
  39. [30, :day],
  40. [12, :month]].map do |count, name|
  41. if secs > 0
  42. secs, n = secs.divmod count
  43. n = n.to_i
  44. n > 0 ? "#{n} #{name}#{n > 1 ? 's' : ''}" : nil
  45. end
  46. end.compact.reverse.join ' '
  47. end
  48. end
  49. class ::OpenSSL::PKey::EC
  50. def type
  51. :ecc
  52. end
  53. def size
  54. self.group.degree
  55. end
  56. def curve
  57. self.group.curve_name
  58. end
  59. def to_s
  60. "ECC #{self.size} bits"
  61. end
  62. def status
  63. case self.size
  64. when 0...160
  65. :critical
  66. when 160...192
  67. :error
  68. when 192...256
  69. :warning
  70. when 256...364
  71. else
  72. :good
  73. end
  74. end
  75. end
  76. class ::OpenSSL::PKey::RSA
  77. def type
  78. :rsa
  79. end
  80. def size
  81. self.n.num_bits
  82. end
  83. def to_s
  84. "RSA #{self.size} bits"
  85. end
  86. def status
  87. case self.size
  88. when 0...1024
  89. :critical
  90. when 1024...2048
  91. :error
  92. when 2048...4096
  93. else
  94. :good
  95. end
  96. end
  97. end
  98. class ::OpenSSL::PKey::DSA
  99. def type
  100. :dsa
  101. end
  102. def size
  103. self.p.num_bits
  104. end
  105. def to_s
  106. "DSA #{self.size} bits"
  107. end
  108. def status
  109. return :critical
  110. end
  111. end
  112. class ::OpenSSL::PKey::DH
  113. def type
  114. :dh
  115. end
  116. def size
  117. self.p.num_bits
  118. end
  119. def to_s
  120. "DH #{self.size} bits"
  121. end
  122. def status
  123. case self.size
  124. when 0...1024
  125. :critical
  126. when 1024...2048
  127. :error
  128. when 2048...4096
  129. else
  130. :good
  131. end
  132. end
  133. end
  134. class ::OpenSSL::X509::Store
  135. def add_chains(chains)
  136. chains = [chains] unless chains.is_a? Enumerable
  137. chains.each do |chain|
  138. case chain
  139. when ::OpenSSL::X509::Certificate
  140. self.add_cert chain
  141. else
  142. if File.directory?(chain)
  143. Dir.entries(chain)
  144. .collect { |e| File.join chain, e }
  145. .select { |e| File.file? e }
  146. .each { |f| self.add_file f }
  147. else
  148. self.add_file chain
  149. end
  150. end
  151. end
  152. end
  153. end