Browse Source

load up accounts

Chris Mague 7 years ago
parent
commit
0109a673b6
3 changed files with 45 additions and 0 deletions
  1. 15 0
      Rakefile
  2. 29 0
      lib/neoinfra/accounts.rb
  3. 1 0
      models/accounts.rb

+ 15 - 0
Rakefile

@@ -1,10 +1,19 @@
 # frozen_string_literal: true
 
+lib_dir = File.join(File.dirname(File.expand_path(__FILE__)), 'lib')
+$LOAD_PATH.unshift(lib_dir) unless
+  $LOAD_PATH.include?(lib_dir) || $LOAD_PATH.include?(lib_dir)
+
+models_dir = File.join(File.dirname(File.expand_path(__FILE__)), 'models')
+$LOAD_PATH.unshift(models_dir) unless
+  $LOAD_PATH.include?(models_dir) || $LOAD_PATH.include?(models_dir)
+
 task default: :full_test
 
 require 'rubocop/rake_task'
 require 'rake'
 require 'rspec/core/rake_task'
+require 'neoinfra/accounts'
 
 RuboCop::RakeTask.new(:rubocop) do |t|
   t.options = ['--display-cop-names']
@@ -15,4 +24,10 @@ RSpec::Core::RakeTask.new(:spec) do |t|
   t.rspec_opts = '--format documentation'
 end
 
+desc 'Load accounts into the neo4j container'
+task :load_accounts do
+  j = NeoInfra::Accounts.new
+  j.load
+end
+
 task full_test: %i[rubocop spec]

+ 29 - 0
lib/neoinfra/accounts.rb

@@ -1,12 +1,41 @@
 # frozen_string_literal: true
 
+require 'accounts'
 require 'yaml'
 require 'fog'
+require 'neo4j'
 require 'neoinfra/config'
 
 # NeoInfra Account information
 module NeoInfra
   # Provide informations about the accounts available
   class Accounts
+    def load
+      @cfg = NeoInfra::Config.new
+
+      neo4j_url = "http://#{@cfg.neo4j[:host]}:#{@cfg.neo4j[:port]}"
+      Neo4j::Session.open(:server_db, neo4j_url)
+
+      @cfg.accounts.each do |account|
+        base_conf = {
+          provider: 'AWS',
+          aws_access_key_id: account[:key],
+          aws_secret_access_key: account[:secret]
+        }
+
+        # Grab the current user id string
+        iam = Fog::AWS::IAM.new(base_conf)
+
+        next unless AwsAccount.where(name: account[:name]).empty?
+        account = AwsAccount.new(
+          name:       account[:name],
+          account_id: iam.users.current.arn.split(':')[4],
+          user_id:    iam.users.current.id,
+          key_md5:    Digest:: MD5.hexdigest(account[:key]),
+          secret_md5: Digest:: MD5.hexdigest(account[:secret])
+        )
+        account.save
+      end
+    end
   end
 end

+ 1 - 0
models/accounts.rb

@@ -10,4 +10,5 @@ class AwsAccount
   # We get the md5 since so we can search if we only know the creds
   property :key_md5
   property :secret_md5
+  property :user_id
 end