defra_ruby_aws 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77814e6b155751f6f9008333ae24e48849522e3dc948ff983255a19534e29fc6
4
- data.tar.gz: 551dd9f6bef0b52acdab49a6c7121627fb8fb6959ff119e6d3e00bf00dc292a9
3
+ metadata.gz: 1da7a16c3acdb14eb8690cdebe2586e93ec72acc9a93f3a4ad9f2f9ce315334c
4
+ data.tar.gz: 5a65b5bc761856abb099cbdf26da4335eba87d0afb443338e2529cfa6f30f863
5
5
  SHA512:
6
- metadata.gz: d02db2c572ae6e19af365393ebc8e6d804b65d783e9ff80f72c2a820e8a2bc54443c6e4e32aeac9b32c8ec8057f8f106c1f4e5f249e1f95adc31c8023a87f8ef
7
- data.tar.gz: bb0d956c1e085dd7c1ebb62d406fec5bc74938d8464c6f1a98698892086e5af6cae1a968e62f8d7b6bdbbbbf0e10fc3cd5ebb55b0d0b7952afcefa7864bb00aa
6
+ metadata.gz: 6f382ccf1699597d1040a9978eadf01c7e96c9b7fa1826dd8c2ac96f1bdd58f96286fe83809e8a9ab2be4cc4843e4db607d27290797270a758b47e4097830a81
7
+ data.tar.gz: 518b0337d6568a44c4fce640e4ebc0775147503dd78df1b2bafb79195ca708855c204ed843906d759ef0de9451f47dfb8026d975c8eb7007c8fc64855d2a652a
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Defra Ruby Aws
2
2
 
3
- [![Build Status](https://travis-ci.com/DEFRA/defra-ruby-aws.svg?branch=main)](https://travis-ci.com/DEFRA/defra-ruby-aws)
3
+ ![Build Status](https://github.com/DEFRA/defra-ruby-aws/workflows/CI/badge.svg?branch=main)
4
4
  [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_defra-ruby-aws&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=DEFRA_defra-ruby-aws)
5
5
  [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_defra-ruby-aws&metric=coverage)](https://sonarcloud.io/dashboard?id=DEFRA_defra-ruby-aws)
6
6
  [![security](https://hakiri.io/github/DEFRA/defra-ruby-aws/main.svg)](https://hakiri.io/github/DEFRA/defra-ruby-aws/main)
@@ -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-1"
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,16 +22,24 @@ module DefraRuby
21
22
  credentials[:secret_access_key]
22
23
  end
23
24
 
24
- def load(file)
25
- BucketLoaderService.run(self, file)
25
+ def encryption_type
26
+ @_encryption_type ||= @encrypt_with_kms ? "aws:kms" : :AES256
26
27
  end
27
28
 
28
- def presigned_url(file_name)
29
- PresignedUrlService.run(self, file_name)
29
+ def load(file, options = {})
30
+ BucketLoaderService.run(self, file, options)
30
31
  end
31
32
 
32
- def delete(file_name)
33
- DeleteFileFromBucketService.run(self, file_name)
33
+ def presigned_url(file_name, options = {})
34
+ PresignedUrlService.run(self, file_name, options)
35
+ end
36
+
37
+ def delete(file_name, options = {})
38
+ DeleteFileFromBucketService.run(self, file_name, options)
39
+ end
40
+
41
+ def list_files(directory_name, options = {})
42
+ ListBucketFilesService.run(self, directory_name, options)
34
43
  end
35
44
 
36
45
  private
@@ -47,6 +56,14 @@ module DefraRuby
47
56
  "eu-west-1"
48
57
  end
49
58
 
59
+ def setup_encrypt_with_kms(encrypt_with_kms)
60
+ return false if encrypt_with_kms.nil?
61
+
62
+ # Handle the argument being either a string or a boolean, or some other
63
+ # value e.g. "foo"
64
+ encrypt_with_kms.to_s.downcase == "true"
65
+ end
66
+
50
67
  def validate!
51
68
  validate_presence_of_name!
52
69
  validate_presence_of_credentials!
@@ -5,11 +5,12 @@ module DefraRuby
5
5
  class UnsuccessfulOperation < StandardError; end
6
6
 
7
7
  class Response
8
- attr_reader :error
8
+ attr_reader :error, :result
9
9
 
10
10
  def initialize(response_exe)
11
11
  @success = true
12
12
  @error = nil
13
+ @result = nil
13
14
 
14
15
  capture_response(response_exe)
15
16
  end
@@ -23,7 +24,8 @@ module DefraRuby
23
24
  attr_reader :success
24
25
 
25
26
  def capture_response(response_exe)
26
- raise UnsuccessfulOperation unless response_exe.call
27
+ @result = response_exe.call
28
+ raise UnsuccessfulOperation unless @result
27
29
  rescue StandardError => e
28
30
  @error = e
29
31
  @success = false
@@ -5,13 +5,14 @@ module DefraRuby
5
5
  class BucketLoaderService
6
6
  include HasAwsBucketConfiguration
7
7
 
8
- def self.run(bucket, file)
9
- new(bucket, file).run
8
+ def self.run(bucket, file, options = {})
9
+ new(bucket, file, options).run
10
10
  end
11
11
 
12
- def initialize(bucket, file)
12
+ def initialize(bucket, file, options)
13
13
  @bucket = bucket
14
14
  @file = file
15
+ @dir = options[:s3_directory]
15
16
  end
16
17
 
17
18
  def run
@@ -20,13 +21,19 @@ module DefraRuby
20
21
 
21
22
  private
22
23
 
23
- attr_reader :bucket, :file
24
+ attr_reader :bucket, :file, :dir
24
25
 
25
26
  def response_exe
26
27
  lambda do
27
- s3_bucket.object(File.basename(file.path)).upload_file(file.path, server_side_encryption: "aws:kms")
28
+ s3_bucket
29
+ .object(destination)
30
+ .upload_file(file.path, server_side_encryption: bucket.encryption_type)
28
31
  end
29
32
  end
33
+
34
+ def destination
35
+ [*dir, File.basename(file.path)].compact.join("/")
36
+ end
30
37
  end
31
38
  end
32
39
  end
@@ -5,13 +5,14 @@ module DefraRuby
5
5
  class DeleteFileFromBucketService
6
6
  include HasAwsBucketConfiguration
7
7
 
8
- def self.run(bucket, file_name)
9
- new(bucket, file_name).run
8
+ def self.run(bucket, file_name, options = {})
9
+ new(bucket, file_name, options).run
10
10
  end
11
11
 
12
- def initialize(bucket, file_name)
12
+ def initialize(bucket, file_name, options)
13
13
  @bucket = bucket
14
14
  @file_name = file_name
15
+ @dir = options[:s3_directory]
15
16
  end
16
17
 
17
18
  def run
@@ -20,15 +21,19 @@ module DefraRuby
20
21
 
21
22
  private
22
23
 
23
- attr_reader :bucket, :file_name
24
+ attr_reader :bucket, :file_name, :dir
24
25
 
25
26
  def response_exe
26
27
  lambda do
27
- delete_object_output = s3_bucket.object(file_name).delete
28
+ delete_object_output = s3_bucket.object(destination).delete
28
29
 
29
30
  delete_object_output.request_charged.length.positive?
30
31
  end
31
32
  end
33
+
34
+ def destination
35
+ [*dir, file_name].compact.join("/")
36
+ end
32
37
  end
33
38
  end
34
39
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefraRuby
4
+ module Aws
5
+ class ListBucketFilesService
6
+ include HasAwsBucketConfiguration
7
+
8
+ def self.run(bucket, folder, options = {})
9
+ new(bucket, folder, options).run
10
+ end
11
+
12
+ def initialize(bucket, folder, options)
13
+ @bucket = bucket
14
+ @folder = folder
15
+ @dir = options[:s3_directory]
16
+ end
17
+
18
+ def run
19
+ Response.new(response_exe)
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :bucket, :dir
25
+
26
+ def response_exe
27
+ lambda do
28
+ s3_bucket.objects(prefix: @folder).collect(&:key)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -5,17 +5,18 @@ module DefraRuby
5
5
  class PresignedUrlService
6
6
  include HasAwsBucketConfiguration
7
7
 
8
- def self.run(bucket, file_name)
9
- new(bucket, file_name).run
8
+ def self.run(bucket, file_name, options = {})
9
+ new(bucket, file_name, options).run
10
10
  end
11
11
 
12
- def initialize(bucket, file_name)
12
+ def initialize(bucket, file_name, options)
13
13
  @bucket = bucket
14
14
  @file_name = file_name
15
+ @dir = options[:s3_directory]
15
16
  end
16
17
 
17
18
  def run
18
- s3_bucket.object(file_name).presigned_url(
19
+ s3_bucket.object(destination).presigned_url(
19
20
  :get,
20
21
  expires_in: 20 * 60, # 20 minutes in seconds
21
22
  secure: true,
@@ -26,7 +27,11 @@ module DefraRuby
26
27
 
27
28
  private
28
29
 
29
- attr_reader :bucket, :file_name
30
+ attr_reader :bucket, :file_name, :dir
31
+
32
+ def destination
33
+ [*dir, file_name].compact.join("/")
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module DefraRuby
4
4
  module Aws
5
- VERSION = "0.3.0"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
@@ -8,6 +8,7 @@ require_relative "aws/services/concerns/has_aws_bucket_configuration"
8
8
  require_relative "aws/services/bucket_loader_service"
9
9
  require_relative "aws/services/presigned_url_service"
10
10
  require_relative "aws/services/delete_file_from_bucket_service"
11
+ require_relative "aws/services/list_bucket_files_service"
11
12
 
12
13
  module DefraRuby
13
14
  module Aws
data/spec/examples.txt CHANGED
@@ -1,22 +1,48 @@
1
- example_id | status | run_time |
2
- ----------------------------------------------------------------------------------- | ------ | --------------- |
3
- ./spec/defra_ruby_aws_spec.rb[1:1] | passed | 0.01013 seconds |
4
- ./spec/features/upload_file_to_bucket_spec.rb[1:1] | passed | 0.01626 seconds |
5
- ./spec/features/upload_file_to_bucket_spec.rb[1:2] | passed | 0.00509 seconds |
6
- ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:1:1] | passed | 0.00012 seconds |
7
- ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:2:1] | passed | 0.00011 seconds |
8
- ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:3:1] | passed | 0.00121 seconds |
9
- ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:4:1] | passed | 0.00011 seconds |
10
- ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:2:1] | passed | 0.0014 seconds |
11
- ./spec/lib/defra_ruby/aws/configuration_spec.rb[1:1:1] | passed | 0.00066 seconds |
12
- ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:1:1] | passed | 0.00017 seconds |
13
- ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:2:1] | passed | 0.00011 seconds |
14
- ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:3:1] | passed | 0.0025 seconds |
15
- ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:4:1] | passed | 0.00013 seconds |
16
- ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:5:1] | passed | 0.00014 seconds |
17
- ./spec/lib/defra_ruby/aws/response_spec.rb[1:2:1:1] | passed | 0.00088 seconds |
18
- ./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] | passed | 0.00061 seconds |
20
- ./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:1] | passed | 0.00875 seconds |
21
- ./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:2:1] | passed | 0.00067 seconds |
22
- ./spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb[1:1:1] | passed | 0.00408 seconds |
1
+ example_id | status | run_time |
2
+ ------------------------------------------------------------------------------------- | ------ | --------------- |
3
+ ./spec/defra_ruby_aws_spec.rb[1:1] | passed | 0.00115 seconds |
4
+ ./spec/features/upload_file_to_bucket_spec.rb[1:1] | passed | 0.00635 seconds |
5
+ ./spec/features/upload_file_to_bucket_spec.rb[1:2] | passed | 0.00572 seconds |
6
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:1:1] | passed | 0.00014 seconds |
7
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:2:1] | passed | 0.00012 seconds |
8
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:3:1] | passed | 0.00013 seconds |
9
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:4:1] | passed | 0.00174 seconds |
10
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:5:1:1] | passed | 0.00011 seconds |
11
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:5:2:1] | passed | 0.0001 seconds |
12
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:6:1] | passed | 0.00011 seconds |
13
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:1:1] | passed | 0.00016 seconds |
14
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:1:2] | passed | 0.00012 seconds |
15
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:2:1] | passed | 0.00011 seconds |
16
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:7:2:2] | passed | 0.00012 seconds |
17
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:1:1] | passed | 0.0001 seconds |
18
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:1:2] | passed | 0.00012 seconds |
19
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:2:1] | passed | 0.00012 seconds |
20
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:2:2] | passed | 0.00011 seconds |
21
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:3:1] | passed | 0.0001 seconds |
22
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:3:2] | passed | 0.00011 seconds |
23
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:4:1] | passed | 0.00011 seconds |
24
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:4:2] | passed | 0.00011 seconds |
25
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:5:1] | passed | 0.00012 seconds |
26
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:1:8:5:2] | passed | 0.0001 seconds |
27
+ ./spec/lib/defra_ruby/aws/bucket_spec.rb[1:2:1] | passed | 0.00056 seconds |
28
+ ./spec/lib/defra_ruby/aws/configuration_spec.rb[1:1:1] | passed | 0.00058 seconds |
29
+ ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:1:1] | passed | 0.00193 seconds |
30
+ ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:2:1] | passed | 0.00015 seconds |
31
+ ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:3:1] | passed | 0.0001 seconds |
32
+ ./spec/lib/defra_ruby/aws/response_spec.rb[1:1:4:1] | passed | 0.00012 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.00066 seconds |
35
+ ./spec/lib/defra_ruby/aws/response_spec.rb[1:3:1:1] | passed | 0.0001 seconds |
36
+ ./spec/lib/defra_ruby/aws/response_spec.rb[1:3:2:1] | passed | 0.00009 seconds |
37
+ ./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:1:1] | passed | 0.00849 seconds |
38
+ ./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:1:2:1] | passed | 0.00065 seconds |
39
+ ./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:2:1] | passed | 0.0006 seconds |
40
+ ./spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb[1:1:2:2:1] | passed | 0.0006 seconds |
41
+ ./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:1] | passed | 0.00089 seconds |
42
+ ./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:2:1] | passed | 0.00078 seconds |
43
+ ./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:3:1] | passed | 0.00079 seconds |
44
+ ./spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb[1:1:3:2:1] | passed | 0.00077 seconds |
45
+ ./spec/lib/defra_ruby/aws/services/list_bucket_files_service_spec.rb[1:1:1:1] | passed | 0.00247 seconds |
46
+ ./spec/lib/defra_ruby/aws/services/list_bucket_files_service_spec.rb[1:1:2:1] | passed | 0.00093 seconds |
47
+ ./spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb[1:1:1] | passed | 0.02254 seconds |
48
+ ./spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb[1:1:2:1] | passed | 0.00314 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,25 +65,175 @@ 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
 
79
230
  it "loads the given file to the s3 bucket" do
80
231
  result = double(:result)
81
232
  file = double(:file)
233
+ options = double(:options)
82
234
 
83
- expect(BucketLoaderService).to receive(:run).with(bucket, file).and_return(result)
84
- expect(bucket.load(file)).to eq(result)
235
+ expect(BucketLoaderService).to receive(:run).with(bucket, file, options).and_return(result)
236
+ expect(bucket.load(file, options)).to eq(result)
85
237
  end
86
238
  end
87
239
  end
@@ -49,6 +49,16 @@ module DefraRuby
49
49
  end
50
50
  end
51
51
 
52
+ describe "#result" do
53
+ context "when the response includes a result object" do
54
+ let(:response_exe) { -> { "I am an object" } }
55
+
56
+ it "returns the s3 result object" do
57
+ expect(described_class.new(response_exe).result).to eq "I am an object"
58
+ end
59
+ end
60
+ end
61
+
52
62
  describe "#error" do
53
63
  context "when the response throws an error" do
54
64
  let(:response_exe) { -> { raise StandardError, "Boom!" } }
@@ -5,31 +5,94 @@ 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(:configs) do
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
- it "loads the given file to the s3 bucket" do
21
- aws_resource = double(:aws_resource)
22
- s3_bucket = double(:s3_bulk_bucket)
23
- file = double(:file, path: "foo/bar/baz/test.csv")
24
- s3_object = double(:s3_object)
25
- result = double(:result)
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
+
36
+ context "when an s3_directory is provided" do
37
+ it "loads the given file to the s3 bucket at the correct location using AWS:KMS" do
38
+ aws_resource = double(:aws_resource)
39
+ s3_bucket = double(:s3_bulk_bucket)
40
+ file = double(:file, path: "foo/bar/baz/test.csv")
41
+ s3_object = double(:s3_object)
42
+ result = double(:result)
43
+ options = { s3_directory: "directory" }
44
+
45
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
46
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
47
+ expect(s3_bucket).to receive(:object).with("directory/test.csv").and_return(s3_object)
48
+ expect(s3_object).to receive(:upload_file).with("foo/bar/baz/test.csv", server_side_encryption: :AES256).and_return(result)
49
+
50
+ expect(described_class.run(bucket, file, options)).to be_a(Response)
51
+ end
52
+ end
53
+ end
54
+
55
+ context "when 'encrypt_with_kms' is set" do
56
+ let(:configs) do
57
+ {
58
+ credentials: credentials,
59
+ name: "bulk",
60
+ encrypt_with_kms: true
61
+ }
62
+ end
63
+
64
+ it "loads the given file to the s3 bucket using AWS:KMS" do
65
+ aws_resource = double(:aws_resource)
66
+ s3_bucket = double(:s3_bulk_bucket)
67
+ file = double(:file, path: "foo/bar/baz/test.csv")
68
+ s3_object = double(:s3_object)
69
+ result = double(:result)
70
+
71
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
72
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
73
+ expect(s3_bucket).to receive(:object).with("test.csv").and_return(s3_object)
74
+ expect(s3_object).to receive(:upload_file).with("foo/bar/baz/test.csv", server_side_encryption: "aws:kms").and_return(result)
75
+
76
+ expect(described_class.run(bucket, file)).to be_a(Response)
77
+ end
78
+
79
+ context "when an s3_directory is provided" do
80
+ it "loads the given file to the s3 bucket at the correct location using AWS:KMS" do
81
+ aws_resource = double(:aws_resource)
82
+ s3_bucket = double(:s3_bulk_bucket)
83
+ file = double(:file, path: "foo/bar/baz/test.csv")
84
+ s3_object = double(:s3_object)
85
+ result = double(:result)
86
+ options = { s3_directory: %w[directory second_directory] }
26
87
 
27
- expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
28
- expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
29
- expect(s3_bucket).to receive(:object).with("test.csv").and_return(s3_object)
30
- expect(s3_object).to receive(:upload_file).with("foo/bar/baz/test.csv", server_side_encryption: "aws:kms").and_return(result)
88
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
89
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
90
+ expect(s3_bucket).to receive(:object).with("directory/second_directory/test.csv").and_return(s3_object)
91
+ expect(s3_object).to receive(:upload_file).with("foo/bar/baz/test.csv", server_side_encryption: "aws:kms").and_return(result)
31
92
 
32
- expect(described_class.run(bucket, file)).to be_a(Response)
93
+ expect(described_class.run(bucket, file, options)).to be_a(Response)
94
+ end
95
+ end
33
96
  end
34
97
  end
35
98
  end
@@ -33,6 +33,25 @@ module DefraRuby
33
33
  expect(response).to be_successful
34
34
  end
35
35
 
36
+ context "when an s3_directory is provided" do
37
+ it "returns a successful response" do
38
+ aws_resource = double(:aws_resource)
39
+ s3_bucket = double(:s3_bulk_bucket)
40
+ file_name = "test.csv"
41
+ s3_object = double(:s3_object)
42
+ result = double(:result, request_charged: "present")
43
+ options = { s3_directory: "directory" }
44
+
45
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
46
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
47
+ expect(s3_bucket).to receive(:object).with("directory/test.csv").and_return(s3_object)
48
+ expect(s3_object).to receive(:delete).and_return(result)
49
+
50
+ response = described_class.run(bucket, file_name, options)
51
+ expect(response).to be_successful
52
+ end
53
+ end
54
+
36
55
  context "when the response body returns an empty charged requests" do
37
56
  it "returns a non successful response" do
38
57
  aws_resource = double(:aws_resource)
@@ -49,6 +68,25 @@ module DefraRuby
49
68
  response = described_class.run(bucket, file_name)
50
69
  expect(response).to_not be_successful
51
70
  end
71
+
72
+ context "when an s3_directory is provided" do
73
+ it "returns a non successful response" do
74
+ aws_resource = double(:aws_resource)
75
+ s3_bucket = double(:s3_bulk_bucket)
76
+ file_name = "test.csv"
77
+ s3_object = double(:s3_object)
78
+ result = double(:result, request_charged: "")
79
+ options = { s3_directory: %w[directory second_directory] }
80
+
81
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
82
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
83
+ expect(s3_bucket).to receive(:object).with("directory/second_directory/test.csv").and_return(s3_object)
84
+ expect(s3_object).to receive(:delete).and_return(result)
85
+
86
+ response = described_class.run(bucket, file_name, options)
87
+ expect(response).to_not be_successful
88
+ end
89
+ end
52
90
  end
53
91
  end
54
92
  end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ module DefraRuby
6
+ module Aws
7
+ RSpec.describe ListBucketFilesService do
8
+ describe "#run" do
9
+ let(:configs) do
10
+ {
11
+ credentials: {
12
+ access_key_id: "key_id",
13
+ secret_access_key: "secret"
14
+ },
15
+ name: "bulk"
16
+ }
17
+ end
18
+ let(:bucket) { Bucket.new(configs) }
19
+
20
+ context "when an s3_directory is provided" do
21
+ it "returns a successful response" do
22
+ aws_resource = double(:aws_resource)
23
+ s3_bucket = double(:s3_bulk_bucket)
24
+ s3_object1 = double(:s3_object)
25
+ s3_object2 = double(:s3_object)
26
+ s3_objects = [s3_object1, s3_object2]
27
+ s3_directory = "/a_directory"
28
+ options = {}
29
+
30
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
31
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
32
+ expect(s3_bucket).to receive(:objects).with(prefix: s3_directory).and_return(s3_objects)
33
+ expect(s3_object1).to receive(:key).and_return("object1").at_least(:once)
34
+ expect(s3_object2).to receive(:key).and_return("object2").at_least(:once)
35
+
36
+ response = described_class.run(bucket, s3_directory, options)
37
+ expect(response).to be_successful
38
+ expect(response.result).to match_array([s3_object1.key, s3_object2.key])
39
+ end
40
+ end
41
+
42
+ context "when an s3_directory is not provided" do
43
+ it "returns a non successful response" do
44
+ aws_resource = double(:aws_resource)
45
+ s3_bucket = double(:s3_bulk_bucket)
46
+ s3_directory = nil
47
+
48
+ expect(::Aws::S3::Resource).to receive(:new).and_return(aws_resource)
49
+ expect(aws_resource).to receive(:bucket).with("bulk").and_return(s3_bucket)
50
+ expect(s3_bucket).to receive(:objects).with(prefix: s3_directory).and_return(nil)
51
+
52
+ response = described_class.run(bucket, s3_directory)
53
+ expect(response).to_not be_successful
54
+ expect(response.result).to be_nil
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -26,6 +26,19 @@ module DefraRuby
26
26
  expect(presigned_url).to include("Amz-Credential")
27
27
  expect(presigned_url).to include("Amz-Signature")
28
28
  end
29
+
30
+ context "when an s3_directory is provided" do
31
+ let(:presigned_url) { described_class.run(bucket, "testfile.csv", { s3_directory: "directory" }) }
32
+
33
+ it "returns a presigned url for a given file in the bucket" do
34
+ expect(presigned_url).to include("https://test.s3.eu-west-1.amazonaws.com/directory/testfile.csv")
35
+ expect(presigned_url).to include("response-content-disposition=attachment")
36
+ expect(presigned_url).to include("response-content-type=text%2Fcsv")
37
+ expect(presigned_url).to include("Amz-Expires")
38
+ expect(presigned_url).to include("Amz-Credential")
39
+ expect(presigned_url).to include("Amz-Signature")
40
+ end
41
+ end
29
42
  end
30
43
  end
31
44
  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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-19 00:00:00.000000000 Z
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-s3
@@ -155,6 +155,7 @@ files:
155
155
  - lib/defra_ruby/aws/services/bucket_loader_service.rb
156
156
  - lib/defra_ruby/aws/services/concerns/has_aws_bucket_configuration.rb
157
157
  - lib/defra_ruby/aws/services/delete_file_from_bucket_service.rb
158
+ - lib/defra_ruby/aws/services/list_bucket_files_service.rb
158
159
  - lib/defra_ruby/aws/services/presigned_url_service.rb
159
160
  - lib/defra_ruby/aws/version.rb
160
161
  - spec/defra_ruby_aws_spec.rb
@@ -165,6 +166,7 @@ files:
165
166
  - spec/lib/defra_ruby/aws/response_spec.rb
166
167
  - spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb
167
168
  - spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb
169
+ - spec/lib/defra_ruby/aws/services/list_bucket_files_service_spec.rb
168
170
  - spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb
169
171
  - spec/spec_helper.rb
170
172
  - spec/support/aws_sdk_s3.rb
@@ -178,7 +180,7 @@ licenses:
178
180
  - The Open Government Licence (OGL) Version 3
179
181
  metadata:
180
182
  allowed_push_host: https://rubygems.org
181
- post_install_message:
183
+ post_install_message:
182
184
  rdoc_options: []
183
185
  require_paths:
184
186
  - lib
@@ -193,8 +195,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
195
  - !ruby/object:Gem::Version
194
196
  version: '0'
195
197
  requirements: []
196
- rubygems_version: 3.0.0
197
- signing_key:
198
+ rubygems_version: 3.1.2
199
+ signing_key:
198
200
  specification_version: 4
199
201
  summary: Defra ruby on rails AWS helpers
200
202
  test_files:
@@ -214,3 +216,4 @@ test_files:
214
216
  - spec/lib/defra_ruby/aws/services/bucket_loader_service_spec.rb
215
217
  - spec/lib/defra_ruby/aws/services/delete_file_from_bucket_service_spec.rb
216
218
  - spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb
219
+ - spec/lib/defra_ruby/aws/services/list_bucket_files_service_spec.rb