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

add new method to test insertability of proposed entries into log #1410

Merged
merged 8 commits into from
May 14, 2023
21 changes: 1 addition & 20 deletions pkg/generated/models/intoto_v002_schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion pkg/generated/models/tuf_v001_schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions pkg/generated/restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/types/alpine/alpine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (u UnmarshalFailsTester) Verifier() (pki.PublicKey, error) {
return nil, nil
}

func (u UnmarshalFailsTester) Insertable() (bool, error) {
return false, nil
}

func TestAlpineType(t *testing.T) {
// empty to start
if VersionMap.Count() != 0 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/types/alpine/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,19 @@ func (v V001Entry) Verifier() (pki.PublicKey, error) {
}
return x509.NewPublicKey(bytes.NewReader(*v.AlpineModel.PublicKey.Content))
}

func (v V001Entry) Insertable() (bool, error) {
if v.AlpineModel.Package == nil {
return false, fmt.Errorf("missing package entry")
}
if len(v.AlpineModel.Package.Content) == 0 {
return false, fmt.Errorf("missing package content")
}
if v.AlpineModel.PublicKey == nil {
return false, fmt.Errorf("missing public key")
}
if v.AlpineModel.PublicKey.Content == nil || len(*v.AlpineModel.PublicKey.Content) == 0 {
return false, fmt.Errorf("missing public key content")
}
return true, nil
}
123 changes: 122 additions & 1 deletion pkg/types/alpine/v0.0.1/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func TestCrossFieldValidation(t *testing.T) {
t.Errorf("unexpected result in '%v': %v", tc.caseDesc, err)
}

if tc.expectUnmarshalSuccess {
if ok, err := v.Insertable(); !ok || err != nil {
t.Errorf("unexpected result in calling Insertable on valid proposed entry: %v", err)
}
}

b, err := v.Canonicalize(context.TODO())
if (err == nil) != tc.expectCanonicalizeSuccess {
t.Errorf("unexpected result from Canonicalize for '%v': %v", tc.caseDesc, err)
Expand All @@ -164,9 +170,13 @@ func TestCrossFieldValidation(t *testing.T) {
if err != nil {
t.Errorf("unexpected err from Unmarshalling canonicalized entry for '%v': %v", tc.caseDesc, err)
}
if _, err := types.UnmarshalEntry(pe); err != nil {
ei, err := types.UnmarshalEntry(pe)
if err != nil {
t.Errorf("unexpected err from type-specific unmarshalling for '%v': %v", tc.caseDesc, err)
}
if ok, err := ei.Insertable(); ok || err == nil {
t.Errorf("unexpected success calling Insertable on entry created from canonicalized content")
}
}

verifier, err := v.Verifier()
Expand All @@ -187,3 +197,114 @@ func TestCrossFieldValidation(t *testing.T) {
}
}
}

func TestInsertable(t *testing.T) {
type TestCase struct {
caseDesc string
entry V001Entry
expectSuccess bool
}

pub := strfmt.Base64([]byte("pub"))

testCases := []TestCase{
{
caseDesc: "valid entry",
entry: V001Entry{
AlpineModel: models.AlpineV001Schema{
Package: &models.AlpineV001SchemaPackage{
Content: strfmt.Base64("package"),
},
PublicKey: &models.AlpineV001SchemaPublicKey{
Content: &pub,
},
},
},
expectSuccess: true,
},
{
caseDesc: "missing key content",
entry: V001Entry{
AlpineModel: models.AlpineV001Schema{
Package: &models.AlpineV001SchemaPackage{
Content: strfmt.Base64("package"),
},
PublicKey: &models.AlpineV001SchemaPublicKey{
//Content: &pub,
},
},
},
expectSuccess: false,
},
{
caseDesc: "missing public key",
entry: V001Entry{
AlpineModel: models.AlpineV001Schema{
Package: &models.AlpineV001SchemaPackage{
Content: strfmt.Base64("package"),
},
/*
PublicKey: &models.AlpineV001SchemaPublicKey{
Content: &pub,
},
*/
},
},
expectSuccess: false,
},
{
caseDesc: "missing package content",
entry: V001Entry{
AlpineModel: models.AlpineV001Schema{
Package: &models.AlpineV001SchemaPackage{
//Content: strfmt.Base64("package"),
},
PublicKey: &models.AlpineV001SchemaPublicKey{
Content: &pub,
},
},
},
expectSuccess: false,
},
{
caseDesc: "missing package",
entry: V001Entry{
AlpineModel: models.AlpineV001Schema{
/*
Package: &models.AlpineV001SchemaPackage{
Content: strfmt.Base64("package"),
},
*/
PublicKey: &models.AlpineV001SchemaPublicKey{
Content: &pub,
},
},
},
expectSuccess: false,
},
{
caseDesc: "empty model",
entry: V001Entry{
AlpineModel: models.AlpineV001Schema{
/*
Package: &models.AlpineV001SchemaPackage{
Content: strfmt.Base64("package"),
},
PublicKey: &models.AlpineV001SchemaPublicKey{
Content: &pub,
},
*/
},
},
expectSuccess: false,
},
}

for _, tc := range testCases {
t.Run(tc.caseDesc, func(t *testing.T) {
if ok, err := tc.entry.Insertable(); ok != tc.expectSuccess {
t.Errorf("unexpected result calling Insertable: %v", err)
}
})
}
}
4 changes: 4 additions & 0 deletions pkg/types/cose/cose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (u UnmarshalFailsTester) Verifier() (pki.PublicKey, error) {
return nil, nil
}

func (u UnmarshalFailsTester) Insertable() (bool, error) {
return false, nil
}

func TestCOSEType(t *testing.T) {
// empty to start
if VersionMap.Count() != 0 {
Expand Down
23 changes: 23 additions & 0 deletions pkg/types/cose/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,26 @@ func (v V001Entry) Verifier() (pki.PublicKey, error) {
}
return x509.NewPublicKey(bytes.NewReader(*v.CoseObj.PublicKey))
}

func (v V001Entry) Insertable() (bool, error) {
if len(v.CoseObj.Message) == 0 {
return false, errors.New("missing COSE Sign1 message")
}
if v.CoseObj.PublicKey == nil || len(*v.CoseObj.PublicKey) == 0 {
return false, errors.New("missing public key")
}
if v.CoseObj.Data == nil {
return false, errors.New("missing COSE data property")
}
if len(v.envelopeHash) == 0 {
return false, errors.New("envelope hash has not been computed")
}
if v.keyObj == nil {
return false, errors.New("public key has not been parsed")
}
if v.sign1Msg == nil {
return false, errors.New("signature has not been validated")
}

return true, nil
}
Loading