mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-24 16:17:11 +08:00
Summary: Sasha proposed to use the certificate principal instead of the host name when establishing new SSH connections. What I did: Replaced `ReadKeys()` function in `auth/init.go` with `ReadIdentity()` which, instead of a simple "key signer" returns a more comprehensive structure called "Identity" The structure has the `Cert` field which can be used to obtain "valid principals". The first principal is used as an SSH username, instead of the hostname like before.
154 lines
4.0 KiB
Go
154 lines
4.0 KiB
Go
/*
|
|
Copyright 2016 Gravitational, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log/syslog"
|
|
"os"
|
|
|
|
"github.com/gravitational/kingpin"
|
|
"github.com/gravitational/trace"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
logrusSyslog "github.com/Sirupsen/logrus/hooks/syslog"
|
|
)
|
|
|
|
// CLI tools by default log into syslog, not stderr
|
|
func InitLoggerCLI() {
|
|
log.SetLevel(log.InfoLevel)
|
|
// clear existing hooks:
|
|
log.StandardLogger().Hooks = make(log.LevelHooks)
|
|
log.SetFormatter(&trace.TextFormatter{})
|
|
|
|
hook, err := logrusSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
|
|
if err != nil {
|
|
// syslog not available
|
|
log.Warn("syslog not available. reverting to stderr")
|
|
} else {
|
|
// ... and disable stderr:
|
|
log.AddHook(hook)
|
|
log.SetOutput(ioutil.Discard)
|
|
}
|
|
}
|
|
|
|
// Configures the logger to dump everything to stderr
|
|
func InitLoggerDebug() {
|
|
// clear existing hooks:
|
|
log.StandardLogger().Hooks = make(log.LevelHooks)
|
|
log.SetFormatter(&trace.TextFormatter{})
|
|
log.SetOutput(os.Stderr)
|
|
log.SetLevel(log.DebugLevel)
|
|
}
|
|
|
|
// FatalError is for CLI front-ends: it detects gravitational.Trace debugging
|
|
// information, sends it to the logger, strips it off and prints a clean message to stderr
|
|
func FatalError(err error) {
|
|
var unwrap func(error) error
|
|
unwrap = func(err error) error {
|
|
te, ok := err.(trace.Error)
|
|
if ok {
|
|
return unwrap(te.OrigError())
|
|
}
|
|
return err
|
|
}
|
|
log.Errorf(err.Error())
|
|
fmt.Fprintln(os.Stderr, "Error: "+unwrap(err).Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Consolef prints the same message to a 'ui console' (if defined) and also to
|
|
// the logger with INFO priority
|
|
func Consolef(w io.Writer, msg string, params ...interface{}) {
|
|
msg = fmt.Sprintf(msg, params...)
|
|
if w != nil {
|
|
fmt.Fprintln(w, msg)
|
|
}
|
|
log.Info(msg)
|
|
}
|
|
|
|
// InitCLIParser configures kingpin command line args parser with
|
|
// some defaults common for all Teleport CLI tools
|
|
func InitCLIParser(appName, appHelp string) (app *kingpin.Application) {
|
|
app = kingpin.New(appName, appHelp)
|
|
|
|
// hide "--help" flag
|
|
app.HelpFlag.Hidden()
|
|
app.HelpFlag.NoEnvar()
|
|
|
|
// set our own help template
|
|
return app.UsageTemplate(defaultUsageTemplate)
|
|
}
|
|
|
|
// Usage template with compactly formatted commands.
|
|
var defaultUsageTemplate = `{{define "FormatCommand"}}\
|
|
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
|
|
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
|
|
{{end}}\
|
|
|
|
{{define "FormatCommands"}}\
|
|
{{range .FlattenedCommands}}\
|
|
{{if not .Hidden}}\
|
|
{{.FullCommand | printf "%-12s" }}{{if .Default}}*{{end}} {{ .Help }}
|
|
{{end}}\
|
|
{{end}}\
|
|
{{end}}\
|
|
|
|
{{define "FormatUsage"}}\
|
|
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
|
|
{{if .Help}}
|
|
{{.Help|Wrap 0}}\
|
|
{{end}}\
|
|
|
|
{{end}}\
|
|
|
|
{{if .Context.SelectedCommand}}\
|
|
usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatUsage" .Context.SelectedCommand}}
|
|
{{else}}\
|
|
Usage: {{.App.Name}}{{template "FormatUsage" .App}}
|
|
{{end}}\
|
|
{{if .Context.Flags}}\
|
|
Flags:
|
|
{{.Context.Flags|FlagsToTwoColumnsCompact|FormatTwoColumns}}
|
|
{{end}}\
|
|
{{if .Context.Args}}\
|
|
Args:
|
|
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
|
|
{{end}}\
|
|
{{if .Context.SelectedCommand}}\
|
|
|
|
{{ if .Context.SelectedCommand.Commands}}\
|
|
Commands:
|
|
{{if .Context.SelectedCommand.Commands}}\
|
|
{{template "FormatCommands" .Context.SelectedCommand}}
|
|
{{end}}\
|
|
{{end}}\
|
|
|
|
{{else if .App.Commands}}\
|
|
Commands:
|
|
{{template "FormatCommands" .App}}
|
|
Try '{{.App.Name}} help [command]' to get help for a given command.
|
|
{{end}}\
|
|
|
|
{{ if .Context.SelectedCommand }}\
|
|
{{ range .Context.SelectedCommand.Aliases}}\
|
|
{{ . }}
|
|
{{end}}\
|
|
{{end}}
|
|
`
|