Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New certificate schema, mandatory type, chart sha and timestamp #75

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,14 @@ func NewVerifyCmd(config *viper.Viper) *cobra.Command {

cmd.Println(string(b))

} else if outputFormatFlag == "yaml" {
} else {
b, err := yaml.Marshal(result)
if err != nil {
return err
}

cmd.Println(string(b))
} else {
cmd.Print(result)
}

reportErr := chartverifier.
NewReportBuilder().
SetCertificate(&result).
SetChartUri(args[0]).
Generate()

if reportErr != nil {
cmd.Println("Report failure :" + reportErr.Error())
return err
}

return nil
},
}
Expand Down
78 changes: 21 additions & 57 deletions cmd/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

"github.com/redhat-certification/chart-verifier/pkg/chartverifier"
"github.com/redhat-certification/chart-verifier/pkg/chartverifier/checks"
)

Expand Down Expand Up @@ -113,18 +114,12 @@ func TestCertify(t *testing.T) {
require.NoError(t, cmd.Execute())
require.NotEmpty(t, outBuf.String())

expected := "Tool:\n" +
" verifier-version: 1.0.0\n" +
" chart-uri: ../pkg/chartverifier/checks/chart-0.1.0-v3.valid.tgz\n" +
"Chart:\n" +
" Name: chart\n" +
" version: 1.16.0\n" +
"ok: true\n" +
"\n" +
"is-helm-v3:\n" +
"\tok: true\n" +
"\treason: " + checks.Helm3Reason + "\n"
require.Equal(t, expected, outBuf.String())
expected := "results:\n" +
" - check: is-helm-v3\n" +
" type: Mandatory\n" +
" outcome: PASS\n" +
" reason: API version is V2, used in Helm 3\n"
require.Contains(t, outBuf.String(), expected)
})

t.Run("Should display JSON certificate when option --output and argument values are given", func(t *testing.T) {
Expand All @@ -143,30 +138,14 @@ func TestCertify(t *testing.T) {
require.NotEmpty(t, outBuf.String())

// attempts to deserialize the command's output, expecting a json string
actual := map[string]interface{}{}
err := json.Unmarshal([]byte(outBuf.String()), &actual)
certificate := chartverifier.Certificate{}
err := json.Unmarshal([]byte(outBuf.String()), &certificate)
require.NoError(t, err)

expected := map[string]interface{}{
"metadata": map[string]interface{}{
"tool": map[string]interface{}{
"verifier-version": "1.0.0",
"chart-uri": "../pkg/chartverifier/checks/chart-0.1.0-v3.valid.tgz",
},
"chart": map[string]interface{}{
"name": "chart",
"version": "1.16.0",
},
},
"ok": true,
"results": map[string]interface{}{
"is-helm-v3": map[string]interface{}{
"ok": true,
"reason": checks.Helm3Reason,
},
},
}
require.Equal(t, expected, actual)
require.True(t, len(certificate.Results) == 1, "Expected only 1 result")
require.Equal(t, certificate.Results[0].Check, "is-helm-v3")
require.Equal(t, certificate.Results[0].Outcome, chartverifier.PassOutcomeType)
require.Equal(t, certificate.Results[0].Type, chartverifier.MandatoryCheckType)
require.Equal(t, certificate.Results[0].Reason, checks.Helm3Reason)
})

t.Run("Should display YAML certificate when option --output and argument values are given", func(t *testing.T) {
Expand All @@ -185,30 +164,15 @@ func TestCertify(t *testing.T) {
require.NotEmpty(t, outBuf.String())

// attempts to deserialize the command's output, expecting a json string
actual := map[string]interface{}{}
err := yaml.Unmarshal([]byte(outBuf.String()), &actual)
certificate := chartverifier.Certificate{}
err := yaml.Unmarshal([]byte(outBuf.String()), &certificate)
require.NoError(t, err)
require.True(t, len(certificate.Results) == 1, "Expected only 1 result")
require.Equal(t, certificate.Results[0].Check, "is-helm-v3")
require.Equal(t, certificate.Results[0].Outcome, chartverifier.PassOutcomeType)
require.Equal(t, certificate.Results[0].Type, chartverifier.MandatoryCheckType)
require.Equal(t, certificate.Results[0].Reason, checks.Helm3Reason)

expected := map[string]interface{}{
"metadata": map[string]interface{}{
"tool": map[string]interface{}{
"verifier-version": "1.0.0",
"chart-uri": "../pkg/chartverifier/checks/chart-0.1.0-v3.valid.tgz",
},
"chart": map[string]interface{}{
"name": "chart",
"version": "1.16.0",
},
},
"ok": true,
"results": map[string]interface{}{
"is-helm-v3": map[string]interface{}{
"ok": true,
"reason": checks.Helm3Reason,
},
},
}
require.Equal(t, expected, actual)
})

}
Expand Down
121 changes: 67 additions & 54 deletions pkg/chartverifier/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,89 @@

package chartverifier

import "strconv"
import (
"github.com/redhat-certification/chart-verifier/pkg/chartverifier/checks"
helmchart "helm.sh/helm/v3/pkg/chart"
pedjak marked this conversation as resolved.
Show resolved Hide resolved
)

type chartMetadata struct {
Name string `json:"name" yaml:"name"`
Version string `json:"version" yaml:"version"`
}
var CertificateApiVersion = "v1"
pedjak marked this conversation as resolved.
Show resolved Hide resolved
var CertificateKind = "verify-report"
isutton marked this conversation as resolved.
Show resolved Hide resolved

type OutcomeType string

const (
MandatoryCheckType checks.CheckType = "Mandatory"
OptionalCheckType checks.CheckType = "Optional"
ExperimentalCheckType checks.CheckType = "Experimental"

type runMetadata struct {
Version string `json:"verifier-version" yaml:"verifier-version"`
ChartUri string `json:"chart-uri" yaml:"chart-uri"`
FailOutcomeType OutcomeType = "FAIL"
PassOutcomeType OutcomeType = "PASS"
UnknownOutcomeType OutcomeType = "UNKNOWN"
)

type Certificate struct {
isutton marked this conversation as resolved.
Show resolved Hide resolved
Apiversion string `json:"apiversion" yaml:"apiversion"`
Kind string `json:"kind" yaml:"kind"`
Metadata CertificateMetadata `json:"metadata" yaml:"metadata"`
Results []*CheckReport `json:"results" yaml:"results"`
}

type metadata struct {
RunMetadata runMetadata `json:"tool" yaml:"tool"`
ChartMetadata chartMetadata `json:"chart" yaml:"chart"`
type CertificateMetadata struct {
isutton marked this conversation as resolved.
Show resolved Hide resolved
ToolMetadata ToolMetadata `json:"tool" yaml:"tool"`
ChartData *helmchart.Metadata `json:"chart" yaml:"chart"`
Overrides string `json: "chart-overrides" yaml:"chart-overrides"`
}

func newMetadata(name, version, chartUri, toolVersion string) *metadata {
return &metadata{
RunMetadata: runMetadata{
ChartUri: chartUri,
Version: toolVersion,
},
ChartMetadata: chartMetadata{
Name: name,
Version: version,
},
}
type ToolMetadata struct {
Version string `json:"verifier-version" yaml:"verifier-version"`
ChartUri string `json:"chart-uri" yaml:"chart-uri"`
Digest string `json:"digest" yaml:"digest"`
LastCertifiedTime string `json:"lastCertifiedTime" yaml:"lastCertifiedTime"`
CertifiedOpenShiftVersions string `json:"certifiedOpenShiftVersions" yaml:"certifiedOpenShiftVersions"`
}

type certificate struct {
Ok bool `json:"ok" yaml:"ok"`
Metadata *metadata `json:"metadata" yaml:"metadata"`
CheckResultMap checkResultMap `json:"results" yaml:"results"`
type CheckReport struct {
isutton marked this conversation as resolved.
Show resolved Hide resolved
Check string `json:"check" yaml:"check"`
Type checks.CheckType `json:"type" yaml:"type"`
Outcome OutcomeType `json:"outcome" yaml:"outcome"`
Reason string `json:"reason" yaml:"reason"`
}

type checkResultMap map[string]checkResult
func newCertificate() Certificate {

type checkResult struct {
Ok bool `json:"ok" yaml:"ok"`
Reason string `json:"reason" yaml:"reason"`
}
certificate := Certificate{Apiversion: CertificateApiVersion, Kind: CertificateKind}
certificate.Metadata = CertificateMetadata{}
certificate.Metadata.ToolMetadata = ToolMetadata{}

func newCertificate(name, version, chartUri, toolVersion string, ok bool, resultMap checkResultMap) Certificate {
return &certificate{
Metadata: newMetadata(name, version, chartUri, toolVersion),
Ok: ok,
CheckResultMap: resultMap,
}
return certificate
}

func (c *certificate) IsOk() bool {
return c.Ok
func (c *Certificate) AddCheck(checkName string, checkType checks.CheckType) *CheckReport {
newCheck := CheckReport{}
newCheck.Check = checkName
newCheck.Type = checkType
newCheck.Outcome = UnknownOutcomeType
c.Results = append(c.Results, &newCheck)
return &newCheck
}

func (c *certificate) String() string {
report := "Tool:\n" +
" verifier-version: " + c.Metadata.RunMetadata.Version + "\n" +
" chart-uri: " + c.Metadata.RunMetadata.ChartUri + "\n" +
"Chart:\n" +
" Name: " + c.Metadata.ChartMetadata.Name + "\n" +
" version: " + c.Metadata.ChartMetadata.Version + "\n" +
"ok: " + strconv.FormatBool(c.Ok) + "\n" +
"\n"

for k, v := range c.CheckResultMap {
report += k + ":\n" +
"\tok: " + strconv.FormatBool(v.Ok) + "\n" +
"\treason: " + v.Reason + "\n"
func (cr *CheckReport) SetResult(outcome bool, reason string) {
if outcome {
cr.Outcome = PassOutcomeType
} else {
cr.Outcome = FailOutcomeType
}
cr.Reason = reason
}

return report
func (c *Certificate) IsOk() bool {
pedjak marked this conversation as resolved.
Show resolved Hide resolved

outcome := true
for _, check := range c.Results {
if !(check.Outcome == PassOutcomeType) {
outcome = false
break
}
}
return outcome
}
Loading