Use Fallback Schemas for Metadata

This commit is contained in:
Samuel Lorch 2025-05-12 14:49:13 +02:00
parent eb55164696
commit 91df259a87

View file

@ -1,11 +1,14 @@
package helper
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"github.com/passbolt/go-passbolt/api"
"github.com/santhosh-tekuri/jsonschema"
)
func GetResourceMetadata(ctx context.Context, c *api.Client, resource api.Resource, rType api.ResourceType) (string, error) {
@ -54,8 +57,6 @@ func GetResourceMetadata(ctx context.Context, c *api.Client, resource api.Resour
if err != nil {
return "", fmt.Errorf("Decrypt Metadata: %w", err)
}
/*
var schemaDefinition api.ResourceTypeSchema
err = json.Unmarshal([]byte(rType.Definition), &schemaDefinition)
@ -68,6 +69,15 @@ func GetResourceMetadata(ctx context.Context, c *api.Client, resource api.Resour
return "", fmt.Errorf("Workaround Unmarshal Json Schema String: %w", err)
}
if tmp == "[]" {
// Use The Builtin Fallback Schemas in this Case
schema, ok := api.ResourceSchemas[rType.Slug]
if !ok {
return "", fmt.Errorf("Server Does not have the Required json Schema and there is no fallback available for type: %v", rType.Slug)
}
tmp = string(schema)
}
err = json.Unmarshal([]byte(tmp), &schemaDefinition)
if err != nil {
return "", fmt.Errorf("Workaround Unmarshal Json Schema: %w", err)
@ -80,7 +90,7 @@ func GetResourceMetadata(ctx context.Context, c *api.Client, resource api.Resour
comp := jsonschema.NewCompiler()
err = comp.AddResource("metadata.json", bytes.NewReader(schemaDefinition.Secret))
err = comp.AddResource("metadata.json", bytes.NewReader(schemaDefinition.Resource))
if err != nil {
return "", fmt.Errorf("Adding Json Schema: %w", err)
}
@ -94,7 +104,6 @@ func GetResourceMetadata(ctx context.Context, c *api.Client, resource api.Resour
if err != nil {
return "", fmt.Errorf("Validating Secret Data: %w", err)
}
*/
return decMetadata, nil
}