Skip to content

Commit

Permalink
fix: Ensure GCP gateway timeouts match target service timeout. (#617)
Browse files Browse the repository at this point in the history
Co-Authored-By: Andy Warns <[email protected]>
  • Loading branch information
tjholm and awarns-impower authored May 30, 2024
1 parent 811df85 commit f74db93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cloud/common/deploy/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func NewImage(ctx *pulumi.Context, name string, args *ImageArgs, opts ...pulumi.
}

buildContext := fmt.Sprintf("%s/build-%s", os.TempDir(), name)
//#nosec G301 - Patch to remove exec perms
err = os.MkdirAll(buildContext, os.ModePerm)
// Set Read/Write/Execute permissions for owner and group in compliance with https://securego.io/docs/rules/g301.html
err = os.MkdirAll(buildContext, 0o750)
if err != nil {
return nil, err
}
Expand Down
38 changes: 25 additions & 13 deletions cloud/gcp/deploy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import (
)

type nameUrlPair struct {
name string
invokeUrl string
name string
invokeUrl string
timeoutSeconds int
}

func (p *NitricGcpPulumiProvider) Api(ctx *pulumi.Context, parent pulumi.Resource, name string, apiConfig *deploymentspb.Api) error {
Expand Down Expand Up @@ -124,17 +125,25 @@ func (p *NitricGcpPulumiProvider) Api(ctx *pulumi.Context, parent pulumi.Resourc

// collect name arn pairs for output iteration
for k, v := range services {
nameUrlPairs = append(nameUrlPairs, pulumi.All(k, v.Url).ApplyT(func(args []interface{}) (nameUrlPair, error) {
nameUrlPairs = append(nameUrlPairs, pulumi.All(k, v.Url, v.Service.Template.Spec().TimeoutSeconds()).ApplyT(func(args []interface{}) (nameUrlPair, error) {
name, nameOk := args[0].(string)
url, urlOk := args[1].(string)
timeoutPtr, timeoutOk := args[2].(*int)

timeout := 15

if timeoutOk && timeoutPtr != nil {
timeout = *timeoutPtr
}

if !nameOk || !urlOk {
return nameUrlPair{}, fmt.Errorf("invalid data %T %v", args, args)
}

return nameUrlPair{
name: name,
invokeUrl: url,
name: name,
invokeUrl: url,
timeoutSeconds: timeout,
}, nil
}))
}
Expand All @@ -144,22 +153,24 @@ func (p *NitricGcpPulumiProvider) Api(ctx *pulumi.Context, parent pulumi.Resourc
// Replace Nitric API Extensions with google api gateway extensions
doc := pulumi.All(nameUrlPairs...).ApplyT(func(pairs []interface{}) (string, error) {
naps := make(map[string]string)
timeouts := make(map[string]int)

for _, p := range pairs {
if pair, ok := p.(nameUrlPair); ok {
naps[pair.name] = pair.invokeUrl
timeouts[pair.name] = pair.timeoutSeconds
} else {
return "", fmt.Errorf("failed to resolve Cloud Run container URL for api %s, invalid name URL pair value %T %v, %s", name, p, p, help.BugInNitricHelpText())
}
}

for k, p := range v2doc.Paths {
p.Get = gcpOperation(name, p.Get, naps)
p.Post = gcpOperation(name, p.Post, naps)
p.Patch = gcpOperation(name, p.Patch, naps)
p.Put = gcpOperation(name, p.Put, naps)
p.Delete = gcpOperation(name, p.Delete, naps)
p.Options = gcpOperation(name, p.Options, naps)
p.Get = gcpOperation(name, p.Get, naps, timeouts)
p.Post = gcpOperation(name, p.Post, naps, timeouts)
p.Patch = gcpOperation(name, p.Patch, naps, timeouts)
p.Put = gcpOperation(name, p.Put, naps, timeouts)
p.Delete = gcpOperation(name, p.Delete, naps, timeouts)
p.Options = gcpOperation(name, p.Options, naps, timeouts)
v2doc.Paths[k] = p
}

Expand Down Expand Up @@ -266,7 +277,7 @@ func keepOperation(opExt map[string]interface{}) (string, bool) {
return name, true
}

func gcpOperation(apiName string, op *openapi2.Operation, urls map[string]string) *openapi2.Operation {
func gcpOperation(apiName string, op *openapi2.Operation, urls map[string]string, timeouts map[string]int) *openapi2.Operation {
if op == nil {
return nil
}
Expand Down Expand Up @@ -298,10 +309,11 @@ func gcpOperation(apiName string, op *openapi2.Operation, urls map[string]string
}
}

op.Extensions["x-google-backend"] = map[string]string{
op.Extensions["x-google-backend"] = map[string]any{
// Append the name of the target origin api gateway to the target address
"address": fmt.Sprintf("%s/x-nitric-api/%s", urls[name], apiName),
"path_translation": "APPEND_PATH_TO_ADDRESS",
"deadline": timeouts[name],
}

return op
Expand Down

0 comments on commit f74db93

Please sign in to comment.