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.

73 lines
1.4KB

  1. module CryptCheck
  2. module Tls
  3. class Grade
  4. attr_reader :server, :grade
  5. def initialize(server)
  6. @server = server
  7. @states = @server.states
  8. @checks = @server.performed_checks
  9. Logger.info { '' }
  10. Logger.ap :checks, @checks
  11. Logger.ap :states, @states
  12. @grade = calculate_grade
  13. color = case @grade
  14. when 'A', 'A+'
  15. :best
  16. when 'B', 'B+'
  17. :great
  18. when 'C', 'C+'
  19. :good
  20. when 'E'
  21. :warning
  22. when 'F'
  23. :error
  24. when 'G'
  25. :critical
  26. when 'T', 'V'
  27. :unknown
  28. end
  29. Logger.info { "Grade : #{self.grade.colorize color }" }
  30. end
  31. def to_h
  32. { checks: @checks, states: @states }
  33. end
  34. private
  35. def calculate_grade
  36. return 'V' unless @server.valid?
  37. return 'T' unless @server.trusted?
  38. case
  39. when !@states[:critical].empty?
  40. return 'G'
  41. when !@states[:error].empty?
  42. return 'F'
  43. when !@states[:warning].empty?
  44. return 'E'
  45. end
  46. [[:good, 'D', 'C'],
  47. [:great, 'C', 'B'],
  48. [:best, 'B', 'A']].each do |type, score1, score2|
  49. expected = @checks[type]
  50. unless expected.empty?
  51. available = @states[type]
  52. return score1 if available.empty?
  53. missed = expected - available
  54. unless missed.empty?
  55. Logger.info { "Missing #{type} : #{missed}" }
  56. return score2
  57. end
  58. end
  59. end
  60. 'A+'
  61. end
  62. end
  63. end
  64. end