diff --git a/client/changelist/change.go b/client/changelist/change.go index 77544dc661..f7b2f7766d 100644 --- a/client/changelist/change.go +++ b/client/changelist/change.go @@ -1,9 +1,19 @@ package changelist +const ( + // Scopes for TufChanges are simply the TUF roles. + // Unfortunately because of targets delegations, we can only + // cover the base roles. + ScopeRoot = "root" + ScopeTargets = "targets" + ScopeSnapshot = "snapshot" + ScopeTimestamp = "timestamp" +) + // TufChange represents a change to a TUF repo type TufChange struct { // Abbreviated because Go doesn't permit a field and method of the same name - Actn int `json:"action"` + Actn string `json:"action"` Role string `json:"role"` ChangeType string `json:"type"` ChangePath string `json:"path"` @@ -11,7 +21,7 @@ type TufChange struct { } // NewTufChange initializes a tufChange object -func NewTufChange(action int, role, changeType, changePath string, content []byte) *TufChange { +func NewTufChange(action string, role, changeType, changePath string, content []byte) *TufChange { return &TufChange{ Actn: action, Role: role, @@ -22,7 +32,7 @@ func NewTufChange(action int, role, changeType, changePath string, content []byt } // Action return c.Actn -func (c TufChange) Action() int { +func (c TufChange) Action() string { return c.Actn } diff --git a/client/changelist/interface.go b/client/changelist/interface.go index fd24b65c54..a9b09b71fe 100644 --- a/client/changelist/interface.go +++ b/client/changelist/interface.go @@ -22,17 +22,17 @@ type Changelist interface { const ( // ActionCreate represents a Create action - ActionCreate = iota + ActionCreate = "create" // ActionUpdate represents an Update action - ActionUpdate + ActionUpdate = "update" // ActionDelete represents a Delete action - ActionDelete + ActionDelete = "delete" ) // Change is the interface for a TUF Change type Change interface { // "create","update", or "delete" - Action() int + Action() string // Where the change should be made. // For TUF this will be the role diff --git a/client/client.go b/client/client.go index 6c8e3a8aa9..e196a4fedd 100644 --- a/client/client.go +++ b/client/client.go @@ -250,7 +250,7 @@ func (r *NotaryRepository) AddTarget(target *Target) error { return err } - c := changelist.NewTufChange(changelist.ActionCreate, "targets", "target", target.Name, metaJSON) + c := changelist.NewTufChange(changelist.ActionCreate, changelist.ScopeTargets, "target", target.Name, metaJSON) err = cl.Add(c) if err != nil { return err @@ -258,6 +258,20 @@ func (r *NotaryRepository) AddTarget(target *Target) error { return cl.Close() } +func (r *NotaryRepository) RemoveTarget(targetName string) error { + cl, err := changelist.NewFileChangelist(filepath.Join(r.tufRepoPath, "changelist")) + if err != nil { + return err + } + logrus.Debugf("Removing target \"%s\"", targetName) + c := changelist.NewTufChange(changelist.ActionDelete, changelist.ScopeTargets, "target", targetName, nil) + err = cl.Add(c) + if err != nil { + return err + } + return nil +} + // ListTargets lists all targets for the current repository func (r *NotaryRepository) ListTargets() ([]*Target, error) { c, err := r.bootstrapClient() diff --git a/client/helpers.go b/client/helpers.go index 003f73fa91..0200ac2dce 100644 --- a/client/helpers.go +++ b/client/helpers.go @@ -5,6 +5,7 @@ import ( "net/http" "time" + "github.com/Sirupsen/logrus" "github.com/docker/notary/client/changelist" "github.com/endophage/gotuf" "github.com/endophage/gotuf/data" @@ -26,13 +27,16 @@ func getRemoteStore(baseURL, gun string, rt http.RoundTripper) (store.RemoteStor func applyChangelist(repo *tuf.TufRepo, cl changelist.Changelist) error { changes := cl.List() - var err error + logrus.Debugf("applying %d changes", len(changes)) for _, c := range changes { - if c.Scope() == "targets" { - applyTargetsChange(repo, c) - } - if err != nil { - return err + switch c.Scope() { + case changelist.ScopeTargets: + err := applyTargetsChange(repo, c) + if err != nil { + return err + } + default: + logrus.Debug("scope not supported: ", c.Scope()) } } return nil @@ -40,16 +44,21 @@ func applyChangelist(repo *tuf.TufRepo, cl changelist.Changelist) error { func applyTargetsChange(repo *tuf.TufRepo, c changelist.Change) error { var err error - meta := &data.FileMeta{} - err = json.Unmarshal(c.Content(), meta) - if err != nil { - return nil - } - if c.Action() == changelist.ActionCreate { + switch c.Action() { + case changelist.ActionCreate: + logrus.Debug("changelist add: ", c.Path()) + meta := &data.FileMeta{} + err = json.Unmarshal(c.Content(), meta) + if err != nil { + return err + } files := data.Files{c.Path(): *meta} _, err = repo.AddTargets("targets", files) - } else if c.Action() == changelist.ActionDelete { + case changelist.ActionDelete: + logrus.Debug("changelist remove: ", c.Path()) err = repo.RemoveTargets("targets", c.Path()) + default: + logrus.Debug("action not yet supported: ", c.Action()) } if err != nil { return err diff --git a/cmd/notary/tuf.go b/cmd/notary/tuf.go index 430fb1a085..05951740c0 100644 --- a/cmd/notary/tuf.go +++ b/cmd/notary/tuf.go @@ -17,7 +17,7 @@ import ( ) // FIXME: This should not be hardcoded -const hardcodedBaseURL = "https://notary-server:4443" +const hardcodedBaseURL = "http://notary-server:4443" var retriever passphrase.Retriever @@ -218,14 +218,15 @@ func tufRemove(cmd *cobra.Command, args []string) { gun := args[0] targetName := args[1] - //c := changelist.NewTufChange(changelist.ActionDelete, "targets", "target", targetName, nil) - //err := cl.Add(c) - //if err != nil { - // fatalf(err.Error()) - //} - - // TODO(diogo): Implement RemoveTargets in libnotary - fmt.Println("Removing target ", targetName, " from ", gun) + repo, err := notaryclient.NewNotaryRepository(viper.GetString("baseTrustDir"), gun, hardcodedBaseURL, + getTransport(), retriever) + if err != nil { + fatalf(err.Error()) + } + err = repo.RemoveTarget(targetName) + if err != nil { + fatalf(err.Error()) + } } func verify(cmd *cobra.Command, args []string) {