Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 5629a55

Browse files
author
Ali Yousuf
authored
Merge branch 'master' into feat/pkcs8
2 parents 5c4e43b + 23eceed commit 5629a55

24 files changed

Lines changed: 1669 additions & 523 deletions

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Contributing to notary
22

3-
## Before reporting an issue...
3+
## Before reporting an issue...
44

55
### If your problem is with...
66

@@ -26,7 +26,7 @@ By following these simple rules you will get better and faster feedback on your
2626

2727
- search the bugtracker for an already reported issue
2828

29-
### If you found an issue that describes your problem:
29+
### If you found an issue that describes your problem:
3030

3131
- please read other user comments first, and confirm this is the same issue: a given error condition might be indicative of different problems - you may also find a workaround in the comments
3232
- please refrain from adding "same thing here" or "+1" comments

client/client.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/Sirupsen/logrus"
16+
canonicaljson "github.com/docker/go/canonical/json"
1617
"github.com/docker/notary"
1718
"github.com/docker/notary/client/changelist"
1819
"github.com/docker/notary/cryptoservice"
@@ -128,9 +129,10 @@ func (r *NotaryRepository) GetGUN() data.GUN {
128129
// Target represents a simplified version of the data TUF operates on, so external
129130
// applications don't have to depend on TUF data types.
130131
type Target struct {
131-
Name string // the name of the target
132-
Hashes data.Hashes // the hash of the target
133-
Length int64 // the size in bytes of the target
132+
Name string // the name of the target
133+
Hashes data.Hashes // the hash of the target
134+
Length int64 // the size in bytes of the target
135+
Custom *canonicaljson.RawMessage // the custom data provided to describe the file at TARGETPATH
134136
}
135137

136138
// TargetWithRole represents a Target that exists in a particular role - this is
@@ -141,7 +143,7 @@ type TargetWithRole struct {
141143
}
142144

143145
// NewTarget is a helper method that returns a Target
144-
func NewTarget(targetName string, targetPath string) (*Target, error) {
146+
func NewTarget(targetName, targetPath string, targetCustom *canonicaljson.RawMessage) (*Target, error) {
145147
b, err := ioutil.ReadFile(targetPath)
146148
if err != nil {
147149
return nil, err
@@ -152,7 +154,7 @@ func NewTarget(targetName string, targetPath string) (*Target, error) {
152154
return nil, err
153155
}
154156

155-
return &Target{Name: targetName, Hashes: meta.Hashes, Length: meta.Length}, nil
157+
return &Target{Name: targetName, Hashes: meta.Hashes, Length: meta.Length, Custom: targetCustom}, nil
156158
}
157159

158160
func rootCertKey(gun data.GUN, privKey data.PrivateKey) (data.PublicKey, error) {
@@ -360,7 +362,7 @@ func (r *NotaryRepository) AddTarget(target *Target, roles ...data.RoleName) err
360362
}
361363
logrus.Debugf("Adding target \"%s\" with sha256 \"%x\" and size %d bytes.\n", target.Name, target.Hashes["sha256"], target.Length)
362364

363-
meta := data.FileMeta{Length: target.Length, Hashes: target.Hashes}
365+
meta := data.FileMeta{Length: target.Length, Hashes: target.Hashes, Custom: target.Custom}
364366
metaJSON, err := json.Marshal(meta)
365367
if err != nil {
366368
return err
@@ -417,6 +419,7 @@ func (r *NotaryRepository) ListTargets(roles ...data.RoleName) ([]*TargetWithRol
417419
Name: targetName,
418420
Hashes: targetMeta.Hashes,
419421
Length: targetMeta.Length,
422+
Custom: targetMeta.Custom,
420423
},
421424
Role: validRole.Name,
422425
}
@@ -472,7 +475,7 @@ func (r *NotaryRepository) GetTargetByName(name string, roles ...data.RoleName)
472475
}
473476
// Check that we didn't error, and that we assigned to our target
474477
if err := r.tufRepo.WalkTargets(name, role, getTargetVisitorFunc, skipRoles...); err == nil && foundTarget {
475-
return &TargetWithRole{Target: Target{Name: name, Hashes: resultMeta.Hashes, Length: resultMeta.Length}, Role: resultRoleName}, nil
478+
return &TargetWithRole{Target: Target{Name: name, Hashes: resultMeta.Hashes, Length: resultMeta.Length, Custom: resultMeta.Custom}, Role: resultRoleName}, nil
476479
}
477480
}
478481
return nil, fmt.Errorf("No trust data for %s", name)
@@ -516,7 +519,7 @@ func (r *NotaryRepository) GetAllTargetMetadataByName(name string) ([]TargetSign
516519
for targetName, resultMeta := range targetMetaToAdd {
517520
targetInfo := TargetSignedStruct{
518521
Role: validRole,
519-
Target: Target{Name: targetName, Hashes: resultMeta.Hashes, Length: resultMeta.Length},
522+
Target: Target{Name: targetName, Hashes: resultMeta.Hashes, Length: resultMeta.Length, Custom: resultMeta.Custom},
520523
Signatures: tgt.Signatures,
521524
}
522525
targetInfoList = append(targetInfoList, targetInfo)

client/client_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,13 @@ func testInitRepoPasswordInvalid(t *testing.T, rootType string) {
631631

632632
func addTarget(t *testing.T, repo *NotaryRepository, targetName, targetFile string,
633633
roles ...data.RoleName) *Target {
634-
target, err := NewTarget(targetName, targetFile)
634+
var targetCustom *json.RawMessage
635+
return addTargetWithCustom(t, repo, targetName, targetFile, targetCustom, roles...)
636+
}
637+
638+
func addTargetWithCustom(t *testing.T, repo *NotaryRepository, targetName,
639+
targetFile string, targetCustom *json.RawMessage, roles ...data.RoleName) *Target {
640+
target, err := NewTarget(targetName, targetFile, targetCustom)
635641
require.NoError(t, err, "error creating target")
636642
err = repo.AddTarget(target, roles...)
637643
require.NoError(t, err, "error adding target")
@@ -815,7 +821,8 @@ func testAddTargetToSpecifiedInvalidRoles(t *testing.T, clearCache bool) {
815821
}
816822

817823
for _, invalidRole := range invalidRoles {
818-
target, err := NewTarget("latest", "../fixtures/intermediate-ca.crt")
824+
var targetCustom *json.RawMessage
825+
target, err := NewTarget("latest", "../fixtures/intermediate-ca.crt", targetCustom)
819826
require.NoError(t, err, "error creating target")
820827

821828
err = repo.AddTarget(target, data.CanonicalTargetsRole, invalidRole)
@@ -877,7 +884,8 @@ func TestAddTargetWithInvalidTarget(t *testing.T) {
877884
repo, _ := initializeRepo(t, data.ECDSAKey, "docker.com/notary", ts.URL, false)
878885
defer os.RemoveAll(repo.baseDir)
879886

880-
target, err := NewTarget("latest", "../fixtures/intermediate-ca.crt")
887+
var targetCustom *json.RawMessage
888+
target, err := NewTarget("latest", "../fixtures/intermediate-ca.crt", targetCustom)
881889
require.NoError(t, err, "error creating target")
882890

883891
// Clear the hashes
@@ -889,7 +897,8 @@ func TestAddTargetWithInvalidTarget(t *testing.T) {
889897
// to be propagated.
890898
func TestAddTargetErrorWritingChanges(t *testing.T) {
891899
testErrorWritingChangefiles(t, func(repo *NotaryRepository) error {
892-
target, err := NewTarget("latest", "../fixtures/intermediate-ca.crt")
900+
var targetCustom *json.RawMessage
901+
target, err := NewTarget("latest", "../fixtures/intermediate-ca.crt", targetCustom)
893902
require.NoError(t, err, "error creating target")
894903
return repo.AddTarget(target, data.CanonicalTargetsRole)
895904
})
@@ -1192,8 +1201,14 @@ func testListTarget(t *testing.T, rootType string) {
11921201
// tests need to manually bootstrap timestamp as client doesn't generate it
11931202
err := repo.tufRepo.InitTimestamp()
11941203
require.NoError(t, err, "error creating repository: %s", err)
1204+
var targetCustom json.RawMessage
1205+
rawTargetCustom := []byte("\"Lorem ipsum dolor sit\"")
1206+
err = json.Unmarshal(rawTargetCustom, &targetCustom)
1207+
require.NoError(t, err)
1208+
1209+
latestTarget := addTargetWithCustom(t, repo, "latest", "../fixtures/intermediate-ca.crt", &targetCustom)
1210+
require.Equal(t, targetCustom, *latestTarget.Custom, "Target created does not contain the expected custom data")
11951211

1196-
latestTarget := addTarget(t, repo, "latest", "../fixtures/intermediate-ca.crt")
11971212
currentTarget := addTarget(t, repo, "current", "../fixtures/intermediate-ca.crt")
11981213

11991214
// Apply the changelist. Normally, this would be done by Publish

cmd/notary/integration_test.go

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
"testing"
2424
"time"
2525

26+
"encoding/json"
27+
2628
"github.com/Sirupsen/logrus"
2729
ctxu "github.com/docker/distribution/context"
30+
canonicaljson "github.com/docker/go/canonical/json"
2831
"github.com/docker/notary"
2932
"github.com/docker/notary/client"
3033
"github.com/docker/notary/cryptoservice"
@@ -198,8 +201,9 @@ func TestClientTUFInteraction(t *testing.T) {
198201
defer os.Remove(tempFile.Name())
199202

200203
var (
201-
output string
202-
target = "sdgkadga"
204+
output string
205+
target = "sdgkadga"
206+
target2 = "foobar"
203207
)
204208
// -- tests --
205209

@@ -251,6 +255,62 @@ func TestClientTUFInteraction(t *testing.T) {
251255
output, err = runCommand(t, tempDir, "-s", server.URL, "list", "gun")
252256
require.NoError(t, err)
253257
require.False(t, strings.Contains(string(output), target))
258+
259+
// Test a target with custom data.
260+
tempFileForTargetCustom, err := ioutil.TempFile("", "targetCustom")
261+
require.NoError(t, err)
262+
var customData canonicaljson.RawMessage
263+
err = canonicaljson.Unmarshal([]byte("\"Lorem ipsum dolor sit amet, consectetur adipiscing elit\""), &customData)
264+
require.NoError(t, err)
265+
_, err = tempFileForTargetCustom.Write(customData)
266+
require.NoError(t, err)
267+
tempFileForTargetCustom.Close()
268+
defer os.Remove(tempFileForTargetCustom.Name())
269+
270+
// add a target
271+
_, err = runCommand(t, tempDir, "add", "gun", target2, tempFile.Name(), "--custom", tempFileForTargetCustom.Name())
272+
require.NoError(t, err)
273+
274+
// check status - see target
275+
output, err = runCommand(t, tempDir, "status", "gun")
276+
require.NoError(t, err)
277+
require.Contains(t, output, target2)
278+
279+
// publish repo
280+
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
281+
require.NoError(t, err)
282+
283+
// check status - no targets
284+
output, err = runCommand(t, tempDir, "status", "gun")
285+
require.NoError(t, err)
286+
require.False(t, strings.Contains(string(output), target2))
287+
288+
// list repo - see target
289+
output, err = runCommand(t, tempDir, "-s", server.URL, "list", "gun")
290+
require.NoError(t, err)
291+
require.Contains(t, output, target2)
292+
293+
// Check the file this was written to to inspect metadata
294+
cache, err := nstorage.NewFileStore(
295+
filepath.Join(tempDir, "tuf", filepath.FromSlash("gun"), "metadata"),
296+
"json",
297+
)
298+
require.NoError(t, err)
299+
rawTargets, err := cache.Get("targets")
300+
require.NoError(t, err)
301+
parsedTargets := data.SignedTargets{}
302+
err = json.Unmarshal(rawTargets, &parsedTargets)
303+
require.NoError(t, err)
304+
require.Equal(t, *parsedTargets.Signed.Targets[target2].Custom, customData)
305+
306+
// trigger a lookup error with < 2 args
307+
_, err = runCommand(t, tempDir, "-s", server.URL, "lookup", "gun")
308+
require.Error(t, err)
309+
310+
// lookup target and repo - see target
311+
output, err = runCommand(t, tempDir, "-s", server.URL, "lookup", "gun", target2)
312+
require.NoError(t, err)
313+
require.Contains(t, output, target2)
254314
}
255315

256316
func TestClientDeleteTUFInteraction(t *testing.T) {
@@ -422,6 +482,7 @@ func TestClientTUFAddByHashInteraction(t *testing.T) {
422482
target1 = "sdgkadga"
423483
target2 = "asdfasdf"
424484
target3 = "qwerty"
485+
target4 = "foobar"
425486
)
426487
// -- tests --
427488

@@ -541,6 +602,57 @@ func TestClientTUFAddByHashInteraction(t *testing.T) {
541602
// publish repo
542603
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
543604
require.NoError(t, err)
605+
606+
tempFile, err := ioutil.TempFile("", "targetCustom")
607+
require.NoError(t, err)
608+
var customData canonicaljson.RawMessage
609+
err = canonicaljson.Unmarshal([]byte("\"Lorem ipsum dolor sit amet, consectetur adipiscing elit\""), &customData)
610+
require.NoError(t, err)
611+
_, err = tempFile.Write(customData)
612+
require.NoError(t, err)
613+
tempFile.Close()
614+
defer os.Remove(tempFile.Name())
615+
616+
// add a target by sha512 and custom data
617+
_, err = runCommand(t, tempDir, "addhash", "gun", target4, "3", "--sha512", targetSha512Hex, "--custom", tempFile.Name())
618+
require.NoError(t, err)
619+
620+
// check status - see target
621+
output, err = runCommand(t, tempDir, "status", "gun")
622+
require.NoError(t, err)
623+
require.Contains(t, output, target4)
624+
625+
// publish repo
626+
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
627+
require.NoError(t, err)
628+
629+
// check status - no targets
630+
output, err = runCommand(t, tempDir, "status", "gun")
631+
require.NoError(t, err)
632+
require.False(t, strings.Contains(string(output), target4))
633+
634+
// list repo - see target
635+
output, err = runCommand(t, tempDir, "-s", server.URL, "list", "gun")
636+
require.NoError(t, err)
637+
require.Contains(t, output, target4)
638+
639+
// Check the file this was written to to inspect metadata
640+
cache, err := nstorage.NewFileStore(
641+
filepath.Join(tempDir, "tuf", filepath.FromSlash("gun"), "metadata"),
642+
"json",
643+
)
644+
require.NoError(t, err)
645+
rawTargets, err := cache.Get("targets")
646+
require.NoError(t, err)
647+
parsedTargets := data.SignedTargets{}
648+
err = json.Unmarshal(rawTargets, &parsedTargets)
649+
require.NoError(t, err)
650+
require.Equal(t, *parsedTargets.Signed.Targets[target4].Custom, customData)
651+
652+
// lookup target and repo - see target
653+
output, err = runCommand(t, tempDir, "-s", server.URL, "lookup", "gun", target4)
654+
require.NoError(t, err)
655+
require.Contains(t, output, target4)
544656
}
545657

546658
// Initialize repo and test delegations commands by adding, listing, and removing delegations

0 commit comments

Comments
 (0)