github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/rkt/image/validator.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package image 16 17 import ( 18 "errors" 19 "fmt" 20 "io" 21 22 "github.com/coreos/rkt/pkg/keystore" 23 "github.com/hashicorp/errwrap" 24 25 "github.com/appc/spec/aci" 26 "github.com/appc/spec/schema" 27 "golang.org/x/crypto/openpgp" 28 pgperrors "golang.org/x/crypto/openpgp/errors" 29 ) 30 31 // validator is a general image checker 32 type validator struct { 33 image io.ReadSeeker 34 manifest *schema.ImageManifest 35 } 36 37 // newValidator returns a validator instance if passed image is indeed 38 // an ACI. 39 func newValidator(image io.ReadSeeker) (*validator, error) { 40 manifest, err := aci.ManifestFromImage(image) 41 if err != nil { 42 return nil, err 43 } 44 v := &validator{ 45 image: image, 46 manifest: manifest, 47 } 48 return v, nil 49 } 50 51 // GetImageName returns image name as it is in the image manifest. 52 func (v *validator) GetImageName() string { 53 return v.manifest.Name.String() 54 } 55 56 // ValidateName checks if desired image name is actually the same as 57 // the one in the image manifest. 58 func (v *validator) ValidateName(imageName string) error { 59 name := v.GetImageName() 60 if name != imageName { 61 return fmt.Errorf("error when reading the app name: %q expected but %q found", 62 imageName, name) 63 } 64 return nil 65 } 66 67 // ValidateWithSignature verifies the image against a given signature 68 // file. 69 func (v *validator) ValidateWithSignature(ks *keystore.Keystore, sig io.ReadSeeker) (*openpgp.Entity, error) { 70 if ks == nil { 71 return nil, nil 72 } 73 if _, err := v.image.Seek(0, 0); err != nil { 74 return nil, errwrap.Wrap(errors.New("error seeking ACI file"), err) 75 } 76 if _, err := sig.Seek(0, 0); err != nil { 77 return nil, errwrap.Wrap(errors.New("error seeking signature file"), err) 78 } 79 entity, err := ks.CheckSignature(v.GetImageName(), v.image, sig) 80 if err == pgperrors.ErrUnknownIssuer { 81 log.Print("If you expected the signing key to change, try running:") 82 log.Print(" rkt trust --prefix <image>") 83 } 84 if err != nil { 85 return nil, err 86 } 87 return entity, nil 88 }