Skip to content
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
24 changes: 24 additions & 0 deletions Dockerfile.sut
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM golang:1.26 AS builder
WORKDIR /app
COPY bootz/go.mod bootz/go.sum ./
# Map local monax repository so go mod download can resolve the replace directive inside Docker.
COPY monax /monax
RUN go mod download
COPY bootz .
RUN CGO_ENABLED=0 GOOS=linux go build -o /bootz-sut ./server/sut
RUN mkdir -p /www_dir
RUN mkdir -p server/sut

FROM gcr.io/distroless/static-debian12:latest
WORKDIR /app
COPY --from=builder /bootz-sut /app/bootz-sut
COPY --from=builder /www_dir /www
COPY --from=builder /app/server/sut /app/server/sut
COPY bootz/testdata /app/testdata
# Set WORKDIR to /app/server/sut so relative paths in flags (../../testdata) resolve to /app/testdata.
WORKDIR /app/server/sut
# DHCP needs to run as root.
USER root
HEALTHCHECK --interval=5s --timeout=2s --start-period=3s --retries=3 \
CMD ["/app/bootz-sut", "--healthcheck"]
ENTRYPOINT ["/app/bootz-sut"]
7 changes: 2 additions & 5 deletions dhcp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /bootz-dhcp ./dhcp/main

# hadolint ignore=DL3007
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=builder /bootz-dhcp /bootz-dhcp
# DHCP needs to run as root to bind to port 67 and use raw sockets.
# hadolint ignore=DL3002
USER nonroot
HEALTHCHECK --interval=5s --timeout=2s --start-period=3s --retries=3 \
CMD ["/bootz-dhcp", "--healthcheck"]
ENTRYPOINT ["/bootz-dhcp"]

HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD pgrep dhcp || exit 1

38 changes: 38 additions & 0 deletions dhcp/deploy/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: bootz-dhcp
labels:
app: bootz-dhcp
spec:
replicas: 1
selector:
matchLabels:
app: bootz-dhcp
template:
metadata:
labels:
app: bootz-dhcp
spec:
containers:
- image: bootz-dhcp
name: bootz-dhcp
args:
- -i=eth0
- -records=4c:5d:3c:ef:de:60,10.0.0.2/24,10.0.0.1
- -dns=8.8.8.8
- -bootz_urls=http://bootz-server:8080
- -sut_addr=:4001
ports:
- containerPort: 67
protocol: UDP
name: dhcp
- containerPort: 4001
protocol: TCP
name: grpc
securityContext:
runAsUser: 0
capabilities:
add:
- NET_ADMIN
imagePullPolicy: Never
27 changes: 27 additions & 0 deletions dhcp/deploy/kubernetes.txtpb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# proto-file: proto/monax.proto
# proto-message: monax.Library

components: {
id: "DHCP"
kind: "kubernetes"
provided_interfaces: {
name: "dhcp"
dhcp: {
service_name: "bootz-dhcp"
}
}
provided_interfaces: {
name: "grpc"
grpc: {
service_name: "bootz.DHCPService"
}
}
parameters: {
[type.googleapis.com/monax.kubernetes.KubernetesComponentParameters] {
deployment_path: "deployment.yaml"
service_path: "service.yaml"
wait_for_deployment_timeout_sec: 60
wait_for_service_timeout_sec: 60
}
}
}
16 changes: 16 additions & 0 deletions dhcp/deploy/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: bootz-dhcp
spec:
ports:
- name: dhcp
port: 67
protocol: UDP
targetPort: 67
- name: grpc
port: 4001
protocol: TCP
targetPort: 4001
selector:
app: bootz-dhcp
20 changes: 20 additions & 0 deletions dhcp/plugins/slease/slease.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,23 @@ func parseRecord6(r string) (string, net.IP, error) {
}
return parts[0], ip, nil
}

// AddRecord4 adds an IPv4 lease record dynamically.
func AddRecord4(mac string, ip net.IP, mask net.IPMask, gw net.IP) {
muRw.Lock()
defer muRw.Unlock()
ipv4Records[mac] = &ipv4Entry{
ip: ip,
netmask: mask,
gateway: gw,
}
log.Infof("Dynamically added ipv4 record: %v, %v, %v, %v", mac, ip, mask, gw)
}

// RemoveRecord4 removes an IPv4 lease record dynamically.
func RemoveRecord4(mac string) {
muRw.Lock()
defer muRw.Unlock()
delete(ipv4Records, mac)
log.Infof("Dynamically removed ipv4 record for MAC: %v", mac)
}
90 changes: 90 additions & 0 deletions dhcp/sutserver/sutserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2026 Google LLC
//
// 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
//
// https://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 sutserver

import (
"context"
"fmt"
"net"
"net/netip"

log "github.com/golang/glog"
"github.com/openconfig/bootz/dhcp/plugins/slease"
pb "github.com/openconfig/bootz/server/tests/proto/sut"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

type Server struct {
pb.UnimplementedDHCPServiceServer
grpcServer *grpc.Server
}

func New() *Server {
return &Server{}
}

// Start starts the DHCP SUT gRPC server.
func (s *Server) Start(addr string) error {
lis, err := net.Listen("tcp", addr)
if err != nil {
return err
}
s.grpcServer = grpc.NewServer()
pb.RegisterDHCPServiceServer(s.grpcServer, s)
reflection.Register(s.grpcServer)
log.Infof("Starting DHCP SUT gRPC server on %s", addr)
go func() {
if err := s.grpcServer.Serve(lis); err != nil {
log.Errorf("DHCP SUT gRPC server failed: %v", err)
}
}()
return nil
}

// Stop stops the DHCP SUT gRPC server.
func (s *Server) Stop() {
if s.grpcServer != nil {
s.grpcServer.GracefulStop()
}
}

// CreateLease creates a DHCP lease dynamically.
func (s *Server) CreateLease(ctx context.Context, req *pb.CreateLeaseRequest) (*pb.CreateLeaseResponse, error) {
ip, err := netip.ParseAddr(req.GetIpAddress())
if err != nil {
return nil, err
}
gw := net.ParseIP(req.GetGateway())
if gw == nil {
return nil, fmt.Errorf("invalid gateway: %s", req.GetGateway())
}

mask := net.CIDRMask(int(req.GetMaskLen()), ip.BitLen())

for _, mac := range req.GetMacAddresses() {
slease.AddRecord4(mac, net.ParseIP(req.GetIpAddress()), mask, gw)
Comment on lines +75 to +78

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.

high

net.CIDRMask returns nil if the mask length is invalid (e.g., negative or greater than the bit length). If mask is nil, passing it to slease.AddRecord4 can cause panics or unexpected behavior in the DHCP server. Please validate that mask is not nil before proceeding.

	mask := net.CIDRMask(int(req.GetMaskLen()), ip.BitLen())
	if mask == nil {
		return nil, fmt.Errorf("invalid mask length %d for IP bit length %d", req.GetMaskLen(), ip.BitLen())
	}

	for _, mac := range req.GetMacAddresses() {
		slease.AddRecord4(mac, net.ParseIP(req.GetIpAddress()), mask, gw)

}

return &pb.CreateLeaseResponse{}, nil
}

// RemoveLease removes a DHCP lease dynamically.
func (s *Server) RemoveLease(ctx context.Context, req *pb.RemoveLeaseRequest) (*pb.RemoveLeaseResponse, error) {
for _, mac := range req.GetMacAddresses() {
slease.RemoveRecord4(mac)
}
return &pb.RemoveLeaseResponse{}, nil
}
90 changes: 87 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,125 @@ require (
github.com/insomniacslk/dhcp v0.0.0-20260407060928-11b94ed970f2
github.com/openconfig/attestz v0.6.12
github.com/openconfig/gnmi v0.14.1
github.com/openconfig/gnoigo v0.0.0-20240820205259-23ac4e061cc2
github.com/openconfig/gnsi v1.9.1
github.com/openconfig/monax v0.0.0-20260603195803-1899be60f203
github.com/openconfig/ondatra v0.12.2
go.mozilla.org/pkcs7 v0.9.0
google.golang.org/grpc v1.81.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.6.1
google.golang.org/protobuf v1.36.11
)

require (
bitbucket.org/creachadair/stringset v0.0.14 // indirect
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chappjc/logrus-prefix v0.0.0-20180227015900-3a1d64819adb // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/docker v28.1.1+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.14 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/jstemmer/go-junit-report/v2 v2.1.0 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/go-archive v0.1.0 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.6.0 // indirect
github.com/moby/sys/user v0.4.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.1.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/open-traffic-generator/snappi/gosnappi v1.33.3 // indirect
github.com/openconfig/gnoi v0.5.0 // indirect
github.com/openconfig/gnpsi v0.3.2 // indirect
github.com/openconfig/gocloser v0.0.0-20220310182203-c6c950ed3b0b // indirect
github.com/openconfig/goyang v1.6.0 // indirect
github.com/openconfig/gribi v1.8.1 // indirect
github.com/openconfig/ygnmi v0.11.1 // indirect
github.com/openconfig/ygot v0.34.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/p4lang/p4runtime v1.4.1 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/cobra v1.9.1 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/spf13/viper v1.21.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.44.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.44.0 // indirect
go.opentelemetry.io/otel/metric v1.44.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.44.0 // indirect
go.opentelemetry.io/otel/trace v1.44.0 // indirect
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
golang.org/x/mod v0.32.0 // indirect
golang.org/x/net v0.51.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/oauth2 v0.36.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.45.0 // indirect
golang.org/x/term v0.40.0 // indirect
golang.org/x/text v0.34.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.41.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.2 // indirect
k8s.io/apimachinery v0.29.2 // indirect
k8s.io/client-go v0.29.2 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading