defra_ruby_aws 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/defra_ruby/aws/bucket.rb +14 -1
- data/lib/defra_ruby/aws/services/bucket_loader_service.rb +3 -1
- data/lib/defra_ruby/aws/version.rb +1 -1
- data/spec/examples.txt +37 -19
- data/spec/lib/defra_ruby/aws/bucket_spec.rb +155 -4
- data/spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb +47 -20
- 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: 7f0d5054fbb79afd158de9df8f72a117b757369315a0d66c06ee3664e14e906a
|
4
|
+
data.tar.gz: e41c2a7d8583c2b9e4d0835586074048f9d78d1bb0eb0829c04f7c5e1a189e04
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2955c3c540c3885b6ad13a1f05663ad80bc41dabbe088dceeb2500a1275f80072d237e68eceef863131f1d238142b848b35c38dc5cd58a8e888795cbd1ecb8e6
|
7
|
+
data.tar.gz: 1911659d171b1c4d0dd83702977527457bca619b90b37903a2b15a625ba02d1d91bb31fc20be3934e266885602ddb1245383a4453fce6bae6bb37c5167cd4354
|
data/README.md
CHANGED
@@ -41,7 +41,9 @@ DefraRuby::Aws.configure do |config|
|
|
41
41
|
secret_access_key: "SECRET_ACCESS_KEY"
|
42
42
|
},
|
43
43
|
# optional - Default to "eu-west-1"
|
44
|
-
region: "eu-west-
|
44
|
+
region: "eu-west-2",
|
45
|
+
# optional - Default to false. Will use AES256
|
46
|
+
encrypt_with_kms: true
|
45
47
|
}]
|
46
48
|
end
|
47
49
|
```
|
@@ -71,6 +73,7 @@ presigned_url = bucket.presigned_url("test-upload-file.csv")
|
|
71
73
|
```
|
72
74
|
|
73
75
|
### Delete a file from the bucket
|
76
|
+
|
74
77
|
```ruby
|
75
78
|
bucket = DefraRuby::Aws.get_bucket("defra-ruby-aws")
|
76
79
|
response = bucket.delete("test-upload-file.csv")
|
@@ -93,7 +96,7 @@ All contributions should be submitted via a pull request.
|
|
93
96
|
|
94
97
|
THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:
|
95
98
|
|
96
|
-
http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3
|
99
|
+
<http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3>
|
97
100
|
|
98
101
|
The following attribution statement MUST be cited in your products and applications when using this information.
|
99
102
|
|
@@ -3,12 +3,13 @@
|
|
3
3
|
module DefraRuby
|
4
4
|
module Aws
|
5
5
|
class Bucket
|
6
|
-
attr_reader :bucket_name, :credentials, :region
|
6
|
+
attr_reader :bucket_name, :credentials, :region, :encrypt_with_kms
|
7
7
|
|
8
8
|
def initialize(configs)
|
9
9
|
@credentials = configs[:credentials]
|
10
10
|
@bucket_name = configs[:name]
|
11
11
|
@region = setup_region(configs[:region])
|
12
|
+
@encrypt_with_kms = setup_encrypt_with_kms(configs[:encrypt_with_kms])
|
12
13
|
|
13
14
|
validate!
|
14
15
|
end
|
@@ -21,6 +22,10 @@ module DefraRuby
|
|
21
22
|
credentials[:secret_access_key]
|
22
23
|
end
|
23
24
|
|
25
|
+
def encryption_type
|
26
|
+
@_encryption_type ||= @encrypt_with_kms ? "aws:kms" : :AES256
|
27
|
+
end
|
28
|
+
|
24
29
|
def load(file)
|
25
30
|
BucketLoaderService.run(self, file)
|
26
31
|
end
|
@@ -47,6 +52,14 @@ module DefraRuby
|
|
47
52
|
"eu-west-1"
|
48
53
|
end
|
49
54
|
|
55
|
+
def setup_encrypt_with_kms(encrypt_with_kms)
|
56
|
+
return false if encrypt_with_kms.nil?
|
57
|
+
|
58
|
+
# Handle the argument being either a string or a boolean, or some other
|
59
|
+
# value e.g. "foo"
|
60
|
+
encrypt_with_kms.to_s.downcase == "true"
|
61
|
+
end
|
62
|
+
|
50
63
|
def validate!
|
51
64
|
validate_presence_of_name!
|
52
65
|
validate_presence_of_credentials!
|
@@ -24,7 +24,9 @@ module DefraRuby
|
|
24
24
|
|
25
25
|
def response_exe
|
26
26
|
lambda do
|
27
|
-
s3_bucket
|
27
|
+
s3_bucket
|
28
|
+
.object(File.basename(file.path))
|
29
|
+
.upload_file(file.path, server_side_encryption: bucket.encryption_type)
|
28
30
|
end
|
29
31
|
end
|
30
32
|
end
|
data/spec/examples.txt
CHANGED
@@ -1,22 +1,40 @@
|
|
1
1
|
example_id | status | run_time |
|
2
2
|
----------------------------------------------------------------------------------- | ------ | --------------- |
|
3
|
-
./spec/defra_ruby_aws_spec.rb[1:1] | passed | 0.
|
4
|
-
./spec/features/upload_file_to_bucket_spec.rb[1:1] | passed | 0.
|
5
|
-
./spec/features/upload_file_to_bucket_spec.rb[1:2] | passed | 0.
|
6
|
-
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:1:1] | passed | 0.
|
7
|
-
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:2:1] | passed | 0.
|
8
|
-
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:3:1] | passed | 0.
|
9
|
-
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:4:1] | passed | 0.
|
10
|
-
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:
|
11
|
-
./spec/lib/defra_ruby/aws/
|
12
|
-
./spec/lib/defra_ruby/aws/
|
13
|
-
./spec/lib/defra_ruby/aws/
|
14
|
-
./spec/lib/defra_ruby/aws/
|
15
|
-
./spec/lib/defra_ruby/aws/
|
16
|
-
./spec/lib/defra_ruby/aws/
|
17
|
-
./spec/lib/defra_ruby/aws/
|
3
|
+
./spec/defra_ruby_aws_spec.rb[1:1] | passed | 0.00095 seconds |
|
4
|
+
./spec/features/upload_file_to_bucket_spec.rb[1:1] | passed | 0.01317 seconds |
|
5
|
+
./spec/features/upload_file_to_bucket_spec.rb[1:2] | passed | 0.00448 seconds |
|
6
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:1:1] | passed | 0.00143 seconds |
|
7
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:2:1] | passed | 0.00013 seconds |
|
8
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:3:1] | passed | 0.0001 seconds |
|
9
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:4:1] | passed | 0.0001 seconds |
|
10
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:5:1:1] | passed | 0.00008 seconds |
|
11
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:5:2:1] | passed | 0.00008 seconds |
|
12
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:6:1] | passed | 0.00009 seconds |
|
13
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:1:1] | passed | 0.00009 seconds |
|
14
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:1:2] | passed | 0.00009 seconds |
|
15
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:2:1] | passed | 0.0001 seconds |
|
16
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:2:2] | passed | 0.0009 seconds |
|
17
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:1:1] | passed | 0.00008 seconds |
|
18
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:1:2] | passed | 0.00009 seconds |
|
19
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:2:1] | passed | 0.00009 seconds |
|
20
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:2:2] | passed | 0.00009 seconds |
|
21
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:3:1] | passed | 0.00009 seconds |
|
22
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:3:2] | passed | 0.00008 seconds |
|
23
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:4:1] | passed | 0.00008 seconds |
|
24
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:4:2] | passed | 0.00008 seconds |
|
25
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:5:1] | passed | 0.00009 seconds |
|
26
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:5:2] | passed | 0.00008 seconds |
|
27
|
+
./spec/lib/defra_ruby/aws/bucket_spec.rb[1:2:1] | passed | 0.00045 seconds |
|
28
|
+
./spec/lib/defra_ruby/aws/configuration_spec.rb[1:1:1] | passed | 0.00046 seconds |
|
29
|
+
./spec/lib/defra_ruby/aws/response_spec.rb[1:1:1:1] | passed | 0.00153 seconds |
|
30
|
+
./spec/lib/defra_ruby/aws/response_spec.rb[1:1:2:1] | passed | 0.00009 seconds |
|
31
|
+
./spec/lib/defra_ruby/aws/response_spec.rb[1:1:3:1] | passed | 0.00012 seconds |
|
32
|
+
./spec/lib/defra_ruby/aws/response_spec.rb[1:1:4:1] | passed | 0.0001 seconds |
|
33
|
+
./spec/lib/defra_ruby/aws/response_spec.rb[1:1:5:1] | passed | 0.00011 seconds |
|
34
|
+
./spec/lib/defra_ruby/aws/response_spec.rb[1:2:1:1] | passed | 0.0008 seconds |
|
18
35
|
./spec/lib/defra_ruby/aws/response_spec.rb[1:2:2:1] | passed | 0.0001 seconds |
|
19
|
-
./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:1]
|
20
|
-
./spec/lib/defra_ruby/aws/services/
|
21
|
-
./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:
|
22
|
-
./spec/lib/defra_ruby/aws/services/
|
36
|
+
./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:1:1] | passed | 0.00063 seconds |
|
37
|
+
./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:2:1] | passed | 0.00911 seconds |
|
38
|
+
./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:1] | passed | 0.00062 seconds |
|
39
|
+
./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:2:1] | passed | 0.00059 seconds |
|
40
|
+
./spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb[1:1:1] | passed | 0.00398 seconds |
|
@@ -7,6 +7,8 @@ module DefraRuby
|
|
7
7
|
RSpec.describe Bucket do
|
8
8
|
subject(:bucket) { described_class.new(configs) }
|
9
9
|
|
10
|
+
let(:credentials) { { access_key_id: "access_key", secret_access_key: "secret" } }
|
11
|
+
|
10
12
|
describe "#initialize" do
|
11
13
|
context "when a name configuration is missing" do
|
12
14
|
let(:configs) do
|
@@ -63,16 +65,165 @@ module DefraRuby
|
|
63
65
|
expect { bucket }.to raise_error("DefraRuby::Aws buckets configurations: missing secret access key for bucket foo")
|
64
66
|
end
|
65
67
|
end
|
68
|
+
|
69
|
+
context "when 'region' is not set" do
|
70
|
+
context "because it has not been added to the config" do
|
71
|
+
let(:configs) do
|
72
|
+
{
|
73
|
+
name: "foo",
|
74
|
+
credentials: credentials
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
it "defaults the region to 'eu-west-1'" do
|
79
|
+
expect(bucket.region).to eq("eu-west-1")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "because its value is an empty string" do
|
84
|
+
let(:configs) do
|
85
|
+
{
|
86
|
+
name: "foo",
|
87
|
+
credentials: credentials,
|
88
|
+
region: ""
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
it "defaults the region to 'eu-west-1'" do
|
93
|
+
expect(bucket.region).to eq("eu-west-1")
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when 'region' is set" do
|
99
|
+
let(:region) { "eu-west-2" }
|
100
|
+
let(:configs) do
|
101
|
+
{
|
102
|
+
name: "foo",
|
103
|
+
credentials: credentials,
|
104
|
+
region: region
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
it "sets the region to match" do
|
109
|
+
expect(bucket.region).to eq(region)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "when 'encrypt_with_kms' is not set" do
|
114
|
+
context "because it has not been added to the config" do
|
115
|
+
let(:configs) do
|
116
|
+
{
|
117
|
+
name: "foo",
|
118
|
+
credentials: credentials
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
it "defaults encrypt_with_kms to false" do
|
123
|
+
expect(bucket.encrypt_with_kms).to be false
|
124
|
+
end
|
125
|
+
|
126
|
+
it "sets encryption_type to :AES256" do
|
127
|
+
expect(bucket.encryption_type).to eq(:AES256)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "because its value is an empty string" do
|
132
|
+
let(:configs) do
|
133
|
+
{
|
134
|
+
name: "foo",
|
135
|
+
credentials: credentials,
|
136
|
+
encrypt_with_kms: ""
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
it "defaults encrypt_with_kms to false" do
|
141
|
+
expect(bucket.encrypt_with_kms).to be false
|
142
|
+
end
|
143
|
+
|
144
|
+
it "sets encryption_type to :AES256" do
|
145
|
+
expect(bucket.encryption_type).to eq(:AES256)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "when 'encrypt_with_kms' is set" do
|
151
|
+
let(:encrypt_with_kms) { true }
|
152
|
+
let(:configs) do
|
153
|
+
{
|
154
|
+
name: "foo",
|
155
|
+
credentials: credentials,
|
156
|
+
encrypt_with_kms: encrypt_with_kms
|
157
|
+
}
|
158
|
+
end
|
159
|
+
|
160
|
+
context "to true as a boolean" do
|
161
|
+
let(:encrypt_with_kms) { true }
|
162
|
+
|
163
|
+
it "defaults encrypt_with_kms to true" do
|
164
|
+
expect(bucket.encrypt_with_kms).to be true
|
165
|
+
end
|
166
|
+
|
167
|
+
it "sets encryption_type to aws:kms" do
|
168
|
+
expect(bucket.encryption_type).to eq("aws:kms")
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context "to true as a string" do
|
173
|
+
let(:encrypt_with_kms) { "true" }
|
174
|
+
|
175
|
+
it "defaults encrypt_with_kms to true" do
|
176
|
+
expect(bucket.encrypt_with_kms).to be true
|
177
|
+
end
|
178
|
+
|
179
|
+
it "sets encryption_type to aws:kms" do
|
180
|
+
expect(bucket.encryption_type).to eq("aws:kms")
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
context "to false as a boolean" do
|
185
|
+
let(:encrypt_with_kms) { false }
|
186
|
+
|
187
|
+
it "defaults encrypt_with_kms to false" do
|
188
|
+
expect(bucket.encrypt_with_kms).to be false
|
189
|
+
end
|
190
|
+
|
191
|
+
it "sets encryption_type to aws:kms" do
|
192
|
+
expect(bucket.encryption_type).to eq(:AES256)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
context "to false as a string" do
|
197
|
+
let(:encrypt_with_kms) { "false" }
|
198
|
+
|
199
|
+
it "defaults encrypt_with_kms to false" do
|
200
|
+
expect(bucket.encrypt_with_kms).to be false
|
201
|
+
end
|
202
|
+
|
203
|
+
it "sets encryption_type to aws:kms" do
|
204
|
+
expect(bucket.encryption_type).to eq(:AES256)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
context "to something not recognised" do
|
209
|
+
let(:encrypt_with_kms) { "bar" }
|
210
|
+
|
211
|
+
it "defaults encrypt_with_kms to false" do
|
212
|
+
expect(bucket.encrypt_with_kms).to be false
|
213
|
+
end
|
214
|
+
|
215
|
+
it "sets encryption_type to :AES256" do
|
216
|
+
expect(bucket.encryption_type).to eq(:AES256)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
66
220
|
end
|
67
221
|
|
68
222
|
describe "#load" do
|
69
223
|
let(:configs) do
|
70
224
|
{
|
71
225
|
name: "foo",
|
72
|
-
credentials:
|
73
|
-
secret_access_key: "secret",
|
74
|
-
access_key_id: "access_key"
|
75
|
-
}
|
226
|
+
credentials: credentials
|
76
227
|
}
|
77
228
|
end
|
78
229
|
|
@@ -5,31 +5,58 @@ require "spec_helper"
|
|
5
5
|
module DefraRuby
|
6
6
|
module Aws
|
7
7
|
RSpec.describe BucketLoaderService do
|
8
|
+
|
8
9
|
describe "#run" do
|
9
|
-
let(:
|
10
|
-
{
|
11
|
-
credentials: {
|
12
|
-
access_key_id: "key_id",
|
13
|
-
secret_access_key: "secret"
|
14
|
-
},
|
15
|
-
name: "bulk"
|
16
|
-
}
|
17
|
-
end
|
10
|
+
let(:credentials) { { access_key_id: "key_id", secret_access_key: "secret" } }
|
18
11
|
let(:bucket) { Bucket.new(configs) }
|
19
12
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
13
|
+
context "when 'encrypt_with_kms' is not set" do
|
14
|
+
let(:configs) do
|
15
|
+
{
|
16
|
+
credentials: credentials,
|
17
|
+
name: "bulk"
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
it "loads the given file to the s3 bucket using :AES256" do
|
22
|
+
aws_resource = double(:aws_resource)
|
23
|
+
s3_bucket = double(:s3_bulk_bucket)
|
24
|
+
file = double(:file, path: "foo/bar/baz/test.csv")
|
25
|
+
s3_object = double(:s3_object)
|
26
|
+
result = double(:result)
|
27
|
+
|
28
|
+
expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
|
29
|
+
expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
|
30
|
+
expect(s3_bucket).to receive(:object).with("test.csv").and_return(s3_object)
|
31
|
+
expect(s3_object).to receive(:upload_file).with("foo/bar/baz/test.csv", server_side_encryption: :AES256).and_return(result)
|
32
|
+
|
33
|
+
expect(described_class.run(bucket, file)).to be_a(Response)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when 'encrypt_with_kms' is set" do
|
38
|
+
let(:configs) do
|
39
|
+
{
|
40
|
+
credentials: credentials,
|
41
|
+
name: "bulk",
|
42
|
+
encrypt_with_kms: true
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
it "loads the given file to the s3 bucket using AWS:KMS" do
|
47
|
+
aws_resource = double(:aws_resource)
|
48
|
+
s3_bucket = double(:s3_bulk_bucket)
|
49
|
+
file = double(:file, path: "foo/bar/baz/test.csv")
|
50
|
+
s3_object = double(:s3_object)
|
51
|
+
result = double(:result)
|
26
52
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
53
|
+
expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
|
54
|
+
expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
|
55
|
+
expect(s3_bucket).to receive(:object).with("test.csv").and_return(s3_object)
|
56
|
+
expect(s3_object).to receive(:upload_file).with("foo/bar/baz/test.csv", server_side_encryption: "aws:kms").and_return(result)
|
31
57
|
|
32
|
-
|
58
|
+
expect(described_class.run(bucket, file)).to be_a(Response)
|
59
|
+
end
|
33
60
|
end
|
34
61
|
end
|
35
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defra_ruby_aws
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Defra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-s3
|