github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/image_verify.go (about)

     1  // Copyright 2017 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 main
    16  
    17  import (
    18  	"os"
    19  
    20  	"github.com/hashicorp/errwrap"
    21  	"github.com/rkt/rkt/store/imagestore"
    22  	"github.com/rkt/rkt/store/treestore"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  var (
    27  	cmdImageVerify = &cobra.Command{
    28  		Use:   "verify IMAGE...",
    29  		Short: "Verify one or more rendered images in the local store",
    30  		Long: `Verify is able to check that, based on the stored hash value, a rendered image on disk has not changed.
    31  
    32  This command may be used if the user suspects disk corruption might have damaged the rkt store.`,
    33  		Run: runWrapper(runVerifyImage),
    34  	}
    35  )
    36  
    37  func init() {
    38  	cmdImage.AddCommand(cmdImageVerify)
    39  }
    40  
    41  func runVerifyImage(cmd *cobra.Command, args []string) int {
    42  	if len(args) < 1 {
    43  		stderr.Print("must provide at least one image ID")
    44  		return 254
    45  	}
    46  
    47  	s, err := imagestore.NewStore(storeDir())
    48  	if err != nil {
    49  		stderr.PrintE("cannot open store", err)
    50  		return 254
    51  	}
    52  
    53  	ts, err := treestore.NewStore(treeStoreDir(), s)
    54  	if err != nil {
    55  		stderr.PrintE("cannot open treestore", err)
    56  		return 254
    57  	}
    58  
    59  	for _, img := range args {
    60  		key, err := getStoreKeyFromAppOrHash(s, img)
    61  		if err != nil {
    62  			stderr.Printf("unable to resolve store key for image %s: %v", img, err)
    63  			return 254
    64  		}
    65  		id, err := ts.GetID(key)
    66  		if err != nil {
    67  			stderr.Printf("unable to get treestoreID for image %s: %v", img, err)
    68  			return 254
    69  		}
    70  		_, err = ts.Check(id)
    71  		if isNotRenderedErr(err) {
    72  			stdout.Printf("image %q not rendered; no verification needed", img)
    73  			continue
    74  		}
    75  		if err != nil {
    76  			stdout.Printf("tree cache verification failed for image %s: %v;  rebuilding...", img, err)
    77  			_, _, err = ts.Render(key, true)
    78  			if err != nil {
    79  				stderr.Printf("unable to repair cache for image %s: %v", img, err)
    80  				return 254
    81  			}
    82  		} else {
    83  			stdout.Printf("successfully verified checksum for image: %q (%q)", img, key)
    84  		}
    85  	}
    86  	return 0
    87  }
    88  
    89  func isNotRenderedErr(err error) bool {
    90  	containsIsNotExist := false
    91  	containsReadHashErr := false
    92  	errwrap.Walk(err, func(e error) {
    93  		if os.IsNotExist(e) {
    94  			containsIsNotExist = true
    95  		}
    96  		if e == treestore.ErrReadHashfile {
    97  			containsReadHashErr = true
    98  		}
    99  	})
   100  	return containsIsNotExist && containsReadHashErr
   101  }