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

Support X-Forwarded-For Header for parsing ClientIPs #50

Merged
merged 1 commit into from
May 18, 2024
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
7 changes: 6 additions & 1 deletion config/crd/bases/boot.ironcore.dev_httpbootconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ spec:
referenced object inside the same namespace.
properties:
name:
default: ""
description: |-
Name of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
TODO: Add other useful fields. apiVersion, kind, uid?
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Drop `kubebuilder:default` when controller-gen doesn't need it https://github.com/kubernetes-sigs/kubebuilder/issues/3896.
type: string
type: object
x-kubernetes-map-type: atomic
Expand Down
7 changes: 6 additions & 1 deletion config/crd/bases/boot.ironcore.dev_ipxebootconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ spec:
referenced object inside the same namespace.
properties:
name:
default: ""
description: |-
Name of the referent.
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
This field is effectively required, but due to backwards compatibility is
allowed to be empty. Instances of this type with an empty value here are
almost certainly wrong.
TODO: Add other useful fields. apiVersion, kind, uid?
More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Drop `kubebuilder:default` when controller-gen doesn't need it https://github.com/kubernetes-sigs/kubebuilder/issues/3896.
type: string
type: object
x-kubernetes-map-type: atomic
Expand Down
28 changes: 24 additions & 4 deletions server/bootserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"path"
"path/filepath"
"strings"
"text/template"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -73,11 +74,30 @@ func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client,
return
}

clientIPs := []string{}
clientIPs = append(clientIPs, clientIP)

// Attempt to extract IPs from X-Forwarded-For if present
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
for _, ip := range strings.Split(xff, ",") {
trimmedIP := strings.TrimSpace(ip)
if trimmedIP != "" {
clientIPs = append(clientIPs, trimmedIP)
}
}
}

var ipxeConfigs bootv1alpha1.IPXEBootConfigList
if err := k8sClient.List(ctx, &ipxeConfigs, client.MatchingFields{"spec.systemIP": clientIP}); err != nil {
log.Info("Failed to list IPXEBootConfig", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
for _, ip := range clientIPs {
if err := k8sClient.List(ctx, &ipxeConfigs, client.MatchingFields{"spec.systemIP": ip}); err != nil {
log.Info("Failed to list IPXEBootConfig for IP", "IP", ip, "error", err)
continue
}

if len(ipxeConfigs.Items) > 0 {
log.Info("Found IPXEBootConfig", "IP", ip)
break
}
}

data := defaultIpxeTemplateData
Expand Down