Skip to content

Commit

Permalink
refactor: upgrade with latest boxo
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed May 10, 2023
1 parent aef104e commit ba5a83b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 126 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ipfs/gateway-conformance
go 1.20

require (
github.com/ipfs/boxo v0.8.1
github.com/ipfs/boxo v0.8.2-0.20230510114019-33e3f0cd052b
github.com/ipfs/go-cid v0.4.1
github.com/ipld/go-ipld-prime v0.20.0
github.com/libp2p/go-libp2p v0.26.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/huin/goupnp v1.0.3 h1:N8No57ls+MnjlB+JPiCVSOyy/ot7MJTqlo7rn+NYSqQ=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.8.1 h1:3DkKBCK+3rdEB5t77WDShUXXhktYwH99mkAsgajsKrU=
github.com/ipfs/boxo v0.8.1/go.mod h1:xJ2hVb4La5WyD7GvKYE0lq2g1rmQZoCD2K4WNrV6aZI=
github.com/ipfs/boxo v0.8.2-0.20230510114019-33e3f0cd052b h1:6EVpfwbBgwhfZOA19i55jOGokKOy+OaQAm1dg4RbXmc=
github.com/ipfs/boxo v0.8.2-0.20230510114019-33e3f0cd052b/go.mod h1:Ej2r08Z4VIaFKqY08UXMNhwcLf6VekHhK8c+KqA1B9Y=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down
14 changes: 7 additions & 7 deletions tests/t0124_gateway_ipns_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"testing"

"github.com/ipfs/gateway-conformance/tooling/car"
. "github.com/ipfs/gateway-conformance/tooling/check"
"github.com/ipfs/gateway-conformance/tooling/ipns"
"github.com/ipfs/gateway-conformance/tooling/test"
. "github.com/ipfs/gateway-conformance/tooling/ipns"
"github.com/ipfs/gateway-conformance/tooling/specs"
. "github.com/ipfs/gateway-conformance/tooling/test"
)

Expand All @@ -19,7 +18,7 @@ func TestGatewayIPNSRecord(t *testing.T) {
// ipfs dag import ../t0124-gateway-ipns-record/fixtures.car &&
// ipfs routing put /ipns/${IPNS_KEY} ../t0124-gateway-ipns-record/${IPNS_KEY}.ipns-record
// '
ipns := ipns.MustOpenIPNSRecordWithKey("t0124/k51qzi5uqu5dh71qgwangrt6r0nd4094i88nsady6qgd1dhjcyfsaqmpp143ab.ipns-record")
ipns := MustOpenIPNSRecordWithKey("t0124/k51qzi5uqu5dh71qgwangrt6r0nd4094i88nsady6qgd1dhjcyfsaqmpp143ab.ipns-record")
ipnsKey := ipns.Key()

tests := SugarTests{
Expand Down Expand Up @@ -57,7 +56,7 @@ func TestGatewayIPNSRecord(t *testing.T) {
Header("Cache-Control").Contains("public, max-age=3155760000"),
).
Body(
IsIPNSKey().
IsIPNSKey(ipnsKey).
IsValid().
PointsTo("/ipfs/%s", fileCID.String()),
),
Expand Down Expand Up @@ -85,7 +84,7 @@ func TestGatewayIPNSRecord(t *testing.T) {
Header("Cache-Control").Contains("public, max-age=3155760000"),
).
Body(
IsIPNSKey().
IsIPNSKey(ipnsKey).
IsValid().
PointsTo("/ipfs/%s", fileCID.String()),
),
Expand All @@ -109,5 +108,6 @@ func TestGatewayIPNSRecord(t *testing.T) {
},
}

test.Run(t, tests)
RunIfSpecsAreEnabled(t, tests,
specs.IPNSResolver)
}
Binary file not shown.
25 changes: 14 additions & 11 deletions tooling/check/ipns.go → tooling/ipns/check.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package check
package ipns

import (
"fmt"

"github.com/ipfs/gateway-conformance/tooling/ipns"
"github.com/ipfs/gateway-conformance/tooling/check"
)

var _ Check[[]byte] = &CheckIsIPNSKey{}
var _ check.Check[[]byte] = &CheckIsIPNSKey{}

type CheckIsIPNSKey struct {
shouldBeValid bool
expectedValue string
pubKey string
}

func IsIPNSKey() *CheckIsIPNSKey {
func IsIPNSKey(keyId string) *CheckIsIPNSKey {
return &CheckIsIPNSKey{
shouldBeValid: true,
pubKey: keyId,
expectedValue: "",
}
}

Expand All @@ -29,14 +32,14 @@ func (c *CheckIsIPNSKey) PointsTo(value string, rest ...any) *CheckIsIPNSKey {
return c
}

func (c *CheckIsIPNSKey) Check(ipnsKey []byte) CheckOutput {
record, err := ipns.UnmarshalIpnsRecord(ipnsKey)
func (c *CheckIsIPNSKey) Check(recordPayload []byte) check.CheckOutput {
record, err := UnmarshalIpnsRecord(recordPayload, c.pubKey)

if err != nil {
if c.shouldBeValid {
return CheckOutput{
return check.CheckOutput{
Success: false,
Reason: fmt.Sprintf("IPNS key '%s' is not valid: %v", ipnsKey, err),
Reason: fmt.Sprintf("IPNS key '%s' is not valid: %v", recordPayload, err),
}
} else {
panic("not implemented")
Expand All @@ -45,14 +48,14 @@ func (c *CheckIsIPNSKey) Check(ipnsKey []byte) CheckOutput {

if c.expectedValue != "" {
if record.Value() != c.expectedValue {
return CheckOutput{
return check.CheckOutput{
Success: false,
Reason: fmt.Sprintf("IPNS key '%s' points to '%s', but expected value is '%s'", ipnsKey, record.Value(), c.expectedValue),
Reason: fmt.Sprintf("IPNS key '%s' points to '%s', but expected value is '%s'", recordPayload, record.Value(), c.expectedValue),
}
}
}

return CheckOutput{
return check.CheckOutput{
Success: true,
}
}
6 changes: 3 additions & 3 deletions tooling/check/ipns_test.go → tooling/ipns/check_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package check
package ipns

import (
"os"
Expand All @@ -8,15 +8,15 @@ import (
)

func TestIpnsCanOpenARecord(t *testing.T) {
path := "../../fixtures/t0124/k51qzi5uqu5dhjjqhpcuvdnskq4mz84a2xg1rpqzi6s5460q2612egkfjsk42x.ipns-record"
path := "./_fixtures/k51qzi5uqu5dgh7y9l90nqs6tvnzcm9erbt8fhzg3fu79p5qt9zb2izvfu51ki.ipns-record"

// read file:
data, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}

check := IsIPNSKey().IsValid()
check := IsIPNSKey("k51qzi5uqu5dgh7y9l90nqs6tvnzcm9erbt8fhzg3fu79p5qt9zb2izvfu51ki").IsValid()
// ipfs name inspect --verify $IPNS_KEY < curl_output_filename > verify_output &&
output := check.Check(data)

Expand Down
22 changes: 6 additions & 16 deletions tooling/ipns/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,24 @@ func extractPubkeyFromPath(path string) (string, error) {
return matches[1], nil
}


func OpenIPNSRecord(absPath string) (*IpnsRecord, error) {
data, err := os.ReadFile(absPath)
func OpenIPNSRecordWithKey(absPath string) (*IpnsRecord, error) {
// name is [pubkey](_anything)?.ipns-record
pubkey, err := extractPubkeyFromPath(absPath)
if err != nil {
return nil, err
}

r, err := UnmarshalIpnsRecord(data)
data, err := os.ReadFile(absPath)
if err != nil {
return nil, err
}

return r, nil
}

func OpenIPNSRecordWithKey(absPath string) (*IpnsRecord, error) {
// name is [pubkey](_anything)?.ipns-record
pubkey, err := extractPubkeyFromPath(absPath)
r, err := UnmarshalIpnsRecord(data, pubkey)
if err != nil {
return nil, err
}

r, err := OpenIPNSRecord(absPath)
if err != nil {
return nil, err
}

return r.WithKey(pubkey), nil
return r, nil
}

func MustOpenIPNSRecordWithKey(file string) *IpnsRecord {
Expand Down
24 changes: 11 additions & 13 deletions tooling/ipns/ipns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,31 @@ func TestExtractPath(t *testing.T) {
}

func TestLoadIPNSRecord(t *testing.T) {
path := "../../fixtures/t0124/k51qzi5uqu5dhjjqhpcuvdnskq4mz84a2xg1rpqzi6s5460q2612egkfjsk42x.ipns-record"
ipns, err := OpenIPNSRecord(path)
path := "../../fixtures/t0124/k51qzi5uqu5dh71qgwangrt6r0nd4094i88nsady6qgd1dhjcyfsaqmpp143ab.ipns-record"
ipns, err := OpenIPNSRecordWithKey(path)

assert.Nil(t, err)
assert.Equal(t, "k51qzi5uqu5dhjjqhpcuvdnskq4mz84a2xg1rpqzi6s5460q2612egkfjsk42x", ipns.Key())
assert.Equal(t, "k51qzi5uqu5dh71qgwangrt6r0nd4094i88nsady6qgd1dhjcyfsaqmpp143ab", ipns.Key())
assert.Equal(t, ipns.Value(), "/ipfs/bafkreidfdrlkeq4m4xnxuyx6iae76fdm4wgl5d4xzsb77ixhyqwumhz244")
assert.True(t, ipns.Entry.Validity.After(YEAR_100))
assert.True(t, ipns.Validity().After(YEAR_100))

verify, err := ipns.Verify()
assert.Nil(t, err)
assert.True(t, verify)
err = ipns.Valid()
assert.NoError(t, err)
}

func TestLoadTestRecord(t *testing.T) {
// Test record created with (100 years):
// ipfs name publish --allow-offline -t 876000h --key=self "/ipfs/$( echo "helloworld" | ipfs add --inline -q )"
// ipfs routing get /ipns/${K} > ${K}.ipns-record

path := "../../k51qzi5uqu5dgh7y9l90nqs6tvnzcm9erbt8fhzg3fu79p5qt9zb2izvfu51ki.ipns-record"
ipns, err := OpenIPNSRecord(path)
path := "./_fixtures/k51qzi5uqu5dgh7y9l90nqs6tvnzcm9erbt8fhzg3fu79p5qt9zb2izvfu51ki.ipns-record"
ipns, err := OpenIPNSRecordWithKey(path)

assert.Nil(t, err)
assert.Equal(t, "k51qzi5uqu5dgh7y9l90nqs6tvnzcm9erbt8fhzg3fu79p5qt9zb2izvfu51ki", ipns.Key())
assert.Equal(t, ipns.Value(), "/ipfs/bafyaaeykceeaeeqlnbswy3dpo5xxe3debimaw")
assert.True(t, ipns.Entry.Validity.After(YEAR_100))
assert.True(t, ipns.Validity().After(YEAR_100))

verify, err := ipns.Verify()
assert.Nil(t, err)
assert.True(t, verify)
err = ipns.Valid()
assert.NoError(t, err)
}
65 changes: 8 additions & 57 deletions tooling/ipns/kubo_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package ipns
import (
"bytes"
"encoding/json"
"fmt"
"time"

"github.com/gogo/protobuf/proto"
Expand All @@ -16,23 +15,22 @@ import (
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/dagjson"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
mbase "github.com/multiformats/go-multibase"
)

// IpnsInspectEntry contains the deserialized values from an IPNS Entry:
// https://github.com/ipfs/specs/blob/main/ipns/IPNS.md#record-serialization-format
type IpnsInspectEntry struct {
Value string `json:"value"`
Value string `json:"value"`
ValidityType *ipns_pb.IpnsEntry_ValidityType `json:"validityType"`
Validity *time.Time `json:"validity"`
Sequence uint64 `json:"sequence"`
TTL *uint64 `json:"ttl"`
PublicKey string `json:"publicKey"`
SignatureV1 string `json:"signatureV1"`
SignatureV2 string `json:"signatureV2"`
Data interface{} `json:"data"`
Validity *time.Time `json:"validity"`
Sequence uint64 `json:"sequence"`
TTL *uint64 `json:"ttl"`
PublicKey string `json:"publicKey"`
SignatureV1 string `json:"signatureV1"`
SignatureV2 string `json:"signatureV2"`
Data interface{} `json:"data"`
}

func unmarshalIPNSEntry(data []byte) (*ipns_pb.IpnsEntry, error) {
Expand Down Expand Up @@ -90,7 +88,6 @@ func unmarshalIPNSRecord(entry *ipns_pb.IpnsEntry) (*IpnsInspectEntry, error) {
result.Validity = &validity
}


return &result, nil
}

Expand All @@ -99,49 +96,3 @@ type IpnsInspectValidation struct {
Reason string
PublicKey peer.ID
}

func verify(key string, entry *ipns_pb.IpnsEntry) (*IpnsInspectValidation, error) {
id, err := peer.Decode(key)
if err != nil {
return nil, err
}

validation := &IpnsInspectValidation{
PublicKey: id,
}

pub, err := id.ExtractPublicKey()
if err != nil {
// Make sure it works with all those RSA that cannot be embedded into the
// Peer ID.
if len(entry.PubKey) > 0 {
pub, err = ic.UnmarshalPublicKey(entry.PubKey)
if err != nil {
return nil, err
}

// Verify the public key matches the name we are verifying.
entryID, err := peer.IDFromPublicKey(pub)

if err != nil {
return nil, err
}

if id != entryID {
return nil, fmt.Errorf("record public key does not match the verified name")
}
}
}
if err != nil {
return nil, err
}

err = ipns.Validate(pub, entry)
if err == nil {
validation.Valid = true
} else {
validation.Reason = err.Error()
}

return validation, nil
}
Loading

0 comments on commit ba5a83b

Please sign in to comment.