mirror of https://github.com/docker/docs.git
delegations CLI UX improvements
Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
parent
ffe603a968
commit
774b66c9fe
|
|
@ -32,9 +32,9 @@ var cmdDelegationRemoveTemplate = usageTemplate{
|
||||||
}
|
}
|
||||||
|
|
||||||
var cmdDelegationAddTemplate = usageTemplate{
|
var cmdDelegationAddTemplate = usageTemplate{
|
||||||
Use: "add [ GUN ] [ Role ] <PEM file path 1> ...",
|
Use: "add [ GUN ] [ Role ] <X509 file path 1> ...",
|
||||||
Short: "Add a keys to delegation using the provided public key certificate PEMs.",
|
Short: "Add a keys to delegation using the provided public key X509 certificates.",
|
||||||
Long: "Add a keys to delegation using the provided public key certificate PEMs in a specific Global Unique Name.",
|
Long: "Add a keys to delegation using the provided public key PEM encoded X509 certificates in a specific Global Unique Name.",
|
||||||
}
|
}
|
||||||
|
|
||||||
var paths []string
|
var paths []string
|
||||||
|
|
@ -84,7 +84,7 @@ func (d *delegationCommander) delegationsList(cmd *cobra.Command, args []string)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Println("")
|
cmd.Println("")
|
||||||
prettyPrintRoles(delegationRoles, cmd.Out())
|
prettyPrintRoles(delegationRoles, cmd.Out(), "delegations")
|
||||||
cmd.Println("")
|
cmd.Println("")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -100,6 +100,11 @@ func (d *delegationCommander) delegationRemove(cmd *cobra.Command, args []string
|
||||||
gun := args[0]
|
gun := args[0]
|
||||||
role := args[1]
|
role := args[1]
|
||||||
|
|
||||||
|
// Check if role is valid delegation name before requiring any user input
|
||||||
|
if !data.IsDelegation(role) {
|
||||||
|
return fmt.Errorf("invalid delegation name %s", role)
|
||||||
|
}
|
||||||
|
|
||||||
// If we're only given the gun and the role, attempt to remove all data for this delegation
|
// If we're only given the gun and the role, attempt to remove all data for this delegation
|
||||||
if len(args) == 2 && paths == nil {
|
if len(args) == 2 && paths == nil {
|
||||||
removeAll = true
|
removeAll = true
|
||||||
|
|
@ -143,10 +148,11 @@ func (d *delegationCommander) delegationRemove(cmd *cobra.Command, args []string
|
||||||
cmd.Println("")
|
cmd.Println("")
|
||||||
if removeAll {
|
if removeAll {
|
||||||
cmd.Printf("Forced removal (including all keys and paths) of delegation role %s to repository \"%s\" staged for next publish.\n", role, gun)
|
cmd.Printf("Forced removal (including all keys and paths) of delegation role %s to repository \"%s\" staged for next publish.\n", role, gun)
|
||||||
|
} else {
|
||||||
|
cmd.Printf(
|
||||||
|
"Removal of delegation role %s with keys %s and paths %s, to repository \"%s\" staged for next publish.\n",
|
||||||
|
role, keyIDs, paths, gun)
|
||||||
}
|
}
|
||||||
cmd.Printf(
|
|
||||||
"Removal of delegation role %s with keys %s and paths %s, to repository \"%s\" staged for next publish.\n",
|
|
||||||
role, keyIDs, paths, gun)
|
|
||||||
cmd.Println("")
|
cmd.Println("")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,18 @@ func TestRemoveInvalidDelegationName(t *testing.T) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveAllInvalidDelegationName(t *testing.T) {
|
||||||
|
// Cleanup after test
|
||||||
|
defer os.RemoveAll(testTrustDir)
|
||||||
|
|
||||||
|
// Setup commander
|
||||||
|
commander := setup()
|
||||||
|
|
||||||
|
// Should error due to invalid delegation name (should be prefixed by "targets/")
|
||||||
|
err := commander.delegationRemove(commander.GetCommand(), []string{"gun", "INVALID_NAME"})
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddInvalidNumArgs(t *testing.T) {
|
func TestAddInvalidNumArgs(t *testing.T) {
|
||||||
// Setup commander
|
// Setup commander
|
||||||
commander := setup()
|
commander := setup()
|
||||||
|
|
|
||||||
|
|
@ -189,7 +189,7 @@ func TestClientDelegationsInteraction(t *testing.T) {
|
||||||
// list delegations - none yet
|
// list delegations - none yet
|
||||||
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, output, "No such roles published in this repository.")
|
assert.Contains(t, output, "No delegations present in this repository.")
|
||||||
|
|
||||||
// add new valid delegation with single new cert
|
// add new valid delegation with single new cert
|
||||||
output, err = runCommand(t, tempDir, "delegation", "add", "gun", "targets/delegation", tempFile.Name())
|
output, err = runCommand(t, tempDir, "delegation", "add", "gun", "targets/delegation", tempFile.Name())
|
||||||
|
|
@ -204,7 +204,7 @@ func TestClientDelegationsInteraction(t *testing.T) {
|
||||||
// list delegations - none yet because still unpublished
|
// list delegations - none yet because still unpublished
|
||||||
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, output, "No such roles published in this repository.")
|
assert.Contains(t, output, "No delegations present in this repository.")
|
||||||
|
|
||||||
// publish repo
|
// publish repo
|
||||||
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
|
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
|
||||||
|
|
@ -281,7 +281,7 @@ func TestClientDelegationsInteraction(t *testing.T) {
|
||||||
// list delegations - we should see no delegations
|
// list delegations - we should see no delegations
|
||||||
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, output, "No such roles published in this repository.")
|
assert.Contains(t, output, "No delegations present in this repository.")
|
||||||
|
|
||||||
// add delegation with multiple certs and multiple paths
|
// add delegation with multiple certs and multiple paths
|
||||||
output, err = runCommand(t, tempDir, "delegation", "add", "gun", "targets/delegation", tempFile.Name(), tempFile2.Name(), "--paths", "path1,path2")
|
output, err = runCommand(t, tempDir, "delegation", "add", "gun", "targets/delegation", tempFile.Name(), tempFile2.Name(), "--paths", "path1,path2")
|
||||||
|
|
@ -350,7 +350,7 @@ func TestClientDelegationsInteraction(t *testing.T) {
|
||||||
// remove by force to delete the delegation entirely
|
// remove by force to delete the delegation entirely
|
||||||
output, err = runCommand(t, tempDir, "delegation", "remove", "gun", "targets/delegation", "-y")
|
output, err = runCommand(t, tempDir, "delegation", "remove", "gun", "targets/delegation", "-y")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, output, "Removal of delegation role")
|
assert.Contains(t, output, "Forced removal (including all keys and paths) of delegation role")
|
||||||
|
|
||||||
// publish repo
|
// publish repo
|
||||||
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
|
_, err = runCommand(t, tempDir, "-s", server.URL, "publish", "gun")
|
||||||
|
|
@ -359,7 +359,7 @@ func TestClientDelegationsInteraction(t *testing.T) {
|
||||||
// list delegations - we should see no delegations
|
// list delegations - we should see no delegations
|
||||||
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
output, err = runCommand(t, tempDir, "-s", server.URL, "delegation", "list", "gun")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Contains(t, output, "No such roles published in this repository.")
|
assert.Contains(t, output, "No delegations present in this repository.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits a string into lines, and returns any lines that are not empty (
|
// Splits a string into lines, and returns any lines that are not empty (
|
||||||
|
|
|
||||||
|
|
@ -176,9 +176,9 @@ func prettyPrintTargets(ts []*client.TargetWithRole, writer io.Writer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pretty-prints the list of provided Roles
|
// Pretty-prints the list of provided Roles
|
||||||
func prettyPrintRoles(rs []*data.Role, writer io.Writer) {
|
func prettyPrintRoles(rs []*data.Role, writer io.Writer, roleType string) {
|
||||||
if len(rs) == 0 {
|
if len(rs) == 0 {
|
||||||
writer.Write([]byte("\nNo such roles published in this repository.\n\n"))
|
writer.Write([]byte(fmt.Sprintf("\nNo %s present in this repository.\n\n", roleType)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -220,13 +220,13 @@ func generateCertificate(t *testing.T, gun string, expireInHours int64) *x509.Ce
|
||||||
// are no roles.
|
// are no roles.
|
||||||
func TestPrettyPrintZeroRoles(t *testing.T) {
|
func TestPrettyPrintZeroRoles(t *testing.T) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
prettyPrintRoles([]*data.Role{}, &b)
|
prettyPrintRoles([]*data.Role{}, &b, "delegations")
|
||||||
text, err := ioutil.ReadAll(&b)
|
text, err := ioutil.ReadAll(&b)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
lines := strings.Split(strings.TrimSpace(string(text)), "\n")
|
lines := strings.Split(strings.TrimSpace(string(text)), "\n")
|
||||||
assert.Len(t, lines, 1)
|
assert.Len(t, lines, 1)
|
||||||
assert.Equal(t, "No such roles published in this repository.", lines[0])
|
assert.Equal(t, "No delegations present in this repository.", lines[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Roles are sorted by name, and the name, paths, and KeyIDs are printed.
|
// Roles are sorted by name, and the name, paths, and KeyIDs are printed.
|
||||||
|
|
@ -241,7 +241,7 @@ func TestPrettyPrintSortedRoles(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
prettyPrintRoles(unsorted, &b)
|
prettyPrintRoles(unsorted, &b, "delegations")
|
||||||
text, err := ioutil.ReadAll(&b)
|
text, err := ioutil.ReadAll(&b)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue