Skip to content

Commit

Permalink
coordinator: fix equality checks for manifest properties (#777)
Browse files Browse the repository at this point in the history
* fix DisableSecretBinding ignored in Marble.Equal

* fix AcceptedTCBStatuses and AcceptedAdvisories ignored in PackageProperties.Equal

---------

Signed-off-by: Daniel Weiße <[email protected]>
Co-authored-by: Thomas Tendyck <[email protected]>
  • Loading branch information
daniel-weisse and thomasten authored Dec 16, 2024
1 parent 512f38c commit e7ee0c1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
19 changes: 4 additions & 15 deletions coordinator/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/edgelesssys/marblerun/coordinator/quote"
"github.com/edgelesssys/marblerun/coordinator/user"
"github.com/edgelesssys/marblerun/util"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -94,26 +95,14 @@ type Marble struct {

// Equal returns true if two Marble definitions are equal.
func (m Marble) Equal(other Marble) bool {
if len(m.TLS) != len(other.TLS) {
if !util.SliceEqualElements(m.TLS, other.TLS) {
return false
}

mTLS := make([]string, len(m.TLS))
copy(mTLS, m.TLS)
otherTLS := make([]string, len(other.TLS))
copy(otherTLS, other.TLS)

sort.Strings(mTLS)
sort.Strings(otherTLS)
for i := range mTLS {
if mTLS[i] != otherTLS[i] {
return false
}
}

return m.Package == other.Package &&
m.MaxActivations == other.MaxActivations &&
m.Parameters.Equal(other.Parameters)
m.Parameters.Equal(other.Parameters) &&
m.DisableSecretBinding == other.DisableSecretBinding
}

// Parameters contains lists for files, environment variables and commandline arguments that should be passed to an application.
Expand Down
6 changes: 6 additions & 0 deletions coordinator/quote/ert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"strings"

"github.com/edgelesssys/marblerun/util"
"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -68,6 +69,11 @@ func (p PackageProperties) Equal(other PackageProperties) bool {
return false
}

if !util.SliceEqualElements(p.AcceptedAdvisories, other.AcceptedAdvisories) ||
!util.SliceEqualElements(p.AcceptedTCBStatuses, other.AcceptedTCBStatuses) {
return false
}

return true
}

Expand Down
15 changes: 15 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: BUSL-1.1
package util

import (
"cmp"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
Expand All @@ -19,6 +20,7 @@ import (
"log"
"net"
"os"
"slices"

"golang.org/x/crypto/hkdf"
)
Expand Down Expand Up @@ -181,3 +183,16 @@ func IsRawSGXQuote(quote []byte) bool {

return true
}

// SliceEqualElements checks if a slice contains the same elements as another slice.
// Order of elements does not matter.
// Elements must be of type [cmp.Ordered].
func SliceEqualElements[T cmp.Ordered](a, b []T) bool {
aCopy := make([]T, len(a))
bCopy := make([]T, len(b))
copy(aCopy, a)
copy(bCopy, b)
slices.Sort(aCopy)
slices.Sort(bCopy)
return slices.Equal(aCopy, bCopy)
}
37 changes: 37 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,40 @@ func TestIsRawSGXQuote(t *testing.T) {
})
}
}

func TestSliceEqualElements(t *testing.T) {
testCases := map[string]struct {
sliceA, sliceB []string
want bool
}{
"empty slices": {
sliceA: []string{},
sliceB: []string{},
want: true,
},
"one empty slice": {
sliceA: []string{"foo"},
sliceB: []string{},
want: false,
},
"equal slices": {
sliceA: []string{"foo", "bar"},
sliceB: []string{"foo", "bar"},
want: true,
},
"element order doesn't matter": {
sliceA: []string{"foo", "bar"},
sliceB: []string{"bar", "foo"},
want: true,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)

assert.Equal(tc.want, SliceEqualElements(tc.sliceA, tc.sliceB))
assert.Equal(tc.want, SliceEqualElements(tc.sliceB, tc.sliceA))
})
}
}

0 comments on commit e7ee0c1

Please sign in to comment.