Skip to content

Commit

Permalink
use one bucket per stage for all public sites
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Oct 13, 2021
1 parent 4503b44 commit fa3ca33
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 85 deletions.
39 changes: 16 additions & 23 deletions api/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

type Deploy struct {
req *dto.DeployRequest
stage *workspace.Stage
awsClient *aws.AWS
publicBuckets map[string]string
req *dto.DeployRequest
stage *workspace.Stage
awsClient *aws.AWS
publicBucket string
}

func New() *Deploy {
Expand All @@ -36,9 +36,9 @@ func (d *Deploy) Invoke(ctx context.Context, req *dto.DeployRequest) (*dto.Deplo
return nil, err
}
return &dto.DeployResponse{
Rest: d.stage.Endpoints.Rest,
Ws: d.stage.Endpoints.Ws,
PublicBuckets: d.publicBuckets,
Rest: d.stage.Endpoints.Rest,
Ws: d.stage.Endpoints.Ws,
PublicBucket: d.publicBucket,
}, nil
}

Expand Down Expand Up @@ -75,11 +75,11 @@ func (d *Deploy) applyInfrastructure() error {
if err != nil {
return fmt.Errorf("could not read terraform output variable for api ws url - %v", err)
}
sites, err := tf.Output("static_websites", false)
public, err := tf.Output("public", false)
if err != nil {
return fmt.Errorf("coult not read terraform output variable for static websites - %v", err)
}
if err := d.updateWebsitesConfig(sites); err != nil {
if err := d.updatePublicConfig(public); err != nil {
return err
}
d.stage.Endpoints = &workspace.StageEndpoints{
Expand Down Expand Up @@ -140,23 +140,16 @@ func (d *Deploy) updateLambdaFunction(f *workspace.Function) error {
return d.awsClient.WaitLambdaFunctionUpdated(lambdaName)
}

func (d *Deploy) updateWebsitesConfig(tfOutput string) error {
type sitesOutput struct {
Name string `json:"name"`
func (d *Deploy) updatePublicConfig(tfOutput string) error {

This comment has been minimized.

Copy link
@ianic

ianic Oct 13, 2021

Member

Ovdje se koristi samo jedan output iz terraforma bucket, url se ne koristi.
Nepotrebno je da onda vracamo json pa njega raspakujemo, jednostavnije je da vratimo jedan string kao sto gore radimo za url, ws_url.
Onda mozemo izbaciti ovu funkciju updatePublicConfig.

type publicOutput struct {
Bucket string `json:"bucket"`
Url string `json:"url"`
}
os := &[]sitesOutput{}
if err := json.Unmarshal([]byte(tfOutput), os); err != nil {
po := &publicOutput{}
if err := json.Unmarshal([]byte(tfOutput), po); err != nil {
return err
}
d.publicBuckets = make(map[string]string)
for _, o := range *os {
for _, s := range d.stage.Public {
if o.Name == s.Name {
s.Bucket = o.Bucket
}
d.publicBuckets[o.Name] = o.Bucket
}
}
d.publicBucket = po.Bucket
d.stage.Public.Bucket = po.Bucket
return nil
}
6 changes: 3 additions & 3 deletions api/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type DeployRequest struct {
}

type DeployResponse struct {
Rest string
Ws string
PublicBuckets map[string]string
Rest string
Ws string
PublicBucket string
}

type DestroyRequest struct {
Expand Down
6 changes: 3 additions & 3 deletions api/security/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package security
const CredentialsTemplate = `{
"Version": "2012-10-17",
"Statement": [
{{- range .Public}}
{{ if ne .Public.Bucket "" }}
{
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::{{.Bucket}}/*"
"Resource": "arn:aws:s3:::{{.Public.Bucket}}/*"
},
{{- end}}
{{ end }}
{{ if ne .LogGroup "" }}
{
"Action": [
Expand Down
4 changes: 2 additions & 2 deletions api/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *Security) projectPolicyTemplateData() (*projectPolicyTemplateData, erro
AccountID: s.awsClient.AccountID(),
}
if s.stage != nil {
ppt.Public = s.stage.Public
ppt.Public = *s.stage.Public
ppt.LogGroup = workspace.ProjectResource(s.req.ProjectName, s.stage.Name)
}
return ppt, nil
Expand Down Expand Up @@ -112,6 +112,6 @@ type projectPolicyTemplateData struct {
Bucket string
Region string
AccountID string
Public []*workspace.PublicSite
Public workspace.Public
LogGroup string
}
5 changes: 2 additions & 3 deletions api/security/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ func TestProjectCredentialsWithStage(t *testing.T) {
},
stage: &workspace.Stage{
Name: "test-stage",
Public: []*workspace.PublicSite{
{Bucket: "publicSite1"},
{Bucket: "publicSite2"},
Public: &workspace.Public{
Bucket: "public-bucket",
},
},
awsClient: &awsMock{},
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (d *Cmd) deployRequest() (*workspace.Project, error) {
ResourceTags: d.ctx.ResourceTags(),
}

b, err := d.ctx.Backend(DeployHTTPMethod)
b, err := d.ctx.Backend()
if err != nil {
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions cli/cmd/deploy/sites.go → cli/cmd/deploy/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (d *Cmd) publicSiteUpdates() (resourceDiff, error) {
return diff, err
}
var stageSites []string
for _, s := range d.ctx.Stage.Public {
for _, s := range d.ctx.Stage.Public.Sites {
stageSites = append(stageSites, s.Name)
}
diff.added = diffArrays(localSites, stageSites)
Expand All @@ -26,17 +26,17 @@ func (d *Cmd) publicSiteUpdates() (resourceDiff, error) {
if err != nil {
return diff, err
}
d.ctx.Stage.Public = append(d.ctx.Stage.Public, &workspace.PublicSite{
d.ctx.Stage.Public.Sites = append(d.ctx.Stage.Public.Sites, &workspace.PublicSite{
Name: a,
Hash: hash,
})
diff.updated = append(diff.updated, a)
}
diff.removed = diffArrays(stageSites, localSites)
for _, r := range diff.removed {
for idx, s := range d.ctx.Stage.Public {
for idx, s := range d.ctx.Stage.Public.Sites {
if s.Name == r {
d.ctx.Stage.Public = append(d.ctx.Stage.Public[:idx], d.ctx.Stage.Public[idx+1:]...)
d.ctx.Stage.Public.Sites = append(d.ctx.Stage.Public.Sites[:idx], d.ctx.Stage.Public.Sites[idx+1:]...)
}
}
}
Expand All @@ -46,7 +46,7 @@ func (d *Cmd) publicSiteUpdates() (resourceDiff, error) {
if err != nil {
return diff, err
}
for _, s := range d.ctx.Stage.Public {
for _, s := range d.ctx.Stage.Public.Sites {
if s.Name == i && hash != s.Hash {
s.Hash = hash
diff.updated = append(diff.updated, i)
Expand All @@ -59,7 +59,7 @@ func (d *Cmd) publicSiteUpdates() (resourceDiff, error) {
func (d *Cmd) updatePublicSiteContent() error {
for _, u := range d.publicDiff.updated {
var site *workspace.PublicSite
for _, s := range d.ctx.Stage.Public {
for _, s := range d.ctx.Stage.Public.Sites {
if s.Name == u {
site = s
break
Expand All @@ -81,12 +81,13 @@ func (d *Cmd) updatePublicSiteContent() error {
if err != nil {
return err
}
relPath = filepath.Join(site.Name, relPath)
ui.Info("uploading file %s...", relPath)
buf, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if err := d.awsClient.PutObjectToS3Bucket(site.Bucket, relPath, buf); err != nil {
if err := d.awsClient.PutObjectToS3Bucket(d.ctx.Stage.Public.Bucket, relPath, buf); err != nil {
return err
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/project/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (c *Context) SetStage(s *workspace.Stage) error {
}

// TODO: ovome nije mjesto ovdje prebaci negdje
func (c *Context) Backend(method string) (*backend.Backend, error) {
token, err := c.authToken(method)
func (c *Context) Backend() (*backend.Backend, error) {
token, err := c.authToken()
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (c *stageCmd) createStage(accountName string) (*workspace.Stage, error) {
stage := &workspace.Stage{
Name: c.stage,
Account: accountName,
Public: &workspace.Public{},
}
if len(c.ctx.Project.Stages) == 0 {
stage.Default = true
Expand Down
13 changes: 5 additions & 8 deletions terraform/modules/funcs/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ output "functions" {
]
}

output "static_websites" {
value = [for k, v in local.static_websites :
{
name : v.name
bucket : aws_s3_bucket.static_websites[k].id
url : aws_s3_bucket.static_websites[k].website_endpoint
}
]
output "public" {
value = {
bucket : aws_s3_bucket.public.id
url : aws_s3_bucket.public.website_endpoint
}
}
12 changes: 5 additions & 7 deletions terraform/modules/funcs/s3.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
resource "aws_s3_bucket" "static_websites" {
for_each = local.static_websites
bucket_prefix = "mantil-public-${var.project_name}-${each.value.name}-"
resource "aws_s3_bucket" "public" {
bucket_prefix = "mantil-public-${var.project_name}-"
acl = "public-read"
force_destroy = true

Expand All @@ -11,8 +10,7 @@ resource "aws_s3_bucket" "static_websites" {
}

resource "aws_s3_bucket_policy" "public_read" {
for_each = aws_s3_bucket.static_websites
bucket = each.value.id
bucket = aws_s3_bucket.public.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
Expand All @@ -22,8 +20,8 @@ resource "aws_s3_bucket_policy" "public_read" {
Principal = "*"
Action = "s3:GetObject"
Resource = [
each.value.arn,
"${each.value.arn}/*",
aws_s3_bucket.public.arn,
"${aws_s3_bucket.public.arn}/*",
]
},
]
Expand Down
18 changes: 5 additions & 13 deletions terraform/templates/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ locals {
}
{{- end}}
}
static_websites = {
{{- range .Public}}
{{.Name}} = {
name = "{{.Name}}"
}
{{- end}}
}
global_env = {
{{- range $key, $value := .GlobalEnv}}
{{$key}} = "{{$value}}"
Expand Down Expand Up @@ -60,7 +53,6 @@ module "funcs" {
project_name = local.project_name
functions = local.functions
s3_bucket = local.project_bucket
static_websites = local.static_websites
global_env = local.global_env
}

Expand All @@ -82,13 +74,13 @@ module "api" {
lambda_name : f.arn,
}
],
[ for w in module.funcs.static_websites :
[
{
type : "HTTP_PROXY"
method : "GET"
integration_method: "GET"
route : "/public/${w.name}"
uri : "http://${w.url}"
route : "/public"
uri : "http://${module.funcs.public.url}"
}
])
}
Expand All @@ -101,8 +93,8 @@ output "functions_bucket" {
value = local.project_bucket
}

output "static_websites" {
value = module.funcs.static_websites
output "public" {
value = module.funcs.public
}

output "ws_url" {
Expand Down
2 changes: 1 addition & 1 deletion terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type ProjectTemplateData struct {
Bucket string
BucketPrefix string
Functions []*workspace.Function
Public []*workspace.PublicSite
Public *workspace.Public
Region string
Stage string
RuntimeFunctionsBucket string
Expand Down
13 changes: 5 additions & 8 deletions terraform/testdata/project.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ locals {
}
}
}
static_websites = {
}
global_env = {
env1 = "value1"
env2 = "value2"
Expand Down Expand Up @@ -57,7 +55,6 @@ module "funcs" {
project_name = local.project_name
functions = local.functions
s3_bucket = local.project_bucket
static_websites = local.static_websites
global_env = local.global_env
}

Expand All @@ -79,13 +76,13 @@ module "api" {
lambda_name : f.arn,
}
],
[ for w in module.funcs.static_websites :
[
{
type : "HTTP_PROXY"
method : "GET"
integration_method: "GET"
route : "/public/${w.name}"
uri : "http://${w.url}"
route : "/public"
uri : "http://${module.funcs.public.url}"
}
])
}
Expand All @@ -98,8 +95,8 @@ output "functions_bucket" {
value = local.project_bucket
}

output "static_websites" {
value = module.funcs.static_websites
output "public" {
value = module.funcs.public
}

output "ws_url" {
Expand Down
10 changes: 7 additions & 3 deletions workspace/public.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package workspace

type Public struct {
Bucket string `yaml:"bucket"`
Sites []*PublicSite `yaml:"sites"`
}

type PublicSite struct {
Name string `yaml:"name"`
Bucket string `yaml:"bucket"`
Hash string `yaml:"hash"`
Name string `yaml:"name"`
Hash string `yaml:"hash"`
}
Loading

0 comments on commit fa3ca33

Please sign in to comment.