-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.rb
executable file
·61 lines (53 loc) · 1.36 KB
/
get.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/ruby
require 'rubygems'
require 'aws-sdk-core'
require 'aws-sdk'
require 'yaml'
require 'base64'
REGION = 'us-east-1'
file = ARGV.shift
bucket = ARGV.shift
if(file.nil? or bucket.nil?)
STDERR.puts "#{$0} FILE BUCKET"
exit 1
end
def decrypt_data_key(blob)
kms = Aws::KMS::Client.new(region: REGION)
res = kms.decrypt(ciphertext_blob: blob)
puts "Decrypted #{res[:plaintext].size} bytes of key data"
return res[:plaintext]
end
def get_key(bucket, file)
begin
s3c = Aws::S3::Client.new(region: REGION)
res = s3c.get_object(bucket: bucket,
key: file)
puts "Got #{res[:body].size} bytes of encrypted key data"
key_blob_strio = res[:body]
key_blob = key_blob_strio.read
decrypt_data_key(key_blob)
rescue Aws::S3::Errors::ServiceError => e
puts "S3 error: #{e}"
exit 1
end
end
def get_file(bucket, file, key)
begin
s3c = Aws::S3::Encryption::Client.new(encryption_key: key,
region: REGION)
res = s3c.get_object(bucket: bucket,
key: file)
f = File.new(file, 'w')
f.puts res[:body].read
rescue Aws::S3::Errors::ServiceError => e
puts "S3 error: #{e}"
exit 1
end
end
data_key = get_key(bucket, file + ".key")
get_file(bucket, file, data_key)
# Local variables:
# mode: ruby
# tab-width: 4
# indent-tabs-mode: nil
# end: