Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(localMeta): Ignore deleted delegated targets #522

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
16 changes: 16 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"

Expand Down Expand Up @@ -493,10 +494,25 @@ func (c *Client) getDelegationPathFromRaw(snapshot *data.Snapshot, delegatedTarg
return nil, err
}
for targetPath := range targets.Targets {
// Gets target file from remote store
_, resp, err := c.getTargetFileMetaDelegationPath(targetPath, snapshot)
// We only need to test one targets file:
// - If it is valid, it means the delegated targets has been validated
// - If it is not, the delegated targets isn't valid
if errors.As(err, &ErrMissingRemoteMetadata{}) {
// As this function is used to fill the local store cache, the targets
// will be downloaded from the remote store as the local store cache is
// empty, meaning that the delegated targets may not exist anymore. In
// that case, we can't get the delegation path. Ignore the delegated targets.
return nil, nil
}
if errors.As(err, &ErrUnknownTarget{}) {
// As this function is used to fill the local store cache, the target file
// will be downloaded from the remote store as the local store cache is
// empty, meaning that the target file may not exist anymore. In
// that case, ignore the file and try another.
continue
}
return resp, err
}
return nil, nil
Expand Down
26 changes: 25 additions & 1 deletion client/delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ func TestPersistedMeta(t *testing.T) {
}
}

func TestGetDelegationPathWithNoTargetFile(t *testing.T) {
// In this test, we have created a target file c.txt for a delegation
// c.json, then we remove that target file and check if c.json is loaded
// in the localMeta. It shouldn't as it has no target file at all and shouldn't
// be used.
verify.IsExpired = func(t time.Time) bool { return false }
client, closer := initTestDelegationClient(t, "testdata/php-tuf-fixtures/TUFTestFixture2LevelDelegation")
defer closer()
_, err := client.Update()
assert.Nil(t, err)

err = client.getLocalMeta()
assert.Nil(t, err)

_, ok := client.localMeta["a.json"]
assert.True(t, ok)

_, ok = client.localMeta["b.json"]
assert.True(t, ok)

_, ok = client.localMeta["c.json"]
assert.False(t, ok)
}

func versionOfStoredTargets(name string, store map[string]json.RawMessage) (int64, error) {
rawTargets, ok := store[name]
if !ok {
Expand Down Expand Up @@ -296,7 +320,7 @@ func initTestDelegationClient(t *testing.T, dirPrefix string) (*Client, func() e
}
name := f.Name()
// ignoring consistent snapshot when loading initial state
if len(strings.Split(name, ".")) == 1 && strings.HasSuffix(name, ".json") {
if len(strings.Split(name, ".")) < 3 && strings.HasSuffix(name, ".json") {
rawFile, err := os.ReadFile(initialStateDir + "/" + name)
assert.Nil(t, err)
assert.Nil(t, c.local.SetMeta(name, rawFile))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"signatures": [
{
"keyid": "05e17c1501d627b2597322f80d33aacec6f30a507552d3326a88913422b0e30b",
"sig": "72b215c194090407abc8b7c513d69fd340abbe63037d26cbc2e4dd88c095a14da8d6e4aae0561f265e97d5c658b0260be2031af73c26e157e5f062c5f4aceb00"
}
],
"signed": {
"_type": "targets",
"delegations": {
"keys": {
"718fedad390b4d0d470b890781eb8c94e5a7e975aebe65fc0862246c945fce68": {
"keyid_hash_algorithms": [
"sha256",
"sha512"
],
"keytype": "ed25519",
"keyval": {
"public": "82f52e4503dbb364fabe8e5567f1cf909d4175d45468a021dfe75653db9ac98c"
},
"scheme": "ed25519"
},
"9ca81f7ff17f6218246474a51b47eb035741bc472557ef5ac493e279f446b85b": {
"keyid_hash_algorithms": [
"sha256",
"sha512"
],
"keytype": "ed25519",
"keyval": {
"public": "06e4dee0de7826c8d539a6112940b7459892b4ecaf696e67dc064aea0923f95c"
},
"scheme": "ed25519"
}
},
"roles": [
{
"keyids": [
"718fedad390b4d0d470b890781eb8c94e5a7e975aebe65fc0862246c945fce68"
],
"name": "b",
"paths": [
"*.txt"
],
"terminating": false,
"threshold": 1
},
{
"keyids": [
"9ca81f7ff17f6218246474a51b47eb035741bc472557ef5ac493e279f446b85b"
],
"name": "c",
"paths": [
"*.txt"
],
"terminating": false,
"threshold": 1
}
]
},
"expires": "2020-04-01T07:27:10Z",
"spec_version": "1.0.0",
"targets": {
"a.txt": {
"custom": {},
"hashes": {
"sha256": "3f90cedf303207851bbdc5f857e018daf93b4c0083306cef17df547b42e4e985",
"sha512": "f4631ef7ea7b015d7b88e411842fafeb78a72f0181bec72ea9754604ede74ea0e491bf8411659aabc96304fc764d0131ce49ba86066ab5f7b7480dde719e0bfd"
},
"length": 15
}
},
"version": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"signatures": [
{
"keyid": "718fedad390b4d0d470b890781eb8c94e5a7e975aebe65fc0862246c945fce68",
"sig": "6a4f4bb63d7b6db4b6c09ce081fd229052ca087c98d25653c8d187de04dbedfc219aa9482774b939d84eb404fd51cfe383f9ea65e19a1d3cc79b642701bcff0d"
}
],
"signed": {
"_type": "targets",
"delegations": {
"keys": {},
"roles": []
},
"expires": "2020-04-01T07:27:10Z",
"spec_version": "1.0.0",
"targets": {
"b.txt": {
"custom": {},
"hashes": {
"sha256": "949c6a4318dabe8bbd140cef99ea669ba031919ccf9bce0f5b4d0b61d1c0aa2e",
"sha512": "f4cc9ce5c73b37e2a6707af7a0ea614ea5fa428bd2509e3af4528a5d330ce98a09c4dd98c859ad9b27b8aba24e1eacbf1af8393fdbfed899cecb995c87a11e3c"
},
"length": 15
}
},
"version": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"signatures": [
{
"keyid": "9ca81f7ff17f6218246474a51b47eb035741bc472557ef5ac493e279f446b85b",
"sig": "9f0c5ef7e0a11012256c4a47ed757f9e90e930238f6a6e5c758a014f1b768519acb2b7d9aa9bd0456d71ba09b032598086e42d832830391e307c926c16f5b303"
}
],
"signed": {
"_type": "targets",
"delegations": {
"keys": {},
"roles": []
},
"expires": "2020-04-01T07:27:10Z",
"spec_version": "1.0.0",
"targets": {
"c.txt": {
"custom": {},
"hashes": {
"sha256": "946e2ea9180de673891ae09ce0edfb207966bc32bd6324ebdad2c50c82075ffd",
"sha512": "ddb5fb256a368d778b5fdd03d497ad79944c766245f3cccfc8b098b14c488ec424a68b86a6a2add36db4ef0f0214f15dbe0d63fbc5ca7a9619fb4c39544d78a9"
},
"length": 15
}
},
"version": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"signatures": [
{
"keyid": "d4dab4b4d68b91665a6d0dac5b4e64677aa6d853fc787669168b4b4ba9822129",
"sig": "d0bf76a5cfc0aee1b8a1b1bf0ed8ca646a1a6d5f205945c515e8546bfd3c1e6b5e07cc0b93836bd030dd05ba68f177aecb05f6bf90c6702fd178e53310022506"
}
],
"signed": {
"_type": "root",
"consistent_snapshot": true,
"expires": "2020-12-31T05:48:20Z",
"keys": {
"3a05831328273e4b821c3bbe1fed0c5332749d8e071675879af26a401a5c85ae": {
"keyid_hash_algorithms": [
"sha256",
"sha512"
],
"keytype": "ed25519",
"keyval": {
"public": "6bac59b8d9e1aae02fae6fba6e7fe3fc9fe5b4a9fe98c3fca255d8c8ec3e5b35"
},
"scheme": "ed25519"
},
"77dfdca206c0fe1b8e55d67d21dd0e195a0998a9d2b56c6d3ee8f68d04c21e93": {
"keyid_hash_algorithms": [
"sha256",
"sha512"
],
"keytype": "ed25519",
"keyval": {
"public": "6400d770c7c1bce4b3d59ce0079ed686e843b6500bbea77d869a1ae7df4565a1"
},
"scheme": "ed25519"
},
"d4dab4b4d68b91665a6d0dac5b4e64677aa6d853fc787669168b4b4ba9822129": {
"keyid_hash_algorithms": [
"sha256",
"sha512"
],
"keytype": "ed25519",
"keyval": {
"public": "28bf74baa87ed923f8fa27e3292684f8ec4730ce0bdc65150ed58199206ce089"
},
"scheme": "ed25519"
},
"e4dae3872d28d29f7624a702bfd25f68453544d597229ee9e0a8569d1f940cf4": {
"keyid_hash_algorithms": [
"sha256",
"sha512"
],
"keytype": "ed25519",
"keyval": {
"public": "e6ae9d3b67d7b3ce274130291dd90287f32b8fd72bfb4ac5430859ebd1c28a46"
},
"scheme": "ed25519"
}
},
"roles": {
"root": {
"keyids": [
"d4dab4b4d68b91665a6d0dac5b4e64677aa6d853fc787669168b4b4ba9822129"
],
"threshold": 1
},
"snapshot": {
"keyids": [
"77dfdca206c0fe1b8e55d67d21dd0e195a0998a9d2b56c6d3ee8f68d04c21e93"
],
"threshold": 1
},
"targets": {
"keyids": [
"e4dae3872d28d29f7624a702bfd25f68453544d597229ee9e0a8569d1f940cf4"
],
"threshold": 1
},
"timestamp": {
"keyids": [
"3a05831328273e4b821c3bbe1fed0c5332749d8e071675879af26a401a5c85ae"
],
"threshold": 1
}
},
"spec_version": "1.0.0",
"version": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"signatures": [
{
"keyid": "77dfdca206c0fe1b8e55d67d21dd0e195a0998a9d2b56c6d3ee8f68d04c21e93",
"sig": "61db8765350398f7f750853337d9a55c5d6e790812d29146b5b45d5fd43d2a42c474a7a9fab263c3a50a28114a82f79dbf24ff1f99ae737a8d06f332f9f7d103"
}
],
"signed": {
"_type": "snapshot",
"expires": "2020-01-08T00:00:00Z",
"meta": {
"targets.json": {
"version": 1
}
},
"spec_version": "1.0.0",
"version": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"signatures": [
{
"keyid": "e4dae3872d28d29f7624a702bfd25f68453544d597229ee9e0a8569d1f940cf4",
"sig": "c150e8ed5d352f366a979f4c4b9d556350c414c2da7ef1279045aaed3438c60872142d0dfe5ddbb627fec2d8fb7c5d8e692e04a87230b78d74714c5db035620a"
}
],
"signed": {
"_type": "targets",
"delegations": {
"keys": {},
"roles": []
},
"expires": "2020-04-01T07:27:10Z",
"spec_version": "1.0.0",
"targets": {},
"version": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"signatures": [
{
"keyid": "3a05831328273e4b821c3bbe1fed0c5332749d8e071675879af26a401a5c85ae",
"sig": "1d668531c7a0960cf90825faa684106a8aef0799c1b47e72301bac45d87f2dd42c14f1a3ac7db862323ca5177dd4fd686573ea92aea99638f17414dde561c00b"
}
],
"signed": {
"_type": "timestamp",
"expires": "2020-01-02T00:00:00Z",
"meta": {
"snapshot.json": {
"hashes": {
"sha256": "f4ca389c2c9fbc592d91d4e693c31113b8803a11bcb5ecd973581fa0e3d34ce0",
"sha512": "92a0989e44c0e9f16d3e56268a3b8dd4e4416ee2ac91a4c871a405f1e426062651ec4effa0078fc4409c8b0422ccad9b1aa197db58f178406f398562b2e98195"
},
"length": 431,
"version": 1
}
},
"spec_version": "1.0.0",
"version": 1
}
}
Loading
Loading