-
Notifications
You must be signed in to change notification settings - Fork 137
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
Store MAC as lowercase #279
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
@@ -29,15 +30,26 @@ func (s *server) Push(ctx context.Context, in *hardware.PushRequest) (*hardware. | |
// must be a copy so deferred cacheInFlight.Dec matches the Inc | ||
labels = prometheus.Labels{"method": "Push", "op": ""} | ||
|
||
hw := in.Data | ||
if hw.Id == "" { | ||
hw := in.GetData() | ||
if hw == nil { | ||
err := errors.New("expected data not to be nil") | ||
logger.Error(err) | ||
return &hardware.Empty{}, err | ||
} | ||
|
||
// we know hw is non-nil at this point, since we returned early above | ||
// if it was nil | ||
if hw.GetId() == "" { | ||
metrics.CacheTotals.With(labels).Inc() | ||
metrics.CacheErrors.With(labels).Inc() | ||
err := errors.New("id must be set to a UUID, got id: " + hw.Id) | ||
logger.Error(err) | ||
return &hardware.Empty{}, err | ||
} | ||
|
||
// normalize data prior to storing in the database | ||
normalizeHardwareData(hw) | ||
|
||
// validate the hardware data to avoid duplicate mac address | ||
err := s.validateHardwareData(ctx, hw) | ||
if err != nil { | ||
|
@@ -292,22 +304,35 @@ func (s *server) Delete(ctx context.Context, in *hardware.DeleteRequest) (*hardw | |
} | ||
|
||
func (s *server) validateHardwareData(ctx context.Context, hw *hardware.Hardware) error { | ||
interfaces := hw.GetNetwork().GetInterfaces() | ||
for i := range hw.GetNetwork().GetInterfaces() { | ||
data, _ := s.db.GetByMAC(ctx, interfaces[i].GetDhcp().GetMac()) | ||
if data != "" { | ||
logger.With("MAC", interfaces[i].GetDhcp().GetMac()).Info(duplicateMAC) | ||
for _, iface := range hw.GetNetwork().GetInterfaces() { | ||
mac := iface.GetDhcp().GetMac() | ||
|
||
if data, _ := s.db.GetByMAC(ctx, mac); data != "" { | ||
logger.With("MAC", mac).Info(duplicateMAC) | ||
|
||
newhw := hardware.Hardware{} | ||
err := json.Unmarshal([]byte(data), &newhw) | ||
if err != nil { | ||
if err := json.Unmarshal([]byte(data), &newhw); err != nil { | ||
logger.Error(err, "Failed to unmarshal hardware data") | ||
return err | ||
} | ||
|
||
if newhw.Id == hw.Id { | ||
return nil | ||
} | ||
return errors.New(fmt.Sprintf(conflictMACAddr, interfaces[i].GetDhcp().GetMac())) | ||
|
||
return fmt.Errorf(conflictMACAddr, mac) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func normalizeHardwareData(hw *hardware.Hardware) { | ||
// Ensure MAC is stored as lowercase | ||
for _, iface := range hw.GetNetwork().GetInterfaces() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I switched things to use the generated proto getters rather than nil checks, since they already include the nil checks and it also eliminates the use of slice indexing. |
||
dhcp := iface.GetDhcp() | ||
if mac := dhcp.GetMac(); mac != "" { | ||
dhcp.Mac = strings.ToLower(mac) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package grpcserver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tinkerbell/tink/protos/hardware" | ||
) | ||
|
||
func Test_server_normalizeHardwareData(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
given *hardware.Hardware | ||
expected *hardware.Hardware | ||
}{ | ||
{ | ||
name: "Expect MAC to be normalized to lowercase", | ||
given: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:42:0E:D9:C7:53", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:42:0e:d9:c7:53", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Expect MAC to be normalized to lowercase, multiple interfaces", | ||
given: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:42:0E:D9:C7:53", | ||
}, | ||
}, | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:4F:0E:D9:C7:5E", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:42:0e:d9:c7:53", | ||
}, | ||
}, | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:4f:0e:d9:c7:5e", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Expect MAC to be normalized to lowercase, nultiple interfaces, mixed casing", | ||
given: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:42:0e:d9:c7:53", | ||
}, | ||
}, | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:4F:0E:D9:C7:5E", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:42:0e:d9:c7:53", | ||
}, | ||
}, | ||
{ | ||
Dhcp: &hardware.Hardware_DHCP{ | ||
Mac: "02:4f:0e:d9:c7:5e", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Handle nil DHCP", | ||
given: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
{ | ||
Dhcp: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Handle nil Interface", | ||
given: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
nil, | ||
}, | ||
}, | ||
}, | ||
expected: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: []*hardware.Hardware_Network_Interface{ | ||
nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Handle nil Interfaces", | ||
given: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: nil, | ||
}, | ||
}, | ||
expected: &hardware.Hardware{ | ||
Network: &hardware.Hardware_Network{ | ||
Interfaces: nil, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Handle nil Network", | ||
given: &hardware.Hardware{Network: nil}, | ||
expected: &hardware.Hardware{Network: nil}, | ||
}, | ||
{ | ||
name: "Handle nil Hardware", | ||
given: nil, | ||
expected: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.NotPanics(t, func() { normalizeHardwareData(tt.given) }) | ||
assert.Equal(t, tt.expected, tt.given) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it would make sense to add a test for the empty string here prior to calling
s.db.GetByMAC()
.Would we expect that to be an error condition or would we allow a hardware definition without a mac address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was questioning similar, whether we should transform the input at all, or just let validate/push throw an error if the hardware mac was not "valid" (lowercase, non-empty).
That said, I don't know if hardware addresses can be treated as optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worry that transforming the data means that any stateful tools (terraform, k8s controllers, salt, etc) that read back the lowercase value will need to be aware of the transform and copy that logic themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative I thought of was to make the comparisons case-insensitive, but with it being stored embedded inside of a stored json blob that seemed a bit more error prone.