Reduce complexity for newHTTPSign()
Signed-off-by: Martijn van der Kleijn <martijn.niji@gmail.com>
This commit is contained in:
parent
fffff7bd62
commit
0f7828d232
1 changed files with 85 additions and 60 deletions
|
@ -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
|
||||
func newHTTPSign(config *HTTPSignConfig) (*HTTPSign, error) {
|
||||
var signer ssh.Signer
|
||||
var err error
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -105,8 +128,14 @@ func newHTTPSign(config *HTTPSignConfig) (*HTTPSign, error) {
|
|||
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()
|
||||
if err != nil {
|
||||
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 &HTTPSign{
|
||||
Signer: signer,
|
||||
cert: config.cert,
|
||||
}, nil
|
||||
return signer, nil
|
||||
}
|
||||
|
||||
// SignRequest signs a HTTP request
|
||||
|
|
Loading…
Reference in a new issue