Add SSH Key Management
This commit is contained in:
parent
ef76844922
commit
bcd1805001
62 changed files with 1004 additions and 188 deletions
45
internal/models/keys.go
Normal file
45
internal/models/keys.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"github.com/gliderlabs/ssh"
|
||||
gossh "golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// KeysTable is the name of the table the [Keys] model is stored in.
|
||||
const KeysTable = "keys"
|
||||
|
||||
// Keys represents a distillery ssh key
|
||||
type Keys struct {
|
||||
Pk uint `gorm:"column:pk;primaryKey"`
|
||||
|
||||
User string `gorm:"column:user;not null"` // username of the ssh key
|
||||
|
||||
Signature []byte `gorm:"column:signature;not null"` // signature of the ssh key
|
||||
Comment string `gorm:"column:comment"`
|
||||
}
|
||||
|
||||
// PublicKey returns the public key corresponding to this keys.
|
||||
// If the key cannot be parsed, returns nil.
|
||||
func (keys *Keys) PublicKey() ssh.PublicKey {
|
||||
key, err := ssh.ParsePublicKey(keys.Signature)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
func (keys *Keys) SignatureString() string {
|
||||
// try to get the public key
|
||||
key := keys.PublicKey()
|
||||
if key == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// marshal the key!
|
||||
return string(gossh.MarshalAuthorizedKey(key))
|
||||
}
|
||||
|
||||
// SetPublicKey stores a specific public key in this key
|
||||
func (keys *Keys) SetPublicKey(key ssh.PublicKey) {
|
||||
keys.Signature = key.Marshal()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue