github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/verify/hook.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package verify
    10  
    11  import (
    12  	"fmt"
    13  	"path/filepath"
    14  
    15  	"github.com/vchain-us/vcn/pkg/store"
    16  
    17  	"github.com/spf13/cobra"
    18  	"github.com/vchain-us/vcn/pkg/bundle"
    19  
    20  	"github.com/vchain-us/vcn/pkg/api"
    21  	"github.com/vchain-us/vcn/pkg/extractor/dir"
    22  )
    23  
    24  type hook struct {
    25  	a       api.Artifact
    26  	rawDiff bool
    27  }
    28  
    29  func newHook(cmd *cobra.Command, a *api.Artifact) *hook {
    30  	if a != nil {
    31  		h := hook{
    32  			a: a.Copy(),
    33  		}
    34  		h.rawDiff, _ = cmd.Flags().GetBool("raw-diff")
    35  		dir.RemoveMetadata(a)
    36  		return &h
    37  	}
    38  	return nil
    39  }
    40  
    41  func (h *hook) finalize(alertConfig *api.AlertConfig, output string) error {
    42  	if h != nil && output == "" {
    43  		manifest, path := dir.Metadata(h.a)
    44  		if manifest != nil && path != "" {
    45  			oldManifest, err := store.ReadManifest(h.a.Kind, path)
    46  			if err != nil {
    47  				oldManifest, err = bundle.ReadManifest(filepath.Join(path, bundle.ManifestFilename))
    48  			}
    49  			if err != nil {
    50  				return nil // ignore missing manifest
    51  			}
    52  			// check old manifest integrity
    53  			oldDigest, err := oldManifest.Digest()
    54  			if err != nil {
    55  				fmt.Printf("Diff is unavailable because '%s' is invalid.\n\n", bundle.ManifestFilename)
    56  				return nil // ignore bad manifest
    57  			}
    58  			v, err := api.Verify(oldDigest.Encoded())
    59  			if err != nil {
    60  				return err
    61  			}
    62  			if v != nil && !v.Unknown() {
    63  				var report string
    64  				var equal bool
    65  				var err error
    66  				if h.rawDiff {
    67  					report, equal, err = manifest.Diff(*oldManifest)
    68  				} else {
    69  					report, equal, err = manifest.DiffByPath(*oldManifest)
    70  				}
    71  				if err != nil {
    72  					return err
    73  				}
    74  				if !equal {
    75  					fmt.Printf("Diff since %s\n\n%s\n\n", v.Date(), report)
    76  					if alertConfig != nil {
    77  						alertConfig.Metadata["diff"] = report
    78  					}
    79  				}
    80  			} else {
    81  				fmt.Printf("Diff is unavailable because '%s' has been tampered.\n\n", bundle.ManifestFilename)
    82  			}
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func (h *hook) lcFinalizeWithoutAlert(user *api.LcUser, output string, txId uint64) error {
    89  	if h != nil && output == "" {
    90  		manifest, path := dir.Metadata(h.a)
    91  		if manifest != nil && path != "" {
    92  			oldManifest, err := store.ReadManifest(h.a.Kind, path)
    93  			if err != nil {
    94  				oldManifest, err = bundle.ReadManifest(filepath.Join(path, bundle.ManifestFilename))
    95  			}
    96  			if err != nil {
    97  				return nil // ignore missing manifest
    98  			}
    99  			// check old manifest integrity
   100  			oldDigest, err := oldManifest.Digest()
   101  			if err != nil {
   102  				fmt.Printf("Diff is unavailable because '%s' is invalid.\n\n", bundle.ManifestFilename)
   103  				return nil // ignore bad manifest
   104  			}
   105  			oldArtifact, _, err := user.LoadArtifact(oldDigest.Encoded(), "", "", txId, nil)
   106  
   107  			if err != nil {
   108  				if err == api.ErrNotFound {
   109  					fmt.Printf("%s was not notarized", oldDigest.Encoded())
   110  				} else {
   111  					return err
   112  				}
   113  			}
   114  			if oldArtifact != nil {
   115  				var report string
   116  				var equal bool
   117  				var err error
   118  				if h.rawDiff {
   119  					report, equal, err = manifest.Diff(*oldManifest)
   120  				} else {
   121  					report, equal, err = manifest.DiffByPath(*oldManifest)
   122  				}
   123  				if err != nil {
   124  					return err
   125  				}
   126  				if !equal {
   127  					fmt.Printf("Diff since %s\n\n%s\n\n", oldArtifact.Timestamp, report)
   128  				}
   129  			} else {
   130  				fmt.Printf("Diff is unavailable because '%s' has been tampered.\n\n", bundle.ManifestFilename)
   131  			}
   132  		}
   133  	}
   134  	return nil
   135  }