accounts.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. provider: 'AWS',
  18. aws_access_key_id: account[:key],
  19. aws_secret_access_key: account[:secret]
  20. }
  21. # Grab the current user id string
  22. iam = Fog::AWS::IAM.new(base_conf)
  23. next unless AwsAccount.where(name: account[:name]).empty?
  24. account = AwsAccount.new(
  25. name: account[:name],
  26. account_id: iam.users.current.arn.split(':')[4],
  27. user_id: iam.users.current.id,
  28. key_md5: Digest:: MD5.hexdigest(account[:key]),
  29. secret_md5: Digest:: MD5.hexdigest(account[:secret])
  30. )
  31. account.save
  32. end
  33. end
  34. end
  35. end