| 1 |
// Copyright 2023 The Archivista Contributors |
| 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 attestationcollection |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"encoding/json" |
| 20 |
"github.com/google/uuid" |
| 21 |
"github.com/in-toto/archivista/ent" |
| 22 |
"github.com/in-toto/archivista/pkg/metadatastorage" |
| 23 |
"github.com/in-toto/go-witness/attestation" |
| 24 |
"github.com/in-toto/go-witness/log" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
Predicate = "https://witness.testifysec.com/attestation-collection/v0.1" |
| 29 |
) |
| 30 |
|
| 31 |
// attestation.Collection from go-witness will try to parse each of the attestations by calling their factory functions, |
| 32 |
// which require the attestations to be registered in the go-witness library. We don't really care about the actual attestation |
| 33 |
// data for the purposes here, so just leave it as a raw message. |
| 34 |
type ParsedCollection struct { |
| 35 |
attestation.Collection |
| 36 |
Attestations []struct { |
| 37 |
Type string `json:"type"` |
| 38 |
Attestation json.RawMessage `json:"attestation"` |
| 39 |
} `json:"attestations"` |
| 40 |
} |
| 41 |
|
| 42 |
func Parse(data []byte) (metadatastorage.Storer, error) { |
| 43 |
parsedCollection := ParsedCollection{} |
| 44 |
if err := json.Unmarshal(data, &parsedCollection); err != nil { |
| 45 |
return parsedCollection, err |
| 46 |
} |
| 47 |
|
| 48 |
return parsedCollection, nil |
| 49 |
} |
| 50 |
|
| 51 |
func (parsedCollection ParsedCollection) Store(ctx context.Context, tx *ent.Tx, stmtID uuid.UUID) error { |
| 52 |
collection, err := tx.AttestationCollection.Create(). |
| 53 |
SetStatementID(stmtID). |
| 54 |
SetName(parsedCollection.Name). |
| 55 |
Save(ctx) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
for _, a := range parsedCollection.Attestations { |
| 61 |
newAttestation, err := tx.Attestation.Create(). |
| 62 |
SetAttestationCollectionID(collection.ID). |
| 63 |
SetType(a.Type). |
| 64 |
Save(ctx) |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
|
| 69 |
// we parse if a parser is available. otherwise, we ignore it if no parser is available. |
| 70 |
if parser, exists := registeredParsers[a.Type]; exists { |
| 71 |
if err := parser(ctx, tx, newAttestation, a.Type, a.Attestation); err != nil { |
| 72 |
log.Errorf("failed to parse attestation of type %s: %w", a.Type, err) |
| 73 |
return err |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return nil |
| 79 |
} |