Skip to content
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
7 changes: 5 additions & 2 deletions internal/witness/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ func NewWitnessGateway(group WitnessGroup, client *http.Client, oldSize uint64,
vs := dedupVerifiers(vs)
if len(vs) > 0 {
witnesses = append(witnesses, &witnessClient{
client: client,
url: u,
client: client,
// Can't use path.Join here as it'll nobble the double-slash in URLs.
// url.JoinPath returns an error, which we can't handle here, but we already know that the URL is valid since
// it's coming via WitnessGroup.WitnessEndpoints(). So we'll just do the string manipulation ourselves.
url: strings.TrimRight(u, "/") + "/add-checkpoint",
Comment on lines +128 to +131

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could the / be trimmed in WitnessGroup.WitnessEndpoints()? This would ensure all URLs are aligned at the source.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Seems more correct to do it here; if it weren't for the potential to return an error, I'd have just used url.JoinPath here which would have done this normalisation internally.

verifiers: vs,
size: oldSize,
})
Expand Down
18 changes: 9 additions & 9 deletions internal/witness/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ func TestWitnessGateway_Update(t *testing.T) {
t.Fatal(err)
}

switch r.URL.String() {
case w1u.Path:
switch r.URL.Path {
case w1u.Path + "/add-checkpoint":
witCalls.Add(1)
_, _ = w.Write(sigForSigner(t, cp, wit1Skey))
case w2u.Path:
case w2u.Path + "/add-checkpoint":
witCalls.Add(1)
_, _ = w.Write(sigForSigner(t, cp, wit2Skey))
case wbu.Path:
case wbu.Path + "/add-checkpoint":
witCalls.Add(1)
_, _ = w.Write([]byte("this is not a signature\n"))
case "/wit_multi/add-checkpoint":
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestWitnessConflict(t *testing.T) {
var wit1 tessera.Witness
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w1u := mustURL(t, wit1.URL)
if got, want := r.URL.String(), w1u.Path; got != want {
if got, want := r.URL.String(), w1u.Path+"/add-checkpoint"; got != want {
t.Fatalf("got request to URL %q but expected %q", got, want)
}

Expand Down Expand Up @@ -474,7 +474,7 @@ func TestWitnessStateEvolution(t *testing.T) {
var count int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w1u := mustURL(t, wit1.URL)
if got, want := r.URL.String(), w1u.Path; got != want {
if got, want := r.URL.String(), w1u.Path+"/add-checkpoint"; got != want {
t.Fatalf("got request to URL %q but expected %q", got, want)
}

Expand Down Expand Up @@ -539,7 +539,7 @@ func TestSlipperyWitness(t *testing.T) {
var count int
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w1u := mustURL(t, wit1.URL)
if got, want := r.URL.String(), w1u.Path; got != want {
if got, want := r.URL.String(), w1u.Path+"/add-checkpoint"; got != want {
t.Fatalf("Got request to URL %q but expected %q", got, want)
}

Expand Down Expand Up @@ -587,9 +587,9 @@ func TestWitnessReusesProofs(t *testing.T) {
w2u := mustURL(t, wit2.URL)

switch r.URL.String() {
case w1u.Path:
case w1u.Path + "/add-checkpoint":
_, _ = w.Write(sigForSigner(t, n.Text, wit1Skey))
case w2u.Path:
case w2u.Path + "/add-checkpoint":
_, _ = w.Write(sigForSigner(t, n.Text, wit2Skey))
default:
t.Fatalf("Unknown case: %s", r.URL.String())
Expand Down
6 changes: 2 additions & 4 deletions witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,10 @@ func NewWitness(vkey string, witnessRoot *url.URL) (Witness, error) {
return Witness{}, err
}

u := witnessRoot.JoinPath("/add-checkpoint")

return Witness{
Key: v,
URL: u.String(),
}, err
URL: witnessRoot.String(),
}, nil
}

// Witness represents a single witness that can be reached in order to perform a witnessing operation.
Expand Down
21 changes: 11 additions & 10 deletions witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tessera_test
import (
"net/url"
"slices"
"strings"
"testing"

f_note "github.com/transparency-dev/formats/note"
Expand Down Expand Up @@ -146,42 +147,42 @@ func TestWitnessGroup_URLs(t *testing.T) {
{
desc: "witness 1",
group: tessera.NewWitnessGroup(1, wit1),
expectedURLs: []string{"https://b1.example.com/wit1prefix/add-checkpoint"},
expectedURLs: []string{"https://b1.example.com/wit1prefix"},
},
{
desc: "witness 2",
group: tessera.NewWitnessGroup(1, wit2),
expectedURLs: []string{"https://b1.example.com/wit2prefix/add-checkpoint"},
expectedURLs: []string{"https://b1.example.com/wit2prefix"},
},
{
desc: "witness 3",
group: tessera.NewWitnessGroup(1, wit3),
expectedURLs: []string{"https://witness.example.com/add-checkpoint"},
expectedURLs: []string{"https://witness.example.com"},
},
{
desc: "all witnesses in one group",
group: tessera.NewWitnessGroup(1, wit1, wit2, wit3),
expectedURLs: []string{
"https://b1.example.com/wit1prefix/add-checkpoint",
"https://b1.example.com/wit2prefix/add-checkpoint",
"https://witness.example.com/add-checkpoint",
"https://b1.example.com/wit1prefix",
"https://b1.example.com/wit2prefix",
"https://witness.example.com",
},
},
{
desc: "all witnesses with duplicates in nests",
group: tessera.NewWitnessGroup(2, tessera.NewWitnessGroup(1, wit1, wit2), tessera.NewWitnessGroup(1, wit1, wit3)),
expectedURLs: []string{
"https://b1.example.com/wit1prefix/add-checkpoint",
"https://b1.example.com/wit2prefix/add-checkpoint",
"https://witness.example.com/add-checkpoint",
"https://b1.example.com/wit1prefix",
"https://b1.example.com/wit2prefix",
"https://witness.example.com",
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
gotURLs := make([]string, 0)
for u := range tC.group.WitnessEndpoints() {
gotURLs = append(gotURLs, u)
gotURLs = append(gotURLs, strings.TrimRight(u, "/"))
}
slices.Sort(gotURLs)
slices.Sort(tC.expectedURLs)
Expand Down
Loading