|
| 1 | +// |
| 2 | +// Copyright 2025 The GUAC Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package reference |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + jsoniter "github.com/json-iterator/go" |
| 24 | + |
| 25 | + "github.com/guacsec/guac/pkg/assembler" |
| 26 | + "github.com/guacsec/guac/pkg/assembler/clients/generated" |
| 27 | + "github.com/guacsec/guac/pkg/assembler/helpers" |
| 28 | + attestation "github.com/guacsec/guac/pkg/certifier/attestation/reference" |
| 29 | + "github.com/guacsec/guac/pkg/handler/processor" |
| 30 | + "github.com/guacsec/guac/pkg/ingestor/parser/common" |
| 31 | + "github.com/guacsec/guac/pkg/logging" |
| 32 | +) |
| 33 | + |
| 34 | +var json = jsoniter.ConfigCompatibleWithStandardLibrary |
| 35 | + |
| 36 | +const ( |
| 37 | + justification = "Retrieved from reference predicate" |
| 38 | +) |
| 39 | + |
| 40 | +type parser struct { |
| 41 | + doc *processor.Document |
| 42 | + pkg *generated.PkgInputSpec |
| 43 | + collectedReference []assembler.IsOccurrenceIngest |
| 44 | + identifierStrings *common.IdentifierStrings |
| 45 | + timeScanned time.Time |
| 46 | +} |
| 47 | + |
| 48 | +// newReferenceParser initializes the parser |
| 49 | +func NewReferenceParser() common.DocumentParser { |
| 50 | + return &parser{ |
| 51 | + identifierStrings: &common.IdentifierStrings{}, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// initializeReferenceParser clears out all values for the next iteration |
| 56 | +func (r *parser) initializeReferenceParser() { |
| 57 | + r.doc = nil |
| 58 | + r.pkg = nil |
| 59 | + r.collectedReference = make([]assembler.IsOccurrenceIngest, 0) |
| 60 | + r.identifierStrings = &common.IdentifierStrings{} |
| 61 | + r.timeScanned = time.Now() |
| 62 | +} |
| 63 | + |
| 64 | +// Parse breaks out the document into the graph components |
| 65 | +func (r *parser) Parse(ctx context.Context, doc *processor.Document) error { |
| 66 | + logger := logging.FromContext(ctx) |
| 67 | + r.initializeReferenceParser() |
| 68 | + r.doc = doc |
| 69 | + |
| 70 | + statement, err := parseReferenceStatement(doc.Blob) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to parse reference predicate: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + r.timeScanned = time.Now() |
| 76 | + |
| 77 | + if err := r.parseSubject(statement); err != nil { |
| 78 | + logger.Warnf("unable to parse subject of statement: %v", err) |
| 79 | + return fmt.Errorf("unable to parse subject of statement: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + if err := r.parseReferences(ctx, statement); err != nil { |
| 83 | + logger.Warnf("unable to parse reference statement: %v", err) |
| 84 | + return fmt.Errorf("unable to parse reference statement: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func parseReferenceStatement(p []byte) (*attestation.ReferenceStatement, error) { |
| 91 | + statement := attestation.ReferenceStatement{} |
| 92 | + if err := json.Unmarshal(p, &statement); err != nil { |
| 93 | + return nil, fmt.Errorf("failed to unmarshal reference predicate: %w", err) |
| 94 | + } |
| 95 | + return &statement, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (r *parser) parseSubject(s *attestation.ReferenceStatement) error { |
| 99 | + if len(s.Statement.Subject) == 0 { |
| 100 | + return fmt.Errorf("no subject found in reference statement") |
| 101 | + } |
| 102 | + |
| 103 | + for _, sub := range s.Statement.Subject { |
| 104 | + p, err := helpers.PurlToPkg(sub.Uri) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("failed to parse uri: %s to a package with error: %w", sub.Uri, err) |
| 107 | + } |
| 108 | + r.pkg = p |
| 109 | + r.identifierStrings.PurlStrings = append(r.identifierStrings.PurlStrings, sub.Uri) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// parseReferences parses the attestation to collect the reference information |
| 115 | +func (r *parser) parseReferences(_ context.Context, s *attestation.ReferenceStatement) error { |
| 116 | + if r.pkg == nil { |
| 117 | + return fmt.Errorf("package not specified for reference information") |
| 118 | + } |
| 119 | + |
| 120 | + for _, ref := range s.Predicate.References { |
| 121 | + refData := assembler.IsOccurrenceIngest{ |
| 122 | + Pkg: r.pkg, |
| 123 | + Artifact: &generated.ArtifactInputSpec{ |
| 124 | + Algorithm: "sha256", |
| 125 | + Digest: ref.Digest.SHA256, |
| 126 | + }, |
| 127 | + IsOccurrence: &generated.IsOccurrenceInputSpec{ |
| 128 | + Justification: justification, |
| 129 | + Collector: "GUAC", |
| 130 | + Origin: "GUAC Reference", |
| 131 | + DocumentRef: ref.DownloadLocation, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + r.collectedReference = append(r.collectedReference, refData) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func (r *parser) GetPredicates(ctx context.Context) *assembler.IngestPredicates { |
| 142 | + logger := logging.FromContext(ctx) |
| 143 | + preds := &assembler.IngestPredicates{} |
| 144 | + |
| 145 | + if r.pkg == nil { |
| 146 | + logger.Error("error getting predicates: unable to find package element") |
| 147 | + return preds |
| 148 | + } |
| 149 | + |
| 150 | + preds.IsOccurrence = r.collectedReference |
| 151 | + return preds |
| 152 | +} |
| 153 | + |
| 154 | +// GetIdentities gets the identity node from the document if they exist |
| 155 | +func (r *parser) GetIdentities(ctx context.Context) []common.TrustInformation { |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func (r *parser) GetIdentifiers(ctx context.Context) (*common.IdentifierStrings, error) { |
| 160 | + common.RemoveDuplicateIdentifiers(r.identifierStrings) |
| 161 | + return r.identifierStrings, nil |
| 162 | +} |
0 commit comments