Skip to content

Commit c7628e3

Browse files
authored
Move to OPAQUE (#811)
Fixes #813
1 parent 2fd36dd commit c7628e3

File tree

13 files changed

+170
-568
lines changed

13 files changed

+170
-568
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ install: ## Install all binaries
4343
.PHONY: lint
4444
lint: $(BIN)/golangci-lint $(BIN)/buf ## Lint Go and protobuf
4545
test -z "$$($(BIN)/buf format -d . | tee /dev/stderr)"
46-
$(GO) vet ./...
4746
$(BIN)/golangci-lint run
4847
$(BIN)/buf format -d --exit-code
4948

buf.gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ plugins:
44
out: private/gen/modules
55
opt:
66
- paths=source_relative
7-
- default_api_level=API_HYBRID
7+
- default_api_level=API_OPAQUE

cmd/release/main_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func TestCalculateNewReleaseModules(t *testing.T) {
4444
assertModuleStates(t, map[string]releaseModuleState{
4545
"envoyproxy/envoy": {
4646
status: modules.New,
47-
references: []*statev1alpha1.ModuleReference{{
47+
references: []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{
4848
Name: "bb554f53ad8d3a2a2ae4cbd7102a3e20ae00b558",
4949
Digest: "dummyManifestDigestEnvoy",
50-
}},
50+
}.Build()},
5151
}}, got)
5252
assert.True(t, shouldRelease(got))
5353
})
@@ -65,10 +65,10 @@ func TestCalculateNewReleaseModules(t *testing.T) {
6565
assertModuleStates(t, map[string]releaseModuleState{
6666
"envoyproxy/envoy": {
6767
status: modules.Updated,
68-
references: []*statev1alpha1.ModuleReference{{
68+
references: []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{
6969
Name: "7850b6bb6494e3bfc093b1aff20282ab30b67940",
7070
Digest: "updatedDummyManifestDigestEnvoy",
71-
}},
71+
}.Build()},
7272
}}, got)
7373
assert.True(t, shouldRelease(got))
7474
})
@@ -109,17 +109,17 @@ func TestCalculateNewReleaseModules(t *testing.T) {
109109
},
110110
"envoyproxy/envoy": {
111111
status: modules.Updated,
112-
references: []*statev1alpha1.ModuleReference{{
112+
references: []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{
113113
Name: "7850b6bb6494e3bfc093b1aff20282ab30b67940",
114114
Digest: "updatedDummyManifestDigestEnvoy",
115-
}},
115+
}.Build()},
116116
},
117117
"gogo/protobuf": {
118118
status: modules.New,
119-
references: []*statev1alpha1.ModuleReference{{
119+
references: []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{
120120
Name: "8892e00f944642b7dc8d81b419879fd4be12f056",
121121
Digest: "newDummyManifestDigestGogoProtobuf",
122-
}},
122+
}.Build()},
123123
}}, got)
124124
assert.True(t, shouldRelease(got))
125125
})
@@ -143,14 +143,14 @@ func TestCalculateNewReleaseModules(t *testing.T) {
143143
"envoyproxy/envoy": {
144144
status: modules.Updated,
145145
references: []*statev1alpha1.ModuleReference{
146-
{
146+
statev1alpha1.ModuleReference_builder{
147147
Name: "v0.1.0",
148148
Digest: "dummyManifestDigestEnvoy",
149-
},
150-
{
149+
}.Build(),
150+
statev1alpha1.ModuleReference_builder{
151151
Name: "v0.2.0",
152152
Digest: "updatedDummyManifestDigestEnvoy",
153-
},
153+
}.Build(),
154154
},
155155
}}, got)
156156
assert.True(t, shouldRelease(got))
@@ -177,17 +177,17 @@ func TestCalculateNewReleaseModules(t *testing.T) {
177177
},
178178
"new/foo": {
179179
status: modules.New,
180-
references: []*statev1alpha1.ModuleReference{{
180+
references: []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{
181181
Name: "ref3",
182182
Digest: "dummyManifestDigestNewFoo",
183-
}},
183+
}.Build()},
184184
},
185185
"new/bar": {
186186
status: modules.New,
187-
references: []*statev1alpha1.ModuleReference{{
187+
references: []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{
188188
Name: "ref4",
189189
Digest: "dummyManifestDigestNewBar",
190-
}},
190+
}.Build()},
191191
}}, got)
192192
assert.True(t, shouldRelease(got))
193193
})
@@ -205,17 +205,17 @@ func TestMapGlobalStateReferences(t *testing.T) {
205205
t.Run("not_nil_state", func(t *testing.T) {
206206
t.Parallel()
207207

208-
globalState := &statev1alpha1.GlobalState{
208+
globalState := statev1alpha1.GlobalState_builder{
209209
Modules: []*statev1alpha1.GlobalStateReference{
210-
{
210+
statev1alpha1.GlobalStateReference_builder{
211211
ModuleName: "test-org/test-repo",
212212
LatestReference: "v1.0.0",
213-
}, {
213+
}.Build(), statev1alpha1.GlobalStateReference_builder{
214214
ModuleName: "other-org/other-repo",
215215
LatestReference: "v1.0.0",
216-
},
216+
}.Build(),
217217
},
218-
}
218+
}.Build()
219219
got := mapGlobalStateReferences(globalState)
220220
require.Equal(t, map[string]string{
221221
"test-org/test-repo": "v1.0.0",
@@ -232,13 +232,13 @@ func TestCreateReleaseBody(t *testing.T) {
232232
"test-org/test-repo": {
233233
status: modules.New,
234234
references: []*statev1alpha1.ModuleReference{
235-
{
235+
statev1alpha1.ModuleReference_builder{
236236
Name: "v1.0.0",
237237
Digest: "fakedigest",
238-
}, {
238+
}.Build(), statev1alpha1.ModuleReference_builder{
239239
Name: "v1.1.0",
240240
Digest: "fakedigest",
241-
},
241+
}.Build(),
242242
},
243243
},
244244
}
@@ -267,25 +267,25 @@ func TestCreateReleaseBody(t *testing.T) {
267267
"test-org/new-repo": {
268268
status: modules.New,
269269
references: []*statev1alpha1.ModuleReference{
270-
{
270+
statev1alpha1.ModuleReference_builder{
271271
Name: "v1.0.0",
272272
Digest: "fakedigest",
273-
}, {
273+
}.Build(), statev1alpha1.ModuleReference_builder{
274274
Name: "v1.1.0",
275275
Digest: "fakedigest",
276-
},
276+
}.Build(),
277277
},
278278
},
279279
"test-org/updated-repo": {
280280
status: modules.Updated,
281281
references: []*statev1alpha1.ModuleReference{
282-
{
282+
statev1alpha1.ModuleReference_builder{
283283
Name: "v1.0.0",
284284
Digest: "fakedigest",
285-
}, {
285+
}.Build(), statev1alpha1.ModuleReference_builder{
286286
Name: "v1.1.0",
287287
Digest: "fakedigest",
288-
},
288+
}.Build(),
289289
},
290290
},
291291
}
@@ -341,7 +341,7 @@ func TestCreateReleaseBody(t *testing.T) {
341341
//nolint:exhaustive // other module states should not have any reference
342342
switch state {
343343
case modules.New, modules.Updated:
344-
references = []*statev1alpha1.ModuleReference{{Name: "v1.0.0", Digest: "fakedigest"}}
344+
references = []*statev1alpha1.ModuleReference{statev1alpha1.ModuleReference_builder{Name: "v1.0.0", Digest: "fakedigest"}.Build()}
345345
}
346346
// map order is not guaranteed
347347
mods[modName] = releaseModuleState{
@@ -437,10 +437,10 @@ func TestWriteReferencesTable(t *testing.T) {
437437
populateReferences := func(refsCount int) []*statev1alpha1.ModuleReference {
438438
refs := make([]*statev1alpha1.ModuleReference, refsCount)
439439
for i := range refsCount {
440-
refs[i] = &statev1alpha1.ModuleReference{
440+
refs[i] = statev1alpha1.ModuleReference_builder{
441441
Name: fmt.Sprintf("commit%03d", i),
442442
Digest: fmt.Sprintf("digest%03d", i),
443-
}
443+
}.Build()
444444
}
445445
return refs
446446
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ require (
1717
)
1818

1919
require (
20+
buf.build/go/protoyaml v0.3.1 // indirect
2021
cel.dev/expr v0.19.1 // indirect
2122
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
23+
github.com/bufbuild/protocompile v0.14.1 // indirect
2224
github.com/davecgh/go-spew v1.1.1 // indirect
2325
github.com/google/cel-go v0.22.1 // indirect
2426
github.com/google/go-querystring v1.1.0 // indirect
@@ -27,6 +29,7 @@ require (
2729
github.com/stoewer/go-strcase v1.3.0 // indirect
2830
golang.org/x/crypto v0.31.0 // indirect
2931
golang.org/x/exp v0.0.0-20250103183323-7d7fa50e5329 // indirect
32+
golang.org/x/sync v0.10.0 // indirect
3033
golang.org/x/sys v0.29.0 // indirect
3134
golang.org/x/text v0.21.0 // indirect
3235
google.golang.org/genproto/googleapis/api v0.0.0-20250102185135-69823020774d // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.2-20241127180247-a33202765966.1 h1:BICM6du/XzvEgeorNo4xgohK3nMTmEPViGyd5t7xVqk=
22
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.2-20241127180247-a33202765966.1/go.mod h1:JnMVLi3qrNYPODVpEKG7UjHLl/d2zR221e66YCSmP2Q=
3+
buf.build/go/protoyaml v0.3.1 h1:ucyzE7DRnjX+mQ6AH4JzN0Kg50ByHHu+yrSKbgQn2D4=
4+
buf.build/go/protoyaml v0.3.1/go.mod h1:0TzNpFQDXhwbkXb/ajLvxIijqbve+vMQvWY/b3/Dzxg=
35
cel.dev/expr v0.19.1 h1:NciYrtDRIR0lNCnH1LFJegdjspNx9fI59O7TWcua/W4=
46
cel.dev/expr v0.19.1/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
57
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
68
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
79
github.com/bufbuild/buf v1.49.0 h1:KFSyea50xgvfnqGfK7Z4UCPTYhoCoUYAK2IoRd8/oUA=
810
github.com/bufbuild/buf v1.49.0/go.mod h1:PssF/YByu7WU6K3FxRmoRxi4ZViugL7rjAaoVvxknso=
11+
github.com/bufbuild/protocompile v0.14.1 h1:iA73zAf/fyljNjQKwYzUHD6AD4R8KMasmwa/FBatYVw=
12+
github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU5DoEw9xY/FUi1c=
913
github.com/bufbuild/protovalidate-go v0.8.2 h1:sgzXHkHYP6HnAsL2Rd3I1JxkYUyEQUv9awU1PduMxbM=
1014
github.com/bufbuild/protovalidate-go v0.8.2/go.mod h1:K6w8iPNAXBoIivVueSELbUeUl+MmeTQfCDSug85pn3M=
1115
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -64,6 +68,8 @@ golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
6468
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
6569
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
6670
golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
71+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
72+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
6773
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
6874
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
6975
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=

internal/githubutil/githubutil.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package githubutil
1616

1717
import (
1818
"context"
19-
"encoding/json"
2019
"errors"
2120
"fmt"
2221
"io"
@@ -26,6 +25,7 @@ import (
2625
"strings"
2726
"time"
2827

28+
"github.com/bufbuild/buf/private/pkg/protoencoding"
2929
"github.com/bufbuild/modules/private/bufpkg/bufstate"
3030
statev1alpha1 "github.com/bufbuild/modules/private/gen/modules/state/v1alpha1"
3131
"github.com/google/go-github/v64/github"
@@ -191,15 +191,15 @@ func (c *Client) DownloadReleaseState(
191191
ctx context.Context,
192192
release *github.RepositoryRelease,
193193
) (*statev1alpha1.GlobalState, error) {
194-
manifestBytes, _, err := c.downloadAsset(ctx, release, bufstate.ModStateFileName)
194+
data, _, err := c.downloadAsset(ctx, release, bufstate.ModStateFileName)
195195
if err != nil {
196196
return nil, err
197197
}
198-
var moduleReleases statev1alpha1.GlobalState
199-
if err := json.Unmarshal(manifestBytes, &moduleReleases); err != nil {
198+
var globalState statev1alpha1.GlobalState
199+
if err := protoencoding.NewJSONUnmarshaler(protoencoding.EmptyResolver).Unmarshal(data, &globalState); err != nil {
200200
return nil, err
201201
}
202-
return &moduleReleases, nil
202+
return &globalState, nil
203203
}
204204

205205
// downloadAsset uses the GitHub API to download the asset with the given name from the release.

private/bufpkg/bufstate/global_state.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package bufstate
1616

1717
import (
18-
"encoding/json"
1918
"fmt"
2019
"io"
2120
"sort"
2221

22+
"github.com/bufbuild/buf/private/pkg/protoencoding"
2323
statev1alpha1 "github.com/bufbuild/modules/private/gen/modules/state/v1alpha1"
2424
"go.uber.org/multierr"
2525
)
@@ -33,12 +33,16 @@ func (rw *ReadWriter) ReadGlobalState(reader io.ReadCloser) (_ *statev1alpha1.Gl
3333
retErr = multierr.Append(retErr, fmt.Errorf("close file: %w", err))
3434
}
3535
}()
36+
bytes, err := io.ReadAll(reader)
37+
if err != nil {
38+
return nil, fmt.Errorf("read global state file: %w", err)
39+
}
3640
var globalState statev1alpha1.GlobalState
37-
if err := json.NewDecoder(reader).Decode(&globalState); err != nil {
38-
return nil, fmt.Errorf("read file: %w", err)
41+
if err := protoencoding.NewJSONUnmarshaler(protoencoding.EmptyResolver).Unmarshal(bytes, &globalState); err != nil {
42+
return nil, fmt.Errorf("unmarshal global state: %w", err)
3943
}
4044
if err := rw.validator.Validate(&globalState); err != nil {
41-
return nil, fmt.Errorf("validate: %w", err)
45+
return nil, fmt.Errorf("validate global state: %w", err)
4246
}
4347
return &globalState, nil
4448
}
@@ -57,10 +61,14 @@ func (rw *ReadWriter) WriteGlobalState(writer io.WriteCloser, globalState *state
5761
sort.Slice(mods, func(i, j int) bool {
5862
return mods[i].GetModuleName() < mods[j].GetModuleName()
5963
})
60-
globalState.Modules = mods
61-
data, err := json.MarshalIndent(globalState, "", " ")
64+
globalState.SetModules(mods)
65+
data, err := protoencoding.NewJSONMarshaler(
66+
protoencoding.EmptyResolver,
67+
protoencoding.JSONMarshalerWithUseProtoNames(),
68+
protoencoding.JSONMarshalerWithIndent(),
69+
).Marshal(globalState)
6270
if err != nil {
63-
return fmt.Errorf("json marshal state: %w", err)
71+
return fmt.Errorf("marshal global state: %w", err)
6472
}
6573
if _, err := writer.Write(data); err != nil {
6674
return fmt.Errorf("write to file: %w", err)

0 commit comments

Comments
 (0)