mirror of
https://github.com/kairos-io/kcrypt-challenger.git
synced 2025-07-14 07:34:08 +00:00
Extract method and simplify "if" logic
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
parent
83f529b53d
commit
eefd5f2c2c
@ -89,11 +89,27 @@ func (c *Client) waitPass(p *block.Partition, attempts int) (pass string, err er
|
|||||||
return rand, err
|
return rand, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for tries := 0; tries < attempts; tries++ {
|
for tries := 0; tries < attempts; tries++ {
|
||||||
var generated bool
|
var generated bool
|
||||||
pass, generated, err = getPass(challengeEndpoint, p)
|
pass, generated, err = getPass(challengeEndpoint, p)
|
||||||
if generated {
|
if generated { // passphrase is encrypted
|
||||||
// Decode what the challenger server gave us
|
return c.decryptPassphrase(pass)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil || err == errPartNotFound { // passphrase not encrypted or not available
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second) // network errors? retry
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// decryptPassphrase decodes (base64) and decrypts the passphrase returned
|
||||||
|
// by the challenger server.
|
||||||
|
func (c *Client) decryptPassphrase(pass string) (string, error) {
|
||||||
blob, err := base64.RawURLEncoding.DecodeString(pass)
|
blob, err := base64.RawURLEncoding.DecodeString(pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -107,18 +123,7 @@ func (c *Client) waitPass(p *block.Partition, attempts int) (pass string, err er
|
|||||||
if c.Config.Kcrypt.Challenger.TPMDevice != "" {
|
if c.Config.Kcrypt.Challenger.TPMDevice != "" {
|
||||||
opts = append(opts, tpm.WithDevice(c.Config.Kcrypt.Challenger.TPMDevice))
|
opts = append(opts, tpm.WithDevice(c.Config.Kcrypt.Challenger.TPMDevice))
|
||||||
}
|
}
|
||||||
pass, err := tpm.DecodeBlob(blob, opts...)
|
passBytes, err := tpm.DecodeBlob(blob, opts...)
|
||||||
return string(pass), err
|
|
||||||
}
|
|
||||||
|
|
||||||
if pass != "" || err == nil {
|
return string(passBytes), err
|
||||||
return
|
|
||||||
}
|
|
||||||
if err == errPartNotFound {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Otherwise, we might have a generic network error and we retry
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user