accounts.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # frozen_string_literal: true
  2. require 'accounts'
  3. require 'yaml'
  4. require 'fog'
  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 load
  12. @cfg = NeoInfra::Config.new
  13. neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
  14. Neo4j::Session.open(:server_db, neo4j_url)
  15. @cfg.accounts.each do |account|
  16. base_conf = {
  17. aws_access_key_id: account[:key],
  18. aws_secret_access_key: account[:secret]
  19. }
  20. # Grab the current user id string
  21. iam = Fog::AWS::IAM.new(base_conf)
  22. next unless AwsAccount.where(name: account[:name]).empty?
  23. account = AwsAccount.new(
  24. name: account[:name],
  25. account_id: iam.users.current.arn.split(':')[4],
  26. user_id: iam.users.current.id,
  27. key_md5: Digest:: MD5.hexdigest(account[:key]),
  28. secret_md5: Digest:: MD5.hexdigest(account[:secret])
  29. )
  30. account.save
  31. end
  32. end
  33. end
  34. end