Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Updated pkgs: switched psigning from perrors to errors and fixed some…
Browse files Browse the repository at this point in the history
… file closing for unpackage
  • Loading branch information
tiffanyfay committed Oct 22, 2015
1 parent 77cb347 commit 19f88f7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 56 deletions.
16 changes: 8 additions & 8 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type catalogsMetrics interface {
}

type managesSigning interface {
ValidateSignature(keyringFile string, signedFile string, signatureFile string) perror.PulseError
ValidateSignature(keyringFile string, signedFile string, signatureFile string) error
}

type ControlOpt func(*pluginControl)
Expand Down Expand Up @@ -254,17 +254,17 @@ func (p *pluginControl) Load(path string) (core.CatalogedPlugin, perror.PulseErr
err := p.signingManager.ValidateSignature(p.keyringFile, path, signatureFile)
if err != nil {
if p.pluginTrust == 1 {
return nil, err
return nil, perror.New(err)
}
controlLogger.WithFields(f).Error(err)
} else {
signed = true
}
}

path, _, e := unpackage.Unpackager(path)
if e != nil {
return nil, perror.New(e)
path, _, err := unpackage.Unpackager(path)
if err != nil {
return nil, perror.New(err)
}

controlLogger.WithFields(f).Info("plugin load called")
Expand All @@ -275,9 +275,9 @@ func (p *pluginControl) Load(path string) (core.CatalogedPlugin, perror.PulseErr
return nil, pe
}

pl, err := p.pluginManager.LoadPlugin(path, p.eventManager)
if err != nil {
return nil, err
pl, pe := p.pluginManager.LoadPlugin(path, p.eventManager)
if pe != nil {
return nil, pe
}
pl.Signed = signed

Expand Down
4 changes: 2 additions & 2 deletions control/control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ type mocksigningManager struct {
signed bool
}

func (ps *mocksigningManager) ValidateSignature(string, string, string) perror.PulseError {
func (ps *mocksigningManager) ValidateSignature(string, string, string) error {
if ps.signed {
return nil
}
return perror.New(errors.New("fake"))
return errors.New("fake")
}

// Uses the dummy collector plugin to simulate Loading
Expand Down
47 changes: 6 additions & 41 deletions pkg/psigning/psigning.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import (
"os"
"time"

log "github.com/Sirupsen/logrus"

"github.com/intelsdi-x/pulse/core/perror"
"golang.org/x/crypto/openpgp"
)

Expand All @@ -43,65 +40,33 @@ var (
)

//ValidateSignature is exported for plugin authoring
func (s *SigningManager) ValidateSignature(keyringFile, signedFile, signatureFile string) perror.PulseError {
smLogger := log.WithFields(log.Fields{
"_block": "ValidateSignature",
"_module": "psigning",
})
func (s *SigningManager) ValidateSignature(keyringFile, signedFile, signatureFile string) error {
keyringf, err := os.Open(keyringFile)
if err != nil {
fields := map[string]interface{}{
"error": err,
"file": keyringFile,
}
pe := perror.New(ErrKeyringFileNotFound, fields)
smLogger.WithFields(fields).Error(ErrKeyringFileNotFound)
return pe
return fmt.Errorf("%v: %v\n%v", ErrKeyringFileNotFound, keyringFile, err)
}
defer keyringf.Close()

keyring, err := openpgp.ReadKeyRing(keyringf)
if err != nil {
fields := map[string]interface{}{
"error": err,
}
pe := perror.New(ErrUnableToReadKeyring, fields)
smLogger.WithFields(fields).Error(ErrUnableToReadKeyring)
return pe
return fmt.Errorf("%v: %v\n%v", ErrUnableToReadKeyring, keyringFile, err)
}

signed, err := os.Open(signedFile)
if err != nil {
fields := map[string]interface{}{
"error": err,
"file": signedFile,
}
pe := perror.New(ErrSignedFileNotFound, fields)
smLogger.WithFields(fields).Error(ErrSignedFileNotFound)
return pe
return fmt.Errorf("%v: %v\n%v", ErrSignedFileNotFound, signedFile, err)
}
defer signed.Close()

signature, err := os.Open(signatureFile)
if err != nil {
fields := map[string]interface{}{
"error": err,
"file": signatureFile,
}
pe := perror.New(ErrSignatureFileNotFound, fields)
smLogger.WithFields(fields).Error(ErrSignatureFileNotFound)
return pe
return fmt.Errorf("%v: %v\n%v", ErrSignatureFileNotFound, signatureFile, err)
}
defer signature.Close()

checked, err := openpgp.CheckArmoredDetachedSignature(keyring, signed, signature)
if err != nil {
fields := map[string]interface{}{
"error": err,
}
pe := perror.New(ErrCheckSignature, fields)
smLogger.WithFields(fields).Error(ErrCheckSignature)
return pe
return fmt.Errorf("%v\n%v", ErrCheckSignature, err)
}

var signedby string
Expand Down
12 changes: 8 additions & 4 deletions pkg/psigning/psigning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ func TestValidateSignature(t *testing.T) {

Convey("Valid files and bad signature", t, func() {
err := s.ValidateSignature(keyringFile, unsignedFile, signatureFile)
So(err.Error(), ShouldResemble, "Error checking signature")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Error checking signature")
})

Convey("Invalid keyring", t, func() {
err := s.ValidateSignature("", signedFile, signatureFile)
So(err.Error(), ShouldResemble, "Keyring file (.gpg) not found")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Keyring file (.gpg) not found")
})

Convey("Invalid signed file", t, func() {
err := s.ValidateSignature(keyringFile, "", signatureFile)
So(err.Error(), ShouldResemble, "Signed file not found")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Signed file not found")
})

Convey("Invalid signature file", t, func() {
err := s.ValidateSignature(keyringFile, signedFile, "")
So(err.Error(), ShouldResemble, "Signature file (.asc) not found. Did you use the -a flag?")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Signature file (.asc) not found. Did you use the -a flag?")
})
}
3 changes: 2 additions & 1 deletion pkg/unpackage/unpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func Untar(path string, reader *bufio.Reader) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("%v: %v\n%v", ErrCreatingFile, file, err)
}
defer w.Close()
_, err = io.Copy(w, tr)
if err != nil {
return nil, fmt.Errorf("%v: %v\n%v", ErrCopyingFile, file, err)
Expand All @@ -162,7 +163,6 @@ func Untar(path string, reader *bufio.Reader) ([]byte, error) {
return nil, fmt.Errorf("%v: %v\n%v", ErrReadManifest, file, err)
}
}
w.Close()
case tar.TypeDir:
//Create directory
fmt.Println("x", hdr.Name)
Expand Down Expand Up @@ -200,6 +200,7 @@ func Unzip(path string, f *os.File) (*bufio.Reader, error) {
if err != nil {
return nil, fmt.Errorf("%v: %v\n%v", ErrUnzip, path, err)
}
defer reader.Close()
return bufio.NewReader(reader), nil
}

Expand Down

0 comments on commit 19f88f7

Please sign in to comment.