Files
teleport/lib/client/identity.go
T
Sasha Klizhentas a1116b7783 Set some defaults from identity file.
This commit reduces the amount of typing some users have
to do when using identity file:

* Teleport user is set from the certificate
* Auth preference is set to local

In addition it fixes several UX problems:

* Commands `tctl auth sign` and `tsh login -o`  now include
trusted CA keys in the identity file.

* Command `tsh ssh -i` is now noninteractive and only
uses identity file for authentication, in case
if identity file can not authenticate, no other interactive
or non-interactive authentication methods will be used.
Before this commit, tsh tried to fallback to interactive
login mode or read the keys from agent socket.
2018-09-13 16:47:14 -07:00

128 lines
3.5 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 client
import (
"io"
"io/ioutil"
"os"
"github.com/gravitational/teleport/lib/auth/native"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/trace"
)
// NewKey generates a new unsigned key. Such key must be signed by a
// Teleport CA (auth server) before it becomes useful.
func NewKey() (key *Key, err error) {
priv, pub, err := native.GenerateKeyPair("")
if err != nil {
return nil, trace.Wrap(err)
}
return &Key{
Priv: priv,
Pub: pub,
}, nil
}
// IdentityFileFormat describes possible file formats how a user identity can be sotred
type IdentityFileFormat string
const (
// IdentityFormatFile is when a key + cert are stored concatenated into a single file
IdentityFormatFile IdentityFileFormat = "file"
// IdentityFormatOpenSSH is OpenSSH-compatible format, when a key and a cert are stored in
// two different files (in the same directory)
IdentityFormatOpenSSH IdentityFileFormat = "openssh"
// DefaultIdentityFormat is what Teleport uses by default
DefaultIdentityFormat = IdentityFormatFile
)
// MakeIdentityFile takes a username + their credentials and saves them to disk
// in a specified format
func MakeIdentityFile(filePath string, key *Key, format IdentityFileFormat, certAuthorities []services.CertAuthority) (err error) {
const (
// the files and the dir will be created with these permissions:
fileMode = 0600
dirMode = 0700
)
if filePath == "" {
return trace.BadParameter("identity location is not specified")
}
var output io.Writer = os.Stdout
switch format {
// dump user identity into a single file:
case IdentityFormatFile:
f, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, fileMode)
if err != nil {
return trace.Wrap(err)
}
output = f
defer f.Close()
// write key:
if _, err = output.Write(key.Priv); err != nil {
return trace.Wrap(err)
}
// append cert:
if _, err = output.Write(key.Cert); err != nil {
return trace.Wrap(err)
}
// append trusted host certificate authorities
for _, ca := range certAuthorities {
for _, publicKey := range ca.GetCheckingKeys() {
data, err := sshutils.MarshalAuthorizedHostsFormat(ca.GetClusterName(), publicKey, nil)
if err != nil {
return trace.Wrap(err)
}
if _, err = output.Write([]byte(data)); err != nil {
return trace.Wrap(err)
}
if _, err = output.Write([]byte("\n")); err != nil {
return trace.Wrap(err)
}
}
}
// dump user identity into separate files:
case IdentityFormatOpenSSH:
keyPath := filePath
certPath := keyPath + "-cert.pub"
err = ioutil.WriteFile(certPath, key.Cert, fileMode)
if err != nil {
return trace.Wrap(err)
}
err = ioutil.WriteFile(keyPath, key.Priv, fileMode)
if err != nil {
return trace.Wrap(err)
}
default:
return trace.BadParameter("unsupported identity format: %q, use either %q or %q",
format, IdentityFormatFile, IdentityFormatOpenSSH)
}
return nil
}