Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/pkg/cli/client/byoc/aws/byoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package aws
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -104,7 +106,17 @@ func (b *ByocAws) setUp(ctx context.Context) error {
term.Debug("Failed to get subdomain zone:", err)
// return err; FIXME: ignore this error for now
} else {
b.ProjectDomain = b.getProjectDomain(domain.Zone)
// Use STS to get the account ID
cfg, err := b.driver.LoadConfig(ctx)
if err != nil {
return annotateAwsError(err)
}
identity, err := sts.NewFromConfig(cfg).GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return annotateAwsError(err)
}

b.ProjectDomain = b.getProjectDomain(*identity.Account, domain.Zone)
if b.ProjectDomain != "" {
b.ShouldDelegateSubdomain = true
}
Expand Down Expand Up @@ -629,15 +641,14 @@ func (b *ByocAws) getPrivateFqdn(fqn qualifiedName) string {
return fmt.Sprintf("%s.%s", safeFqn, b.PrivateDomain) // TODO: consider merging this with ServiceDNS
}

func (b *ByocAws) getProjectDomain(zone string) string {
func (b *ByocAws) getProjectDomain(account, zone string) string {
if b.ProjectName == "" {
return "" // no project name => no custom domain
}
projectLabel := byoc.DnsSafeLabel(b.ProjectName)
if projectLabel == byoc.DnsSafeLabel(b.TenantID) {
return byoc.DnsSafe(zone) // the zone will already have the tenant ID
}
return projectLabel + "." + byoc.DnsSafe(zone)
h := sha256.New()
fmt.Fprintf(h, "%s.%s.%s.%s.%s", account, b.ProjectName, b.PulumiStack, b.TenantID, zone)

return pkg.Base36ID(binary.LittleEndian.Uint64(h.Sum(nil)[:8])) + "." + byoc.DnsSafe(zone)
}

func (b *ByocAws) TearDown(ctx context.Context) error {
Expand Down
29 changes: 15 additions & 14 deletions src/pkg/cli/client/byoc/aws/byoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"io"
"path"
"regexp"
"strings"
"sync"
"testing"
Expand All @@ -32,17 +33,17 @@ func TestDomainMultipleProjectSupport(t *testing.T) {
PublicFqdn string
PrivateFqdn string
}{
{"tenant1", "tenant1", "web", port80, "web--80.example.com", "web.example.com", "web.tenant1.internal"},
{"tenant1", "tenant1", "web", hostModePort, "web.tenant1.internal:80", "web.example.com", "web.tenant1.internal"},
{"project1", "tenant1", "web", port80, "web--80.project1.example.com", "web.project1.example.com", "web.project1.internal"},
{"Project1", "tenant1", "web", port80, "web--80.project1.example.com", "web.project1.example.com", "web.project1.internal"},
{"project1", "tenant1", "web", hostModePort, "web.project1.internal:80", "web.project1.example.com", "web.project1.internal"},
{"project1", "tenant1", "api", port8080, "api--8080.project1.example.com", "api.project1.example.com", "api.project1.internal"},
{"tenant1", "tenant1", "web", port80, "web--80.example.com", "web.example.com", "web.tenant1.internal"},
{"tenant1", "tenant1", "web", hostModePort, "web.tenant1.internal:80", "web.example.com", "web.tenant1.internal"},
{"Project1", "tenant1", "web", port80, "web--80.project1.example.com", "web.project1.example.com", "web.project1.internal"},
{"Tenant2", "tenant1", "web", port80, "web--80.tenant2.example.com", "web.tenant2.example.com", "web.tenant2.internal"},
{"tenant1", "tenAnt1", "web", port80, "web--80.example.com", "web.example.com", "web.tenant1.internal"},
{"tenant1", "tenant1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.tenant1.internal"},
{"tenant1", "tenant1", "web", hostModePort, `web\.tenant1.internal:80`, `web.[0-9a-z]{12}.example\.com`, "web.tenant1.internal"},
{"project1", "tenant1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.project1.internal"},
{"Project1", "tenant1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.project1.internal"},
{"project1", "tenant1", "web", hostModePort, `web\.project1\.internal:80`, `web\.[0-9a-z]{12}\.example\.com`, "web.project1.internal"},
{"project1", "tenant1", "api", port8080, `api--8080\.[0-9a-z]{12}\.example\.com`, `api\.[0-9a-z]{12}\.example\.com`, "api.project1.internal"},
{"tenant1", "tenant1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.tenant1.internal"},
{"tenant1", "tenant1", "web", hostModePort, `web\.tenant1\.internal:80`, `web\.[0-9a-z]{12}\.example\.com`, "web.tenant1.internal"},
{"Project1", "tenant1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.project1.internal"},
{"Tenant2", "tenant1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.tenant2.internal"},
{"tenant1", "tenAnt1", "web", port80, `web--80\.[0-9a-z]{12}\.example\.com`, `web\.[0-9a-z]{12}\.example\.com`, "web.tenant1.internal"},
}

for _, tt := range tests {
Expand All @@ -52,15 +53,15 @@ func TestDomainMultipleProjectSupport(t *testing.T) {
if _, err := b.LoadProject(context.Background()); err != nil {
t.Fatalf("LoadProject() failed: %v", err)
}
b.ProjectDomain = b.getProjectDomain("example.com")
b.ProjectDomain = b.getProjectDomain("123456789012", "example.com")

endpoint := b.getEndpoint(tt.Fqn, tt.Port)
if endpoint != tt.EndPoint {
if !regexp.MustCompile(tt.EndPoint).MatchString(endpoint) {
t.Errorf("expected endpoint %q, got %q", tt.EndPoint, endpoint)
}

publicFqdn := b.getPublicFqdn(tt.Fqn)
if publicFqdn != tt.PublicFqdn {
if !regexp.MustCompile(tt.PublicFqdn).MatchString(publicFqdn) {
t.Errorf("expected public fqdn %q, got %q", tt.PublicFqdn, publicFqdn)
}

Expand Down
19 changes: 17 additions & 2 deletions src/pkg/cli/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"regexp"
"strings"
"syscall"
"time"

"github.com/DefangLabs/defang/src/pkg"
Expand Down Expand Up @@ -177,12 +178,26 @@ func Tail(ctx context.Context, client client.Client, params TailOptions) error {
}

func isTransientError(err error) bool {
if errors.Is(err, io.ErrUnexpectedEOF) {
return true
}

// Consider connection reset error as transient
if errors.Is(err, syscall.ECONNRESET) {
return true
}

// Network timeouts are transient, not using Temporary() because it's not always accurate
// See https://pkg.go.dev/net#Error
if neterr, ok := err.(interface{ Timeout() bool }); ok && neterr.Timeout() {
return true
}

// TODO: detect ALB timeout (504) or Fabric restart and reconnect automatically
code := connect.CodeOf(err)
// Reconnect on Error: internal: stream error: stream ID 5; INTERNAL_ERROR; received from peer
return code == connect.CodeUnavailable ||
(code == connect.CodeInternal && !connect.IsWireError(err)) ||
errors.Is(err, io.ErrUnexpectedEOF)
(code == connect.CodeInternal && !connect.IsWireError(err))

}

Expand Down
8 changes: 6 additions & 2 deletions src/pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ func (l *OneOrList) UnmarshalJSON(data []byte) error {
}

func RandomID() string {
const uint64msb = 1 << 63 // always set the MSB to ensure we get ≥12 digits
return strconv.FormatUint(rand.Uint64()|uint64msb, 36)[1:]
return Base36ID(rand.Uint64())
}

func IsValidRandomID(s string) bool {
_, err := strconv.ParseUint(s, 36, 64)
return len(s) == 12 && err == nil
}

func Base36ID(i uint64) string {
const uint64msb = 1 << 63 // always set the MSB to ensure we get ≥12 digits
return strconv.FormatUint(i|uint64msb, 36)[1:]
}

func Min(a, b int) int {
if a < b {
return a
Expand Down
Loading