Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions build/rofl/scheduler/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ const (
MetadataKeyProxyCustomDomains = "net.oasis.proxy.custom_domains"
// MetadataKeyProxyProtocol is the name of the metadata key that stores the PROXY protocol version.
MetadataKeyProxyProtocol = "net.oasis.proxy.proxy_protocol"
// MetadataKeyProxyTimeoutRW is the name of the metadata key that stores the requested proxy
// read/write timeout (in seconds).
MetadataKeyProxyTimeoutRW = "net.oasis.proxy.timeout_rw"
// MetadataKeyProxyTimeoutConnection is the name of the metadata key that stores the requested
// maximum proxy connection duration (in seconds).
MetadataKeyProxyTimeoutConnection = "net.oasis.proxy.timeout_connection"
)
152 changes: 152 additions & 0 deletions cmd/rofl/build/tdx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,158 @@ func TestValidateAppProxyProtocol(t *testing.T) {
}
}

func TestValidateAppProxyTimeouts(t *testing.T) {
tmpDir := t.TempDir()
t.Chdir(tmpDir)

manifest := testContainerManifest()
manifest.Artifacts = &buildRofl.ArtifactsConfig{
Container: buildRofl.ContainerArtifactsConfig{
Compose: globalComposeFile,
},
}

for _, tc := range []struct {
annotations string
expected PortMapping
err string
}{
{
annotations: `"net.oasis.proxy.ports.8080.timeout_rw": "300"
"net.oasis.proxy.ports.8080.timeout_connection": 3600
"net.oasis.proxy.ports.8080.timeout_connect": 5`,
expected: PortMapping{TimeoutConnect: 5, TimeoutConnection: 3600, TimeoutRW: 300},
},
{
annotations: `"net.oasis.proxy.ports.8080.mode": passthrough`,
},
{
annotations: `"net.oasis.proxy.ports.8080.timeout_rw": 0`,
err: "has an invalid timeout_rw",
},
{
annotations: `"net.oasis.proxy.ports.8080.timeout_connection": 86401`,
err: "has an invalid timeout_connection",
},
{
annotations: `"net.oasis.proxy.ports.8080.timeout_connect": 5s`,
err: "has an invalid timeout_connect",
},
} {
compose := []byte(`services:
web:
image: ghcr.io/oasisprotocol/test:latest
ports:
- target: 80
published: "8080"
protocol: tcp
annotations:
` + tc.annotations + `
`)
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, globalComposeFile), compose, 0o600))

cfg, err := ValidateApp(manifest, "", ValidationOpts{Offline: true})
if tc.err != "" {
require.ErrorContains(t, err, tc.err)
continue
}
require.NoError(t, err)
require.Len(t, cfg.Ports, 1)
require.Equal(t, tc.expected.TimeoutConnect, cfg.Ports[0].TimeoutConnect)
require.Equal(t, tc.expected.TimeoutConnection, cfg.Ports[0].TimeoutConnection)
require.Equal(t, tc.expected.TimeoutRW, cfg.Ports[0].TimeoutRW)
}
}

func TestValidateAppDuplicateCustomDomains(t *testing.T) {
tmpDir := t.TempDir()
t.Chdir(tmpDir)

manifest := testContainerManifest()
manifest.Artifacts = &buildRofl.ArtifactsConfig{
Container: buildRofl.ContainerArtifactsConfig{
Compose: globalComposeFile,
},
}

for _, tc := range [][2]string{
{"app.example.org", "App.Example.org."},
{"xn--bcher-kva.example.org", "Bücher.example.org"},
} {
compose := []byte(`services:
web:
image: ghcr.io/oasisprotocol/test:latest
ports:
- target: 80
published: "8080"
protocol: tcp
- target: 81
published: "8081"
protocol: tcp
annotations:
"net.oasis.proxy.ports.8080.custom_domain": ` + tc[0] + `
"net.oasis.proxy.ports.8081.custom_domain": ` + tc[1] + `
`)
require.NoError(t, os.WriteFile(filepath.Join(tmpDir, globalComposeFile), compose, 0o600))

_, err := ValidateApp(manifest, "", ValidationOpts{Offline: true})
require.ErrorContains(t, err, "is already in use", tc[1])
}
}

func TestCanonicalProxyDomain(t *testing.T) {
// These must match the canonical names used by the proxy (rofl-proxy `canonical_name`).
for _, tc := range [][2]string{
{"App.Example.ORG.", "app.example.org"},
{"bücher.example.org", "xn--bcher-kva.example.org"},
{"BÜCHER.Example.org.", "xn--bcher-kva.example.org"},
{"XN--BCHER-KVA.example.org", "xn--bcher-kva.example.org"},
{"straße.example", "xn--strae-oqa.example"},
{"bücher。example。org", "xn--bcher-kva.example.org"},
} {
canonical, err := canonicalProxyDomain(tc[0])
require.NoError(t, err, tc[0])
require.Equal(t, tc[1], canonical, tc[0])
}

for _, domain := range []string{"", "docs.example..", "docs。。example", "xn--a.example.org"} {
_, err := canonicalProxyDomain(domain)
require.Error(t, err, domain)
}
}

func TestValidateAppCustomDomainEmptyLabel(t *testing.T) {
tmpDir := t.TempDir()
t.Chdir(tmpDir)

manifest := testContainerManifest()
manifest.Artifacts = &buildRofl.ArtifactsConfig{
Container: buildRofl.ContainerArtifactsConfig{
Compose: globalComposeFile,
},
}

for _, tc := range []struct {
domain string
valid bool
}{
{"docs.example.org", true},
{"docs.example.org.", true},
{"docs.example..", false},
{"docs..example.org", false},
{".example.org", false},
} {
writeComposeFile(t, filepath.Join(tmpDir, globalComposeFile), "8080", tc.domain)

_, err := ValidateApp(manifest, "", ValidationOpts{Offline: true})
if tc.valid {
require.NoError(t, err, tc.domain)
} else {
require.ErrorContains(t, err, "is invalid", tc.domain)
}
}
}

func testContainerManifest() *buildRofl.Manifest {
return &buildRofl.Manifest{
Name: "test-app",
Expand Down
79 changes: 71 additions & 8 deletions cmd/rofl/build/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -43,6 +44,52 @@ type PortMapping struct {
CustomDomain string
// ProxyProtocol is the PROXY protocol version that the service expects on the port (if any).
ProxyProtocol string
// TimeoutConnect is the proxy timeout for connecting to the service in seconds (zero for default).
TimeoutConnect uint64
// TimeoutConnection is the maximum proxy connection duration in seconds (zero for default).
TimeoutConnection uint64
// TimeoutRW is the proxy read/write timeout in seconds (zero for default).
TimeoutRW uint64
}

// maxProxyTimeout is the maximum proxy timeout (in seconds) that can be configured for a port.
const maxProxyTimeout = 24 * 60 * 60

// proxyDomainProfile converts domains to the IDNA ASCII form in the same way as the proxy
// (UTS #46 non-transitional processing without STD3 or hyphen restrictions).
var proxyDomainProfile = idna.New(
idna.MapForLookup(),
idna.Transitional(false),
idna.StrictDomainName(false),
idna.CheckHyphens(false),
idna.CheckJoiners(true),
idna.BidiRule(),
)

// canonicalProxyDomain returns the canonical form of a domain under which the proxy routes it.
//
// Domains that only differ in case, a trailing dot or their Unicode and ASCII spelling are the
// same domain to the proxy.
func canonicalProxyDomain(domain string) (string, error) {
canonical, err := proxyDomainProfile.ToASCII(domain)
if err != nil {
return "", err
}
// Conversion is done first as it can map other characters (e.g. U+3002) to dots.
canonical = strings.TrimSuffix(canonical, ".")
if slices.Contains(strings.Split(canonical, "."), "") {
return "", fmt.Errorf("empty label")
}
return canonical, nil
}

// parseProxyTimeout parses a proxy timeout annotation value (in seconds).
func parseProxyTimeout(value string) (uint64, error) {
timeout, err := strconv.ParseUint(value, 10, 64)
if err != nil || timeout == 0 || timeout > maxProxyTimeout {
return 0, fmt.Errorf("must be a number of seconds between 1 and %d", maxProxyTimeout)
}
return timeout, nil
}

// ValidateApp validates the ROFL app manifest for the selected deployment.
Expand Down Expand Up @@ -190,19 +237,31 @@ func validateComposeFile(composeFile string, manifest *buildRofl.Manifest, opts
genericDomain := fmt.Sprintf("p%s", port.Published)
customDomain := service.Annotations[fmt.Sprintf("net.oasis.proxy.ports.%s.custom_domain", port.Published)]
if customDomain != "" {
if _, ok := customDomains[customDomain]; ok {
canonicalDomain, err := canonicalProxyDomain(customDomain)
if err != nil {
return nil, fmt.Errorf("custom domain '%s' of service '%s' is invalid: %w", customDomain, serviceName, err)
}
if _, ok := customDomains[canonicalDomain]; ok {
return nil, fmt.Errorf("custom domain '%s' of service '%s' is already in use", customDomain, serviceName)
}
customDomains[customDomain] = struct{}{}
customDomains[canonicalDomain] = struct{}{}
}
proxyProtocol := service.Annotations[fmt.Sprintf("net.oasis.proxy.ports.%s.proxy_protocol", port.Published)]
proxyTimeout := func(setting string) uint64 {
// Invalid values are rejected when validating annotations below.
timeout, _ := parseProxyTimeout(service.Annotations[fmt.Sprintf("net.oasis.proxy.ports.%s.timeout_%s", port.Published, setting)])
return timeout
}
appCfg.Ports = append(appCfg.Ports, &PortMapping{
ServiceName: serviceName,
Port: port.Published,
ProxyMode: proxyMode,
GenericDomain: genericDomain,
CustomDomain: customDomain,
ProxyProtocol: proxyProtocol,
ServiceName: serviceName,
Port: port.Published,
ProxyMode: proxyMode,
GenericDomain: genericDomain,
CustomDomain: customDomain,
ProxyProtocol: proxyProtocol,
TimeoutConnect: proxyTimeout("connect"),
TimeoutConnection: proxyTimeout("connection"),
TimeoutRW: proxyTimeout("rw"),
})
}

Expand Down Expand Up @@ -238,6 +297,10 @@ func validateComposeFile(composeFile string, manifest *buildRofl.Manifest, opts
default:
return nil, fmt.Errorf("proxy port annotation of service '%s', port '%d' has an invalid PROXY protocol version: %s", serviceName, port, value)
}
case "timeout_connect", "timeout_connection", "timeout_rw":
if _, err := parseProxyTimeout(value); err != nil {
return nil, fmt.Errorf("proxy port annotation of service '%s', port '%d' has an invalid %s: %w", serviceName, port, keyAtoms[5], err)
}
default:
}
default:
Expand Down
26 changes: 26 additions & 0 deletions cmd/rofl/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"maps"
"os"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -184,6 +185,7 @@ var (
if proxyProtocol := extractProxyProtocol(extraCfg); proxyProtocol != "" {
machineDeployment.Metadata[scheduler.MetadataKeyProxyProtocol] = proxyProtocol
}
maps.Copy(machineDeployment.Metadata, extractProxyTimeouts(extraCfg))

var acc wallet.Account
switch common.TxUnsigned {
Expand Down Expand Up @@ -507,6 +509,30 @@ func extractProxyProtocol(extraCfg *roflCmdBuild.AppExtraConfig) string {
return ""
}

// extractProxyTimeouts returns the deployment metadata that requests proxy timeouts from the
// provider. The provider's proxy handles all ports of an instance together, so the maximum
// timeouts across ports are requested while the proxy inside the instance enforces the exact
// per-port timeouts.
func extractProxyTimeouts(extraCfg *roflCmdBuild.AppExtraConfig) map[string]string {
metadata := make(map[string]string)
if extraCfg == nil {
return metadata
}

var timeoutRW, timeoutConnection uint64
for _, p := range extraCfg.Ports {
timeoutRW = max(timeoutRW, p.TimeoutRW)
timeoutConnection = max(timeoutConnection, p.TimeoutConnection)
}
if timeoutRW > 0 {
metadata[scheduler.MetadataKeyProxyTimeoutRW] = strconv.FormatUint(timeoutRW, 10)
}
if timeoutConnection > 0 {
metadata[scheduler.MetadataKeyProxyTimeoutConnection] = strconv.FormatUint(timeoutConnection, 10)
}
return metadata
}

func init() {
providerFlags := flag.NewFlagSet("", flag.ContinueOnError)
// Default to Testnet playground provider.
Expand Down
30 changes: 30 additions & 0 deletions cmd/rofl/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package rofl

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/oasisprotocol/cli/build/rofl/scheduler"
roflCmdBuild "github.com/oasisprotocol/cli/cmd/rofl/build"
)

func TestExtractProxyTimeouts(t *testing.T) {
require.Empty(t, extractProxyTimeouts(nil))
require.Empty(t, extractProxyTimeouts(&roflCmdBuild.AppExtraConfig{
Ports: []*roflCmdBuild.PortMapping{{Port: "8080"}},
}))

// The maximum across all ports should be requested.
metadata := extractProxyTimeouts(&roflCmdBuild.AppExtraConfig{
Ports: []*roflCmdBuild.PortMapping{
{Port: "80", TimeoutRW: 300, TimeoutConnect: 10},
{Port: "8080", TimeoutRW: 60, TimeoutConnection: 3600},
{Port: "9090"},
},
})
require.Equal(t, map[string]string{
scheduler.MetadataKeyProxyTimeoutRW: "300",
scheduler.MetadataKeyProxyTimeoutConnection: "3600",
}, metadata)
}
Loading