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

vault: detect namespace change in config reload #14298

Merged
merged 3 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion nomad/structs/config/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ func (c *VaultConfig) Equals(b *VaultConfig) bool {
if c.TLSKeyFile != b.TLSKeyFile {
return false
}
if c.TLSSkipVerify != b.TLSSkipVerify {

if c.TLSSkipVerify == nil || b.TLSSkipVerify == nil {
if c.TLSSkipVerify != b.TLSSkipVerify {
return false
}
} else if *c.TLSSkipVerify != *b.TLSSkipVerify {
Comment on lines +249 to +253
Copy link
Member

Choose a reason for hiding this comment

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

i was thinking, maybe we could have pointer.Compare similar to (and replacing) helper.CompareTimePtrs - caveat being it should only be used on basic types (not on structs with also pointer fields)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, lemme take a crack at that.

Copy link
Member

@shoenig shoenig Aug 24, 2022

Choose a reason for hiding this comment

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

caveat being it should only be used on basic types

what am I thinking, we can enforce that with a type parameter constraint!

Copy link
Member

@shoenig shoenig Aug 24, 2022

Choose a reason for hiding this comment

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

pretty sure this is effectively what we'd want

// Compare returns whether a and b contain the same value.
func Compare[A constraints.Ordered](a, b *A) bool {
// but also check nil
	return *a == *b
}```

Copy link
Member

@schmichael schmichael Aug 24, 2022

Choose a reason for hiding this comment

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

Yeah, something like this? https://go.dev/play/p/nE1aQVTzsuz

Copy link
Member

Choose a reason for hiding this comment

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

the problem is I want to prevent comparisons like this:

https://go.dev/play/p/Lh26c-YWmBd

using constraints.Ordered as a constraint prevents comparing pointers of anything but underlying Numeric/String types which are safe

Copy link
Member Author

Choose a reason for hiding this comment

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

hrm, I seem to be stuck there.

type primitivePointer interface {
	*bool | *string | *int // etc, we can add the rest here
}

// Compare is an equality check for pointers that point to primitives.
func Compare[A primitivePointer](a, b A) bool {
	if a == nil || b == nil {
		return a == b
	}

	return *a == *b
}

Gives me:

# github.com/hashicorp/nomad/helper/pointer
helper/pointer/pointer.go:28:10: invalid operation: pointers of a (variable of type A constrained by primitivePointer) must have identical base types
helper/pointer/pointer.go:28:16: invalid operation: pointers of b (variable of type A constrained by primitivePointer) must have identical base types

For now I'm going to merge the bugfix and then we'll come up with some nicer comparison functions in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I posted that without seeing the other solutions. In any case, probably better for another PR.

return false
}

if c.TLSServerName != b.TLSServerName {
return false
}
Expand Down
9 changes: 3 additions & 6 deletions nomad/structs/config/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/helper/pointer"
"github.com/shoenig/test/must"
)

func TestVaultConfig_Merge(t *testing.T) {
Expand Down Expand Up @@ -102,8 +101,7 @@ func TestVaultConfig_Equals(t *testing.T) {
TLSServerName: "1",
}

// TODO: must.Equals(t, c, c2) should work here?
require.True(t, c.Equals(c2))
must.Equals(t, c1, c2)

c3 := &VaultConfig{
Enabled: pointer.Of(true),
Expand Down Expand Up @@ -139,6 +137,5 @@ func TestVaultConfig_Equals(t *testing.T) {
TLSServerName: "1",
}

// TODO: must.NotEquals(t, c3, c4) should work here?
require.False(t, c3.Equals(c4))
must.NotEquals(t, c3, c4)
}