-
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1313 from safing/feature/profile-sync
Add profile merging and prepare for sync
- Loading branch information
Showing
14 changed files
with
502 additions
and
27 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package profile | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDeriveProfileID(t *testing.T) { | ||
t.Parallel() | ||
|
||
fps := []Fingerprint{ | ||
{ | ||
Type: FingerprintTypePathID, | ||
Operation: FingerprintOperationEqualsID, | ||
Value: "/sbin/init", | ||
}, | ||
{ | ||
Type: FingerprintTypePathID, | ||
Operation: FingerprintOperationPrefixID, | ||
Value: "/", | ||
}, | ||
{ | ||
Type: FingerprintTypeEnvID, | ||
Key: "PORTMASTER_PROFILE", | ||
Operation: FingerprintOperationEqualsID, | ||
Value: "TEST-1", | ||
}, | ||
{ | ||
Type: FingerprintTypeTagID, | ||
Key: "tag-key-1", | ||
Operation: FingerprintOperationEqualsID, | ||
Value: "tag-key-2", | ||
}, | ||
} | ||
|
||
// Create rand source for shuffling. | ||
rnd := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint:gosec | ||
|
||
// Test 100 times. | ||
for i := 0; i < 100; i++ { | ||
// Shuffle fingerprints. | ||
rnd.Shuffle(len(fps), func(i, j int) { | ||
fps[i], fps[j] = fps[j], fps[i] | ||
}) | ||
|
||
// Check if fingerprint matches. | ||
id := deriveProfileID(fps) | ||
assert.Equal(t, "PTSRP7rdCnmvdjRoPMTrtjj7qk7PxR1a9YdBWUGwnZXJh2", id) | ||
} | ||
} |
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,57 @@ | ||
package profile | ||
|
||
import ( | ||
"strings" | ||
|
||
"golang.org/x/exp/slices" | ||
) | ||
|
||
// Icon describes an icon. | ||
type Icon struct { | ||
Type IconType | ||
Value string | ||
} | ||
|
||
// IconType describes the type of an Icon. | ||
type IconType string | ||
|
||
// Supported icon types. | ||
const ( | ||
IconTypeFile IconType = "path" | ||
IconTypeDatabase IconType = "database" | ||
) | ||
|
||
func (t IconType) sortOrder() int { | ||
switch t { | ||
case IconTypeDatabase: | ||
return 1 | ||
case IconTypeFile: | ||
return 2 | ||
default: | ||
return 100 | ||
} | ||
} | ||
|
||
func sortAndCompactIcons(icons []Icon) []Icon { | ||
// Sort. | ||
slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int { | ||
aOrder := a.Type.sortOrder() | ||
bOrder := b.Type.sortOrder() | ||
|
||
switch { | ||
case aOrder != bOrder: | ||
return aOrder - bOrder | ||
case a.Value != b.Value: | ||
return strings.Compare(a.Value, b.Value) | ||
default: | ||
return 0 | ||
} | ||
}) | ||
|
||
// De-duplicate. | ||
icons = slices.CompactFunc[[]Icon, Icon](icons, func(a, b Icon) bool { | ||
return a.Type == b.Type && a.Value == b.Value | ||
}) | ||
|
||
return icons | ||
} |
Oops, something went wrong.