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

Add ability to handle public/permissioned identities #9

Merged
merged 2 commits into from
Jul 2, 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
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.21
require (
github.com/datatrails/go-datatrails-common-api-gen v0.3.7
github.com/golang/protobuf v1.5.3
github.com/stretchr/testify v1.9.0
github.com/zeebo/bencode v1.0.0
google.golang.org/protobuf v1.31.0
gotest.tools/v3 v3.5.1
)

require (
Expand All @@ -16,14 +16,15 @@ require (
github.com/containerd/cgroups/v3 v3.0.2 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/datatrails/go-datatrails-common v0.10.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand All @@ -36,4 +37,5 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/zeebo/bencode v1.0.0 h1:zgop0Wu1nu4IexAZeCZ5qbsjU4O1vMrfCrVgUjbHVuA=
github.com/zeebo/bencode v1.0.0/go.mod h1:Ct7CkrWIQuLWAy9M3atFHYq4kG9Ao/SsY5cdtCXmp9Y=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
Expand Down Expand Up @@ -86,8 +86,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
2 changes: 1 addition & 1 deletion simplehash/schemav2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/datatrails/go-datatrails-common-api-gen/attribute/v2/attribute"
"github.com/datatrails/go-datatrails-common-api-gen/marshalers/simpleoneof"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"gotest.tools/v3/assert"
)

var (
Expand Down
9 changes: 8 additions & 1 deletion simplehash/schemav3.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ func V3FromEventJSON(eventJson []byte) (V3Event, error) {
if err != nil {
return V3Event{}, err
}

// change all instances of public identities to permissioned identities
// we only use permissioned identities as part of the v3 hash schema
eventShashV3.Identity = v2assets.PermissionedIdentityFromPublic(eventShashV3.Identity)

return eventShashV3, nil
}

// V2FromEventResponse transforms a single event in grpc proto format (message bus
// V3FromEventResponse transforms a single event in grpc proto format (message bus
// compatible) to the canonical, publicly verifiable, api format.
func V3FromEventResponse(marshaler *simpleoneof.Marshaler, event *v2assets.EventResponse) (V3Event, error) {
eventJson, err := marshaler.Marshal(event)
Expand All @@ -115,6 +120,7 @@ func V3FromEventResponse(marshaler *simpleoneof.Marshaler, event *v2assets.Event
// boundaries.
// - WithPublicFromPermissioned should be set if the event is the
// permissioned (owner) counter part of a public attestation.
// NOTE: should not be used for valid v3 schema
func (h *HasherV3) HashEvent(event *v2assets.EventResponse, opts ...HashOption) error {

o := HashOptions{}
Expand Down Expand Up @@ -149,6 +155,7 @@ func (h *HasherV3) HashEvent(event *v2assets.EventResponse, opts ...HashOption)
// boundaries.
// - WithPublicFromPermissioned should be set if the event is the
// permissioned (owner) counter part of a public attestation.
// NOTE: should not be used for valid v3 schema
func (h *HasherV3) HashEventFromJSON(eventJson []byte, opts ...HashOption) error {

o := HashOptions{}
Expand Down
92 changes: 91 additions & 1 deletion simplehash/schemav3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"time"

v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"gotest.tools/v3/assert"
)

var (
Expand Down Expand Up @@ -131,3 +131,93 @@ func TestV3Event_ToPublicIdentity(t *testing.T) {
})
}
}

// TestV3FromEventJSON tests:
//
// 1. permissioned event is correctly interpretted into a v3event.
// 2. public event is correctly interpretted into a v3event.
func TestV3FromEventJSON(t *testing.T) {
type args struct {
eventJson []byte
}
tests := []struct {
name string
args args
expected V3Event
err error
}{
{
name: "positive permissioned",
args: args{
eventJson: []byte(`{"identity":"assets/1234/events/5678"}`),
},
expected: V3Event{
Identity: "assets/1234/events/5678",
},
err: nil,
},
{
name: "positive public",
args: args{
eventJson: []byte(`{"identity":"publicassets/1234/events/5678"}`),
},
expected: V3Event{
Identity: "assets/1234/events/5678",
},
err: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := V3FromEventJSON(test.args.eventJson)

assert.Equal(t, test.err, err)
assert.Equal(t, test.expected, actual)
})
}
}

// TestV3FromEventResponse tests:
//
// 1. permissioned event is correctly interpretted into a v3event.
// 2. public event is correctly interpretted into a v3event.
func TestV3FromEventResponse_ConvertsPublicIdentityToPermissioned(t *testing.T) {
type args struct {
event *v2assets.EventResponse
}
tests := []struct {
name string
args args
expected V3Event
err error
}{
{
name: "positive permissioned",
args: args{
event: &v2assets.EventResponse{Identity: "assets/1234/events/5678"},
},
expected: V3Event{
Identity: "assets/1234/events/5678",
},
err: nil,
},
{
name: "positive public",
args: args{
event: &v2assets.EventResponse{Identity: "publicassets/1234/events/5678"},
},
expected: V3Event{
Identity: "assets/1234/events/5678",
},
err: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := V3FromEventResponse(NewEventMarshaler(), test.args.event)

assert.Equal(t, test.err, err)
assert.Equal(t, test.expected.Identity, actual.Identity)
})
}
}
Loading