Skip to content

Commit

Permalink
feat: add support for schema verification for HpcrContractSignedEncry…
Browse files Browse the repository at this point in the history
…pted and HpcrContractSignedEncryptedContractExpiry
  • Loading branch information
Sashwat-K committed May 14, 2024
1 parent ca5a1d4 commit 9e26686
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 6 additions & 6 deletions common/general/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,26 +341,26 @@ func GenerateTgzBase64(folderFilesPath []string) (string, error) {
}

// VerifyContractWithSchema - function to verify if contract matches schema
func VerifyContractWithSchema(contract string) (bool, error) {
func VerifyContractWithSchema(contract string) error {
jsonData, err := YamlToJson(contract)
if err != nil {
return false, fmt.Errorf("error converting YAML to JSON - %v", err)
return fmt.Errorf("error converting YAML to JSON - %v", err)
}

schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader([]byte(sch.ContractSchema)))
if err != nil {
return false, fmt.Errorf("failed to parse schema - %v", err)
return fmt.Errorf("failed to parse schema - %v", err)
}

report, err := schema.Validate(gojsonschema.NewBytesLoader([]byte(jsonData)))
if err != nil {
return false, fmt.Errorf("failed to validate contract - %v", err)
return fmt.Errorf("failed to validate contract - %v", err)
}

result := report.Valid()

if result {
return true, nil
return nil
} else {
var consolidatedErrors strings.Builder
for i, err := range report.Errors() {
Expand All @@ -370,6 +370,6 @@ func VerifyContractWithSchema(contract string) (bool, error) {
consolidatedErrors.WriteString(err.String())
}

return report.Valid(), fmt.Errorf("validation failed - %s", consolidatedErrors.String())
return fmt.Errorf("validation failed - %s", consolidatedErrors.String())
}
}
4 changes: 1 addition & 3 deletions common/general/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ func TestVerifyContractWithSchema(t *testing.T) {
t.Errorf("failed to read contract - %v", err)
}

result, err := VerifyContractWithSchema(contract)
err = VerifyContractWithSchema(contract)
if err != nil {
t.Errorf("schema verification failed - %v", err)
}

assert.True(t, result)
}
10 changes: 10 additions & 0 deletions contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func HpcrTgzEncrypted(folderPath, encryptionCertificate string) (string, string,

// HpcrContractSignedEncrypted - function to generate Signed and Encrypted contract
func HpcrContractSignedEncrypted(contract, encryptionCertificate, privateKey string) (string, error) {
err := gen.VerifyContractWithSchema(contract)
if err != nil {
return "", fmt.Errorf("schema verification failed - %v", err)
}

if gen.CheckIfEmpty(contract, privateKey) {
return "", fmt.Errorf(emptyParameterErrStatement)
}
Expand All @@ -107,6 +112,11 @@ func HpcrContractSignedEncrypted(contract, encryptionCertificate, privateKey str

// HpcrContractSignedEncryptedContractExpiry - function to generate sign with contract expiry enabled and encrypt contract (with CSR parameters and CSR file)
func HpcrContractSignedEncryptedContractExpiry(contract, encryptionCertificate, privateKey, cacert, caKey, csrDataStr, csrPemData string, expiryDays int) (string, error) {
err := gen.VerifyContractWithSchema(contract)
if err != nil {
return "", fmt.Errorf("schema verification failed - %v", err)
}

if gen.CheckIfEmpty(contract, privateKey, cacert, caKey) {
return "", fmt.Errorf(emptyParameterErrStatement)
}
Expand Down

0 comments on commit 9e26686

Please sign in to comment.