Reduce complexity for newHTTPSign()

Signed-off-by: Martijn van der Kleijn <martijn.niji@gmail.com>
This commit is contained in:
Martijn van der Kleijn 2024-06-14 23:22:15 +02:00
parent fffff7bd62
commit 0f7828d232

View file

@ -65,8 +65,31 @@ func NewHTTPSignWithCert(principal, sshKey, passphrase string) (*HTTPSign, error
// Depending on the configuration it will either use a certificate or a public key // Depending on the configuration it will either use a certificate or a public key
func newHTTPSign(config *HTTPSignConfig) (*HTTPSign, error) { func newHTTPSign(config *HTTPSignConfig) (*HTTPSign, error) {
var signer ssh.Signer var signer ssh.Signer
var err error
if config.sshKey != "" { if config.sshKey != "" {
signer, err = getSignerFromFile(config)
if err != nil {
return nil, err
}
} else {
signer, err = getSignerFromAgent(config)
if err != nil {
return nil, err
}
}
return &HTTPSign{
Signer: signer,
cert: config.cert,
}, nil
}
// getSignerFromFile gets a signer from a given file
func getSignerFromFile(config *HTTPSignConfig) (ssh.Signer, error) {
var signer ssh.Signer
var err error
priv, err := os.ReadFile(config.sshKey) priv, err := os.ReadFile(config.sshKey)
if err != nil { if err != nil {
return nil, err return nil, err
@ -105,8 +128,14 @@ func newHTTPSign(config *HTTPSignConfig) (*HTTPSign, error) {
return nil, err return nil, err
} }
} }
} else {
// if no sshKey is specified, check if we have a ssh-agent and use it return signer, nil
}
// getSignerFromAgent checks if we have an ssh-agent and uses it
func getSignerFromAgent(config *HTTPSignConfig) (ssh.Signer, error) {
var signer ssh.Signer
agent, err := GetAgent() agent, err := GetAgent()
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,12 +163,8 @@ func newHTTPSign(config *HTTPSignConfig) (*HTTPSign, error) {
return nil, fmt.Errorf("no public key found for %s", config.fingerprint) return nil, fmt.Errorf("no public key found for %s", config.fingerprint)
} }
} }
}
return &HTTPSign{ return signer, nil
Signer: signer,
cert: config.cert,
}, nil
} }
// SignRequest signs a HTTP request // SignRequest signs a HTTP request