1
0
Fork 0
mirror of https://github.com/Luzifer/shareport.git synced 2024-11-08 06:10:06 +00:00

Lint: Fix several linter advices

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-11-28 23:52:35 +01:00
parent 6a20dff463
commit d063216324
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 38 additions and 36 deletions

25
main.go
View file

@ -121,8 +121,10 @@ func main() {
} }
config := &ssh.ClientConfig{ config := &ssh.ClientConfig{
User: cfg.RemoteUser, User: cfg.RemoteUser,
Auth: []ssh.AuthMethod{privateKey}, Auth: []ssh.AuthMethod{privateKey},
//#nosec G106 // For now no validation is supported
HostKeyCallback: ssh.InsecureIgnoreHostKey(), HostKeyCallback: ssh.InsecureIgnoreHostKey(),
} }
@ -212,15 +214,14 @@ func main() {
sigC <- syscall.SIGINT sigC <- syscall.SIGINT
}() }()
for { // Wait for signal to occur
select { <-sigC
case <-sigC:
log.Info("Signal triggered, shutting down") // Do a proper teardown
if err := session.Signal(ssh.SIGHUP); err != nil { log.Info("Signal triggered, shutting down")
log.WithError(err).Error("Unable to send TERM signal to remote process") running = false
}
running = false if err := session.Signal(ssh.SIGHUP); err != nil {
return log.WithError(err).Error("Unable to send TERM signal to remote process")
}
} }
} }

View file

@ -3,19 +3,18 @@ package main
import ( import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors"
"fmt"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
) )
func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) { func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
var err error
// read pem block // read pem block
err := errors.New("Pem decode failed, no key found")
pemBlock, _ := pem.Decode(pemBytes) pemBlock, _ := pem.Decode(pemBytes)
if pemBlock == nil { if pemBlock == nil {
return nil, err return nil, errors.New("Pem decode failed, no key found")
} }
// handle encrypted key // handle encrypted key
@ -23,7 +22,7 @@ func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
// decrypt PEM // decrypt PEM
pemBlock.Bytes, err = x509.DecryptPEMBlock(pemBlock, password) pemBlock.Bytes, err = x509.DecryptPEMBlock(pemBlock, password)
if err != nil { if err != nil {
return nil, fmt.Errorf("Decrypting PEM block failed %v", err) return nil, errors.Wrap(err, "Decrypting PEM block failed")
} }
// get RSA, EC or DSA key // get RSA, EC or DSA key
@ -35,45 +34,47 @@ func signerFromPem(pemBytes []byte, password []byte) (ssh.Signer, error) {
// generate signer instance from key // generate signer instance from key
signer, err := ssh.NewSignerFromKey(key) signer, err := ssh.NewSignerFromKey(key)
if err != nil { if err != nil {
return nil, fmt.Errorf("Creating signer from encrypted key failed %v", err) return nil, errors.Wrap(err, "Creating signer from encrypted key failed")
}
return signer, nil
} else {
// generate signer instance from plain key
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, fmt.Errorf("Parsing plain private key failed %v", err)
} }
return signer, nil return signer, nil
} }
// generate signer instance from plain key
signer, err := ssh.ParsePrivateKey(pemBytes)
if err != nil {
return nil, errors.Wrap(err, "Parsing plain private key failed")
}
return signer, nil
} }
func parsePemBlock(block *pem.Block) (interface{}, error) { func parsePemBlock(block *pem.Block) (interface{}, error) {
switch block.Type { switch block.Type {
case "RSA PRIVATE KEY": case "RSA PRIVATE KEY":
key, err := x509.ParsePKCS1PrivateKey(block.Bytes) key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing PKCS private key failed %v", err) return nil, errors.Wrap(err, "Parsing PKCS private key failed")
} else {
return key, nil
} }
return key, nil
case "EC PRIVATE KEY": case "EC PRIVATE KEY":
key, err := x509.ParseECPrivateKey(block.Bytes) key, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing EC private key failed %v", err) return nil, errors.Wrap(err, "Parsing EC private key failed")
} else {
return key, nil
} }
return key, nil
case "DSA PRIVATE KEY": case "DSA PRIVATE KEY":
key, err := ssh.ParseDSAPrivateKey(block.Bytes) key, err := ssh.ParseDSAPrivateKey(block.Bytes)
if err != nil { if err != nil {
return nil, fmt.Errorf("Parsing DSA private key failed %v", err) return nil, errors.Wrap(err, "Parsing DSA private key failed")
} else {
return key, nil
} }
return key, nil
default: default:
return nil, fmt.Errorf("Parsing private key failed, unsupported key type %q", block.Type) return nil, errors.Errorf("Parsing private key failed, unsupported key type %q", block.Type)
} }
} }