Skip to content

Commit

Permalink
Merge pull request #2119 from ripienaar/2118
Browse files Browse the repository at this point in the history
(#2118) Various archive watcher improvements
  • Loading branch information
ripienaar authored Feb 25, 2024
2 parents 531f286 + 106bf91 commit 23c0ec8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
31 changes: 24 additions & 7 deletions aagent/watchers/archivewatcher/archive.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2021-2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -172,7 +172,7 @@ func (w *Watcher) verifyCreates() (string, State, error) {

checksums := filepath.Join(creates, w.properties.ContentChecksums)
if !iu.FileExist(checksums) {
w.Errorf("Checksums file %s does not exist in %s, triggering download: %s", creates, w.properties.ContentChecksums)
w.Errorf("Checksums file %s does not exist in %s, triggering download: %s", checksums, w.properties.ContentChecksums)
return creates, MissingChecksums, nil
}

Expand Down Expand Up @@ -316,12 +316,12 @@ func (w *Watcher) verify(dir string) error {
return fmt.Errorf("checksums file %s does not exist in the archive (%s)", w.properties.ContentChecksums, sumsFile)
}

ok, _, err := iu.FileHasSha256Sum(sumsFile, ccc)
ok, sum, err := iu.FileHasSha256Sum(sumsFile, ccc)
if err != nil {
return fmt.Errorf("failed to checksum file %s: %s", w.properties.ContentChecksums, err)
}
if !ok {
return fmt.Errorf("checksum file %s has an invalid checksum", w.properties.ContentChecksums)
return fmt.Errorf("checksum file %s has an invalid checksum %q != %q", w.properties.ContentChecksums, sum, ccc)
}

ok, err = iu.Sha256VerifyDir(sumsFile, dir, nil, func(file string, ok bool) {
Expand Down Expand Up @@ -466,7 +466,16 @@ func (w *Watcher) downloadSourceToTemp(ctx context.Context) (string, error) {
req.Header.Add("User-Agent", fmt.Sprintf("Choria Archive Watcher %s", build.Version))

if w.properties.Username != "" {
req.SetBasicAuth(w.properties.Username, w.properties.Password)
user, err := w.ProcessTemplate(w.properties.Username)
if err != nil {
return fmt.Errorf("invalid username template: %v", err)
}
pass, err := w.ProcessTemplate(w.properties.Password)
if err != nil {
return fmt.Errorf("invalid password template: %v", err)
}

req.SetBasicAuth(user, pass)
}

resp, err := client.Do(req)
Expand All @@ -482,12 +491,12 @@ func (w *Watcher) downloadSourceToTemp(ctx context.Context) (string, error) {

tf.Close()

ok, _, err := iu.FileHasSha256Sum(tf.Name(), sourceChecksum)
ok, sum, err := iu.FileHasSha256Sum(tf.Name(), sourceChecksum)
if err != nil {
return fmt.Errorf("archive checksum calculation failed: %s", err)
}
if !ok {
return fmt.Errorf("archive checksum missmatch")
return fmt.Errorf("archive checksum %s != %s missmatch", sum, sourceChecksum)
}

return nil
Expand Down Expand Up @@ -555,6 +564,14 @@ func (w *Watcher) handleCheck(s State, err error) error {
w.NotifyWatcherState(w.CurrentState())
return w.SuccessTransition()

case VerifiedOK:
w.NotifyWatcherState(w.CurrentState())
return w.SuccessTransition()

case VerifyFailed:
w.NotifyWatcherState(w.CurrentState())
return w.FailureTransition()

}

return nil
Expand Down
4 changes: 2 additions & 2 deletions aagent/watchers/archivewatcher/archive_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, R.I. Pienaar and the Choria Project contributors
// Copyright (c) 2021-2024, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -106,7 +106,7 @@ var _ = Describe("AAgent/Watchers/MachinesWatcher", func() {

It("Should handle bad checksums", func() {
w.properties.ContentChecksumsChecksum = "x"
Expect(w.verify("testdata/good")).To(MatchError("checksum file SHA256SUMS has an invalid checksum"))
Expect(w.verify("testdata/good")).To(MatchError("checksum file SHA256SUMS has an invalid checksum \"40cb790b7199be45f3116354f87b2bdc3aa520a1eb056aa3608911cf40d1f821\" != \"x\""))
})
})

Expand Down

0 comments on commit 23c0ec8

Please sign in to comment.