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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY cmd/main.go cmd/main.go
COPY cmd/operator/ cmd/operator/
COPY api/ api/
COPY internal/ internal/

Expand All @@ -21,7 +21,7 @@ COPY internal/ internal/
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager ./cmd/operator

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo@v2.33.0
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) -timeout=12m -vv -r -p -coverprofile cover.out
GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) -timeout=12m -vv -r -p --skip-package=cmd/auth-sidecar -coverprofile cover.out

##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/dragonfly-operator -ldflags "-X main.version=$(VERSION)" cmd/main.go
go build -o bin/dragonfly-operator -ldflags "-X main.version=$(VERSION)" ./cmd/operator

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go
go run ./cmd/operator

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
Expand Down
13 changes: 13 additions & 0 deletions cmd/auth-sidecar/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.27-alpine AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/auth-sidecar/ ./cmd/auth-sidecar/
RUN go test ./cmd/auth-sidecar && CGO_ENABLED=0 go build -trimpath -o /auth-sidecar ./cmd/auth-sidecar

FROM alpine:3.24
RUN apk add --no-cache ca-certificates
COPY --from=builder /auth-sidecar /usr/local/bin/auth-sidecar
USER 65534:65534
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/auth-sidecar"]
73 changes: 73 additions & 0 deletions cmd/auth-sidecar/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2026 DragonflyDB authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"crypto/tls"
"crypto/x509"
"errors"
"net"

"github.com/go-ldap/ldap/v3"
)

func connect(cfg config, ldapURL string) (*ldap.Conn, error) {
endpoint, err := parseEndpoint(ldapURL)
if err != nil {
return nil, err
}
tlsConfig, err := loadTLSConfig(endpoint.Hostname(), cfg.TLSCACert, cfg.TLSCert, cfg.TLSKey)
if err != nil {
return nil, err
}
connection, err := ldap.DialURL(endpoint.String(),
ldap.DialWithDialer(&net.Dialer{Timeout: cfg.TimeoutConnection}),
ldap.DialWithTLSConfig(tlsConfig),
Comment thread
moredure marked this conversation as resolved.
Resolved
)
Comment thread
Copilot marked this conversation as resolved.
Resolved
if err != nil {
return nil, err
}
if cfg.UseStartTLS && endpoint.Scheme == "ldap" {
if err = connection.StartTLS(tlsConfig); err != nil {
connection.Close()

@moredure moredure Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

worth packing it with if err != nil inside defer to survive potential panics inside starttls, but I don't feel too strongly about that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i have a few connection improvements that i'll handle in the next PR. Will consider this as well

return nil, err
}
Comment thread
Copilot marked this conversation as resolved.
Resolved
}
return connection, nil
}

func loadTLSConfig(serverName, caCert, cert, key string) (*tls.Config, error) {
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12, ServerName: serverName}
if caCert != "" {
roots, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
if !roots.AppendCertsFromPEM([]byte(caCert)) {
return nil, errors.New("error parsing CA certificate")
}
tlsConfig.RootCAs = roots
}
if cert != "" && key != "" {
certificate, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {
return nil, err
}
tlsConfig.Certificates = []tls.Certificate{certificate}
}
return tlsConfig, nil
}
211 changes: 211 additions & 0 deletions cmd/auth-sidecar/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
Copyright 2026 DragonflyDB authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"errors"
"fmt"
"os"
"strconv"
"time"

"github.com/go-ldap/ldap/v3"
)

type searchScope string

const (
scopeBase searchScope = "base"
scopeOne searchScope = "one"
scopeSub searchScope = "sub"
)

func (scope searchScope) ldapScope() (int, error) {
switch scope {
case scopeBase:
return ldap.ScopeBaseObject, nil
case scopeOne:
return ldap.ScopeSingleLevel, nil
case scopeSub:
return ldap.ScopeWholeSubtree, nil
default:
return 0, fmt.Errorf("LDAP_SEARCH_SCOPE must be base, one or sub, got %q", scope)
}
}

type config struct {
httpConfig
ldapConfig
}

type httpConfig struct {
Port string
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
}

type ldapConfig struct {
// general
Servers []string // TODO: Implement support for multiple LDAP servers.
TimeoutConnection time.Duration
TimeoutLDAPOperation int

// tls
UseStartTLS bool
TLSCACert string
TLSCert string
TLSKey string

// search+bind
SearchBindDN string
SearchBindPassword string
SearchBaseDN string
SearchFilter string
SearchAttribute string
SearchDNAttribute string
SearchScope searchScope
}

func loadConfig() (config, error) {
cfg := config{
Port: getEnvString("HTTP_PORT", "8080"),
Comment on lines +87 to +88
ReadHeaderTimeout: time.Duration(getEnvInt("HTTP_READ_HEADER_TIMEOUT", 5)) * time.Second,
ReadTimeout: time.Duration(getEnvInt("HTTP_READ_TIMEOUT", 10)) * time.Second,
WriteTimeout: time.Duration(getEnvInt("HTTP_WRITE_TIMEOUT", 10)) * time.Second,
IdleTimeout: time.Duration(getEnvInt("HTTP_IDLE_TIMEOUT", 10)) * time.Second,
ShutdownTimeout: time.Duration(getEnvInt("HTTP_SHUTDOWN_TIMEOUT", 10)) * time.Second,

//general
Servers: []string{os.Getenv("LDAP_SERVERS")},
TimeoutConnection: time.Duration(getEnvInt("LDAP_TIMEOUT_CONNECTION", 10)) * time.Second,
TimeoutLDAPOperation: getEnvInt("LDAP_TIMEOUT_LDAP_OPERATION", 10),

// tls
UseStartTLS: getEnvBool("LDAP_USE_STARTTLS", false),
TLSCACert: os.Getenv("LDAP_TLS_CA_CERT"),
TLSCert: os.Getenv("LDAP_TLS_CERT"),
TLSKey: os.Getenv("LDAP_TLS_KEY"),

// search+bind
SearchBindDN: os.Getenv("LDAP_SEARCH_BIND_DN"),
SearchBindPassword: os.Getenv("LDAP_SEARCH_BIND_PASSWORD"),
SearchBaseDN: os.Getenv("LDAP_SEARCH_BASE_DN"),
SearchFilter: getEnvString("LDAP_SEARCH_FILTER", "(objectClass=*)"),
SearchAttribute: getEnvString("LDAP_SEARCH_ATTRIBUTE", "uid"),
SearchDNAttribute: getEnvString("LDAP_SEARCH_DN_ATTRIBUTE", "entryDN"),
SearchScope: searchScope(getEnvString("LDAP_SEARCH_SCOPE", string(scopeSub))),
}
return cfg, cfg.validate()
}

func (cfg config) validate() error {
// http config
if port, err := strconv.ParseUint(cfg.Port, 10, 16); err != nil || port == 0 {
return errors.New("HTTP_PORT must be an integer between 1 and 65535")
}
if cfg.ReadHeaderTimeout <= 0 {
return errors.New("HTTP_READ_HEADER_TIMEOUT must be greater than zero")
}
if cfg.ReadTimeout <= 0 {
return errors.New("HTTP_READ_TIMEOUT must be greater than zero")
}
if cfg.WriteTimeout <= 0 {
return errors.New("HTTP_WRITE_TIMEOUT must be greater than zero")
}
if cfg.IdleTimeout <= 0 {
return errors.New("HTTP_IDLE_TIMEOUT must be greater than zero")
}
if cfg.ShutdownTimeout <= 0 {
return errors.New("HTTP_SHUTDOWN_TIMEOUT must be greater than zero")
}

// ldap config

// general
if len(cfg.Servers) != 1 {
return errors.New("LDAP_SERVERS must contain exactly one endpoint")
}
if _, err := parseEndpoint(cfg.Servers[0]); err != nil {
return err
}
if cfg.TimeoutConnection <= 0 {
return errors.New("LDAP_TIMEOUT_CONNECTION must be greater than zero")
}
if cfg.TimeoutLDAPOperation <= 0 {
return errors.New("LDAP_TIMEOUT_LDAP_OPERATION must be greater than zero")
}

// tls
if _, err := loadTLSConfig("", cfg.TLSCACert, cfg.TLSCert, cfg.TLSKey); err != nil {
return err
}

// search+bind
if cfg.SearchBindDN == "" {
return errors.New("LDAP_SEARCH_BIND_DN is required")
}
if cfg.SearchBindPassword == "" {
return errors.New("LDAP_SEARCH_BIND_PASSWORD is required")
}
if cfg.SearchBaseDN == "" {
return errors.New("LDAP_SEARCH_BASE_DN is required")
}
if cfg.SearchAttribute == "" {
return errors.New("LDAP_SEARCH_ATTRIBUTE is required")
}
if cfg.SearchDNAttribute == "" {
return errors.New("LDAP_SEARCH_DN_ATTRIBUTE is required")
}
if _, err := ldap.CompileFilter(cfg.SearchFilter); err != nil {
return fmt.Errorf("invalid LDAP_SEARCH_FILTER: %w", err)
}
if _, err := cfg.SearchScope.ldapScope(); err != nil {
return err
}

return nil
}

func (cfg config) userFilter(username string) string {
return fmt.Sprintf("(&%s(%s=%s))", cfg.SearchFilter, cfg.SearchAttribute, ldap.EscapeFilter(username))
}

func getEnvString(key, fallback string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return fallback
}

func getEnvInt(key string, fallback int) int {
strValue := os.Getenv(key)
if value, err := strconv.Atoi(strValue); err == nil {
return value
}
return fallback
}

func getEnvBool(name string, fallback bool) bool {
strValue := os.Getenv(name)
if value, err := strconv.ParseBool(strValue); err == nil {
return value
}
return fallback
}
42 changes: 42 additions & 0 deletions cmd/auth-sidecar/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
services:
openldap:
image: osixia/openldap:1.5.0
platform: linux/amd64
command: ["--copy-service"]
environment:
LDAP_ORGANISATION: Dragonfly Demo
LDAP_DOMAIN: example.org
LDAP_ADMIN_PASSWORD: admin-demo
ports:
- "127.0.0.1:1389:389"
volumes:
- ./users.ldif:/container/service/slapd/assets/config/bootstrap/ldif/custom/50-users.ldif:ro
healthcheck:
test: ["CMD", "ldapsearch", "-x", "-H", "ldap://localhost:389", "-D", "cn=admin,dc=example,dc=org", "-w", "admin-demo", "-b", "uid=alice,ou=people,dc=example,dc=org", "-s", "base", "(objectClass=*)"]
interval: 3s
timeout: 3s
retries: 30
start_period: 10s

auth-sidecar:
build:
context: ../..
dockerfile: cmd/auth-sidecar/Dockerfile
environment:
LDAP_SERVERS: ldap://openldap:389
LDAP_SEARCH_BIND_DN: cn=admin,dc=example,dc=org
LDAP_SEARCH_BIND_PASSWORD: admin-demo
LDAP_SEARCH_BASE_DN: ou=people,dc=example,dc=org
LDAP_SEARCH_FILTER: (objectClass=inetOrgPerson)
LDAP_TIMEOUT_CONNECTION: "3"
LDAP_TIMEOUT_LDAP_OPERATION: "3"
ports:
- "127.0.0.1:18080:8080"
depends_on:
openldap:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/healthz"]
interval: 3s
timeout: 3s
retries: 10
Loading