Module: Legion::Audit::ColdStorage
- Defined in:
- lib/legion/audit/cold_storage.rb
Defined Under Namespace
Classes: BackendNotAvailableError
Class Method Summary
collapse
Class Method Details
.backend ⇒ Object
10
11
12
13
|
# File 'lib/legion/audit/cold_storage.rb', line 10
def backend
raw = Legion::Settings[:audit]&.dig(:retention, :cold_backend) || 'local'
raw.to_sym
end
|
.download(path:) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/legion/audit/cold_storage.rb', line 23
def download(path:)
case backend
when :local then local_download(path: path)
when :s3 then s3_download(path: path)
else raise BackendNotAvailableError, "unknown cold_backend: #{backend}"
end
end
|
.local_download(path:) ⇒ Object
37
38
39
|
# File 'lib/legion/audit/cold_storage.rb', line 37
def local_download(path:)
::File.binread(path)
end
|
.local_upload(data:, path:) ⇒ Object
31
32
33
34
35
|
# File 'lib/legion/audit/cold_storage.rb', line 31
def local_upload(data:, path:)
::FileUtils.mkdir_p(::File.dirname(path))
::File.binwrite(path, data)
{ path: path, bytes: data.bytesize }
end
|
.s3_bucket ⇒ Object
48
49
50
51
|
# File 'lib/legion/audit/cold_storage.rb', line 48
def s3_bucket
Legion::Settings[:audit]&.dig(:retention, :s3_bucket) ||
raise(BackendNotAvailableError, 'audit.retention.s3_bucket must be set for :s3 backend')
end
|
.s3_client ⇒ Object
41
42
43
44
45
46
|
# File 'lib/legion/audit/cold_storage.rb', line 41
def s3_client
raise BackendNotAvailableError, 'aws-sdk-s3 gem is required for :s3 cold_backend' \
unless defined?(Aws::S3::Client)
@s3_client ||= Aws::S3::Client.new
end
|
.s3_download(path:) ⇒ Object
60
61
62
63
|
# File 'lib/legion/audit/cold_storage.rb', line 60
def s3_download(path:)
resp = s3_client.get_object(bucket: s3_bucket, key: path)
resp.body.read
end
|
.s3_upload(data:, path:) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/legion/audit/cold_storage.rb', line 53
def s3_upload(data:, path:)
s3_client.put_object(bucket: s3_bucket, key: path, body: data,
content_type: 'application/gzip',
server_side_encryption: 'AES256')
{ path: path, bytes: data.bytesize }
end
|
.upload(data:, path:) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/legion/audit/cold_storage.rb', line 15
def upload(data:, path:)
case backend
when :local then local_upload(data: data, path: path)
when :s3 then s3_upload(data: data, path: path)
else raise BackendNotAvailableError, "unknown cold_backend: #{backend}"
end
end
|