porky_lib 0.9.4 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +11 -8
- data/README.md +45 -6
- data/Rakefile +1 -0
- data/lib/porky_lib/file_service.rb +5 -1
- data/lib/porky_lib/file_service_helper.rb +7 -5
- data/lib/porky_lib/unencrypted/file_service.rb +5 -1
- data/lib/porky_lib/version.rb +1 -1
- data/lib/tasks/file.rake +85 -0
- data/porky_lib.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbd7245d269cc4567f5d51dac830da9baeb088df1b4fb26a47c57115b6b29292
|
4
|
+
data.tar.gz: b1c55f42e2b30acfbe3ca9d88bd55ac9ef5d4b2e080c44563ac7a065eaeb1ff4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44a202622d1b47fe683e051ef790664b18fe0314cff2f1dfed90e630e4f76582cae2ba826349c01b91275413c4df142f6ed92c86d4c309ff19fe74796dbbe236
|
7
|
+
data.tar.gz: d10b7f7b4ac97ef7c3fff635087445e9f92f69d72d5b5c985188afbaa02e77015b54afa4c478eae125fbd11f4a39bb53c67dfabf23f2f5cfb29f201a5772e6f4
|
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.5)
|
5
5
|
aws-sdk-kms
|
6
6
|
aws-sdk-s3
|
7
7
|
msgpack
|
@@ -13,17 +13,17 @@ 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.
|
16
|
+
aws-partitions (1.259.0)
|
17
|
+
aws-sdk-core (3.86.0)
|
18
18
|
aws-eventstream (~> 1.0, >= 1.0.2)
|
19
19
|
aws-partitions (~> 1, >= 1.239.0)
|
20
20
|
aws-sigv4 (~> 1.1)
|
21
21
|
jmespath (~> 1.0)
|
22
|
-
aws-sdk-kms (1.
|
22
|
+
aws-sdk-kms (1.27.0)
|
23
23
|
aws-sdk-core (~> 3, >= 3.71.0)
|
24
24
|
aws-sigv4 (~> 1.1)
|
25
|
-
aws-sdk-s3 (1.
|
26
|
-
aws-sdk-core (~> 3, >= 3.
|
25
|
+
aws-sdk-s3 (1.60.1)
|
26
|
+
aws-sdk-core (~> 3, >= 3.83.0)
|
27
27
|
aws-sdk-kms (~> 1)
|
28
28
|
aws-sigv4 (~> 1.1)
|
29
29
|
aws-sigv4 (1.1.0)
|
@@ -32,6 +32,8 @@ GEM
|
|
32
32
|
bundler (>= 1.2.0, < 3)
|
33
33
|
thor (~> 0.18)
|
34
34
|
byebug (11.0.1)
|
35
|
+
codacy-coverage (2.1.5)
|
36
|
+
simplecov
|
35
37
|
codecov (0.1.16)
|
36
38
|
json
|
37
39
|
simplecov
|
@@ -69,7 +71,7 @@ GEM
|
|
69
71
|
rspec-support (3.9.0)
|
70
72
|
rspec_junit_formatter (0.4.1)
|
71
73
|
rspec-core (>= 2, < 4, != 2.12.0)
|
72
|
-
rubocop (0.
|
74
|
+
rubocop (0.78.0)
|
73
75
|
jaro_winkler (~> 1.5.1)
|
74
76
|
parallel (~> 1.10)
|
75
77
|
parser (>= 2.6)
|
@@ -78,7 +80,7 @@ GEM
|
|
78
80
|
unicode-display_width (>= 1.4.0, < 1.7)
|
79
81
|
rubocop-performance (1.5.1)
|
80
82
|
rubocop (>= 0.71.0)
|
81
|
-
rubocop-rspec (1.37.
|
83
|
+
rubocop-rspec (1.37.1)
|
82
84
|
rubocop (>= 0.68.1)
|
83
85
|
rubocop_runner (2.2.0)
|
84
86
|
ruby-progressbar (1.10.1)
|
@@ -101,6 +103,7 @@ DEPENDENCIES
|
|
101
103
|
bundler
|
102
104
|
bundler-audit
|
103
105
|
byebug
|
106
|
+
codacy-coverage
|
104
107
|
codecov
|
105
108
|
msgpack
|
106
109
|
porky_lib!
|
data/README.md
CHANGED
@@ -160,7 +160,7 @@ file_data = PorkyLib::Unencrypted::FileService.read(bucket_name, file_key)
|
|
160
160
|
# Where file is the data to encrypt and upload to S3 (can be a path or raw data or ruby file object)
|
161
161
|
# bucket_name is the name of the S3 bucket to write to
|
162
162
|
# key_id is the ID of the CMK to use to generate a data encryption key to encrypt the file data
|
163
|
-
# options is an optional parameter for specifying optional metadata about the file
|
163
|
+
# options is an optional parameter for specifying optional metadata about the file and the storage_class of the object
|
164
164
|
file_key = PorkyLib::FileService.write(file, bucket_name, key_id, options)
|
165
165
|
```
|
166
166
|
|
@@ -169,7 +169,7 @@ file_key = PorkyLib::FileService.write(file, bucket_name, key_id, options)
|
|
169
169
|
# Where file is the data to encrypt and upload to S3 (can be a path or ruby file object)
|
170
170
|
# bucket_name is the name of the S3 bucket to write to
|
171
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
|
172
|
+
# options is an optional parameter for specifying optional metadata about the file and the storage_class of the object
|
173
173
|
file_key = PorkyLib::FileService.write_file(file, bucket_name, key_id, options)
|
174
174
|
```
|
175
175
|
|
@@ -178,7 +178,7 @@ file_key = PorkyLib::FileService.write_file(file, bucket_name, key_id, options)
|
|
178
178
|
# Where data is the raw data to encrypt and upload to S3
|
179
179
|
# bucket_name is the name of the S3 bucket to write to
|
180
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
|
181
|
+
# options is an optional parameter for specifying optional metadata about the file and the storage_class of the object
|
182
182
|
file_key = PorkyLib::FileService.write_data(data, bucket_name, key_id, options)
|
183
183
|
```
|
184
184
|
|
@@ -187,7 +187,7 @@ file_key = PorkyLib::FileService.write_data(data, bucket_name, key_id, options)
|
|
187
187
|
# --- DEPRECATED --- Please use write_data or write_file instead of write
|
188
188
|
# Where file is the data to upload to S3 (can be a path or raw data or ruby file object)
|
189
189
|
# bucket_name is the name of the S3 bucket to write to
|
190
|
-
# options is an optional parameter for specifying optional metadata about the file
|
190
|
+
# options is an optional parameter for specifying optional metadata about the file and the storage_class of the object
|
191
191
|
file_key = PorkyLib::Unencrypted::FileService.write(file, bucket_name, options)
|
192
192
|
```
|
193
193
|
|
@@ -195,7 +195,7 @@ file_key = PorkyLib::Unencrypted::FileService.write(file, bucket_name, options)
|
|
195
195
|
```ruby
|
196
196
|
# Where file is the data to encrypt and upload to S3 (can be a path or ruby file object)
|
197
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
|
198
|
+
# options is an optional parameter for specifying optional metadata about the file and the storage_class of the object
|
199
199
|
file_key = PorkyLib::Unencrypted::FileService.write_file(file, bucket_name, options)
|
200
200
|
```
|
201
201
|
|
@@ -203,7 +203,7 @@ file_key = PorkyLib::Unencrypted::FileService.write_file(file, bucket_name, opti
|
|
203
203
|
```ruby
|
204
204
|
# Where data is the raw data to encrypt and upload to S3
|
205
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
|
206
|
+
# options is an optional parameter for specifying optional metadata about the file and the storage_class of the object
|
207
207
|
file_key = PorkyLib::Unencrypted::FileService.write_data(data, bucket_name, options)
|
208
208
|
```
|
209
209
|
|
@@ -224,6 +224,45 @@ To generate a new presigned GET url (used to download files directly from AWS S3
|
|
224
224
|
url = PorkyLib::Symmetric.instance.presigned_get_url(bucket_name, file_key)
|
225
225
|
```
|
226
226
|
|
227
|
+
## Rake task
|
228
|
+
If you want to write or read an encrypted file from the command line, there is a Rake write and read task.
|
229
|
+
|
230
|
+
> Note: the environment variables can be set globally or by prepending them to the rake task command
|
231
|
+
|
232
|
+
### Write file
|
233
|
+
|
234
|
+
Rake task name: `file:write`
|
235
|
+
|
236
|
+
Environment variables:
|
237
|
+
* Required
|
238
|
+
* `FILE_PATH` - Absolute or relative file path
|
239
|
+
* `CMK_KEY_ID` - Alias of the CMK key
|
240
|
+
* `AWS_S3_BUCKET` - AWS S3 bucket name
|
241
|
+
* `AWS_REGION` - AWS region name
|
242
|
+
* `AWS_ACCESS_KEY_ID` - AWS access key ID (credentials)
|
243
|
+
* `AWS_ACCESS_KEY` - AWS secret access key (credentials)
|
244
|
+
* Optional
|
245
|
+
* `AWS_S3_MOCK_CLIENT` - PorkyLib's AWS KMS mock client (defaults to `true`)
|
246
|
+
* `AWS_S3_MAX_FILE_SIZE` - Max file size (defaults to `1MB`)
|
247
|
+
* `AWS_S3_STORAGE_CLASS` - One of STANDARD, REDUCED_REDUNDANCY, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING, GLACIER, DEEP_ARCHIVE (defaults to `STANDARD`)
|
248
|
+
* `AWS_S3_KEEP_FILE_NAME` - Saves the file in AWS S3 with the original file name (defaults to `true`)
|
249
|
+
|
250
|
+
### Read file
|
251
|
+
|
252
|
+
Rake task name: `file:read`
|
253
|
+
|
254
|
+
Environment variables:
|
255
|
+
* Required
|
256
|
+
* `FILE_KEY` - AWS S3 object file key
|
257
|
+
* `AWS_S3_BUCKET` - AWS S3 bucket name
|
258
|
+
* `AWS_REGION` - AWS region name
|
259
|
+
* `AWS_ACCESS_KEY_ID` - AWS access key ID (credentials)
|
260
|
+
* `AWS_ACCESS_KEY` - AWS secret access key (credentials)
|
261
|
+
* Optional
|
262
|
+
* `AWS_S3_MOCK_CLIENT` - PorkyLib's AWS KMS mock client (defaults to `true`)
|
263
|
+
* `AWS_S3_MAX_FILE_SIZE` - Max file size (defaults to `1MB`)
|
264
|
+
* `DESTINATION` - Location to save the file (defaults to `FILE_KEY`)
|
265
|
+
|
227
266
|
## Development
|
228
267
|
|
229
268
|
Development on this project should occur on separate feature branches and pull requests should be submitted. When submitting a
|
data/Rakefile
CHANGED
@@ -77,7 +77,11 @@ class PorkyLib::FileService
|
|
77
77
|
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(data, bucket_name, key_id)
|
78
78
|
raise FileSizeTooLargeError, "Data size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)
|
79
79
|
|
80
|
-
file_key =
|
80
|
+
file_key = if options.key?(:file_name)
|
81
|
+
options[:file_name]
|
82
|
+
else
|
83
|
+
generate_file_key(options)
|
84
|
+
end
|
81
85
|
tempfile = encrypt_file_contents(data, key_id, file_key, options)
|
82
86
|
|
83
87
|
begin
|
@@ -36,11 +36,13 @@ module PorkyLib::FileServiceHelper
|
|
36
36
|
|
37
37
|
def perform_upload(bucket_name, file_key, tempfile, options)
|
38
38
|
obj = s3.bucket(bucket_name).object(file_key)
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
|
40
|
+
upload_options = {
|
41
|
+
metadata: (options[:metadata] if options.key?(:metadata)),
|
42
|
+
storage_class: (options[:storage_class] if options.key?(:storage_class))
|
43
|
+
}.compact
|
44
|
+
|
45
|
+
obj.upload_file(tempfile.path, upload_options)
|
44
46
|
end
|
45
47
|
|
46
48
|
def s3
|
@@ -47,7 +47,11 @@ class PorkyLib::Unencrypted::FileService
|
|
47
47
|
raise FileServiceError, 'Invalid input. One or more input values is nil' if input_invalid?(data, bucket_name)
|
48
48
|
raise FileSizeTooLargeError, "Data size is larger than maximum allowed size of #{max_file_size}" if data_size_invalid?(data)
|
49
49
|
|
50
|
-
file_key =
|
50
|
+
file_key = if options.key?(:file_name)
|
51
|
+
options[:file_name]
|
52
|
+
else
|
53
|
+
generate_file_key(options)
|
54
|
+
end
|
51
55
|
tempfile = write_tempfile(data, file_key)
|
52
56
|
|
53
57
|
begin
|
data/lib/porky_lib/version.rb
CHANGED
data/lib/tasks/file.rake
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'porky_lib'
|
4
|
+
|
5
|
+
namespace :file do
|
6
|
+
desc "Read a file from AWS S3"
|
7
|
+
task :read do
|
8
|
+
# Optional arguments
|
9
|
+
use_mock_client = ENV.fetch('AWS_S3_MOCK_CLIENT', 'true') == 'true'
|
10
|
+
max_file_size = ENV.fetch('AWS_S3_MAX_FILE_SIZE', 1_048_576).to_i
|
11
|
+
destination = ENV.fetch('DESTINATION', ENV['FILE_KEY'])
|
12
|
+
|
13
|
+
# Required arguments
|
14
|
+
arguments = {
|
15
|
+
file_key: ENV['FILE_KEY'],
|
16
|
+
aws_s3_bucket: ENV['AWS_S3_BUCKET'],
|
17
|
+
aws_region: ENV['AWS_REGION'],
|
18
|
+
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
|
19
|
+
aws_access_key: ENV['AWS_ACCESS_KEY']
|
20
|
+
}
|
21
|
+
|
22
|
+
# Checks presence of required arguments and configures porky_lib
|
23
|
+
check_arguments(arguments)
|
24
|
+
setup_porky_lib(arguments, use_mock_client, max_file_size)
|
25
|
+
|
26
|
+
# Reads and writes the file
|
27
|
+
message, = PorkyLib::FileService.instance.read(arguments[:aws_s3_bucket], arguments[:file_key])
|
28
|
+
file = File.open(destination, 'w')
|
29
|
+
file.puts(message)
|
30
|
+
file.close
|
31
|
+
|
32
|
+
puts "SUCCESS - Saved file: '#{destination}' with content of the bucket: '#{arguments[:aws_s3_bucket]}' with file_key: '#{arguments[:file_key]}'"
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Write a file to AWS S3"
|
36
|
+
task :write do
|
37
|
+
# Optional arguments
|
38
|
+
use_mock_client = ENV.fetch('AWS_S3_MOCK_CLIENT', 'true') == 'true'
|
39
|
+
max_file_size = ENV.fetch('AWS_S3_MAX_FILE_SIZE', 1_048_576).to_i
|
40
|
+
storage_class = ENV.fetch('AWS_S3_STORAGE_CLASS', 'STANDARD')
|
41
|
+
keep_file_name = ENV.fetch('AWS_S3_KEEP_FILE_NAME', 'true') == 'true'
|
42
|
+
|
43
|
+
# Required arguments
|
44
|
+
arguments = {
|
45
|
+
file_path: ENV['FILE_PATH'],
|
46
|
+
cmk_key_id: ENV['CMK_KEY_ID'],
|
47
|
+
aws_s3_bucket: ENV['AWS_S3_BUCKET'],
|
48
|
+
aws_region: ENV['AWS_REGION'],
|
49
|
+
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
|
50
|
+
aws_access_key: ENV['AWS_ACCESS_KEY']
|
51
|
+
}
|
52
|
+
|
53
|
+
# Checks presence of required arguments and configures porky_lib
|
54
|
+
check_arguments(arguments)
|
55
|
+
setup_porky_lib(arguments, use_mock_client, max_file_size)
|
56
|
+
|
57
|
+
write_options = {
|
58
|
+
storage_class: storage_class,
|
59
|
+
file_name: (File.basename(arguments[:file_path]) if keep_file_name)
|
60
|
+
}.compact
|
61
|
+
|
62
|
+
# Creates CMK key with empty tags and stores file
|
63
|
+
PorkyLib::Symmetric.instance.create_key([{}], arguments[:cmk_key_id]) unless PorkyLib::Symmetric.instance.cmk_alias_exists?(arguments[:cmk_key_id])
|
64
|
+
file_key = PorkyLib::FileService.instance.write_file(arguments[:file_path], arguments[:aws_s3_bucket], arguments[:cmk_key_id], write_options)
|
65
|
+
|
66
|
+
puts "SUCCESS - Created file: '#{arguments[:file_path]}' bucket: '#{arguments[:aws_s3_bucket]}' file_key: '#{file_key}'"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def check_arguments(arguments)
|
73
|
+
nil_arguments = []
|
74
|
+
arguments.map { |key, value| nil_arguments.push(key.to_s.upcase) if value.nil? && !key.nil? }
|
75
|
+
abort "ERROR - Need to provide as environment variables: #{nil_arguments.join(', ')}" unless nil_arguments.empty?
|
76
|
+
end
|
77
|
+
|
78
|
+
def setup_porky_lib(arguments, use_mock_client, max_file_size)
|
79
|
+
PorkyLib::Config.configure(aws_region: arguments[:aws_region],
|
80
|
+
aws_key_id: arguments[:aws_access_key_id],
|
81
|
+
aws_key_secret: arguments[:aws_access_key],
|
82
|
+
aws_client_mock: use_mock_client,
|
83
|
+
max_file_size: max_file_size)
|
84
|
+
PorkyLib::Config.initialize_aws
|
85
|
+
end
|
data/porky_lib.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency 'bundler'
|
26
26
|
spec.add_development_dependency 'bundler-audit'
|
27
27
|
spec.add_development_dependency 'byebug'
|
28
|
+
spec.add_development_dependency 'codacy-coverage'
|
28
29
|
spec.add_development_dependency 'codecov'
|
29
30
|
spec.add_development_dependency 'msgpack'
|
30
31
|
spec.add_development_dependency 'rake'
|
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.5
|
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-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-kms
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: codacy-coverage
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: codecov
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -390,6 +404,7 @@ files:
|
|
390
404
|
- lib/porky_lib/unencrypted.rb
|
391
405
|
- lib/porky_lib/unencrypted/file_service.rb
|
392
406
|
- lib/porky_lib/version.rb
|
407
|
+
- lib/tasks/file.rake
|
393
408
|
- porky_lib.gemspec
|
394
409
|
homepage: https://github.com/Zetatango/porky_lib
|
395
410
|
licenses: []
|