Skip to content

Commit

Permalink
Statement, predicate and AsStatement method to result
Browse files Browse the repository at this point in the history
Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]>
  • Loading branch information
puerco committed Jan 17, 2025
1 parent c1ee9ed commit 3e86381
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions pkg/scorecard/statement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2025 OpenSSF Scorecard Authors
//
// 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 scorecard

import (
"encoding/json"
"fmt"
"io"

intoto "github.com/in-toto/attestation/go/v1"

docs "github.com/ossf/scorecard/v5/docs/checks"
sce "github.com/ossf/scorecard/v5/errors"
)

const (
PredicateType = "https://scorecard.dev/result/v0.1"
)

type Statement struct {
intoto.Statement
Predicate Predicate `json:"predicate"`
}

// Predicate overrides JSONScorecardResultV2 with a nullable Repo field.
type Predicate struct {
Repo *jsonRepoV2 `json:"repo,omitempty"`
JSONScorecardResultV2
}

// AsStatement converts the results as an in-toto statement.
func (r *Result) AsStatement(writer io.Writer, checkDocs docs.Doc, opt *AsJSON2ResultOption) error {
// Build the attestation subject from the result Repo.
subject := intoto.ResourceDescriptor{
Name: r.Repo.Name,
Uri: fmt.Sprintf("git+https://%s@%s", r.Repo.Name, r.Repo.CommitSHA),
Digest: map[string]string{
"gitCommit": r.Repo.CommitSHA,
},
}

json2, err := r.resultsToJSON2(checkDocs, opt)
if err != nil {
return sce.WithMessage(sce.ErrScorecardInternal, err.Error())
}

out := Statement{
Statement: intoto.Statement{
Type: intoto.StatementTypeUri,
Subject: []*intoto.ResourceDescriptor{
&subject,
},
PredicateType: PredicateType,
},
Predicate: Predicate{
JSONScorecardResultV2: json2,
Repo: nil,
},
}

encoder := json.NewEncoder(writer)
if err := encoder.Encode(&out); err != nil {
return sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("encoder.Encode: %v", err))
}

return nil
}

0 comments on commit 3e86381

Please sign in to comment.