Skip to content

Commit

Permalink
Fix issue tinkerbell#204: Added validation to avoid duplication of ma…
Browse files Browse the repository at this point in the history
…c addresses in the hardware database
  • Loading branch information
parauliya committed Jul 8, 2020
1 parent 9973940 commit 22c4fe3
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions grpc-server/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware.
return &hardware.Empty{}, err
}

// TODO: somewhere here validate json (if ip addr contains cidr, etc.)

// TODO: Add more validation for hardware data in the definition of below function if required
err := s.validateHardwareData(ctx, hw)
if err != nil {
return &hardware.Empty{}, err
}
logger.With("id", hw.Id).Info("data pushed")

var fn func() error
Expand Down Expand Up @@ -285,3 +288,24 @@ func (s *server) Delete(ctx context.Context, in *hardware.DeleteRequest) (*hardw

return &hardware.Empty{}, err
}

func (s *server) validateHardwareData(ctx context.Context, hw *hardware.Hardware) error {
interfaces := hw.GetNetwork().GetInterfaces()
for i := range hw.GetNetwork().GetInterfaces() {
data, _ := db.GetByMAC(ctx, s.db, interfaces[i].GetDhcp().GetMac())
if data != "" {
logger.With("MAC", interfaces[i].GetDhcp().GetMac()).Info("Duplicate MAC address found")
newhw := hardware.Hardware{}
err := json.Unmarshal([]byte(data), &newhw)
if err != nil {
logger.Error(err, "Failed to unmarshal hardware data")
return err
}
if newhw.Id == hw.Id {
return nil
}
return errors.New("conflicting hardware MAC address " + interfaces[i].GetDhcp().GetMac() + " provided with hardware data/info")
}
}
return nil
}

0 comments on commit 22c4fe3

Please sign in to comment.