Class: ZAWS::CloudTrail
- Inherits:
-
Object
- Object
- ZAWS::CloudTrail
- Defined in:
- lib/zaws/services/cloud_trail.rb
Constant Summary collapse
- DEFAULT_DAYS_TO_FETCH =
7
- ZAWS_S3_CACHE =
"#{Dir.home}/.zaws/s3-cache"
Instance Method Summary collapse
- #declare(name, region, bucket_name, verbose = nil) ⇒ Object
- #exists(name, region) ⇒ Object
- #get_cloud_trail_by_bucket(region, bucket_name, as_raw = false, verbose = nil) ⇒ Object
- #get_cloud_trail_by_name(region, trail_name, as_raw = false, verbose = nil) ⇒ Object
- #get_cloud_trails(region, verbose = nil) ⇒ Object
-
#initialize(shellout, aws) ⇒ CloudTrail
constructor
A new instance of CloudTrail.
Constructor Details
#initialize(shellout, aws) ⇒ CloudTrail
Returns a new instance of CloudTrail.
11 12 13 14 |
# File 'lib/zaws/services/cloud_trail.rb', line 11 def initialize(shellout,aws) @shellout=shellout @aws=aws end |
Instance Method Details
#declare(name, region, bucket_name, verbose = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/zaws/services/cloud_trail.rb', line 64 def declare(name,region,bucket_name,verbose=nil) if exists(name,region) puts "CloudTrail already exists. Creation skipped.\n" else bucket_exists=@aws.s3.bucket().exists(bucket_name,region) cmdline = "aws --region #{region} cloudtrail create-subscription " << "--name #{name} --s3-#{bucket_exists ? 'use' : 'new'}-bucket #{bucket_name}" puts @shellout.cli(cmdline,verbose) end end |
#exists(name, region) ⇒ Object
60 61 62 |
# File 'lib/zaws/services/cloud_trail.rb', line 60 def exists(name,region) get_cloud_trails(region).any? {|trail| trail['Name'] === name} end |
#get_cloud_trail_by_bucket(region, bucket_name, as_raw = false, verbose = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/zaws/services/cloud_trail.rb', line 16 def get_cloud_trail_by_bucket(region,bucket_name,as_raw=false,verbose=nil) bucket_name = "s3://#{bucket_name}" if !bucket_name.match('s3://.*') bucket_hash = Digest::SHA1.hexdigest("#{region}#{bucket_name}") dir_name = "#{ZAWS_S3_CACHE}/#{bucket_hash}" FileUtils.mkdir_p(dir_name) dir_name = @aws.s3.bucket.sync(region,bucket_name,dir_name,verbose) results = [] Dir.open(dir_name) { |dir| Dir.glob(File.join(dir, '**', '*')) { |filename| Zlib::GzipReader.open(filename) { |file| log_file = JSON.parse file.read results.push log_file['Records'] } if File.file? filename } } json = {:Records => results.flatten(1)}.to_json if as_raw puts json else puts ZAWS::Helper::Output.cloudtrail(json) end json end |
#get_cloud_trail_by_name(region, trail_name, as_raw = false, verbose = nil) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/zaws/services/cloud_trail.rb', line 45 def get_cloud_trail_by_name(region,trail_name,as_raw=false, verbose=nil) available_cloud_trails = get_cloud_trails(region) bucket_name = available_cloud_trails.find { |available_cloud_trail| available_cloud_trail['Name'] === trail_name }['S3BucketName'] get_cloud_trail_by_bucket(region, bucket_name, as_raw, verbose) end |
#get_cloud_trails(region, verbose = nil) ⇒ Object
54 55 56 57 58 |
# File 'lib/zaws/services/cloud_trail.rb', line 54 def get_cloud_trails(region, verbose=nil) com_line = "aws cloudtrail describe-trails --region #{region}" cloud_trails = JSON.parse @shellout.cli(com_line, verbose) cloud_trails['trailList'] end |