diff --git a/build/rofl/scheduler/metadata.go b/build/rofl/scheduler/metadata.go index b73839ef..f2b175be 100644 --- a/build/rofl/scheduler/metadata.go +++ b/build/rofl/scheduler/metadata.go @@ -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" ) diff --git a/cmd/rofl/build/tdx_test.go b/cmd/rofl/build/tdx_test.go index 88822a19..4720bd76 100644 --- a/cmd/rofl/build/tdx_test.go +++ b/cmd/rofl/build/tdx_test.go @@ -150,6 +150,100 @@ 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, + }, + } + + 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": app.example.org + "net.oasis.proxy.ports.8081.custom_domain": App.Example.org. +`) + 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") +} + func testContainerManifest() *buildRofl.Manifest { return &buildRofl.Manifest{ Name: "test-app", diff --git a/cmd/rofl/build/validate.go b/cmd/rofl/build/validate.go index 6db67425..0f35a560 100644 --- a/cmd/rofl/build/validate.go +++ b/cmd/rofl/build/validate.go @@ -43,6 +43,24 @@ 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 + +// 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. @@ -190,19 +208,29 @@ 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 { + // Domains that only differ in case or a trailing dot are the same domain to the proxy. + canonicalDomain := strings.ToLower(strings.TrimSuffix(customDomain, ".")) + 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"), }) } @@ -238,6 +266,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: diff --git a/cmd/rofl/deploy.go b/cmd/rofl/deploy.go index 72696b38..eadbc420 100644 --- a/cmd/rofl/deploy.go +++ b/cmd/rofl/deploy.go @@ -8,6 +8,7 @@ import ( "maps" "os" "sort" + "strconv" "strings" "time" @@ -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 { @@ -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. diff --git a/cmd/rofl/deploy_test.go b/cmd/rofl/deploy_test.go new file mode 100644 index 00000000..3aa06065 --- /dev/null +++ b/cmd/rofl/deploy_test.go @@ -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) +}