Skip to content

Commit 393a2b6

Browse files
authored
CWCOW: Include merged layer hash (microsoft#2618)
* CWCOW: Include merged layer hash Signed-off-by: Mahati Chamarthy <mahati.chamarthy@gmail.com> * CWCOW: Reuse API Signed-off-by: Mahati Chamarthy <mahati.chamarthy@gmail.com> --------- Signed-off-by: Mahati Chamarthy <mahati.chamarthy@gmail.com>
1 parent 3b9a4af commit 393a2b6

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

internal/gcs-sidecar/handlers.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,7 @@ func (b *Bridge) modifySettings(req *request) (err error) {
641641
log.G(ctx).Debugf("block CIM layer digest %s, path: %s\n", layerHashes[i], physicalDevPath)
642642
}
643643

644-
// skip the merged cim and verify individual layer hashes
645644
hashesToVerify := layerHashes
646-
if len(layerHashes) > 1 {
647-
hashesToVerify = layerHashes[1:]
648-
}
649645

650646
err := b.hostState.securityOptions.PolicyEnforcer.EnforceVerifiedCIMsPolicy(req.ctx, containerID, hashesToVerify)
651647
if err != nil {

pkg/ociwclayer/cim/import.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,33 @@ func WithParentLayers(parentLayers []*cimfs.BlockCIM) BlockCIMLayerImportOpt {
9696
}
9797
}
9898

99-
func writeIntegrityChecksumInfoFile(ctx context.Context, blockPath string) error {
99+
func GetIntegrityChecksum(ctx context.Context, blockPath string, pathName string) (string, error) {
100100
log.G(ctx).Debugf("writing integrity checksum file for block CIM `%s`", blockPath)
101101
// for convenience write a file that has the hex encoded root digest of the generated verified CIM.
102102
// this same hex string can be used in the confidential policy.
103+
// also return the integrity checksum as a string for integrity-vhd tooling.
103104
digest, err := cimfs.GetVerificationInfo(blockPath)
104105
if err != nil {
105-
return fmt.Errorf("failed to query verified info of the CIM layer: %w", err)
106+
return "", fmt.Errorf("failed to query verified info of the CIM layer: %w", err)
106107
}
107108

108-
digestFile, err := os.Create(filepath.Join(filepath.Dir(blockPath), "integrity_checksum"))
109-
if err != nil {
110-
return fmt.Errorf("failed to create verification info file: %w", err)
111-
}
112-
defer digestFile.Close()
113-
114109
digestStr := hex.EncodeToString(digest)
115-
if wn, err := digestFile.WriteString(digestStr); err != nil {
116-
return fmt.Errorf("failed to write verification info: %w", err)
117-
} else if wn != len(digestStr) {
118-
return fmt.Errorf("incomplete write of verification info: %w", err)
110+
111+
// only create a file if a path name is provided
112+
if pathName != "" {
113+
digestFile, err := os.Create(filepath.Join(filepath.Dir(blockPath), pathName))
114+
if err != nil {
115+
return "", fmt.Errorf("failed to create verification info file: %w", err)
116+
}
117+
defer digestFile.Close()
118+
119+
if wn, err := digestFile.WriteString(digestStr); err != nil {
120+
return "", fmt.Errorf("failed to write verification info: %w", err)
121+
} else if wn != len(digestStr) {
122+
return "", fmt.Errorf("incomplete write of verification info: %w", err)
123+
}
119124
}
120-
return nil
125+
return digestStr, nil
121126
}
122127

123128
func ImportBlockCIMLayerWithOpts(ctx context.Context, r io.Reader, layer *cimfs.BlockCIM, opts ...BlockCIMLayerImportOpt) (_ int64, err error) {
@@ -164,7 +169,7 @@ func ImportBlockCIMLayerWithOpts(ctx context.Context, r io.Reader, layer *cimfs.
164169
}
165170

166171
if config.dataIntegrity {
167-
if err = writeIntegrityChecksumInfoFile(ctx, layer.BlockPath); err != nil {
172+
if _, err = GetIntegrityChecksum(ctx, layer.BlockPath, "integrity_checksum"); err != nil {
168173
return 0, err
169174
}
170175
}
@@ -358,5 +363,10 @@ func MergeBlockCIMLayersWithOpts(ctx context.Context, sourceCIMs []*cimfs.BlockC
358363
return fmt.Errorf("append VHD footer to block CIM: %w", err)
359364
}
360365
}
366+
if config.dataIntegrity {
367+
if _, err = GetIntegrityChecksum(ctx, mergedCIM.BlockPath, "merged_integrity_checksum"); err != nil {
368+
return err
369+
}
370+
}
361371
return nil
362372
}

0 commit comments

Comments
 (0)