porky_lib 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +9 -9
- data/README.md +15 -0
- data/lib/porky_lib.rb +2 -0
- data/lib/porky_lib/file_service.rb +5 -46
- data/lib/porky_lib/file_service_helper.rb +51 -0
- data/lib/porky_lib/unencrypted.rb +5 -0
- data/lib/porky_lib/unencrypted/file_service.rb +49 -0
- data/lib/porky_lib/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1430e614f2d820b689e17b99735c4c7b24ba907640d427d2bd77c577e06017b
|
4
|
+
data.tar.gz: 278c2837a1a5eeefdb1ee584f69c07e448cad649777da8ac49fd51d477949b92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b3499e0acf1bb5df6e6382d2eacb0fff863f1a3d493fe864fb25662c21c9cb27aae26fbc46c39fdca1a19ae54976d47e095a230dc5cbea66b66b5132e1edf51
|
7
|
+
data.tar.gz: d13230909d362eda803d787b1ff0fdcd3331bfa2672b9e4696f6d3e1128367317b32595bd56284dc2c81f22fd78b63107553ae3de1e7e3953601e933f10abc00
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
porky_lib (0.
|
4
|
+
porky_lib (0.9.0)
|
5
5
|
aws-sdk-kms
|
6
6
|
aws-sdk-s3
|
7
7
|
msgpack
|
@@ -32,7 +32,7 @@ GEM
|
|
32
32
|
bundler (>= 1.2.0, < 3)
|
33
33
|
thor (~> 0.18)
|
34
34
|
byebug (11.0.1)
|
35
|
-
codecov (0.1.
|
35
|
+
codecov (0.1.15)
|
36
36
|
json
|
37
37
|
simplecov
|
38
38
|
url
|
@@ -44,10 +44,10 @@ GEM
|
|
44
44
|
json (2.2.0)
|
45
45
|
msgpack (1.3.1)
|
46
46
|
parallel (1.17.0)
|
47
|
-
parser (2.6.
|
47
|
+
parser (2.6.4.1)
|
48
48
|
ast (~> 2.4.0)
|
49
49
|
rainbow (3.0.0)
|
50
|
-
rake (
|
50
|
+
rake (13.0.0)
|
51
51
|
rbnacl (5.0.0)
|
52
52
|
ffi
|
53
53
|
rbnacl-libsodium (1.0.16)
|
@@ -56,11 +56,11 @@ GEM
|
|
56
56
|
rspec-core (~> 3.8.0)
|
57
57
|
rspec-expectations (~> 3.8.0)
|
58
58
|
rspec-mocks (~> 3.8.0)
|
59
|
-
rspec-collection_matchers (1.
|
59
|
+
rspec-collection_matchers (1.2.0)
|
60
60
|
rspec-expectations (>= 2.99.0.beta1)
|
61
61
|
rspec-core (3.8.0)
|
62
62
|
rspec-support (~> 3.8.0)
|
63
|
-
rspec-expectations (3.8.
|
63
|
+
rspec-expectations (3.8.4)
|
64
64
|
diff-lcs (>= 1.2.0, < 2.0)
|
65
65
|
rspec-support (~> 3.8.0)
|
66
66
|
rspec-mocks (3.8.1)
|
@@ -78,11 +78,11 @@ GEM
|
|
78
78
|
unicode-display_width (>= 1.4.0, < 1.7)
|
79
79
|
rubocop-performance (1.4.1)
|
80
80
|
rubocop (>= 0.71.0)
|
81
|
-
rubocop-rspec (1.
|
82
|
-
rubocop (>= 0.
|
81
|
+
rubocop-rspec (1.36.0)
|
82
|
+
rubocop (>= 0.68.1)
|
83
83
|
rubocop_runner (2.2.0)
|
84
84
|
ruby-progressbar (1.10.1)
|
85
|
-
simplecov (0.17.
|
85
|
+
simplecov (0.17.1)
|
86
86
|
docile (~> 1.1)
|
87
87
|
json (>= 1.8, < 3)
|
88
88
|
simplecov-html (~> 0.10.0)
|
data/README.md
CHANGED
@@ -147,6 +147,13 @@ alias_exists = PorkyLib::Symmetric.instance.cmk_alias_exists?(key_alias)
|
|
147
147
|
file_data = PorkyLib::FileService.read(bucket_name, file_key)
|
148
148
|
```
|
149
149
|
|
150
|
+
### To Read Unencrypted Files From AWS S3
|
151
|
+
```ruby
|
152
|
+
# Where bucket_name is the name of the S3 bucket to read from
|
153
|
+
# file_key is file identifier of the file/data that was written to S3.
|
154
|
+
file_data = PorkyLib::Unencrypted::FileService.read(bucket_name, file_key)
|
155
|
+
```
|
156
|
+
|
150
157
|
### To Write To AWS S3
|
151
158
|
```ruby
|
152
159
|
# Where file is the data to encrypt and upload to S3 (can be raw data or path to a file on disk)
|
@@ -156,6 +163,14 @@ file_data = PorkyLib::FileService.read(bucket_name, file_key)
|
|
156
163
|
file_key = PorkyLib::FileService.write(file, bucket_name, key_id, options)
|
157
164
|
```
|
158
165
|
|
166
|
+
### To Write Unencrypted Files To AWS S3
|
167
|
+
```ruby
|
168
|
+
# Where file is the data to upload to S3 (can be raw data or path to a file on disk)
|
169
|
+
# bucket_name is the name of the S3 bucket to write to
|
170
|
+
# options is an optional parameter for specifying optional metadata about the file
|
171
|
+
file_key = PorkyLib::Unencrypted::FileService.write(file, bucket_name, options)
|
172
|
+
```
|
173
|
+
|
159
174
|
### Generate S3 Presigned POST URL
|
160
175
|
To generate a new presigned POST url (used to upload files directly to AWS S3):
|
161
176
|
```ruby
|
data/lib/porky_lib.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'aws-sdk-s3'
|
4
3
|
require 'singleton'
|
4
|
+
require 'porky_lib/file_service_helper'
|
5
5
|
|
6
6
|
class PorkyLib::FileService
|
7
7
|
include Singleton
|
8
|
+
include FileServiceHelper
|
8
9
|
|
9
10
|
class FileServiceError < StandardError; end
|
10
11
|
class FileSizeTooLargeError < StandardError; end
|
@@ -59,7 +60,7 @@ class PorkyLib::FileService
|
|
59
60
|
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if file_size_invalid?(file)
|
60
61
|
|
61
62
|
data = file_data(file)
|
62
|
-
file_key = options
|
63
|
+
file_key = generate_file_key(options)
|
63
64
|
tempfile = encrypt_file_contents(data, key_id, file_key, options)
|
64
65
|
|
65
66
|
begin
|
@@ -114,40 +115,6 @@ class PorkyLib::FileService
|
|
114
115
|
|
115
116
|
private
|
116
117
|
|
117
|
-
def input_invalid?(file, bucket_name, key_id)
|
118
|
-
file.nil? || bucket_name.nil? || key_id.nil?
|
119
|
-
end
|
120
|
-
|
121
|
-
def file_size_invalid?(file)
|
122
|
-
file.bytesize > max_size || (File.file?(file) && File.size(file) > max_size)
|
123
|
-
end
|
124
|
-
|
125
|
-
def file_data(file)
|
126
|
-
File.file?(file) ? File.read(file) : file
|
127
|
-
end
|
128
|
-
|
129
|
-
def max_size
|
130
|
-
PorkyLib::Config.config[:max_file_size]
|
131
|
-
end
|
132
|
-
|
133
|
-
def max_file_size
|
134
|
-
{
|
135
|
-
B: 1024,
|
136
|
-
KB: 1024 * 1024,
|
137
|
-
MB: 1024 * 1024 * 1024,
|
138
|
-
GB: 1024 * 1024 * 1024 * 1024
|
139
|
-
}.each_pair { |symbol, bytes| return "#{(max_size.to_f / (bytes / 1024)).round(2)}#{symbol}" if max_size < bytes }
|
140
|
-
end
|
141
|
-
|
142
|
-
def perform_upload(bucket_name, file_key, tempfile, options)
|
143
|
-
obj = s3.bucket(bucket_name).object(file_key)
|
144
|
-
if options.key?(:metadata)
|
145
|
-
obj.upload_file(tempfile.path, metadata: options[:metadata])
|
146
|
-
else
|
147
|
-
obj.upload_file(tempfile.path)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
118
|
def decrypt_file_contents(tempfile)
|
152
119
|
file_contents = tempfile.read
|
153
120
|
|
@@ -184,14 +151,6 @@ class PorkyLib::FileService
|
|
184
151
|
}.to_json
|
185
152
|
end
|
186
153
|
|
187
|
-
def write_tempfile(file_contents, file_key)
|
188
|
-
tempfile = Tempfile.new(file_key)
|
189
|
-
tempfile << file_contents
|
190
|
-
tempfile.close
|
191
|
-
|
192
|
-
tempfile
|
193
|
-
end
|
194
|
-
|
195
154
|
def presign_url_expires_in
|
196
155
|
PorkyLib::Config.config[:presign_url_expires_in]
|
197
156
|
end
|
@@ -200,7 +159,7 @@ class PorkyLib::FileService
|
|
200
159
|
@s3_client ||= Aws::S3::Client.new
|
201
160
|
end
|
202
161
|
|
203
|
-
def
|
204
|
-
|
162
|
+
def input_invalid?(file, bucket_name, key_id)
|
163
|
+
file.nil? || bucket_name.nil? || key_id.nil?
|
205
164
|
end
|
206
165
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws-sdk-s3'
|
4
|
+
|
5
|
+
module FileServiceHelper
|
6
|
+
def file_size_invalid?(file)
|
7
|
+
file.bytesize > max_size || (File.file?(file) && File.size(file) > max_size)
|
8
|
+
end
|
9
|
+
|
10
|
+
def file_data(file)
|
11
|
+
File.file?(file) ? File.read(file) : file
|
12
|
+
end
|
13
|
+
|
14
|
+
def write_tempfile(file_contents, file_key)
|
15
|
+
tempfile = Tempfile.new(file_key)
|
16
|
+
tempfile << file_contents
|
17
|
+
tempfile.close
|
18
|
+
|
19
|
+
tempfile
|
20
|
+
end
|
21
|
+
|
22
|
+
def perform_upload(bucket_name, file_key, tempfile, options)
|
23
|
+
obj = s3.bucket(bucket_name).object(file_key)
|
24
|
+
if options.key?(:metadata)
|
25
|
+
obj.upload_file(tempfile.path, metadata: options[:metadata])
|
26
|
+
else
|
27
|
+
obj.upload_file(tempfile.path)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def s3
|
32
|
+
@s3 ||= Aws::S3::Resource.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def max_size
|
36
|
+
PorkyLib::Config.config[:max_file_size]
|
37
|
+
end
|
38
|
+
|
39
|
+
def max_file_size
|
40
|
+
{
|
41
|
+
B: 1024,
|
42
|
+
KB: 1024 * 1024,
|
43
|
+
MB: 1024 * 1024 * 1024,
|
44
|
+
GB: 1024 * 1024 * 1024 * 1024
|
45
|
+
}.each_pair { |symbol, bytes| return "#{(max_size.to_f / (bytes / 1024)).round(2)}#{symbol}" if max_size < bytes }
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate_file_key(options)
|
49
|
+
options.key?(:directory) ? "#{options[:directory]}/#{SecureRandom.uuid}" : SecureRandom.uuid
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'singleton'
|
4
|
+
require 'porky_lib/file_service_helper'
|
5
|
+
|
6
|
+
class PorkyLib::Unencrypted::FileService
|
7
|
+
include Singleton
|
8
|
+
include FileServiceHelper
|
9
|
+
|
10
|
+
class FileServiceError < StandardError; end
|
11
|
+
class FileSizeTooLargeError < StandardError; end
|
12
|
+
|
13
|
+
def read(bucket_name, file_key, options = {})
|
14
|
+
tempfile = Tempfile.new
|
15
|
+
|
16
|
+
begin
|
17
|
+
object = s3.bucket(bucket_name).object(file_key)
|
18
|
+
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if object.content_length > max_size
|
19
|
+
|
20
|
+
object.download_file(tempfile.path, options)
|
21
|
+
rescue Aws::Errors::ServiceError, Seahorse::Client::NetworkingError => e
|
22
|
+
raise FileServiceError, "Attempt to download a file from S3 failed.\n#{e.message}"
|
23
|
+
end
|
24
|
+
|
25
|
+
tempfile.read
|
26
|
+
end
|
27
|
+
|
28
|
+
def write(file, bucket_name, options = {})
|
29
|
+
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name)
|
30
|
+
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if file_size_invalid?(file)
|
31
|
+
|
32
|
+
file_key = generate_file_key(options)
|
33
|
+
tempfile = write_tempfile(file_data(file), file_key)
|
34
|
+
|
35
|
+
begin
|
36
|
+
perform_upload(bucket_name, file_key, tempfile, options)
|
37
|
+
rescue Aws::Errors::ServiceError, Seahorse::Client::NetworkingError => e
|
38
|
+
raise FileServiceError, "Attempt to upload a file to S3 failed.\n#{e.message}"
|
39
|
+
end
|
40
|
+
|
41
|
+
file_key
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def input_invalid?(file, bucket_name)
|
47
|
+
file.nil? || bucket_name.nil?
|
48
|
+
end
|
49
|
+
end
|
data/lib/porky_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: porky_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Fletcher
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-kms
|
@@ -385,7 +385,10 @@ files:
|
|
385
385
|
- lib/porky_lib/aws/kms/client.rb
|
386
386
|
- lib/porky_lib/config.rb
|
387
387
|
- lib/porky_lib/file_service.rb
|
388
|
+
- lib/porky_lib/file_service_helper.rb
|
388
389
|
- lib/porky_lib/symmetric.rb
|
390
|
+
- lib/porky_lib/unencrypted.rb
|
391
|
+
- lib/porky_lib/unencrypted/file_service.rb
|
389
392
|
- lib/porky_lib/version.rb
|
390
393
|
- porky_lib.gemspec
|
391
394
|
homepage: https://github.com/Zetatango/porky_lib
|