audit.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # frozen_string_literal: true
  2. require 'nodes'
  3. require 'accounts'
  4. require 'fog-aws'
  5. require 'neoinfra'
  6. # NeoInfra Account information
  7. module NeoInfra
  8. # Provide informations about the accounts available
  9. class Audit
  10. def audit_nodes
  11. results = Hash.new { |h, k| h[k] = {} }
  12. aws = NeoInfra::Aws.new
  13. @cfg = NeoInfra::Config.new
  14. unless @cfg.tag_policy.has_key? :nodes
  15. puts "no policy set for nodes"
  16. return {:error => "No nodes tag policy"}
  17. end
  18. @cfg.accounts.each do |account|
  19. base_conf = {
  20. provider: 'AWS',
  21. aws_access_key_id: account[:key],
  22. aws_secret_access_key: account[:secret]
  23. }
  24. aws.regions.each do |region|
  25. region_conf = { region: region }
  26. # Get Instances
  27. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  28. new_conn.servers.all.each do |ec2|
  29. %i[required recommended].each do |a|
  30. next unless @cfg.tag_policy[:nodes].has_key? a
  31. next if (ec2.tags.keys.sort & @cfg.tag_policy[:nodes][a].sort) == @cfg.tag_policy[:nodes][a].sort
  32. results[ec2.id].merge!("#{a}_missing_tags" => @cfg.tag_policy[:nodes][a].sort - ec2.tags.keys.sort,
  33. 'tags' => ec2.tags.keys.sort,
  34. 'account' => account[:name],
  35. 'launched' => ec2.created_at,
  36. 'region' => region,
  37. 'ssh_key' => ec2.key_name)
  38. end
  39. end
  40. end
  41. end
  42. results
  43. end
  44. def audit_vpcs
  45. results = Hash.new { |h, k| h[k] = {} }
  46. aws = NeoInfra::Aws.new
  47. @cfg = NeoInfra::Config.new
  48. unless @cfg.tag_policy.has_key? :vpcs
  49. puts "no policy set for vpcs"
  50. return {:error => "No vpc tag policy"}
  51. end
  52. @cfg.accounts.each do |account|
  53. base_conf = {
  54. provider: 'AWS',
  55. aws_access_key_id: account[:key],
  56. aws_secret_access_key: account[:secret]
  57. }
  58. aws.regions.each do |region|
  59. region_conf = { region: region }
  60. # Get Instances
  61. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  62. new_conn.vpcs.all.each do |vpc|
  63. %i[required recommended].each do |a|
  64. # we don't do default vpcs
  65. next if vpc.is_default
  66. next unless @cfg.tag_policy[:vpcs].has_key? a
  67. next if (vpc.tags.keys.sort & @cfg.tag_policy[:vpcs][a].sort) == @cfg.tag_policy[:vpcs][a].sort
  68. results[vpc.id].merge!("#{a}_missing_tags" => @cfg.tag_policy[:vpcs][a].sort - vpc.tags.keys.sort,
  69. 'tags' => vpc.tags.keys.sort,
  70. 'account' => account[:name],
  71. 'region' => region)
  72. end
  73. end
  74. end
  75. end
  76. results
  77. end
  78. def audit_subnets
  79. results = Hash.new { |h, k| h[k] = {} }
  80. aws = NeoInfra::Aws.new
  81. @cfg = NeoInfra::Config.new
  82. unless @cfg.tag_policy.has_key? :subnets
  83. puts "no policy set for subnets"
  84. return {:error => "No subnet tag policy"}
  85. end
  86. @cfg.accounts.each do |account|
  87. base_conf = {
  88. provider: 'AWS',
  89. aws_access_key_id: account[:key],
  90. aws_secret_access_key: account[:secret]
  91. }
  92. aws.regions.each do |region|
  93. region_conf = { region: region }
  94. # Get Instances
  95. new_conn = Fog::Compute.new(region_conf.merge(base_conf))
  96. new_conn.subnets.all.each do |subnet|
  97. %i[required recommended].each do |a|
  98. next if subnet.default_for_az
  99. next unless @cfg.tag_policy[:subnets].has_key? a
  100. next if (subnet.tag_set.keys.sort & @cfg.tag_policy[:subnets][a].sort) == @cfg.tag_policy[:subnets][a].sort
  101. results[subnet.subnet_id].merge!("#{a}_missing_tags" => @cfg.tag_policy[:subnets][a].sort - subnet.tag_set.keys.sort,
  102. 'tags' => subnet.tag_set.keys.sort,
  103. 'account' => account[:name],
  104. 'region' => region)
  105. end
  106. end
  107. end
  108. end
  109. results
  110. end
  111. end
  112. end