accounts.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # frozen_string_literal: true
  2. require 'accounts'
  3. require 'yaml'
  4. require 'fog-aws'
  5. require 'neo4j'
  6. require 'neoinfra/config'
  7. # NeoInfra Account information
  8. module NeoInfra
  9. # Provide informations about the accounts available
  10. class Accounts
  11. def list_names
  12. @cfg = NeoInfra::Config.new
  13. @cfg.accounts.map { |x| x[:name] }
  14. end
  15. def load
  16. @cfg = NeoInfra::Config.new
  17. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  18. Neo4j::Session.open(:server_db, neo4j_url)
  19. @cfg.accounts.each do |account|
  20. base_conf = {
  21. aws_access_key_id: account[:key],
  22. aws_secret_access_key: account[:secret]
  23. }
  24. # Grab the current user id string
  25. iam = Fog::AWS::IAM.new(base_conf)
  26. next unless AwsAccount.where(name: account[:name]).empty?
  27. account = AwsAccount.new(
  28. name: account[:name],
  29. account_id: iam.users.current.arn.split(':')[4],
  30. user_id: iam.users.current.id,
  31. key_md5: Digest:: MD5.hexdigest(account[:key]),
  32. secret_md5: Digest:: MD5.hexdigest(account[:secret])
  33. )
  34. account.save
  35. end
  36. end
  37. end
  38. end