Class: CfnVpn::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/cfnvpn/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(region, bucket, name) ⇒ S3

Returns a new instance of S3.



7
8
9
10
11
12
# File 'lib/cfnvpn/s3.rb', line 7

def initialize(region, bucket, name)
  @client = Aws::S3::Client.new(region: region)
  @bucket = bucket
  @name = name
  @path = "cfnvpn/certificates/#{@name}"
end

Instance Method Details

#create_bucketObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cfnvpn/s3.rb', line 66

def create_bucket
  @client.create_bucket({
    bucket: bucket,
    acl: 'private'
  })

  @client.put_public_access_block({
    bucket: bucket,
    public_access_block_configuration: { 
      block_public_acls: true,
      ignore_public_acls: true,
      block_public_policy: true,
      restrict_public_buckets: true,
    }
  })

  @client.put_bucket_encryption({
    bucket: bucket,
    server_side_encryption_configuration: {
      rules: [
        {
          apply_server_side_encryption_by_default: {
            sse_algorithm: "AES256"
          }
        }
      ]
    }
  })
end

#get_object(file) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/cfnvpn/s3.rb', line 27

def get_object(file)
  file_name = file.split('/').last
  CfnVpn::Log.logger.debug("downloading s3://#{@bucket}/#{@path}/#{file_name} to #{file}")
  @client.get_object(
    response_target: file,
    bucket: @bucket,
    key: "#{@path}/#{file_name}")
end

#get_url(file) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/cfnvpn/s3.rb', line 46

def get_url(file)
  presigner = Aws::S3::Presigner.new(client: @client)
  params = {
    bucket: @bucket,
    key: "#{@path}/#{file}",
    expires_in: 3600
  }
  presigner.presigned_url(:get_object, params)
end

#store_config(config) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/cfnvpn/s3.rb', line 36

def store_config(config)
  CfnVpn::Log.logger.debug("uploading config to s3://#{@bucket}/#{@path}/#{@name}.config.ovpn")
  @client.put_object({
    body: config,
    bucket: @bucket,
    key: "#{@path}/#{@name}.config.ovpn",
    tagging: "cfnvpn:name=#{@name}"
  })
end

#store_embedded_config(config, cn) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/cfnvpn/s3.rb', line 56

def store_embedded_config(config, cn)
  CfnVpn::Log.logger.debug("uploading config to s3://#{@bucket}/#{@path}/#{@name}_#{cn}.config.ovpn")
  @client.put_object({
    body: config,
    bucket: @bucket,
    key: "#{@path}/#{@name}_#{cn}.config.ovpn",
    tagging: "cfnvpn:name=#{@name}"
  })
end

#store_object(file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cfnvpn/s3.rb', line 14

def store_object(file)
  body = File.open(file, 'rb').read
  file_name = file.split('/').last
  CfnVpn::Log.logger.debug("uploading #{file} to s3://#{@bucket}/#{@path}/#{file_name}")
  @client.put_object({
    body: body,
    bucket: @bucket,
    key: "#{@path}/#{file_name}",
    server_side_encryption: "AES256",
    tagging: "cfnvpn:name=#{@name}"
  })
end