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
5 changes: 4 additions & 1 deletion pkg/types/alpine/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,11 @@ func (v V001Entry) Insertable() (bool, error) {
if len(v.AlpineModel.Package.Content) == 0 {
return false, fmt.Errorf("missing package content")
}
if v.AlpineModel.PublicKey == nil || len(*v.AlpineModel.PublicKey.Content) == 0 {
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
}
111 changes: 111 additions & 0 deletions pkg/types/alpine/v0.0.1/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,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)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/types/cose/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (v V001Entry) Insertable() (bool, error) {
if len(v.CoseObj.Message) == 0 {
return false, errors.New("missing COSE Sign1 message")
}
if len(*v.CoseObj.PublicKey) == 0 {
if v.CoseObj.PublicKey == nil || len(*v.CoseObj.PublicKey) == 0 {
return false, errors.New("missing public key")
}
if v.CoseObj.Data == nil {
Expand Down
199 changes: 199 additions & 0 deletions pkg/types/cose/v0.0.1/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,202 @@ func mustContain(t *testing.T, want string, l []string) {
}
t.Fatalf("list %v does not contain %s", l, want)
}

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

key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
der, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
t.Fatal(err)
}
pub := pem.EncodeToMemory(&pem.Block{
Bytes: der,
Type: "PUBLIC KEY",
})
keyObj, err := sigx509.NewPublicKey(bytes.NewReader(pub))
if err != nil {
t.Fatal(err)
}
pubKey := strfmt.Base64(pub)

testCases := []TestCase{
{
caseDesc: "valid entry",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: true,
},
{
caseDesc: "no aad",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: true,
},
{
caseDesc: "missing hash",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
//envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "unparsed message",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
//sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "unparsed public key",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
//keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "missing public key",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
Message: strfmt.Base64([]byte("message")),
//PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "missing unparsed message",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
//Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "missing data",
entry: V001Entry{
CoseObj: models.CoseV001Schema{
/*
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
*/
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "missing cose obj",
entry: V001Entry{
/*
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
*/
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
},
expectSuccess: false,
},
{
caseDesc: "empty obj",
entry: V001Entry{
/*
CoseObj: models.CoseV001Schema{
Data: &models.CoseV001SchemaData{
Aad: strfmt.Base64([]byte("aad")),
},
Message: strfmt.Base64([]byte("message")),
PublicKey: &pubKey,
},
keyObj: keyObj,
sign1Msg: &gocose.Sign1Message{},
envelopeHash: []byte("hash"),
*/
},
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)
}
})
}
}
18 changes: 18 additions & 0 deletions pkg/types/hashedrekord/v0.0.1/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,24 @@ func TestInsertable(t *testing.T) {
},
expectSuccess: false,
},
{
caseDesc: "missing key content",
entry: V001Entry{
HashedRekordObj: models.HashedrekordV001Schema{
Data: &models.HashedrekordV001SchemaData{
Hash: &models.HashedrekordV001SchemaDataHash{
Algorithm: swag.String(models.HashedrekordV001SchemaDataHashAlgorithmSha256),
Value: swag.String("deadbeef"),
},
},
Signature: &models.HashedrekordV001SchemaSignature{
Content: strfmt.Base64("sig"),
PublicKey: &models.HashedrekordV001SchemaSignaturePublicKey{},
},
},
},
expectSuccess: false,
},
{
caseDesc: "missing key content",
entry: V001Entry{
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/helm/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (v V001Entry) Insertable() (bool, error) {
if v.HelmObj.PublicKey == nil {
return false, errors.New("missing public key property")
}
if len(*v.HelmObj.PublicKey.Content) == 0 {
if v.HelmObj.PublicKey.Content == nil || len(*v.HelmObj.PublicKey.Content) == 0 {
return false, errors.New("missing public key content")
}

Expand Down
Loading