2017-08-24 18:43:55 -04:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-25 17:49:40 -04:00
|
|
|
"io"
|
2017-08-24 18:43:55 -04:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2017-10-30 12:21:41 -04:00
|
|
|
"github.com/theupdateframework/notary/client"
|
2017-08-24 18:43:55 -04:00
|
|
|
)
|
|
|
|
|
2018-03-09 12:37:43 -05:00
|
|
|
func prettyPrintTrustInfo(cli command.Cli, remote string) error {
|
2017-11-15 14:50:33 -05:00
|
|
|
signatureRows, adminRolesWithSigs, delegationRoles, err := lookupTrustInfo(cli, remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(signatureRows) > 0 {
|
2018-03-09 12:37:43 -05:00
|
|
|
fmt.Fprintf(cli.Out(), "\nSignatures for %s\n\n", remote)
|
|
|
|
|
2017-11-15 14:50:33 -05:00
|
|
|
if err := printSignatures(cli.Out(), signatureRows); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(cli.Out(), "\nNo signatures for %s\n\n", remote)
|
|
|
|
}
|
|
|
|
signerRoleToKeyIDs := getDelegationRoleToKeyMap(delegationRoles)
|
|
|
|
|
|
|
|
// If we do not have additional signers, do not display
|
|
|
|
if len(signerRoleToKeyIDs) > 0 {
|
2018-03-09 12:37:43 -05:00
|
|
|
fmt.Fprintf(cli.Out(), "\nList of signers and their keys for %s\n\n", remote)
|
2017-11-15 14:50:33 -05:00
|
|
|
if err := printSignerInfo(cli.Out(), signerRoleToKeyIDs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will always have the root and targets information
|
2018-03-09 12:37:43 -05:00
|
|
|
fmt.Fprintf(cli.Out(), "\nAdministrative keys for %s\n\n", remote)
|
2017-11-15 14:50:33 -05:00
|
|
|
printSortedAdminKeys(cli.Out(), adminRolesWithSigs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 17:49:40 -04:00
|
|
|
func printSortedAdminKeys(out io.Writer, adminRoles []client.RoleWithSignatures) {
|
|
|
|
sort.Slice(adminRoles, func(i, j int) bool { return adminRoles[i].Name > adminRoles[j].Name })
|
|
|
|
for _, adminRole := range adminRoles {
|
2018-03-09 12:37:43 -05:00
|
|
|
if formattedAdminRole := formatAdminRole(adminRole); formattedAdminRole != "" {
|
|
|
|
fmt.Fprintf(out, " %s", formattedAdminRole)
|
|
|
|
}
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pretty print with ordered rows
|
2018-07-08 15:08:17 -04:00
|
|
|
func printSignatures(out io.Writer, signatureRows []trustTagRow) error {
|
2017-08-24 18:43:55 -04:00
|
|
|
trustTagCtx := formatter.Context{
|
2017-08-25 17:49:40 -04:00
|
|
|
Output: out,
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewTrustTagFormat(),
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
// convert the formatted type before printing
|
2018-10-23 11:05:44 -04:00
|
|
|
formattedTags := []SignedTagInfo{}
|
2017-08-24 18:43:55 -04:00
|
|
|
for _, sigRow := range signatureRows {
|
|
|
|
formattedSigners := sigRow.Signers
|
|
|
|
if len(formattedSigners) == 0 {
|
|
|
|
formattedSigners = append(formattedSigners, fmt.Sprintf("(%s)", releasedRoleName))
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
formattedTags = append(formattedTags, SignedTagInfo{
|
2017-11-15 14:50:33 -05:00
|
|
|
Name: sigRow.SignedTag,
|
|
|
|
Digest: sigRow.Digest,
|
2017-08-24 18:43:55 -04:00
|
|
|
Signers: formattedSigners,
|
|
|
|
})
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return TagWrite(trustTagCtx, formattedTags)
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|
|
|
|
|
2017-08-25 17:49:40 -04:00
|
|
|
func printSignerInfo(out io.Writer, roleToKeyIDs map[string][]string) error {
|
2017-08-24 18:43:55 -04:00
|
|
|
signerInfoCtx := formatter.Context{
|
2017-08-25 17:49:40 -04:00
|
|
|
Output: out,
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewSignerInfoFormat(),
|
2017-08-24 18:43:55 -04:00
|
|
|
Trunc: true,
|
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
formattedSignerInfo := []SignerInfo{}
|
2017-08-24 18:43:55 -04:00
|
|
|
for name, keyIDs := range roleToKeyIDs {
|
2018-10-23 11:05:44 -04:00
|
|
|
formattedSignerInfo = append(formattedSignerInfo, SignerInfo{
|
2017-08-24 18:43:55 -04:00
|
|
|
Name: name,
|
|
|
|
Keys: keyIDs,
|
|
|
|
})
|
|
|
|
}
|
2018-07-08 15:08:17 -04:00
|
|
|
sort.Slice(formattedSignerInfo, func(i, j int) bool {
|
2018-08-08 11:55:48 -04:00
|
|
|
return sortorder.NaturalLess(formattedSignerInfo[i].Name, formattedSignerInfo[j].Name)
|
2018-07-08 15:08:17 -04:00
|
|
|
})
|
2018-10-23 11:05:44 -04:00
|
|
|
return SignerInfoWrite(signerInfoCtx, formattedSignerInfo)
|
2017-08-24 18:43:55 -04:00
|
|
|
}
|