2018-10-23 11:05:44 -04:00
|
|
|
package trust
|
2017-08-24 18:43:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-08-24 18:43:55 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTrustTagTableFormat = "table {{.SignedTag}}\t{{.Digest}}\t{{.Signers}}"
|
|
|
|
signedTagNameHeader = "SIGNED TAG"
|
|
|
|
trustedDigestHeader = "DIGEST"
|
|
|
|
signersHeader = "SIGNERS"
|
|
|
|
defaultSignerInfoTableFormat = "table {{.Signer}}\t{{.Keys}}"
|
|
|
|
signerNameHeader = "SIGNER"
|
|
|
|
keysHeader = "KEYS"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SignedTagInfo represents all formatted information needed to describe a signed tag:
|
|
|
|
// Name: name of the signed tag
|
|
|
|
// Digest: hex encoded digest of the contents
|
|
|
|
// Signers: list of entities who signed the tag
|
|
|
|
type SignedTagInfo struct {
|
|
|
|
Name string
|
|
|
|
Digest string
|
|
|
|
Signers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignerInfo represents all formatted information needed to describe a signer:
|
|
|
|
// Name: name of the signer role
|
|
|
|
// Keys: the keys associated with the signer
|
|
|
|
type SignerInfo struct {
|
|
|
|
Name string
|
|
|
|
Keys []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTrustTagFormat returns a Format for rendering using a trusted tag Context
|
2018-10-23 11:05:44 -04:00
|
|
|
func NewTrustTagFormat() formatter.Format {
|
2017-08-24 18:43:55 -04:00
|
|
|
return defaultTrustTagTableFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSignerInfoFormat returns a Format for rendering a signer role info Context
|
2018-10-23 11:05:44 -04:00
|
|
|
func NewSignerInfoFormat() formatter.Format {
|
2017-08-24 18:43:55 -04:00
|
|
|
return defaultSignerInfoTableFormat
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
// TagWrite writes the context
|
|
|
|
func TagWrite(ctx formatter.Context, signedTagInfoList []SignedTagInfo) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-08-24 18:43:55 -04:00
|
|
|
for _, signedTag := range signedTagInfoList {
|
|
|
|
if err := format(&trustTagContext{s: signedTag}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
trustTagCtx := trustTagContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
trustTagCtx.Header = formatter.SubHeaderContext{
|
2017-08-24 18:43:55 -04:00
|
|
|
"SignedTag": signedTagNameHeader,
|
|
|
|
"Digest": trustedDigestHeader,
|
|
|
|
"Signers": signersHeader,
|
|
|
|
}
|
|
|
|
return ctx.Write(&trustTagCtx, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type trustTagContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-08-24 18:43:55 -04:00
|
|
|
s SignedTagInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignedTag returns the name of the signed tag
|
|
|
|
func (c *trustTagContext) SignedTag() string {
|
|
|
|
return c.s.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Digest returns the hex encoded digest associated with this signed tag
|
|
|
|
func (c *trustTagContext) Digest() string {
|
|
|
|
return c.s.Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signers returns the sorted list of entities who signed this tag
|
|
|
|
func (c *trustTagContext) Signers() string {
|
|
|
|
sort.Strings(c.s.Signers)
|
|
|
|
return strings.Join(c.s.Signers, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignerInfoWrite writes the context
|
2018-10-23 11:05:44 -04:00
|
|
|
func SignerInfoWrite(ctx formatter.Context, signerInfoList []SignerInfo) error {
|
|
|
|
render := func(format func(subContext formatter.SubContext) error) error {
|
2017-08-24 18:43:55 -04:00
|
|
|
for _, signerInfo := range signerInfoList {
|
|
|
|
if err := format(&signerInfoContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
s: signerInfo,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
signerInfoCtx := signerInfoContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
signerInfoCtx.Header = formatter.SubHeaderContext{
|
2017-08-24 18:43:55 -04:00
|
|
|
"Signer": signerNameHeader,
|
|
|
|
"Keys": keysHeader,
|
|
|
|
}
|
|
|
|
return ctx.Write(&signerInfoCtx, render)
|
|
|
|
}
|
|
|
|
|
|
|
|
type signerInfoContext struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.HeaderContext
|
2017-08-24 18:43:55 -04:00
|
|
|
trunc bool
|
|
|
|
s SignerInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys returns the sorted list of keys associated with the signer
|
|
|
|
func (c *signerInfoContext) Keys() string {
|
|
|
|
sort.Strings(c.s.Keys)
|
|
|
|
truncatedKeys := []string{}
|
|
|
|
if c.trunc {
|
|
|
|
for _, keyID := range c.s.Keys {
|
|
|
|
truncatedKeys = append(truncatedKeys, stringid.TruncateID(keyID))
|
|
|
|
}
|
|
|
|
return strings.Join(truncatedKeys, ", ")
|
|
|
|
}
|
|
|
|
return strings.Join(c.s.Keys, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signer returns the name of the signer
|
|
|
|
func (c *signerInfoContext) Signer() string {
|
|
|
|
return c.s.Name
|
|
|
|
}
|