Skip to content

Commit 71c57e7

Browse files
committed
chore!: Remove deprecated client Init() function
Has been deprecated for a release or so. This fixes some tests and removes others; the removals are all justified: - TestInitRootExpired: testing of InitLocal checks the behavior on expired roots (which is *different*; it allows expired roots) - TestInitRootTooLarge: covered now in TestUpdate We also add a "RawMeta" function, used to get at the root json. I could be persuaded that this should be a "RawRoot" instead. BREAKING CHANGE: remove client.Init() in favor of InitLocal() Signed-off-by: Zachary Newman <[email protected]>
1 parent 2b415d0 commit 71c57e7

File tree

3 files changed

+26
-76
lines changed

3 files changed

+26
-76
lines changed

client/client.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,49 +106,6 @@ func NewClient(local LocalStore, remote RemoteStore) *Client {
106106
}
107107
}
108108

109-
// Init initializes a local repository.
110-
//
111-
// The latest root.json is fetched from remote storage, verified using rootKeys
112-
// and threshold, and then saved in local storage. It is expected that rootKeys
113-
// were securely distributed with the software being updated.
114-
//
115-
// Deprecated: Use c.InitLocal and c.Update to initialize a local repository.
116-
func (c *Client) Init(rootKeys []*data.PublicKey, threshold int) error {
117-
if len(rootKeys) < threshold {
118-
return ErrInsufficientKeys
119-
}
120-
rootJSON, err := c.downloadMetaUnsafe("root.json", defaultRootDownloadLimit)
121-
if err != nil {
122-
return err
123-
}
124-
125-
// create a new key database, and add all the public `rootKeys` to it.
126-
c.db = verify.NewDB()
127-
rootKeyIDs := make([]string, 0, len(rootKeys))
128-
for _, key := range rootKeys {
129-
for _, id := range key.IDs() {
130-
rootKeyIDs = append(rootKeyIDs, id)
131-
if err := c.db.AddKey(id, key); err != nil {
132-
return err
133-
}
134-
}
135-
}
136-
137-
// add a mock "root" role that trusts the passed in key ids. These keys
138-
// will be used to verify the `root.json` we just fetched.
139-
role := &data.Role{Threshold: threshold, KeyIDs: rootKeyIDs}
140-
if err := c.db.AddRole("root", role); err != nil {
141-
return err
142-
}
143-
144-
// verify that the new root is valid.
145-
if err := c.decodeRoot(rootJSON); err != nil {
146-
return err
147-
}
148-
149-
return c.local.SetMeta("root.json", rootJSON)
150-
}
151-
152109
// InitLocal initializes a local repository from root metadata.
153110
//
154111
// The root's keys are extracted from the root and saved in local storage.

client/client_test.go

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,16 @@ func (s *ClientSuite) addRemoteTarget(c *C, name string) {
174174
s.syncRemote(c)
175175
}
176176

177-
func (s *ClientSuite) rootKeys(c *C) []*data.PublicKey {
178-
rootKeys, err := s.repo.RootKeys()
177+
func (s *ClientSuite) rootMeta(c *C) []byte {
178+
meta, err := s.repo.RawMeta("root.json")
179179
c.Assert(err, IsNil)
180-
c.Assert(rootKeys, HasLen, 1)
181-
return rootKeys
180+
return meta
182181
}
183182

184183
func (s *ClientSuite) newClient(c *C) *Client {
185184
s.local = MemoryLocalStore()
186185
client := NewClient(s.local, s.remote)
187-
c.Assert(client.Init(s.rootKeys(c), 1), IsNil)
186+
c.Assert(client.InitLocal(s.rootMeta(c)), IsNil)
188187
return client
189188
}
190189

@@ -243,39 +242,21 @@ func (s *ClientSuite) assertErrExpired(c *C, err error, file string) {
243242
c.Assert(expiredErr.Expired.Unix(), Equals, s.expiredTime.Round(time.Second).Unix())
244243
}
245244

246-
func (s *ClientSuite) TestInitRootTooLarge(c *C) {
247-
client := NewClient(MemoryLocalStore(), s.remote)
248-
s.remote.meta["root.json"] = newFakeFile(make([]byte, defaultRootDownloadLimit+1))
249-
c.Assert(client.Init(s.rootKeys(c), 0), Equals, ErrMetaTooLarge{"root.json", defaultRootDownloadLimit + 1, defaultRootDownloadLimit})
250-
}
251-
252-
func (s *ClientSuite) TestInitRootExpired(c *C) {
253-
s.genKeyExpired(c, "targets")
254-
c.Assert(s.repo.Snapshot(), IsNil)
255-
c.Assert(s.repo.Timestamp(), IsNil)
256-
c.Assert(s.repo.Commit(), IsNil)
257-
s.syncRemote(c)
258-
client := NewClient(MemoryLocalStore(), s.remote)
259-
s.withMetaExpired(func() {
260-
s.assertErrExpired(c, client.Init(s.rootKeys(c), 1), "root.json")
261-
})
262-
}
263-
264245
func (s *ClientSuite) TestInit(c *C) {
265246
client := NewClient(MemoryLocalStore(), s.remote)
266247

267-
// check Init() returns keys.ErrInvalidThreshold with an invalid threshold
268-
c.Assert(client.Init(s.rootKeys(c), 0), Equals, verify.ErrInvalidThreshold)
269-
270-
// check Init() returns signed.ErrRoleThreshold when not enough keys
271-
c.Assert(client.Init(s.rootKeys(c), 2), Equals, ErrInsufficientKeys)
248+
// check invalid json
249+
c.Assert(client.InitLocal(make([]byte, 0)), NotNil)
250+
rootJson := `{ "signatures": [], "signed": {"version": "wrongtype"}, "spec_version": "1.0.0", "version": 1}`
251+
err := client.InitLocal([]byte(rootJson))
252+
c.Assert(err.Error(), Matches, "json: cannot unmarshal string.*")
272253

273254
// check Update() returns ErrNoRootKeys when uninitialized
274-
_, err := client.Update()
255+
_, err = client.Update()
275256
c.Assert(err, Equals, ErrNoRootKeys)
276257

277258
// check Update() does not return ErrNoRootKeys after initialization
278-
c.Assert(client.Init(s.rootKeys(c), 1), IsNil)
259+
c.Assert(client.InitLocal(s.rootMeta(c)), IsNil)
279260
_, err = client.Update()
280261
c.Assert(err, IsNil)
281262
}
@@ -489,6 +470,8 @@ func (s *ClientSuite) TestUpdateRoots(c *C) {
489470
{"testdata/Published2Times_targets_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 2}},
490471
// timestamp role key rotation increase the timestamp.
491472
{"testdata/Published2Times_timestamp_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 1, "targets": 1}},
473+
//root file size > defaultRootDownloadLimit
474+
{"testdata/Published2Times_roottoolarge", ErrMetaTooLarge{Name: "2.root.json", Size: defaultRootDownloadLimit + 1, MaxSize: defaultRootDownloadLimit}, map[string]int64{}},
492475
}
493476

494477
for _, test := range tests {
@@ -1053,10 +1036,11 @@ func (s *ClientSuite) TestUpdateHTTP(c *C) {
10531036
remote, err := HTTPRemoteStore(fmt.Sprintf("http://%s/%s/repository", addr, dir), nil, nil)
10541037
c.Assert(err, IsNil)
10551038
client := NewClient(MemoryLocalStore(), remote)
1056-
rootKeys, err := repo.RootKeys()
1039+
rootMeta, err := repo.SignedMeta("root.json")
1040+
c.Assert(err, IsNil)
1041+
rootJsonBytes, err := json.Marshal(rootMeta)
10571042
c.Assert(err, IsNil)
1058-
c.Assert(rootKeys, HasLen, 1)
1059-
c.Assert(client.Init(rootKeys, 1), IsNil)
1043+
c.Assert(client.InitLocal(rootJsonBytes), IsNil)
10601044

10611045
// check update is ok
10621046
targets, err := client.Update()

repo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,3 +1591,12 @@ func (r *Repo) CheckRoleUnexpired(role string, validAt time.Time) error {
15911591
}
15921592
return nil
15931593
}
1594+
1595+
// RawMeta returns the bytes from the repo metadata dictionary, as-is
1596+
func (r *Repo) RawMeta(roleFilename string) ([]byte, error) {
1597+
meta, ok := r.meta[roleFilename]
1598+
if !ok {
1599+
return nil, ErrMissingMetadata{roleFilename}
1600+
}
1601+
return meta, nil
1602+
}

0 commit comments

Comments
 (0)