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 load public key test #988

Merged
merged 3 commits into from
Apr 10, 2024
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
4 changes: 2 additions & 2 deletions pkg/certificates/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ func LoadPrivateKey(filename string, osWrapper Oser) (*rsa.PrivateKey, error) {
}

// LoadPublicKey loads a single RSA public key from a file.
func LoadPublicKey(filename string) (*rsa.PublicKey, error) {
data, err := LoadFromPEMFile(filename, &OsWrapper{})
func LoadPublicKey(filename string, osWrapper Oser) (*rsa.PublicKey, error) {
data, err := LoadFromPEMFile(filename, osWrapper)
if err != nil {
return nil, err
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/certificates/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,3 +1309,90 @@ func TestLoadPrivateKey(t *testing.T) {
})
}
}

func TestLoadPublicKey(t *testing.T) {
type args struct {
filename string
}

errorSettingUpTypeFormatString := "Error setting up %s: %v"

positivePublicKeyFilename := "public_key_test_filename"
goodPublicKey, err := setupGoodPublicKey()
if err != nil {
t.Errorf(errorSettingUpTypeFormatString, "public key", err)
}

negativeMultipleItemFilename := "negative_multiple_item_test"
multiplePublicKeys := setupGoodPublicKeyPEMData()
multiplePublicKeys = append(multiplePublicKeys, multiplePublicKeys[0])

negativeNoPublicKeyFilename := "negative_no_public_key_test"
noPublicKey := []byte{
0, 0, 0, 0,
}

tests := []struct {
name string
args args
wantOserReadfileArg string
wantOserReadfileResult []byte
want *rsa.PublicKey
wantErr bool
}{
{
name: "Positive Private Key",
args: args{
filename: positivePublicKeyFilename,
},
wantOserReadfileArg: positivePublicKeyFilename,
wantOserReadfileResult: setupGoodPublicKeyPEMData(),
want: goodPublicKey,
wantErr: false,
},
{
name: "Negative multi item test",
args: args{
filename: negativeMultipleItemFilename,
},
wantOserReadfileArg: negativeMultipleItemFilename,
wantOserReadfileResult: multiplePublicKeys,
want: nil,
wantErr: true,
},
{
name: "Negative no private key test",
args: args{
filename: negativeNoPublicKeyFilename,
},
wantOserReadfileArg: negativeNoPublicKeyFilename,
wantOserReadfileResult: noPublicKey,
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

o := mock_certificates.NewMockOser(ctrl)
o.
EXPECT().
ReadFile(gomock.Eq(tt.wantOserReadfileArg)).
Return(tt.wantOserReadfileResult, nil).
Times(1)

got, err := certificates.LoadPublicKey(tt.args.filename, o)
if (err != nil) != tt.wantErr {
t.Errorf("LoadPublicKey() error = %v, wantErr %v", err, tt.wantErr)

return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadPublicKey() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/workceptor/workceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (w *Workceptor) VerifySignature(signature string) error {
if w.VerifyingKey == "" {
return fmt.Errorf("could not verify signature: verifying key not specified")
}
rsaPublicKey, err := certificates.LoadPublicKey(w.VerifyingKey)
rsaPublicKey, err := certificates.LoadPublicKey(w.VerifyingKey, &certificates.OsWrapper{})
if err != nil {
return fmt.Errorf("could not load verifying key file: %s", err.Error())
}
Expand Down
Loading