From d1df59f0b6df8b5b1726b176bd6d9c50b46eaa30 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Thu, 6 Jun 2024 11:04:07 -0700 Subject: [PATCH] feat: Implement plugin system for archivists' parsers (#295) * feat: Implement plugin system for archivists' parsers This commit introduces a flexible plugin system for archivists' parsers, allowing new parsers to be registered dynamically. The system utilizes an initialization function that registers parsers via a `Register` function. The primary changes include: - Creation of `parser_registry.go` to handle parser registration and storage. - Modification of `parserstorer.go` to integrate the new parser registration system and utilize registered parsers during attestation storage. This system enhances the extensibility of the parser functionality and improves maintainability by decoupling the parser registration from the core logic. Changes: - Added `internal/metadatastorage/attestationcollection/parser_registry.go` for parser registration. - Updated `internal/metadatastorage/attestationcollection/parserstorer.go` to support the new plugin system. --------- Signed-off-by: Frederick F. Kautz IV --- .../attestationcollection/parser_registry.go | 35 ++++++++++++++++ .../parser_registry_test.go | 40 +++++++++++++++++++ .../attestationcollection/parserstorer.go | 15 +++++-- 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 pkg/metadatastorage/attestationcollection/parser_registry.go create mode 100644 pkg/metadatastorage/attestationcollection/parser_registry_test.go diff --git a/pkg/metadatastorage/attestationcollection/parser_registry.go b/pkg/metadatastorage/attestationcollection/parser_registry.go new file mode 100644 index 00000000..e031f984 --- /dev/null +++ b/pkg/metadatastorage/attestationcollection/parser_registry.go @@ -0,0 +1,35 @@ +// Copyright 2024 The Archivista Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package attestationcollection + +import ( + "context" + "encoding/json" + "github.com/in-toto/archivista/ent" + "log" +) + +var registeredParsers map[string]AttestationParser + +func init() { + registeredParsers = make(map[string]AttestationParser) +} + +func Register(attestationType string, parser AttestationParser) { + registeredParsers[attestationType] = parser + log.Printf("parser registered: %s", attestationType) +} + +type AttestationParser func(ctx context.Context, tx *ent.Tx, attestation *ent.Attestation, attestationType string, message json.RawMessage) error diff --git a/pkg/metadatastorage/attestationcollection/parser_registry_test.go b/pkg/metadatastorage/attestationcollection/parser_registry_test.go new file mode 100644 index 00000000..909d6c44 --- /dev/null +++ b/pkg/metadatastorage/attestationcollection/parser_registry_test.go @@ -0,0 +1,40 @@ +// Copyright 2024 The Archivista Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package attestationcollection + +import ( + "context" + "encoding/json" + "testing" + + "github.com/in-toto/archivista/ent" + "github.com/stretchr/testify/assert" +) + +func TestRegister(t *testing.T) { + // Define a mock parser function + mockParser := func(ctx context.Context, tx *ent.Tx, attestation *ent.Attestation, attestationType string, message json.RawMessage) error { + return nil + } + + // Register the mock parser + Register("mockType", mockParser) + + // Check if the parser is registered + registeredParser, exists := registeredParsers["mockType"] + var typedParser AttestationParser = mockParser + assert.True(t, exists, "Parser should be registered") + assert.IsType(t, typedParser, registeredParser, "Registered parser should match the mock parser") +} diff --git a/pkg/metadatastorage/attestationcollection/parserstorer.go b/pkg/metadatastorage/attestationcollection/parserstorer.go index e456940e..e7f5bb82 100644 --- a/pkg/metadatastorage/attestationcollection/parserstorer.go +++ b/pkg/metadatastorage/attestationcollection/parserstorer.go @@ -17,11 +17,11 @@ package attestationcollection import ( "context" "encoding/json" - "github.com/google/uuid" "github.com/in-toto/archivista/ent" "github.com/in-toto/archivista/pkg/metadatastorage" "github.com/in-toto/go-witness/attestation" + "github.com/in-toto/go-witness/log" ) const ( @@ -58,12 +58,21 @@ func (parsedCollection ParsedCollection) Store(ctx context.Context, tx *ent.Tx, } for _, a := range parsedCollection.Attestations { - if err := tx.Attestation.Create(). + newAttestation, err := tx.Attestation.Create(). SetAttestationCollectionID(collection.ID). SetType(a.Type). - Exec(ctx); err != nil { + Save(ctx) + if err != nil { return err } + + // we parse if a parser is available. otherwise, we ignore it if no parser is available. + if parser, exists := registeredParsers[a.Type]; exists { + if err := parser(ctx, tx, newAttestation, a.Type, a.Attestation); err != nil { + log.Errorf("failed to parse attestation of type %s: %w", a.Type, err) + return err + } + } } return nil