Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: listen on file descriptor #5973

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions docs/content/en/configuration/miscellaneous/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ server:
{{< ref-common ref="address" description="Common Syntax: Address" text="This option uses a common syntax. " >}}

Configures the listener address for the Main HTTP Server. The address itself is a listener and the scheme must either be
the `unix` scheme or one of the `tcp` schemes. It can configure the host, port, and path the listener responds to. If
the path is configured to anything other than `/` Authelia will handle requests for both `/` and the configured path.
the `unix` scheme, one of the `tcp` schemes or the `fd` scheme. It can configure the host, port, and path the listener responds to. If
the path is configured to anything other than `/` Authelia will handle requests for both `/` and the configured path. For the `fd` scheme, the port number will be used as file descriptor.

__Examples:__

Expand All @@ -82,6 +82,12 @@ server:
address: unix:///var/run/authelia.sock
```

```yaml
server:
address: fd://:3
# When running "systemd-socket-activate -l 9091 go run ./cmd/authelia", the connections to port 9091 will be forwaded to file descriptor 3.
```

### asset_path

{{< confkey type="string " required="no" >}}
Expand Down
27 changes: 14 additions & 13 deletions internal/configuration/schema/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,18 @@ const (

// Address Schemes.
const (
AddressSchemeTCP = "tcp"
AddressSchemeTCP4 = "tcp4"
AddressSchemeTCP6 = "tcp6"
AddressSchemeUDP = "udp"
AddressSchemeUDP4 = "udp4"
AddressSchemeUDP6 = "udp6"
AddressSchemeUnix = "unix"
AddressSchemeLDAP = "ldap"
AddressSchemeLDAPS = "ldaps"
AddressSchemeLDAPI = "ldapi"
AddressSchemeSMTP = "smtp"
AddressSchemeSUBMISSION = "submission"
AddressSchemeSUBMISSIONS = "submissions"
AddressSchemeTCP = "tcp"
AddressSchemeTCP4 = "tcp4"
AddressSchemeTCP6 = "tcp6"
AddressSchemeUDP = "udp"
AddressSchemeUDP4 = "udp4"
AddressSchemeUDP6 = "udp6"
AddressSchemeUnix = "unix"
AddressSchemeLDAP = "ldap"
AddressSchemeLDAPS = "ldaps"
AddressSchemeLDAPI = "ldapi"
AddressSchemeSMTP = "smtp"
AddressSchemeSUBMISSION = "submission"
AddressSchemeSUBMISSIONS = "submissions"
AddressSchemeFileDescriptor = "fd"
)
25 changes: 21 additions & 4 deletions internal/configuration/schema/types_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ func (a *Address) IsExplicitlySecure() bool {
// ValidateListener returns true if the Address is valid for a connection listener.
func (a *Address) ValidateListener() error {
switch a.Scheme() {
case AddressSchemeTCP, AddressSchemeTCP4, AddressSchemeTCP6, AddressSchemeUDP, AddressSchemeUDP4, AddressSchemeUDP6, AddressSchemeUnix:
case AddressSchemeTCP, AddressSchemeTCP4, AddressSchemeTCP6, AddressSchemeUDP, AddressSchemeUDP4, AddressSchemeUDP6, AddressSchemeUnix, AddressSchemeFileDescriptor:
break
default:
return fmt.Errorf("scheme must be one of 'tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6', or 'unix' but is configured as '%s'", a.Scheme())
return fmt.Errorf("scheme must be one of 'tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6', 'unix', or 'fd' but is configured as '%s'", a.Scheme())
}

return nil
Expand All @@ -269,10 +269,10 @@ func (a *Address) ValidateHTTP() error {
}

switch a.Scheme() {
case AddressSchemeUnix:
case AddressSchemeUnix, AddressSchemeFileDescriptor:
return nil
default:
return fmt.Errorf("scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as '%s'", a.Scheme())
return fmt.Errorf("scheme must be one of 'tcp', 'tcp4', 'tcp6', 'unix', or 'fd' but is configured as '%s'", a.Scheme())
}
}

Expand Down Expand Up @@ -468,6 +468,10 @@ func (a *Address) validate() (err error) {
if err = a.validateProtocol(); err != nil {
return err
}
case AddressSchemeFileDescriptor:
if err = a.validateFD(); err != nil {
return err
}
}

a.valid = true
Expand Down Expand Up @@ -545,3 +549,16 @@ func (a *Address) validateUnixSocket() (err error) {

return nil
}

func (a *Address) validateFD() (err error) {
port := a.url.Port()

actualPort, err := strconv.Atoi(port)
if err != nil {
return err
}

a.setport(actualPort)

return nil
}
32 changes: 28 additions & 4 deletions internal/configuration/schema/types_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func TestNewAddressFromString(t *testing.T) {
"a://0.0.0.0",
"",
},
{
"ShouldParseFileDescriptor",
"fd://:4",
&Address{true, false, -1, 4, &url.URL{Scheme: "fd", Host: ":4"}},
":4",
"fd://:4",
"",
},
{
"ShouldNotParseInvalidPort",
"tcp://0.0.0.0:abc",
Expand Down Expand Up @@ -251,18 +259,18 @@ func TestAddress_ValidateErrors(t *testing.T) {
&Address{true, false, -1, 0, &url.URL{Scheme: AddressSchemeLDAP, Host: "127.0.0.1"}},
"",
"scheme must be one of 'smtp', 'submission', or 'submissions' but is configured as 'ldap'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', 'unix', or 'fd' but is configured as 'ldap'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'ldap'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'ldap'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6', or 'unix' but is configured as 'ldap'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6', 'unix', or 'fd' but is configured as 'ldap'",
},
{
"ShouldValidateSMTPAddress",
&Address{true, false, -1, 0, &url.URL{Scheme: AddressSchemeSMTP, Host: "127.0.0.1"}},
"scheme must be one of 'ldap', 'ldaps', or 'ldapi' but is configured as 'smtp'",
"",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', 'unix', or 'fd' but is configured as 'smtp'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'smtp'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', or 'unix' but is configured as 'smtp'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6', or 'unix' but is configured as 'smtp'",
"scheme must be one of 'tcp', 'tcp4', 'tcp6', 'udp', 'udp4', 'udp6', 'unix', or 'fd' but is configured as 'smtp'",
},
{
"ShouldValidateTCPAddress",
Expand Down Expand Up @@ -436,6 +444,22 @@ func TestAddressOutputValues(t *testing.T) {
assert.Equal(t, "example.com", address.Hostname())
assert.Equal(t, "example.com:9092", address.NetworkAddress())
assert.Equal(t, 9092, address.Port())

address = &Address{true, false, -1, 9091, &url.URL{Scheme: AddressSchemeTCP, Host: "0.0.0.0:9091"}}

assert.Equal(t, "tcp://0.0.0.0:9091", address.String())
assert.Equal(t, "tcp", address.Scheme())
assert.Equal(t, "0.0.0.0:9091", address.Host())
assert.Equal(t, "0.0.0.0", address.Hostname())
assert.Equal(t, "0.0.0.0:9091", address.NetworkAddress())
assert.Equal(t, 9091, address.Port())

listener, err = address.Listener()

assert.NotNil(t, listener)
assert.NoError(t, err)

assert.NoError(t, listener.Close())
}

func TestNewAddressUnix(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions internal/configuration/schema/types_addresses_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import (
"fmt"
"net"
"os"
"strconv"
"syscall"
)

Expand All @@ -23,6 +25,11 @@

return ln, err
}
if a.url.Scheme == AddressSchemeFileDescriptor {

Check failure on line 28 in internal/configuration/schema/types_addresses_nix.go

View check run for this annotation

reviewdog / golangci

[golangci] internal/configuration/schema/types_addresses_nix.go#L28

if statements should only be cuddled with assignments (wsl)
Raw output
internal/configuration/schema/types_addresses_nix.go:28:2: if statements should only be cuddled with assignments (wsl)
	if a.url.Scheme == AddressSchemeFileDescriptor {
	^
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
if statements should only be cuddled with assignments (wsl)

fd := os.NewFile(uintptr(a.port), strconv.Itoa(a.port))
defer fd.Close()
return net.FileListener(fd)

Check failure on line 31 in internal/configuration/schema/types_addresses_nix.go

View check run for this annotation

reviewdog / golangci

[golangci] internal/configuration/schema/types_addresses_nix.go#L31

return statements should not be cuddled if block has more than two lines (wsl)
Raw output
internal/configuration/schema/types_addresses_nix.go:31:3: return statements should not be cuddled if block has more than two lines (wsl)
		return net.FileListener(fd)
		^
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
return statements should not be cuddled if block has more than two lines (wsl)

}

return net.Listen(a.Network(), a.NetworkAddress())
}