Polish code
This commit is contained in:
parent
8af3ab3fb6
commit
0870f8113a
10 changed files with 246 additions and 238 deletions
97
sshutil/ssh.go
Normal file
97
sshutil/ssh.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package sshutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/AlexSSD7/linsk/utils"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func GenerateSSHKey() (ssh.Signer, []byte, error) {
|
||||
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "generate rsa private key")
|
||||
}
|
||||
|
||||
signer, err := ssh.NewSignerFromKey(privateKey)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(err, "create signer from key")
|
||||
}
|
||||
|
||||
return signer, ssh.MarshalAuthorizedKey(signer.PublicKey()), nil
|
||||
}
|
||||
|
||||
func RunSSHCmd(ctx context.Context, sc *ssh.Client, cmd string) ([]byte, error) {
|
||||
var ret []byte
|
||||
err := NewSSHSession(ctx, time.Second*15, sc, func(sess *ssh.Session) error {
|
||||
stdout := bytes.NewBuffer(nil)
|
||||
stderr := bytes.NewBuffer(nil)
|
||||
|
||||
sess.Stdout = stdout
|
||||
sess.Stderr = stderr
|
||||
|
||||
err := sess.Run(cmd)
|
||||
if err != nil {
|
||||
return utils.WrapErrWithLog(err, "run cmd", stderr.String())
|
||||
}
|
||||
|
||||
ret = stdout.Bytes()
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func NewSSHSession(ctx context.Context, timeout time.Duration, sc *ssh.Client, fn func(*ssh.Session) error) error {
|
||||
return NewSSHSessionWithDelayedTimeout(ctx, timeout, sc, func(sess *ssh.Session, startTimeout func()) error {
|
||||
startTimeout()
|
||||
return fn(sess)
|
||||
})
|
||||
}
|
||||
|
||||
func NewSSHSessionWithDelayedTimeout(ctx context.Context, timeout time.Duration, sc *ssh.Client, fn func(sess *ssh.Session, startTimeout func()) error) error {
|
||||
s, err := sc.NewSession()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create new ssh session")
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
|
||||
var timedOut bool
|
||||
|
||||
// Start a thread to handle context cancelation.
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timedOut = true
|
||||
_ = sc.Close()
|
||||
case <-done:
|
||||
}
|
||||
}()
|
||||
|
||||
err = fn(s, func() {
|
||||
// Now start a thread which will close the session
|
||||
// down when the timeout hits.
|
||||
go func() {
|
||||
select {
|
||||
case <-time.After(timeout):
|
||||
timedOut = true
|
||||
_ = sc.Close()
|
||||
case <-done:
|
||||
}
|
||||
}()
|
||||
})
|
||||
if timedOut {
|
||||
return fmt.Errorf("timed out (%w)", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
57
sshutil/unix.go
Normal file
57
sshutil/unix.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package sshutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/AlexSSD7/linsk/utils"
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/multierr"
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
func ChangeUnixPass(ctx context.Context, sc *ssh.Client, user string, pwd string) error {
|
||||
if !utils.ValidateUnixUsername(user) {
|
||||
return fmt.Errorf("invalid unix username")
|
||||
}
|
||||
|
||||
return NewSSHSession(ctx, time.Second*10, sc, func(sess *ssh.Session) error {
|
||||
stderr := bytes.NewBuffer(nil)
|
||||
sess.Stderr = stderr
|
||||
|
||||
stdinPipe, err := sess.StdinPipe()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "stdin pipe")
|
||||
}
|
||||
|
||||
err = sess.Start("passwd " + shellescape.Quote(user))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "start change user password cmd")
|
||||
}
|
||||
|
||||
pwdBytes := []byte(pwd + "\n")
|
||||
defer func() {
|
||||
// Clearing the memory up for security.
|
||||
for i := range pwdBytes {
|
||||
pwdBytes[i] = 0
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
// Writing the password. We're doing this two times
|
||||
// as we need to confirm the password.
|
||||
_, _ = stdinPipe.Write(pwdBytes)
|
||||
_, _ = stdinPipe.Write(pwdBytes)
|
||||
}()
|
||||
|
||||
err = sess.Wait()
|
||||
if err != nil {
|
||||
return multierr.Combine(utils.WrapErrWithLog(err, "wait for change user password cmd", stderr.String()))
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue