Refactor away some unnecessary type shuffling
Signatures in particular were read in from both cache and url as `[]byte`, converted to `string`, then back to `[]byte` to pass through to minisign. Lists themselves will be converted to `string` by the parsing code anyway.
This commit is contained in:
parent
ad92be5b9c
commit
fe34d07b68
|
@ -32,14 +32,14 @@ const (
|
||||||
type Source struct {
|
type Source struct {
|
||||||
urls []string
|
urls []string
|
||||||
format SourceFormat
|
format SourceFormat
|
||||||
in string
|
in []byte
|
||||||
minisignKey *minisign.PublicKey
|
minisignKey *minisign.PublicKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (source *Source) checkSignature(bin, sig string) (err error) {
|
func (source *Source) checkSignature(bin, sig []byte) (err error) {
|
||||||
var signature minisign.Signature
|
var signature minisign.Signature
|
||||||
if signature, err = minisign.DecodeSignature(sig); err == nil {
|
if signature, err = minisign.DecodeSignature(string(sig)); err == nil {
|
||||||
_, err = source.minisignKey.Verify([]byte(bin), signature)
|
_, err = source.minisignKey.Verify(bin, signature)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ func (source *Source) checkSignature(bin, sig string) (err error) {
|
||||||
// timeNow can be replaced by tests to provide a static value
|
// timeNow can be replaced by tests to provide a static value
|
||||||
var timeNow = time.Now
|
var timeNow = time.Now
|
||||||
|
|
||||||
func fetchFromCache(cacheFile string, refreshDelay time.Duration) (in string, expired bool, delayTillNextUpdate time.Duration, err error) {
|
func fetchFromCache(cacheFile string, refreshDelay time.Duration) (bin []byte, expired bool, delayTillNextUpdate time.Duration, err error) {
|
||||||
expired = false
|
expired = false
|
||||||
delayTillNextUpdate = time.Duration(0)
|
delayTillNextUpdate = time.Duration(0)
|
||||||
if refreshDelay < MinSourcesUpdateDelay {
|
if refreshDelay < MinSourcesUpdateDelay {
|
||||||
|
@ -58,12 +58,9 @@ func fetchFromCache(cacheFile string, refreshDelay time.Duration) (in string, ex
|
||||||
dlog.Debugf("Cache file [%s] not present", cacheFile)
|
dlog.Debugf("Cache file [%s] not present", cacheFile)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var bin []byte
|
if bin, err = ioutil.ReadFile(cacheFile); err != nil {
|
||||||
bin, err = ioutil.ReadFile(cacheFile)
|
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
in = string(bin)
|
|
||||||
elapsed := timeNow().Sub(fi.ModTime())
|
elapsed := timeNow().Sub(fi.ModTime())
|
||||||
if elapsed < refreshDelay {
|
if elapsed < refreshDelay {
|
||||||
dlog.Debugf("Cache file [%s] is still fresh", cacheFile)
|
dlog.Debugf("Cache file [%s] is still fresh", cacheFile)
|
||||||
|
@ -84,9 +81,9 @@ func fetchFromURL(xTransport *XTransport, u *url.URL) (bin []byte, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, refreshDelay time.Duration) (in string, delayTillNextUpdate time.Duration, err error) {
|
func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, refreshDelay time.Duration) (bin []byte, delayTillNextUpdate time.Duration, err error) {
|
||||||
expired := false
|
expired := false
|
||||||
in, expired, delayTillNextUpdate, err = fetchFromCache(cacheFile, refreshDelay)
|
bin, expired, delayTillNextUpdate, err = fetchFromCache(cacheFile, refreshDelay)
|
||||||
if err == nil && !expired {
|
if err == nil && !expired {
|
||||||
dlog.Debugf("Delay till next update: %v", delayTillNextUpdate)
|
dlog.Debugf("Delay till next update: %v", delayTillNextUpdate)
|
||||||
return
|
return
|
||||||
|
@ -104,7 +101,6 @@ func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, ref
|
||||||
if u, err = url.Parse(urlStr); err != nil {
|
if u, err = url.Parse(urlStr); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var bin []byte
|
|
||||||
if bin, err = fetchFromURL(xTransport, u); err != nil {
|
if bin, err = fetchFromURL(xTransport, u); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -113,7 +109,6 @@ func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, ref
|
||||||
dlog.Warnf("%s: %s", absPath, err)
|
dlog.Warnf("%s: %s", absPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
in = string(bin)
|
|
||||||
delayTillNextUpdate = MinSourcesUpdateDelay
|
delayTillNextUpdate = MinSourcesUpdateDelay
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -144,19 +139,19 @@ func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cac
|
||||||
now := timeNow()
|
now := timeNow()
|
||||||
sigCacheFile := cacheFile + ".minisig"
|
sigCacheFile := cacheFile + ".minisig"
|
||||||
|
|
||||||
var sigStr, in string
|
var bin, sig []byte
|
||||||
var delayTillNextUpdate, sigDelayTillNextUpdate time.Duration
|
var delayTillNextUpdate, sigDelayTillNextUpdate time.Duration
|
||||||
var err, sigErr error
|
var err, sigErr error
|
||||||
var preloadURL string
|
var preloadURL string
|
||||||
if len(urls) <= 0 {
|
if len(urls) <= 0 {
|
||||||
in, delayTillNextUpdate, err = fetchWithCache(xTransport, "", cacheFile, refreshDelay)
|
bin, delayTillNextUpdate, err = fetchWithCache(xTransport, "", cacheFile, refreshDelay)
|
||||||
sigStr, sigDelayTillNextUpdate, sigErr = fetchWithCache(xTransport, "", sigCacheFile, refreshDelay)
|
sig, sigDelayTillNextUpdate, sigErr = fetchWithCache(xTransport, "", sigCacheFile, refreshDelay)
|
||||||
} else {
|
} else {
|
||||||
preloadURL = urls[0]
|
preloadURL = urls[0]
|
||||||
for _, url := range urls {
|
for _, url := range urls {
|
||||||
sigURL := url + ".minisig"
|
sigURL := url + ".minisig"
|
||||||
in, delayTillNextUpdate, err = fetchWithCache(xTransport, url, cacheFile, refreshDelay)
|
bin, delayTillNextUpdate, err = fetchWithCache(xTransport, url, cacheFile, refreshDelay)
|
||||||
sigStr, sigDelayTillNextUpdate, sigErr = fetchWithCache(xTransport, sigURL, sigCacheFile, refreshDelay)
|
sig, sigDelayTillNextUpdate, sigErr = fetchWithCache(xTransport, sigURL, sigCacheFile, refreshDelay)
|
||||||
if err == nil && sigErr == nil {
|
if err == nil && sigErr == nil {
|
||||||
preloadURL = url
|
preloadURL = url
|
||||||
break
|
break
|
||||||
|
@ -177,11 +172,11 @@ func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cac
|
||||||
return source, urlsToPrefetch, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = source.checkSignature(in, sigStr); err != nil {
|
if err = source.checkSignature(bin, sig); err != nil {
|
||||||
return source, urlsToPrefetch, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
dlog.Noticef("Source [%s] loaded", cacheFile)
|
dlog.Noticef("Source [%s] loaded", cacheFile)
|
||||||
source.in = in
|
source.in = bin
|
||||||
return source, urlsToPrefetch, nil
|
return source, urlsToPrefetch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,8 @@ type SourceTestData struct {
|
||||||
|
|
||||||
type SourceTestExpect struct {
|
type SourceTestExpect struct {
|
||||||
success, download bool
|
success, download bool
|
||||||
in, cachePath string
|
in []byte
|
||||||
|
cachePath string
|
||||||
cache []SourceFixture
|
cache []SourceFixture
|
||||||
refresh time.Time
|
refresh time.Time
|
||||||
urls []string
|
urls []string
|
||||||
|
@ -248,9 +249,9 @@ func prepSourceTestCache(t *testing.T, d *SourceTestData, e *SourceTestExpect, s
|
||||||
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
|
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
|
||||||
switch state {
|
switch state {
|
||||||
case TestStateCorrect:
|
case TestStateCorrect:
|
||||||
e.in, e.success, e.refresh = string(e.cache[0].content), true, d.timeUpd
|
e.in, e.success, e.refresh = e.cache[0].content, true, d.timeUpd
|
||||||
case TestStateExpired:
|
case TestStateExpired:
|
||||||
e.in = string(e.cache[0].content)
|
e.in = e.cache[0].content
|
||||||
case TestStatePartial, TestStatePartialSig:
|
case TestStatePartial, TestStatePartialSig:
|
||||||
e.err = "signature"
|
e.err = "signature"
|
||||||
case TestStateMissing, TestStateMissingSig:
|
case TestStateMissing, TestStateMissingSig:
|
||||||
|
@ -269,7 +270,7 @@ func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect
|
||||||
switch state {
|
switch state {
|
||||||
case TestStateCorrect:
|
case TestStateCorrect:
|
||||||
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
|
e.cache = []SourceFixture{d.fixtures[state][source], d.fixtures[state][source+".minisig"]}
|
||||||
e.in, e.success, e.refresh = string(e.cache[0].content), true, d.timeUpd
|
e.in, e.success, e.refresh = e.cache[0].content, true, d.timeUpd
|
||||||
fallthrough
|
fallthrough
|
||||||
case TestStateMissingSig, TestStatePartial, TestStatePartialSig, TestStateReadSigErr:
|
case TestStateMissingSig, TestStatePartial, TestStatePartialSig, TestStateReadSigErr:
|
||||||
d.reqExpect[path+".minisig"]++
|
d.reqExpect[path+".minisig"]++
|
||||||
|
|
Loading…
Reference in New Issue