porky_lib 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/Gemfile.lock +6 -6
- data/README.md +39 -3
- data/lib/porky_lib/file_service.rb +30 -6
- data/lib/porky_lib/file_service_helper.rb +39 -6
- data/lib/porky_lib/unencrypted/file_service.rb +23 -4
- data/lib/porky_lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63d12d040a5b5f4c6b96940a29bd3ff771e713327fa7269488ea731b76fb59aa
|
4
|
+
data.tar.gz: a086399a4c16939060522f80e0e1421268781973ffd15e7fac42dfece572782a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd5e4418205489c4705648c575d23011a139eac1622202e638b3bb20a0a8e753996601b52be9ba05cf4148f3e1b8779b944c7fb87f0c8af834a1c42554b99c8e
|
7
|
+
data.tar.gz: 29b4c3e1243a851e61b2a288666dcd98a6f1d6f4f892a4f250cc0d3edcaf81562a19202dc016dce201b2cb2a0c92df2fe601f8cb64ab822ceb2e04aadcfbb505
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
porky_lib (0.9.
|
4
|
+
porky_lib (0.9.3)
|
5
5
|
aws-sdk-kms
|
6
6
|
aws-sdk-s3
|
7
7
|
msgpack
|
@@ -13,8 +13,8 @@ GEM
|
|
13
13
|
specs:
|
14
14
|
ast (2.4.0)
|
15
15
|
aws-eventstream (1.0.3)
|
16
|
-
aws-partitions (1.
|
17
|
-
aws-sdk-core (3.72.
|
16
|
+
aws-partitions (1.232.0)
|
17
|
+
aws-sdk-core (3.72.1)
|
18
18
|
aws-eventstream (~> 1.0, >= 1.0.2)
|
19
19
|
aws-partitions (~> 1, >= 1.228.0)
|
20
20
|
aws-sigv4 (~> 1.1)
|
@@ -22,7 +22,7 @@ GEM
|
|
22
22
|
aws-sdk-kms (1.25.0)
|
23
23
|
aws-sdk-core (~> 3, >= 3.71.0)
|
24
24
|
aws-sigv4 (~> 1.1)
|
25
|
-
aws-sdk-s3 (1.
|
25
|
+
aws-sdk-s3 (1.53.0)
|
26
26
|
aws-sdk-core (~> 3, >= 3.71.0)
|
27
27
|
aws-sdk-kms (~> 1)
|
28
28
|
aws-sigv4 (~> 1.1)
|
@@ -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.16)
|
36
36
|
json
|
37
37
|
simplecov
|
38
38
|
url
|
@@ -69,7 +69,7 @@ GEM
|
|
69
69
|
rspec-support (3.9.0)
|
70
70
|
rspec_junit_formatter (0.4.1)
|
71
71
|
rspec-core (>= 2, < 4, != 2.12.0)
|
72
|
-
rubocop (0.
|
72
|
+
rubocop (0.76.0)
|
73
73
|
jaro_winkler (~> 1.5.1)
|
74
74
|
parallel (~> 1.10)
|
75
75
|
parser (>= 2.6)
|
data/README.md
CHANGED
@@ -156,21 +156,57 @@ file_data = PorkyLib::Unencrypted::FileService.read(bucket_name, file_key)
|
|
156
156
|
|
157
157
|
### To Write To AWS S3
|
158
158
|
```ruby
|
159
|
-
#
|
159
|
+
# --- DEPRECATED --- Please use write_data or write_file instead of write
|
160
|
+
# Where file is the data to encrypt and upload to S3 (can be a path or raw data or ruby file object)
|
160
161
|
# bucket_name is the name of the S3 bucket to write to
|
161
162
|
# key_id is the ID of the CMK to use to generate a data encryption key to encrypt the file data
|
162
163
|
# options is an optional parameter for specifying optional metadata about the file
|
163
164
|
file_key = PorkyLib::FileService.write(file, bucket_name, key_id, options)
|
164
165
|
```
|
165
166
|
|
166
|
-
### To Write
|
167
|
+
### To Write Files To AWS S3
|
168
|
+
```ruby
|
169
|
+
# Where file is the data to encrypt and upload to S3 (can be a path or ruby file object)
|
170
|
+
# bucket_name is the name of the S3 bucket to write to
|
171
|
+
# key_id is the ID of the CMK to use to generate a data encryption key to encrypt the file data
|
172
|
+
# options is an optional parameter for specifying optional metadata about the file
|
173
|
+
file_key = PorkyLib::FileService.write_file(file, bucket_name, key_id, options)
|
174
|
+
```
|
175
|
+
|
176
|
+
### To Write Data To AWS S3
|
177
|
+
```ruby
|
178
|
+
# Where data is the raw data to encrypt and upload to S3
|
179
|
+
# bucket_name is the name of the S3 bucket to write to
|
180
|
+
# key_id is the ID of the CMK to use to generate a data encryption key to encrypt the file data
|
181
|
+
# options is an optional parameter for specifying optional metadata about the file
|
182
|
+
file_key = PorkyLib::FileService.write_data(data, bucket_name, key_id, options)
|
183
|
+
```
|
184
|
+
|
185
|
+
### To Write Unencrypted To AWS S3
|
167
186
|
```ruby
|
168
|
-
#
|
187
|
+
# --- DEPRECATED --- Please use write_data or write_file instead of write
|
188
|
+
# Where file is the data to upload to S3 (can be a path or raw data or ruby file object)
|
169
189
|
# bucket_name is the name of the S3 bucket to write to
|
170
190
|
# options is an optional parameter for specifying optional metadata about the file
|
171
191
|
file_key = PorkyLib::Unencrypted::FileService.write(file, bucket_name, options)
|
172
192
|
```
|
173
193
|
|
194
|
+
### To Write Unencrypted Files To AWS S3
|
195
|
+
```ruby
|
196
|
+
# Where file is the data to encrypt and upload to S3 (can be a path or ruby file object)
|
197
|
+
# bucket_name is the name of the S3 bucket to write to
|
198
|
+
# options is an optional parameter for specifying optional metadata about the file
|
199
|
+
file_key = PorkyLib::Unencrypted::FileService.write_file(file, bucket_name, options)
|
200
|
+
```
|
201
|
+
|
202
|
+
### To Write Unencrypted Data To AWS S3
|
203
|
+
```ruby
|
204
|
+
# Where data is the raw data to encrypt and upload to S3
|
205
|
+
# bucket_name is the name of the S3 bucket to write to
|
206
|
+
# options is an optional parameter for specifying optional metadata about the file
|
207
|
+
file_key = PorkyLib::Unencrypted::FileService.write_data(data, bucket_name, options)
|
208
|
+
```
|
209
|
+
|
174
210
|
### Generate S3 Presigned POST URL
|
175
211
|
To generate a new presigned POST url (used to upload files directly to AWS S3):
|
176
212
|
```ruby
|
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'singleton'
|
4
4
|
|
5
5
|
class PorkyLib::FileService
|
6
|
+
extend Gem::Deprecate
|
7
|
+
|
6
8
|
include Singleton
|
7
9
|
include PorkyLib::FileServiceHelper
|
8
10
|
|
@@ -56,9 +58,25 @@ class PorkyLib::FileService
|
|
56
58
|
|
57
59
|
def write(file, bucket_name, key_id, options = {})
|
58
60
|
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name, key_id)
|
59
|
-
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if file_size_invalid?(file)
|
60
61
|
|
61
|
-
|
62
|
+
if file?(file)
|
63
|
+
write_file(file, bucket_name, key_id, options)
|
64
|
+
else
|
65
|
+
write_data(file, bucket_name, key_id, options)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
deprecate :write, 'write_file or write_data', 2020, 1
|
69
|
+
|
70
|
+
def write_file(file, bucket_name, key_id, options = {})
|
71
|
+
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name, key_id)
|
72
|
+
|
73
|
+
write_data(read_file(file), bucket_name, key_id, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
def write_data(data, bucket_name, key_id, options = {})
|
77
|
+
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(data, bucket_name, key_id)
|
78
|
+
raise FileSizeTooLargeError, "Data size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)
|
79
|
+
|
62
80
|
file_key = generate_file_key(options)
|
63
81
|
tempfile = encrypt_file_contents(data, key_id, file_key, options)
|
64
82
|
|
@@ -74,11 +92,12 @@ class PorkyLib::FileService
|
|
74
92
|
end
|
75
93
|
|
76
94
|
def overwrite_file(file, file_key, bucket_name, key_id, options = {})
|
77
|
-
raise FileServiceError, 'Invalid input. One or more input values is nil' if
|
95
|
+
raise FileServiceError, 'Invalid input. One or more input values is nil' if overwrite_input_invalid?(file, file_key, bucket_name, key_id)
|
78
96
|
raise FileServiceError, 'Invalid input. file_key cannot be nil if overwriting an existing file' if file_key.nil?
|
79
|
-
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if file_size_invalid?(file)
|
80
97
|
|
81
98
|
data = file_data(file)
|
99
|
+
raise FileSizeTooLargeError, "File size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)
|
100
|
+
|
82
101
|
tempfile = encrypt_file_contents(data, key_id, file_key, options)
|
83
102
|
|
84
103
|
begin
|
@@ -90,6 +109,7 @@ class PorkyLib::FileService
|
|
90
109
|
# Remove tempfile from disk
|
91
110
|
tempfile.unlink
|
92
111
|
end
|
112
|
+
deprecate :overwrite_file, :none, 2020, 0o1
|
93
113
|
|
94
114
|
def presigned_post_url(bucket_name, options = {})
|
95
115
|
file_name = options[:file_name] || SecureRandom.uuid
|
@@ -158,7 +178,11 @@ class PorkyLib::FileService
|
|
158
178
|
@s3_client ||= Aws::S3::Client.new
|
159
179
|
end
|
160
180
|
|
161
|
-
def input_invalid?(
|
162
|
-
|
181
|
+
def input_invalid?(file_or_data, bucket_name, key_id)
|
182
|
+
file_or_data.nil? || bucket_name.nil? || key_id.nil?
|
183
|
+
end
|
184
|
+
|
185
|
+
def overwrite_input_invalid?(file_or_data, file_key, bucket_name, key_id)
|
186
|
+
file_or_data.nil? || file_key.nil? || bucket_name.nil? || key_id.nil?
|
163
187
|
end
|
164
188
|
end
|
@@ -3,17 +3,27 @@
|
|
3
3
|
require 'aws-sdk-s3'
|
4
4
|
|
5
5
|
module PorkyLib::FileServiceHelper
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
extend Gem::Deprecate
|
7
|
+
|
8
|
+
class FileServiceError < StandardError; end
|
9
|
+
|
10
|
+
def data_size_invalid?(data)
|
11
|
+
data.bytesize > max_size
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_data(file_or_content)
|
15
|
+
if file?(file_or_content)
|
16
|
+
read_file(file_or_content)
|
9
17
|
else
|
10
|
-
file_or_content
|
18
|
+
file_or_content
|
11
19
|
end
|
12
20
|
end
|
21
|
+
deprecate :file_data, :none, 2020, 1
|
13
22
|
|
14
|
-
def
|
15
|
-
a_file?(file_or_content) ?
|
23
|
+
def file?(file_or_content)
|
24
|
+
a_file?(file_or_content) || a_path?(file_or_content)
|
16
25
|
end
|
26
|
+
deprecate :file?, :none, 2020, 1
|
17
27
|
|
18
28
|
def write_tempfile(file_contents, file_key)
|
19
29
|
tempfile = Tempfile.new(file_key)
|
@@ -23,6 +33,16 @@ module PorkyLib::FileServiceHelper
|
|
23
33
|
tempfile
|
24
34
|
end
|
25
35
|
|
36
|
+
def read_file(file)
|
37
|
+
raise FileServiceError, 'file cannot be nil' if file.nil?
|
38
|
+
return file if !a_file?(file) && contain_null_byte?(file)
|
39
|
+
raise FileServiceError, 'The specified file does not exist' unless File.file?(file)
|
40
|
+
|
41
|
+
File.read(file)
|
42
|
+
rescue Errno::EACCES
|
43
|
+
raise FileServiceError, 'The specified file cannot be read, no permissions'
|
44
|
+
end
|
45
|
+
|
26
46
|
def perform_upload(bucket_name, file_key, tempfile, options)
|
27
47
|
obj = s3.bucket(bucket_name).object(file_key)
|
28
48
|
if options.key?(:metadata)
|
@@ -58,4 +78,17 @@ module PorkyLib::FileServiceHelper
|
|
58
78
|
def a_file?(file_or_content)
|
59
79
|
!file_or_content.is_a?(String)
|
60
80
|
end
|
81
|
+
|
82
|
+
def a_path?(content_or_path)
|
83
|
+
return false if contain_null_byte?(content_or_path)
|
84
|
+
|
85
|
+
File.file?(content_or_path)
|
86
|
+
end
|
87
|
+
|
88
|
+
def contain_null_byte?(data)
|
89
|
+
null_byte = (+"\u0000").force_encoding("ASCII-8BIT")
|
90
|
+
data = (+data).force_encoding("ASCII-8BIT")
|
91
|
+
|
92
|
+
data.include?(null_byte)
|
93
|
+
end
|
61
94
|
end
|
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'singleton'
|
4
4
|
|
5
5
|
class PorkyLib::Unencrypted::FileService
|
6
|
+
extend Gem::Deprecate
|
7
|
+
|
6
8
|
include Singleton
|
7
9
|
include PorkyLib::FileServiceHelper
|
8
10
|
|
@@ -26,10 +28,27 @@ class PorkyLib::Unencrypted::FileService
|
|
26
28
|
|
27
29
|
def write(file, bucket_name, options = {})
|
28
30
|
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name)
|
29
|
-
|
31
|
+
|
32
|
+
if file?(file)
|
33
|
+
write_file(file, bucket_name, options)
|
34
|
+
else
|
35
|
+
write_data(file, bucket_name, options)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
deprecate :write, 'write_file or write_data', 2020, 1
|
39
|
+
|
40
|
+
def write_file(file, bucket_name, options = {})
|
41
|
+
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(file, bucket_name)
|
42
|
+
|
43
|
+
write_data(read_file(file), bucket_name, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def write_data(data, bucket_name, options = {})
|
47
|
+
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(data, bucket_name)
|
48
|
+
raise FileSizeTooLargeError, "Data size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)
|
30
49
|
|
31
50
|
file_key = generate_file_key(options)
|
32
|
-
tempfile = write_tempfile(
|
51
|
+
tempfile = write_tempfile(data, file_key)
|
33
52
|
|
34
53
|
begin
|
35
54
|
perform_upload(bucket_name, file_key, tempfile, options)
|
@@ -42,7 +61,7 @@ class PorkyLib::Unencrypted::FileService
|
|
42
61
|
|
43
62
|
private
|
44
63
|
|
45
|
-
def input_invalid?(
|
46
|
-
|
64
|
+
def input_invalid?(file_or_data, bucket_name)
|
65
|
+
file_or_data.nil? || bucket_name.nil?
|
47
66
|
end
|
48
67
|
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.9.
|
4
|
+
version: 0.9.3
|
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-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-kms
|