func (a *AuthCommand) ListAuthorities(client *auth.TunClient) error {
	// by default show authorities of both types:
	authTypes := []services.CertAuthType{
		services.UserCA,
		services.HostCA,
	}
	// but if there was a --type switch, only select those:
	if a.authType != "" {
		authTypes = []services.CertAuthType{services.CertAuthType(a.authType)}
		if err := authTypes[0].Check(); err != nil {
			return trace.Wrap(err)
		}
	}
	localAuthName, err := client.GetDomainName()
	if err != nil {
		return trace.Wrap(err)
	}
	var (
		localCAs   []*services.CertAuthority
		trustedCAs []*services.CertAuthority
	)
	for _, t := range authTypes {
		cas, err := client.GetCertAuthorities(t, false)
		if err != nil {
			return trace.Wrap(err)
		}
		for i := range cas {
			if cas[i].DomainName == localAuthName {
				localCAs = append(localCAs, cas[i])
			} else {
				trustedCAs = append(trustedCAs, cas[i])
			}
		}
	}
	localCAsView := func() string {
		t := goterm.NewTable(0, 10, 5, ' ', 0)
		printHeader(t, []string{"CA Type", "Fingerprint"})
		for _, a := range localCAs {
			for _, keyBytes := range a.CheckingKeys {
				fingerprint, err := sshutils.AuthorizedKeyFingerprint(keyBytes)
				if err != nil {
					fingerprint = fmt.Sprintf("<bad key: %v", err)
				}
				fmt.Fprintf(t, "%v\t%v\n", a.Type, fingerprint)
			}
		}
		return fmt.Sprintf("CA keys for the local cluster %v:\n\n", localAuthName) +
			t.String()
	}
	trustedCAsView := func() string {
		t := goterm.NewTable(0, 10, 5, ' ', 0)
		printHeader(t, []string{"Cluster Name", "CA Type", "Fingerprint", "Allowed Logins"})
		for _, a := range trustedCAs {
			for _, keyBytes := range a.CheckingKeys {
				fingerprint, err := sshutils.AuthorizedKeyFingerprint(keyBytes)
				if err != nil {
					fingerprint = fmt.Sprintf("<bad key: %v", err)
				}
				var logins string
				if a.Type == services.HostCA {
					logins = "N/A"
				} else {
					logins = strings.Join(a.AllowedLogins, ",")
					if logins == "" {
						logins = "<nobody>"
					} else if logins == "*" {
						logins = "<everyone>"
					}
				}
				fmt.Fprintf(t, "%v\t%v\t%v\t%v\n", a.DomainName, a.Type, fingerprint, logins)
			}
		}
		return "\nCA Keys for Trusted Clusters:\n\n" + t.String()
	}
	fmt.Printf(localCAsView())
	if len(trustedCAs) > 0 {
		fmt.Printf(trustedCAsView())
	}
	return nil
}
// ListAuthorities shows list of user authorities we trust
ListAuthorities shows list of user authorities we trust
// ListAuthorities shows list of user authorities we trust
func (a *AuthCommand) ListAuthorities(client *auth.TunClient) error {
	authTypes := []services.CertAuthType{
		services.UserCA,
		services.HostCA,
	}
	if a.authType != "" {
		authTypes = []services.CertAuthType{services.CertAuthType(a.authType)}
		if err := authTypes[0].Check(); err != nil {
			return trace.Wrap(err)
		}
	}
	localAuthName, err := client.GetDomainName()
	if err != nil {
		return trace.Wrap(err)
	}
	var (
		localCAs   []*services.CertAuthority
		trustedCAs []*services.CertAuthority
	)
	for _, t := range authTypes {
		cas, err := client.GetCertAuthorities(t, false)
		if err != nil {
			return trace.Wrap(err)
		}
		for i := range cas {
			if cas[i].DomainName == localAuthName {
				localCAs = append(localCAs, cas[i])
			} else {
				trustedCAs = append(trustedCAs, cas[i])
			}
		}
	}
	localCAsView := func() string {
		t := goterm.NewTable(0, 10, 5, ' ', 0)
		printHeader(t, []string{"CA Type", "Fingerprint"})
		for _, a := range localCAs {
			for _, keyBytes := range a.CheckingKeys {
				fingerprint, err := sshutils.AuthorizedKeyFingerprint(keyBytes)
				if err != nil {
					fingerprint = fmt.Sprintf("<bad key: %v", err)
				}
				fmt.Fprintf(t, "%v\t%v\n", a.Type, fingerprint)
			}
		}
		return fmt.Sprintf("CA keys for the local cluster %v:\n\n", localAuthName) +
			t.String()
	}
	trustedCAsView := func() string {
		t := goterm.NewTable(0, 10, 5, ' ', 0)
		printHeader(t, []string{"Cluster Name", "CA Type", "Fingerprint", "Allowed Logins"})
		for _, a := range trustedCAs {
			for _, keyBytes := range a.CheckingKeys {
				fingerprint, err := sshutils.AuthorizedKeyFingerprint(keyBytes)
				if err != nil {
					fingerprint = fmt.Sprintf("<bad key: %v", err)
				}
				var logins string
				if a.Type == services.HostCA {
					logins = "N/A"
				} else {
					logins = strings.Join(a.AllowedLogins, ",")
					if logins == "" {
						logins = "<nobody>"
					} else if logins == "*" {
						logins = "<everyone>"
					}
				}
				fmt.Fprintf(t, "%v\t%v\t%v\t%v\n", a.DomainName, a.Type, fingerprint, logins)
			}
		}
		return "\nCA Keys for Trusted Clusters:\n\n" + t.String()
	}
	fmt.Printf(localCAsView())
	if len(trustedCAs) > 0 {
		fmt.Printf(trustedCAsView())
	}
	return nil
}
func ( a * AuthCommand ) ListAuthorities ( client * auth . TunClient ) error { authTypes := [ ] services . CertAuthType { services . UserCA , services . HostCA , } if a . authType != " " { authTypes = [ ] services . CertAuthType { services . CertAuthType ( a . authType ) } if err := authTypes [ 0 ] . Check ( ) ; err != nil { return trace . Wrap ( err ) } } localAuthName , err := client . GetDomainName ( ) if err != nil { return trace . Wrap ( err ) } var ( localCAs [ ] * services . CertAuthority trustedCAs [ ] * services . CertAuthority ) for _ , t := range authTypes { cas , err := client . GetCertAuthorities ( t , false ) if err != nil { return trace . Wrap ( err ) } for i := range cas { if cas [ i ] . DomainName == localAuthName { localCAs = append ( localCAs , cas [ i ] ) } else { trustedCAs = append ( trustedCAs , cas [ i ] ) } } } localCAsView := func ( ) string { t := goterm . NewTable ( 0 , 10 , 5 , ' ' , 0 ) printHeader ( t , [ ] string { " " , " " } ) for _ , a := range localCAs { for _ , keyBytes := range a . CheckingKeys { fingerprint , err := sshutils . AuthorizedKeyFingerprint ( keyBytes ) if err != nil { fingerprint = fmt . Sprintf ( " " , err ) } fmt . Fprintf ( t , " \t \n " , a . Type , fingerprint ) } } return fmt . Sprintf ( " \n \n " , localAuthName ) + t . String ( ) } trustedCAsView := func ( ) string { t := goterm . NewTable ( 0 , 10 , 5 , ' ' , 0 ) printHeader ( t , [ ] string { " " , " " , " " , " " } ) for _ , a := range trustedCAs { for _ , keyBytes := range a . CheckingKeys { fingerprint , err := sshutils . AuthorizedKeyFingerprint ( keyBytes ) if err != nil { fingerprint = fmt . Sprintf ( " " , err ) } var logins string if a . Type == services . HostCA { logins = " " } else { logins = strings . Join ( a . AllowedLogins , " " ) if logins == " " { logins = " " } else if logins == " " { logins = " " } } fmt . Fprintf ( t , " \t \t \t \n " , a . DomainName , a . Type , fingerprint , logins ) } } return " \n \n \n " + t . String ( ) } fmt . Printf ( localCAsView ( ) ) if len ( trustedCAs ) > 0 { fmt . Printf ( trustedCAsView ( ) ) } return nil }
ListAuthorities shows list of user authorities we trust
ListAuthorities shows list of user authorities we trust
// by default show authorities of both types: // but if there was a --type switch, only select those: