Skip to content

Commit

Permalink
feat: s3 website
Browse files Browse the repository at this point in the history
  • Loading branch information
raykrishardi committed Dec 8, 2023
1 parent c990585 commit 72748e1
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
41 changes: 41 additions & 0 deletions s3.cfndsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@
ownership_controls_rules.append(ownership_control_rule)
end

# s3 website
website_configuration = {}
website = config.has_key?('website') ? config['website'] : {}
if !website.empty?
if website['redirect_requests']
website_configuration['RedirectAllRequestsTo'] = {
'HostName': website['redirect_hostname'],
'Protocol': website['redirect_protocol']
}
else
website_configuration['ErrorDocument'] = website['error_document']
website_configuration['IndexDocument'] = website['index_document']

if website.has_key?('routing_rules')
routing_rules = []
website['routing_rules'].each do |rule|
routing_rule = {}

# Redirect rule
routing_rule['RedirectRule'] = {}
routing_rule['RedirectRule']['HostName'] = rule['redirect_rule']['hostname'] if rule['redirect_rule'].has_key?('hostname')
routing_rule['RedirectRule']['HttpRedirectCode'] = rule['redirect_rule']['http_redirect_code'] if rule['redirect_rule'].has_key?('http_redirect_code')
routing_rule['RedirectRule']['Protocol'] = rule['redirect_rule']['protocol'] if rule['redirect_rule'].has_key?('protocol')

# ReplaceKeyPrefixWith and ReplaceKeyWith cannot be specified together (can only be 1 or the other)
routing_rule['RedirectRule']['ReplaceKeyPrefixWith'] = rule['redirect_rule']['replace_key_prefix_with'] if rule['redirect_rule'].has_key?('replace_key_prefix_with') and !rule['redirect_rule'].has_key?('replace_key_with')
routing_rule['RedirectRule']['ReplaceKeyWith'] = rule['redirect_rule']['replace_key_with'] if rule['redirect_rule'].has_key?('replace_key_with') and !rule['redirect_rule'].has_key?('replace_key_prefix_with')

# Routing rule condition
routing_rule['RoutingRuleCondition'] = {}
routing_rule['RoutingRuleCondition']['HttpErrorCodeReturnedEquals'] = rule['routing_rule_condition']['http_error_code_returned_equals'] if rule['routing_rule_condition'].has_key?('http_error_code_returned_equals')
routing_rule['RoutingRuleCondition']['KeyPrefixEquals'] = rule['routing_rule_condition']['key_prefix_equals'] if rule['routing_rule_condition'].has_key?('key_prefix_equals')

routing_rules.append(routing_rule)
end
website_configuration['RoutingRules'] = routing_rules if !routing_rules.empty?
end
end
end

if bucket_type == 'create_if_not_exists'
Resource("#{safe_bucket_name}") do
Type 'Custom::S3BucketCreateOnly'
Expand Down Expand Up @@ -98,6 +138,7 @@
OwnershipControls ({
Rules: ownership_controls_rules
}) if !ownership_controls_rules.empty?
WebsiteConfiguration website_configuration if !website_configuration.empty?
end
end

Expand Down
39 changes: 39 additions & 0 deletions spec/website_redirect_to_another_bucket_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'yaml'

describe 'compiled component s3' do

context 'cftest' do
it 'compiles test' do
expect(system("cfhighlander cftest #{@validate} --tests tests/website_redirect_to_another_bucket.test.yaml")).to be_truthy
end
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/website_redirect_to_another_bucket/s3.compiled.yaml") }

context "Resource" do


context "Normalbucket" do
let(:resource) { template["Resources"]["Normalbucket"] }

it "is of type AWS::S3::Bucket" do
expect(resource["Type"]).to eq("AWS::S3::Bucket")
end

it "to have property BucketName" do
expect(resource["Properties"]["BucketName"]).to eq({"Fn::Sub"=>"normal-bucket"})
end

it "to have property Tags" do
expect(resource["Properties"]["Tags"]).to eq([{"Key"=>"Name", "Value"=>{"Fn::Sub"=>"${EnvironmentName}-normal-bucket"}}, {"Key"=>"Environment", "Value"=>{"Ref"=>"EnvironmentName"}}, {"Key"=>"EnvironmentType", "Value"=>{"Ref"=>"EnvironmentType"}}])
end

it "to have property WebsiteConfiguration" do
expect(resource["Properties"]["WebsiteConfiguration"]).to eq({"RedirectAllRequestsTo"=>{"HostName"=>"testbucket", "Protocol"=>"http"}})
end

end

end

end
39 changes: 39 additions & 0 deletions spec/website_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'yaml'

describe 'compiled component s3' do

context 'cftest' do
it 'compiles test' do
expect(system("cfhighlander cftest #{@validate} --tests tests/website.test.yaml")).to be_truthy
end
end

let(:template) { YAML.load_file("#{File.dirname(__FILE__)}/../out/tests/website/s3.compiled.yaml") }

context "Resource" do


context "Normalbucket" do
let(:resource) { template["Resources"]["Normalbucket"] }

it "is of type AWS::S3::Bucket" do
expect(resource["Type"]).to eq("AWS::S3::Bucket")
end

it "to have property BucketName" do
expect(resource["Properties"]["BucketName"]).to eq({"Fn::Sub"=>"normal-bucket"})
end

it "to have property Tags" do
expect(resource["Properties"]["Tags"]).to eq([{"Key"=>"Name", "Value"=>{"Fn::Sub"=>"${EnvironmentName}-normal-bucket"}}, {"Key"=>"Environment", "Value"=>{"Ref"=>"EnvironmentName"}}, {"Key"=>"EnvironmentType", "Value"=>{"Ref"=>"EnvironmentType"}}])
end

it "to have property WebsiteConfiguration" do
expect(resource["Properties"]["WebsiteConfiguration"]).to eq({"ErrorDocument"=>"error.html", "IndexDocument"=>"index.html", "RoutingRules"=>[{"RedirectRule"=>{"HostName"=>"test1", "HttpRedirectCode"=>"301", "Protocol"=>"http", "ReplaceKeyWith"=>"test1"}, "RoutingRuleCondition"=>{"HttpErrorCodeReturnedEquals"=>"400", "KeyPrefixEquals"=>"test1"}}, {"RedirectRule"=>{"ReplaceKeyPrefixWith"=>"documents/"}, "RoutingRuleCondition"=>{"KeyPrefixEquals"=>"docs/"}}]})
end

end

end

end
26 changes: 26 additions & 0 deletions tests/website.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
test_metadata:
type: config
name: website
description: s3 website with multiple routing rules

# Insert your tests here
buckets:
normal-bucket:
type: default
website:
redirect_requests: false
error_document: error.html
index_document: index.html
routing_rules:
- redirect_rule:
hostname: "test1"
http_redirect_code: "301"
protocol: "http"
replace_key_with: "test1"
routing_rule_condition:
http_error_code_returned_equals: "400"
key_prefix_equals: "test1"
- redirect_rule:
replace_key_prefix_with: "documents/"
routing_rule_condition:
key_prefix_equals: "docs/"
13 changes: 13 additions & 0 deletions tests/website_redirect_to_another_bucket.test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test_metadata:
type: config
name: website_redirect_to_another_bucket
description: s3 website that redirects to another s3 bucket

# Insert your tests here
buckets:
normal-bucket:
type: default
website:
redirect_requests: true
redirect_hostname: "testbucket"
redirect_protocol: "http"

0 comments on commit 72748e1

Please sign in to comment.