| 40 | } |
| 41 | |
| 42 | func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp []*api.Attestation) (string, error) { |
| 43 | metadataFilePath := s.createJSONLinesFilePath(artifactDigest) |
| 44 | outputPath := s.outputPath |
| 45 | if outputPath == "" { |
| 46 | outputPath = "." |
| 47 | } |
| 48 | |
| 49 | root, err := safepaths.OpenRootDir(outputPath, 0o755) |
| 50 | if err != nil { |
| 51 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to open output directory: %v", err)) |
| 52 | } |
| 53 | defer root.Close() |
| 54 | |
| 55 | f, err := root.Create(s.createJSONLinesFileName(artifactDigest), 0o666, 0o755, true) |
| 56 | if err != nil { |
| 57 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to create file: %v", err)) |
| 58 | } |
| 59 | |
| 60 | for _, resp := range attestationsResp { |
| 61 | bundle := resp.Bundle |
| 62 | attBytes, err := json.Marshal(bundle) |
| 63 | if err != nil { |
| 64 | if err = f.Close(); err != nil { |
| 65 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while marshalling JSON: %v", err)) |
| 66 | } |
| 67 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to marshall attestation to JSON while writing to file: %v", err)) |
| 68 | } |
| 69 | |
| 70 | withNewline := fmt.Sprintf("%s\n", attBytes) |
| 71 | _, err = f.Write([]byte(withNewline)) |
| 72 | if err != nil { |
| 73 | if err = f.Close(); err != nil { |
| 74 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while handling write error: %v", err)) |
| 75 | } |
| 76 | |
| 77 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to write attestations: %v", err)) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if err = f.Close(); err != nil { |
| 82 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file after writing attestations: %v", err)) |
| 83 | } |
| 84 | |
| 85 | return metadataFilePath, nil |
| 86 | } |
| 87 | |
| 88 | func NewLiveStore(outputPath string) *LiveStore { |
| 89 | return &LiveStore{ |