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

feat: mechanism to fingerprint arbitrary data #392

Merged
merged 5 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
108 changes: 108 additions & 0 deletions internal/fingerprint/fingerprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package fingerprint

import (
"crypto/sha512"
eliottness marked this conversation as resolved.
Show resolved Hide resolved
"encoding/base64"
"hash"
"io"
"strconv"
"sync"
)

type Hasher struct {
hash hash.Hash
}

type Hashable interface {
Hash(h *Hasher) error
}

var pool = sync.Pool{New: func() any { return &Hasher{hash: sha512.New()} }}

// New returns a [Hasher] from the pool, ready to use.
func New() *Hasher {
h, _ := pool.Get().(*Hasher)
return h
}

// Close returns this [Hasher] to the pool.
func (h *Hasher) Close() {
h.hash.Reset()
pool.Put(h)
}

// Finish obtains this [Hasher]'s current fingerprint. It does not change the
// underlying state of the [Hasher].
func (h *Hasher) Finish() string {
var buf [sha512.Size]byte
return base64.URLEncoding.EncodeToString(h.hash.Sum(buf[:0]))
}

// Named hashes a named list of values. This creates explicit grouping of the
// values, avoiding that the concatenation of two things has a different hash
// than those same two things one after the other.
func (h *Hasher) Named(name string, vals ...Hashable) error {
var (
soh = []byte{0x01} // Start of key-value-pair beacon
sot = []byte{0x02} // Start of value & end of key beacon
etx = []byte{0x03} // End of key-value-pair beacon
)

Check warning on line 55 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L55

Added line #L55 was not covered by tests
if _, err := h.hash.Write(soh); err != nil {
return err
}

Check warning on line 58 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L57-L58

Added lines #L57 - L58 were not covered by tests

if _, err := io.WriteString(h.hash, name); err != nil {
return err
}

Check warning on line 62 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L61-L62

Added lines #L61 - L62 were not covered by tests

if _, err := h.hash.Write(sot); err != nil {
return err
}

Check warning on line 66 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L65-L66

Added lines #L65 - L66 were not covered by tests

for idx, val := range vals {
if _, err := h.hash.Write(soh); err != nil {
return err
}

Check warning on line 71 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L70-L71

Added lines #L70 - L71 were not covered by tests
if _, err := io.WriteString(h.hash, strconv.Itoa(idx)); err != nil {
return err
}

Check warning on line 74 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L73-L74

Added lines #L73 - L74 were not covered by tests
if _, err := h.hash.Write(sot); err != nil {
return err
}

Check warning on line 77 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L76-L77

Added lines #L76 - L77 were not covered by tests
if err := val.Hash(h); err != nil {
return err
}

Check warning on line 80 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L79-L80

Added lines #L79 - L80 were not covered by tests
if _, err := h.hash.Write(etx); err != nil {
return err
}

Check warning on line 83 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L82-L83

Added lines #L82 - L83 were not covered by tests
}

if _, err := h.hash.Write(etx); err != nil {
return err
}

Check warning on line 88 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L87-L88

Added lines #L87 - L88 were not covered by tests

_, err := io.WriteString(h.hash, name)
return err
}

// Fingerprint is a short-hand for creating a new [Hasher], calling
// [Hashable.Hash] on the provided value (unless it is nil), and then returning
// the [Hasher.Finish] result.
func Fingerprint(val Hashable) (string, error) {
h := New()
defer h.Close()

Check warning on line 100 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L100

Added line #L100 was not covered by tests
if val != nil {
if err := val.Hash(h); err != nil {
return "", err
}

Check warning on line 104 in internal/fingerprint/fingerprint.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/fingerprint.go#L103-L104

Added lines #L103 - L104 were not covered by tests
}

return h.Finish(), nil
}
80 changes: 80 additions & 0 deletions internal/fingerprint/fingerprint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package fingerprint_test

import (
"fmt"
"testing"

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/stretchr/testify/require"
)

func TestCast(t *testing.T) {
type mySlice []int
require.Equal(
t,
fingerprint.List[fingerprint.Int]{0, -1, -2},
fingerprint.Cast(mySlice{0, 1, 2}, func(i int) fingerprint.Int { return fingerprint.Int(-i) }),
)
}

func TestFingerprint(t *testing.T) {
cases := map[string]struct {
hashable fingerprint.Hashable
hash string
}{
"nil": {
hashable: nil,
hash: "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg_SpIdNs6c5H0NE8XYXysP-DGNKHfuwvY7kxvUdBeoGlODJ6-SfaPg==",
},
"bool.true": {
hashable: fingerprint.Bool(true),
hash: "kSDNX67wegjpcf8CSj_L6h46a0QUKm2CyijGxC5PhSWVvPU9gdd28QVBBFq9t8N5UGKUFdDcZsjYbGSlYG0y3g==",
},
"bool.false": {
hashable: fingerprint.Bool(false),
hash: "cZ-mfu9JxLKiuD8MYr3diMEGqq234hrgV8iAK3AONvgf4_FEgS2LBdZtxmPZCLJWReFTJiz21FeqNOaEr54yjQ==",
},
"int": {
hashable: fingerprint.Int(0),
hash: "MbygIJTreBJqUXsgaojHPPqexvcExwMNGCEsrOgg8CXwC_DqaNvz86VDbKY7U797-ArY1d59g1nQt_7Z28OrmQ==",
},
"string.empty": {
hashable: fingerprint.String(""),
hash: "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg_SpIdNs6c5H0NE8XYXysP-DGNKHfuwvY7kxvUdBeoGlODJ6-SfaPg==",
},
"string.test": {
hashable: fingerprint.String("test"),
hash: "7iaw3Ur350mqGo7jwQrpkj9hiYB3Lkc_iBml1JQODbJ6wYX4oOHV-E-IvIh_1nsUNzLDBMxfqa2Ob1f1ACio_w==",
},
"list.empty": {
hashable: fingerprint.List[fingerprint.Hashable]{},
hash: "694_EPPzcQS8IJWmv4sxWZrSlHzcKAJoolH265TUzk5OE1HPYVrIkRPsOHTn3ZIgYz8wuIqBln5yfhTio_MFqg==",
},
"list.items": {
hashable: fingerprint.List[fingerprint.Hashable]{fingerprint.Bool(true), fingerprint.Int(0), fingerprint.String("test")},
hash: "RhBvcI5pNAl8TwC67UQypsmZTcj-Hbc9Zh2rAkTklU_rb4G8cORxp2dJHc1cPXq218SkkCCqPM4lU0te3a4Ufg==",
},
"map": {
hashable: fingerprint.Map(
map[int]bool{1: true, 2: false},
func(k int, v bool) (string, fingerprint.Bool) {
return fmt.Sprintf("key-%d", k), fingerprint.Bool(v)
},
),
hash: "X27oOwHUqLYTjDj82abW23Q5n1zyH2LnrHtzFE0vQfMUZcq5u-rUOuMKvrNxWd8GnvcJVLHc-lc8OJZhw07nFA==",
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
hash, err := fingerprint.Fingerprint(tc.hashable)
require.NoError(t, err)
require.Equal(t, tc.hash, hash)
})
}
}
91 changes: 91 additions & 0 deletions internal/fingerprint/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

package fingerprint

import (
"io"
"slices"
"strconv"
"strings"
)

type Bool bool

func (b Bool) Hash(h *Hasher) error {
_, err := io.WriteString(h.hash, strconv.FormatBool(bool(b)))
return err
}

type Int int

func (i Int) Hash(h *Hasher) error {
_, err := io.WriteString(h.hash, strconv.Itoa(int(i)))
return err
}

type List[T Hashable] []T

func (l List[T]) Hash(h *Hasher) error {
list := make([]Hashable, len(l)+1)
list[0] = Int(len(l))
for idx, val := range l {
list[idx+1] = val
}

Check warning on line 36 in internal/fingerprint/types.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/types.go#L36

Added line #L36 was not covered by tests

return h.Named("list", list...)
}

type String string

func (s String) Hash(h *Hasher) error {
_, err := io.WriteString(h.hash, string(s))
return err
}

func Cast[E any, T ~[]E, H Hashable](slice T, fn func(E) H) List[H] {
res := make(List[H], len(slice))
for idx, val := range slice {
res[idx] = fn(val)
}

Check warning on line 52 in internal/fingerprint/types.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/types.go#L52

Added line #L52 was not covered by tests
return res
}

type (
mapped[T Hashable] []mappedItem[T]
mappedItem[T Hashable] struct {
key string
val T
}
)

func Map[K comparable, V any, H Hashable](m map[K]V, fn func(K, V) (string, H)) mapped[H] {
res := make(mapped[H], 0, len(m))

Check warning on line 66 in internal/fingerprint/types.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/types.go#L66

Added line #L66 was not covered by tests
for key, val := range m {
mkey, mval := fn(key, val)
res = append(res, mappedItem[H]{mkey, mval})
}

Check warning on line 70 in internal/fingerprint/types.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/types.go#L70

Added line #L70 was not covered by tests

slices.SortFunc(res, func(l mappedItem[H], r mappedItem[H]) int {
return strings.Compare(l.key, r.key)
})

return res
}

func (m mapped[T]) Hash(h *Hasher) error {
list := make([]Hashable, len(m)+1)
list[0] = Int(len(m))
for idx, item := range m {
list[idx+1] = item
}

Check warning on line 84 in internal/fingerprint/types.go

View check run for this annotation

Codecov / codecov/patch

internal/fingerprint/types.go#L84

Added line #L84 was not covered by tests

return h.Named("map", list...)
}

func (m mappedItem[T]) Hash(h *Hasher) error {
return h.Named(m.key, m.val)
}
3 changes: 3 additions & 0 deletions internal/injector/aspect/advice/advice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package advice

import (
"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/jennifer/jen"
)
Expand All @@ -29,4 +30,6 @@ type Advice interface {
// short-circuit and not do anything; e.g. import injection may be skipped if
// the import already exists).
Apply(context.AdviceContext) (bool, error)

fingerprint.Hashable
}
11 changes: 8 additions & 3 deletions internal/injector/aspect/advice/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import (
"fmt"

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/advice/code"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/dst"
Expand All @@ -16,10 +17,10 @@
)

type assignValue struct {
Template code.Template
Template *code.Template
}

func AssignValue(template code.Template) *assignValue {
func AssignValue(template *code.Template) *assignValue {
return &assignValue{template}
}

Expand Down Expand Up @@ -52,9 +53,13 @@
return jen.Qual(pkgPath, "AssignValue").Call(a.Template.AsCode())
}

func (a *assignValue) Hash(h *fingerprint.Hasher) error {
return h.Named("assign-value", a.Template)

Check warning on line 57 in internal/injector/aspect/advice/assign.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/advice/assign.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}

func init() {
unmarshalers["assign-value"] = func(node *yaml.Node) (Advice, error) {
var template code.Template
var template *code.Template
if err := node.Decode(&template); err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions internal/injector/aspect/advice/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import (
"fmt"

"github.com/DataDog/orchestrion/internal/fingerprint"
"github.com/DataDog/orchestrion/internal/injector/aspect/advice/code"
"github.com/DataDog/orchestrion/internal/injector/aspect/context"
"github.com/dave/dst"
Expand All @@ -16,13 +17,13 @@
)

type prependStatements struct {
Template code.Template
Template *code.Template
}

// PrependStmts prepends statements to the matched *dst.BlockStmt. This action
// can only be used if the selector matches on a *dst.BlockStmt. The prepended
// statements are wrapped in a new block statement to prevent scope leakage.
func PrependStmts(template code.Template) *prependStatements {
func PrependStmts(template *code.Template) *prependStatements {
return &prependStatements{Template: template}
}

Expand Down Expand Up @@ -51,13 +52,17 @@
return jen.Qual(pkgPath, "PrependStmts").Call(a.Template.AsCode())
}

func (a *prependStatements) Hash(h *fingerprint.Hasher) error {
return h.Named("prepend-statements", a.Template)

Check warning on line 56 in internal/injector/aspect/advice/block.go

View check run for this annotation

Codecov / codecov/patch

internal/injector/aspect/advice/block.go#L55-L56

Added lines #L55 - L56 were not covered by tests
}

func (a *prependStatements) AddedImports() []string {
return a.Template.AddedImports()
}

func init() {
unmarshalers["prepend-statements"] = func(node *yaml.Node) (Advice, error) {
var template code.Template
var template *code.Template
if err := node.Decode(&template); err != nil {
return nil, err
}
Expand Down
Loading