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

Expose targets custom data in the NotaryRepository API #1146

Merged
merged 10 commits into from
May 9, 2017

Conversation

ashfall
Copy link
Contributor

@ashfall ashfall commented May 4, 2017

Closes #1143

@GordonTheTurtle
Copy link

Please sign your commits following these rules:
https://github.com/moby/moby/blob/master/CONTRIBUTING.md#sign-your-work
The easiest way to do this is to amend the last commit:

$ git clone -b "target-custom-data" [email protected]:ashfall/notary.git somewhere
$ cd somewhere
$ git rebase -i HEAD~842354207168
editor opens
change each 'pick' to 'edit'
save the file and quit
$ git commit --amend -s --no-edit
$ git rebase --continue # and repeat the amend for each commit
$ git push -f

Amending updates the existing PR. You DO NOT need to open a new one.

client/client.go Outdated
var customData *canonicaljson.RawMessage
if target.Custom != nil {
customData = new(canonicaljson.RawMessage)
if err := customData.UnmarshalJSON(target.Custom); err != nil {
Copy link
Contributor

@cyli cyli May 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just to validate that Target.Custom is actually valid json data and not a cast []byte?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome catch, thanks! In my first iteration, Target.Custom was a []byte, and this line was meant to do the correct type conversion. We shouldn't need this anymore. Updating!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pressed enter too soon. This was because I was getting "No error is expected but got json: error calling MarshalJSON for type *json.RawMessage: unexpected end of JSON input" in the cases where there was no custom data in the target, in the following line where we do metaJSON, err := json.Marshal(meta).

@ashfall ashfall force-pushed the target-custom-data branch from 821a643 to 1060e4f Compare May 4, 2017 23:52
@@ -1238,6 +1248,48 @@ func testListTarget(t *testing.T, rootType string) {
require.True(t, reflect.DeepEqual(*currentTarget, newCurrentTarget.Target), "current target does not match")
}

func testListTargetWithCustom(t *testing.T, rootType string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: I wonder if it'd make sense to just fold this into testListTarget by adding custom metadata to one of the targets (either latest or custom)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's a good idea. Updating, thanks!

// Open and read a file containing the targetCustom data
func getTargetCustom(targetCustomFilename string) (json.RawMessage, error) {
var nilCustom json.RawMessage
targetCustomFile, err := os.Open(targetCustomFilename)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: ioutil.ReadFile might save some typing here, because it will open and close the file for you.

client/client.go Outdated
Name string // the name of the target
Hashes data.Hashes // the hash of the target
Length int64 // the size in bytes of the target
Custom canonicaljson.RawMessage // the custom data provided to describe the file at TARGETPATH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: would it be easier to make this a pointer, so that we can just assign to and from FileMeta without having check for nil?

@@ -155,6 +157,7 @@ func (t *tufCommander) AddToCommand(cmd *cobra.Command) {
cmdTUFAdd := cmdTUFAddTemplate.ToCommand(t.tufAdd)
cmdTUFAdd.Flags().StringSliceVarP(&t.roles, "roles", "r", nil, "Delegation roles to add this target to")
cmdTUFAdd.Flags().BoolVarP(&t.autoPublish, "publish", "p", false, htAutoPublish)
cmdTUFAdd.Flags().StringVar(&t.custom, "custom", "", "Name of the file containing custom data for this target")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking nit: Maybe "path to the file..." instead of "name of the file"?

Copy link
Contributor

@cyli cyli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for adding this feature!

if err != nil {
return nil, err
}
err = json.Unmarshal(rawTargetCustom, &targetCustom)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be doing targetCustom.Unmarshal(rawTargetCustom). That will also require targetCustom to have been initialized, so line 258 needs to be targetCustom := new(json.RawMessage) then you'll also be able to remove the & when you reference it because new will already return a reference.

@@ -21,6 +21,7 @@ import (
"github.com/docker/distribution/registry/client/auth/challenge"
"github.com/docker/distribution/registry/client/transport"
"github.com/docker/go-connections/tlsconfig"
"github.com/docker/go/canonical/json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's import this aliased to canonicaljson for clarity. It reduces confusion when/if somebody is having type errors related to the encoding/json vs .../canonical/json libraries

@ashfall ashfall force-pushed the target-custom-data branch from 10990ff to 1f4822b Compare May 8, 2017 21:36
ashfall added 10 commits May 8, 2017 16:31
- Update Target definition and NewTarget to store custom data
- tufAdd and tufAddByHash now take a filename as a flag, open and read the file, and pass the custom data bytes to
NewTarget.
- Client APIs now understand Target.Custom
- Test that client lists and gets targets with custom data
- getTargetCustom should return a nil RawMessage, not a []byte
- Test tufAdd with target custom data
- Trigger a usage error for the tufLookup command in a test

Signed-off-by: Ashwini Oruganti <[email protected]>
Signed-off-by: Ashwini Oruganti <[email protected]>
 Signed-off-by: Ashwini Oruganti <[email protected]>
To avoid confusing it with encoding/json.

Signed-off-by: Ashwini Oruganti <[email protected]>
@ashfall ashfall force-pushed the target-custom-data branch from 1f4822b to a6089e0 Compare May 8, 2017 23:31
Copy link
Contributor

@endophage endophage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@endophage endophage merged commit 49d855c into notaryproject:master May 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants