feat(tvix/store/protos): use Validate() function on root node

This updates the code to make use of the new Validate() function defined
on a Node.

Change-Id: I9b6ed694661f41e700f19cc78d53d2224b61852d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9718
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Florian Klink 2023-10-13 01:16:08 +02:00 committed by clbot
parent 2d2c4322d9
commit 786b0324a9
4 changed files with 22 additions and 37 deletions

View file

@ -108,7 +108,7 @@ in
store-protos-go = pkgs.buildGoModule {
name = "store-golang";
src = depot.third_party.gitignoreSource ./store/protos;
vendorHash = "sha256-qPtEQTd1Vol8vhE10AdwTleTLfYS7xaOir3Ti4MJ+Vc=";
vendorHash = "sha256-WAYaIT3h3Cdvo1RB8T7DuoxeKvXfkq8vo/vdkhJQDs0=";
};
# Build the Rust documentation for publishing on docs.tvix.dev.

View file

@ -3,13 +3,12 @@ module code.tvl.fyi/tvix/store/protos
go 1.19
require (
code.tvl.fyi/tvix/castore/protos v0.0.0-20230922125121-72355662d742
code.tvl.fyi/tvix/castore/protos v0.0.0-20231014122118-3fc2ade7dfb2
github.com/google/go-cmp v0.5.6
github.com/nix-community/go-nix v0.0.0-20231009143713-ebca3299475b
github.com/stretchr/testify v1.8.1
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.31.0
lukechampine.com/blake3 v1.1.7
)
require (
@ -23,4 +22,5 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)

View file

@ -1,6 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
code.tvl.fyi/tvix/castore/protos v0.0.0-20230922125121-72355662d742 h1:x7LsxggggaN3acnCMNDO5LZLAV+A+rZ+R8TXzr+Lgsk=
code.tvl.fyi/tvix/castore/protos v0.0.0-20230922125121-72355662d742/go.mod h1:Ejhyvc0dJUWQMxtJxddfFuAF5N8IKIO94q5CP4czY8Y=
code.tvl.fyi/tvix/castore/protos v0.0.0-20231014122118-3fc2ade7dfb2 h1:Z5GS8OUe7L/hKDbb1amArY7QgX0DSD5xaBwWxmh4H3Y=
code.tvl.fyi/tvix/castore/protos v0.0.0-20231014122118-3fc2ade7dfb2/go.mod h1:hj0y8RPthqn1QPj8u2jFe2vzH7NouUoclrwo1/CSbuc=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=

View file

@ -57,55 +57,40 @@ func (p *PathInfo) Validate() (*storepath.StorePath, error) {
return nil, fmt.Errorf("root node must be set")
}
// for all three node types, ensure the name properly parses to a store path,
// and in case it refers to a digest, ensure it has the right length.
if err := rootNode.Validate(); err != nil {
return nil, fmt.Errorf("root node failed validation: %w", err)
}
var storePath *storepath.StorePath
var err error
// for all three node types, ensure the name properly parses to a store path.
// This is a stricter check as the ones already performed in the rootNode.Validate() call.
var rootNodeName []byte
if node := rootNode.GetDirectory(); node != nil {
if len(node.Digest) != 32 {
return nil, fmt.Errorf("invalid digest size for %s, expected %d, got %d", node.Name, 32, len(node.Digest))
}
storePath, err = storepath.FromString(string(node.GetName()))
if err != nil {
return nil, fmt.Errorf("unable to parse %s as StorePath: %w", node.Name, err)
}
rootNodeName = node.GetName()
} else if node := rootNode.GetFile(); node != nil {
if len(node.Digest) != 32 {
return nil, fmt.Errorf("invalid digest size for %s, expected %d, got %d", node.Name, 32, len(node.Digest))
}
storePath, err = storepath.FromString(string(node.GetName()))
if err != nil {
return nil, fmt.Errorf("unable to parse %s as StorePath: %w", node.Name, err)
}
rootNodeName = node.GetName()
} else if node := rootNode.GetSymlink(); node != nil {
storePath, err = storepath.FromString(string(node.GetName()))
if err != nil {
return nil, fmt.Errorf("unable to parse %s as StorePath: %w", node.Name, err)
}
rootNodeName = node.GetName()
} else {
// this would only happen if we introduced a new type
// already caught by rootNode.Validate()
panic("unreachable")
}
storePath, err := storepath.FromString(string(rootNodeName))
if err != nil {
return nil, fmt.Errorf("unable to parse root node name %s as StorePath: %w", rootNodeName, err)
}
// If the Deriver field is populated, ensure it parses to a StorePath.
// We can't check for it to *not* end with .drv, as the .drv files produced by
// recursive Nix end with multiple .drv suffixes, and only one is popped when
// converting to this field.
if p.Deriver != nil {
storePath := storepath.StorePath{
deriverStorePath := storepath.StorePath{
Name: string(p.Deriver.GetName()),
Digest: p.Deriver.GetDigest(),
}
if err := storePath.Validate(); err != nil {
if err := deriverStorePath.Validate(); err != nil {
return nil, fmt.Errorf("invalid deriver field: %w", err)
}
}