Skip to content

Commit

Permalink
fix: fix delegation null json value interoperability (#410)
Browse files Browse the repository at this point in the history
fix: fix delegation null json value

Signed-off-by: Asra Ali <asraa@google.com>

Signed-off-by: Asra Ali <asraa@google.com>
Co-authored-by: Radoslav Dimitrov <dimitrovr@vmware.com>
  • Loading branch information
asraa and rdimitrov committed Oct 6, 2022
1 parent 14853e3 commit 4705874
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
68 changes: 67 additions & 1 deletion client/python_interop/python_interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func (InteropSuite) TestGoClientPythonGenerated(c *C) {
}
}

func generateRepoFS(c *C, dir string, files map[string][]byte, consistentSnapshot bool) *tuf.Repo {
func generateRepoFS(c *C, dir string, files map[string][]byte,
consistentSnapshot bool) *tuf.Repo {
repo, err := tuf.NewRepo(tuf.FileSystemStore(dir, nil))
c.Assert(err, IsNil)
if !consistentSnapshot {
Expand All @@ -107,6 +108,12 @@ func generateRepoFS(c *C, dir string, files map[string][]byte, consistentSnapsho
return repo
}

func refreshRepo(c *C, repo *tuf.Repo) {
c.Assert(repo.Snapshot(), IsNil)
c.Assert(repo.Timestamp(), IsNil)
c.Assert(repo.Commit(), IsNil)
}

func (InteropSuite) TestPythonClientGoGenerated(c *C) {
// clone the Python client if necessary
cwd, err := os.Getwd()
Expand Down Expand Up @@ -161,6 +168,65 @@ func (InteropSuite) TestPythonClientGoGenerated(c *C) {
}
}

// This is a regression test for issue
// https://github.com/theupdateframework/go-tuf/issues/402
func (InteropSuite) TestPythonClientGoGeneratedNullDelegations(c *C) {
// clone the Python client if necessary
cwd, err := os.Getwd()
c.Assert(err, IsNil)

files := map[string][]byte{
"foo.txt": []byte("foo"),
"bar/baz.txt": []byte("baz"),
}

for _, consistentSnapshot := range []bool{false, true} {
// generate repository
tmp := c.MkDir()
// start file server
addr, cleanup := startFileServer(c, tmp)
defer cleanup()
name := fmt.Sprintf("consistent-snapshot-delegations-%t", consistentSnapshot)
dir := filepath.Join(tmp, name)
repo := generateRepoFS(c, dir, files, consistentSnapshot)
// "Reset" top-level targets delegations and re-sign
c.Assert(repo.ResetTargetsDelegations("targets"), IsNil)
refreshRepo(c, repo)

// create initial files for Python client
clientDir := filepath.Join(dir, "client")
currDir := filepath.Join(clientDir, "tufrepo", "metadata", "current")
prevDir := filepath.Join(clientDir, "tufrepo", "metadata", "previous")
c.Assert(os.MkdirAll(currDir, 0755), IsNil)
c.Assert(os.MkdirAll(prevDir, 0755), IsNil)
rootJSON, err := os.ReadFile(filepath.Join(dir, "repository", "1.root.json"))
c.Assert(err, IsNil)
c.Assert(os.WriteFile(filepath.Join(currDir, "root.json"), rootJSON, 0644), IsNil)

args := []string{
filepath.Join(cwd, "testdata", "python-tuf-v1.0.0", "client.py"),
"--repo=http://" + addr + "/" + name,
}
for path := range files {
args = append(args, path)
}

// run Python client update
cmd := exec.Command("python", args...)
cmd.Dir = clientDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
c.Assert(cmd.Run(), IsNil)

// check the target files got downloaded
for path, expected := range files {
actual, err := os.ReadFile(filepath.Join(clientDir, "tuftargets", url.QueryEscape(path)))
c.Assert(err, IsNil)
c.Assert(actual, DeepEquals, expected)
}
}
}

func startFileServer(c *C, dir string) (string, func() error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, IsNil)
Expand Down
3 changes: 1 addition & 2 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,7 @@ func (r *Repo) ResetTargetsDelegationsWithExpires(delegator string, expires time
return fmt.Errorf("error getting delegator (%q) metadata: %w", delegator, err)
}

t.Delegations = &data.Delegations{}
t.Delegations.Keys = make(map[string]*data.PublicKey)
t.Delegations = nil

t.Expires = expires.Round(time.Second)

Expand Down
8 changes: 8 additions & 0 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,14 @@ func (rs *RepoSuite) TestDelegations(c *C) {
t, err := r.targets(delegator)
c.Assert(err, IsNil)

// Check if there are no delegations.
if t.Delegations == nil {
if delegatedRoles != nil {
c.Fatal("expected delegated roles on delegator")
}
return
}

// Check that delegated roles are copied verbatim.
c.Assert(t.Delegations.Roles, DeepEquals, delegatedRoles)

Expand Down

0 comments on commit 4705874

Please sign in to comment.