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 hpke public key #337

Merged
merged 22 commits into from
Jan 23, 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
35 changes: 35 additions & 0 deletions .github/workflows/push-mls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Push MLS Container
on:
push:
branches:
# - mls
- nm/add-hpke-public-key
jobs:
deploy:
concurrency: main
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: xmtpeng
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Git Checkout
uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version-file: go.mod

- name: Push
id: push
run: |
export DOCKER_IMAGE_TAG=mls-dev
IMAGE_TO_DEPLOY=xmtp/node-go@$(dev/docker/build)
echo Successfully pushed $IMAGE_TO_DEPLOY
echo "docker_image=${IMAGE_TO_DEPLOY}" >> $GITHUB_OUTPUT
4 changes: 4 additions & 0 deletions pkg/migrations/mls/20240122230601_add-hpke-key.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SET statement_timeout = 0;

ALTER TABLE welcome_messages
DROP COLUMN IF EXISTS hpke_public_key BYTEA;
4 changes: 4 additions & 0 deletions pkg/migrations/mls/20240122230601_add-hpke-key.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SET statement_timeout = 0;

ALTER TABLE welcome_messages
ADD COLUMN hpke_public_key BYTEA;
7 changes: 6 additions & 1 deletion pkg/mls/api/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (s *Service) SendWelcomeMessages(ctx context.Context, req *mlsv1.SendWelcom

// TODO: Wrap this in a transaction so publishing is all or nothing
for _, input := range req.Messages {
msg, err := s.store.InsertWelcomeMessage(ctx, input.GetV1().InstallationKey, input.GetV1().Data)
msg, err := s.store.InsertWelcomeMessage(ctx, input.GetV1().InstallationKey, input.GetV1().Data, input.GetV1().HpkePublicKey)
if err != nil {
if mlsstore.IsAlreadyExistsError(err) {
continue
Expand Down Expand Up @@ -478,6 +478,11 @@ func validateSendWelcomeMessagesRequest(req *mlsv1.SendWelcomeMessagesRequest) e
if input == nil || input.GetV1() == nil {
return status.Errorf(codes.InvalidArgument, "invalid welcome message")
}

v1 := input.GetV1()
if len(v1.Data) == 0 || len(v1.InstallationKey) == 0 || len(v1.HpkePublicKey) == 0 {
return status.Errorf(codes.InvalidArgument, "invalid welcome message")
}
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/mls/api/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func TestSendWelcomeMessages(t *testing.T) {
V1: &mlsv1.WelcomeMessageInput_V1{
InstallationKey: []byte(installationId),
Data: []byte("test"),
HpkePublicKey: []byte("test"),
},
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/mls/store/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ type WelcomeMessage struct {
CreatedAt time.Time `bun:",notnull"`
InstallationKey []byte `bun:",notnull,type:bytea"`
Data []byte `bun:",notnull,type:bytea"`
HpkePublicKey []byte `bun:"hpke_public_key,notnull,type:bytea"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: s/Hpke/HPKE

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It might add more confusion than it's worth. The generated protos are always going to be Hpke, so changing the model would give the DB the same field name with different casing.

}
13 changes: 7 additions & 6 deletions pkg/mls/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type MlsStore interface {
FetchKeyPackages(ctx context.Context, installationIds [][]byte) ([]*Installation, error)
GetIdentityUpdates(ctx context.Context, walletAddresses []string, startTimeNs int64) (map[string]IdentityUpdateList, error)
InsertGroupMessage(ctx context.Context, groupId []byte, data []byte) (*GroupMessage, error)
InsertWelcomeMessage(ctx context.Context, installationId []byte, data []byte) (*WelcomeMessage, error)
InsertWelcomeMessage(ctx context.Context, installationId []byte, data []byte, hpkePublicKey []byte) (*WelcomeMessage, error)
QueryGroupMessagesV1(ctx context.Context, query *mlsv1.QueryGroupMessagesRequest) (*mlsv1.QueryGroupMessagesResponse, error)
QueryWelcomeMessagesV1(ctx context.Context, query *mlsv1.QueryWelcomeMessagesRequest) (*mlsv1.QueryWelcomeMessagesResponse, error)
}
Expand Down Expand Up @@ -193,13 +193,13 @@ func (s *Store) InsertGroupMessage(ctx context.Context, groupId []byte, data []b
return &message, nil
}

func (s *Store) InsertWelcomeMessage(ctx context.Context, installationId []byte, data []byte) (*WelcomeMessage, error) {
func (s *Store) InsertWelcomeMessage(ctx context.Context, installationId []byte, data []byte, hpkePublicKey []byte) (*WelcomeMessage, error) {
message := WelcomeMessage{
Data: data,
}

var id uint64
err := s.db.QueryRow("INSERT INTO welcome_messages (installation_key, data, installation_key_data_hash) VALUES (?, ?, ?) RETURNING id", installationId, data, sha256.Sum256(append(installationId, data...))).Scan(&id)
err := s.db.QueryRow("INSERT INTO welcome_messages (installation_key, data, installation_key_data_hash, hpke_public_key) VALUES (?, ?, ?, ?) RETURNING id", installationId, data, sha256.Sum256(append(installationId, data...)), hpkePublicKey).Scan(&id)
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil, NewAlreadyExistsError(err)
Expand Down Expand Up @@ -328,9 +328,10 @@ func (s *Store) QueryWelcomeMessagesV1(ctx context.Context, req *mlsv1.QueryWelc
messages = append(messages, &mlsv1.WelcomeMessage{
Version: &mlsv1.WelcomeMessage_V1_{
V1: &mlsv1.WelcomeMessage_V1{
Id: msg.Id,
CreatedNs: uint64(msg.CreatedAt.UnixNano()),
Data: msg.Data,
Id: msg.Id,
CreatedNs: uint64(msg.CreatedAt.UnixNano()),
Data: msg.Data,
HpkePublicKey: msg.HpkePublicKey,
},
},
})
Expand Down
36 changes: 19 additions & 17 deletions pkg/mls/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,14 @@ func TestInsertWelcomeMessage_Single(t *testing.T) {
defer cleanup()

ctx := context.Background()
msg, err := store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data"))
msg, err := store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data"), []byte("hpke"))
require.NoError(t, err)
require.NotNil(t, msg)
require.Equal(t, uint64(1), msg.Id)
require.True(t, msg.CreatedAt.Before(time.Now().UTC()) && msg.CreatedAt.After(started))
require.Equal(t, []byte("installation"), msg.InstallationKey)
require.Equal(t, []byte("data"), msg.Data)
require.Equal(t, []byte("hpke"), msg.HpkePublicKey)

msgs := make([]*WelcomeMessage, 0)
err = store.db.NewSelect().Model(&msgs).Scan(ctx)
Expand All @@ -257,11 +258,11 @@ func TestInsertWelcomeMessage_Duplicate(t *testing.T) {
defer cleanup()

ctx := context.Background()
msg, err := store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data"))
msg, err := store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data"), []byte("hpke"))
require.NoError(t, err)
require.NotNil(t, msg)

msg, err = store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data"))
msg, err = store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data"), []byte("hpke"))
require.Nil(t, msg)
require.IsType(t, &AlreadyExistsError{}, err)
require.True(t, IsAlreadyExistsError(err))
Expand All @@ -272,11 +273,11 @@ func TestInsertWelcomeMessage_ManyOrderedByTime(t *testing.T) {
defer cleanup()

ctx := context.Background()
_, err := store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data1"))
_, err := store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data1"), []byte("hpke"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data2"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data2"), []byte("hpke"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data3"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation"), []byte("data3"), []byte("hpke"))
require.NoError(t, err)

msgs := make([]*WelcomeMessage, 0)
Expand Down Expand Up @@ -361,11 +362,11 @@ func TestQueryWelcomeMessagesV1_Filter(t *testing.T) {
defer cleanup()

ctx := context.Background()
_, err := store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("data1"))
_, err := store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("data1"), []byte("hpke1"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation2"), []byte("data2"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation2"), []byte("data2"), []byte("hpke2"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("data3"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("data3"), []byte("hpke3"))
require.NoError(t, err)

resp, err := store.QueryWelcomeMessagesV1(ctx, &mlsv1.QueryWelcomeMessagesRequest{
Expand All @@ -381,6 +382,7 @@ func TestQueryWelcomeMessagesV1_Filter(t *testing.T) {
require.Len(t, resp.Messages, 2)
require.Equal(t, []byte("data3"), resp.Messages[0].GetV1().Data)
require.Equal(t, []byte("data1"), resp.Messages[1].GetV1().Data)
require.Equal(t, []byte("hpke3"), resp.Messages[0].GetV1().HpkePublicKey)

resp, err = store.QueryWelcomeMessagesV1(ctx, &mlsv1.QueryWelcomeMessagesRequest{
InstallationKey: []byte("installation2"),
Expand Down Expand Up @@ -502,21 +504,21 @@ func TestQueryWelcomeMessagesV1_Paginate(t *testing.T) {
defer cleanup()

ctx := context.Background()
_, err := store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content1"))
_, err := store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content1"), []byte("hpke1"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation2"), []byte("content2"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation2"), []byte("content2"), []byte("hpke2"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content3"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content3"), []byte("hpke3"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation2"), []byte("content4"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation2"), []byte("content4"), []byte("hpke4"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content5"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content5"), []byte("hpke5"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content6"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content6"), []byte("hpke6"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content7"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content7"), []byte("hpke7"))
require.NoError(t, err)
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content8"))
_, err = store.InsertWelcomeMessage(ctx, []byte("installation1"), []byte("content8"), []byte("hpke8"))
require.NoError(t, err)

resp, err := store.QueryWelcomeMessagesV1(ctx, &mlsv1.QueryWelcomeMessagesRequest{
Expand Down
Loading
Loading