1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00
external-secrets/pkg/template/v2/pkcs12_test.go
Mathias Maes a12f3b8292
Add CA-Bundle to pemToPkcs12 output (#3494)
* Add CA-Bundle to pemToPkcs12 output

Signed-off-by: Mathias Maes <mathias.maes@aloxy.io>

* add fullPemToPkcs12 and tests

Signed-off-by: Mathias Maes <mathias.maes@aloxy.io>

---------

Signed-off-by: Mathias Maes <mathias.maes@aloxy.io>
2024-06-03 22:35:02 +02:00

63 lines
9 KiB
Go

/*
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 template
import (
"testing"
gopkcs12 "software.sslmate.com/src/go-pkcs12"
)
const (
expectedPfx string = "MIIKAQIBAzCCCbEGCSqGSIb3DQEHAaCCCaIEggmeMIIJmjCCBAgGCSqGSIb3DQEHBqCCA/kwggP1AgEAMIID7gYJKoZIhvcNAQcBMF0GCSqGSIb3DQEFDTBQMC8GCSqGSIb3DQEFDDAiBBAiMDEyMzQ1Njc4OSIAAAAAAgIIADAKBggqhkiG9w0CCTAdBglghkgBZQMEASoEECIwMTIzNDU2Nzg5IgAAAACAggOA3PfruBHwXSLGGGcI6woqtR/A8eBKsYZt0r6kpLf9w3aC9Idic93fY4lPhf2v3d/tin7CZwboeiT6H4uM5s+TrYWWbZIcHh67femCfMjbwnNSkOuzZOFmprJUUEtdZhP8ifDh0NcqT//0KmwRIYaV4JPxmGFfblRFSaLg0daKfCFKIE7onJMa/uy0i4IREzserCNNSZezCNOdtSfWdvjKrIlCFGopjv6iO1oG1qT6JN7YWDZNzgDposmtkIqwP+tNn+40iyv56uVMXZPJHxWxQMhvo24Iq7Qtni+TE+rLm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Xr2IEai+A/q7xdVdiy//vaS80wHoCSAvvSjpYNg0w0zNiFQ/2Ty+aQg4qHVazocMUlhj2XMWd1vfIpoQSWggTjhYcdIK5z1EsmTAiXK7xHvVOpLI6QVqKHrHBKhmd01Yf/QQyYpvB2/GxlmiXy7WTMFfY3IqP8w6pRQEApKmTUYpx9qVRp2l55R8hOBR9RW2OQGKm10dZRgg87qGbVwbzz3cPx/fPKoGhDu7o7qRwNrKXAbCCjV4eSxoRASFbQUQzJ/Bi6uvZgA8Z42xZXddkFcoGyJZCoi+550QSypEjoRvVy76ff1xxRF+aS9Au3DJKYHQw78018/TdT/62vFI6u92huEcxJTAjBgkqhkiG9w0BCRUxFgQUOEXV6IFYGpCSHi0MPHz4b3W0KOQwRzAvMAsGCWCGSAFlAwQCAQQgcZGaBMgQmJC2O0UWNjaW0aQbipFFKCFVHNmLLObEbKwEECIwMTIzNDU2Nzg5IgAAAAACAggA"
)
func TestPemToPkcs12(t *testing.T) {
// Mock the random generator, to guarantee to always generate the same output
gopkcs12.Modern = gopkcs12.Modern.WithRand(MockRandomReader{})
out, err := pemToPkcs12Pass(certData, keyData, "password")
if err != nil {
t.Errorf("pemToPkcs12Pass() got error '%v', expected none", err)
return
}
if out != expectedPfx {
t.Errorf("pemToPkcs12Pass() got '%s', expected '%s'", out, expectedPfx)
}
}
func TestFullPemToPkcs12(t *testing.T) {
// Mock the random generator, to guarantee to always generate the same output
gopkcs12.Modern = gopkcs12.Modern.WithRand(MockRandomReader{})
out, err := fullPemToPkcs12Pass(certData+"\n"+otherCert, keyData, "password")
if err != nil {
t.Errorf("pemToPkcs12Pass() got error '%v', expected none", err)
return
}
if out != expectedPfxCa {
t.Errorf("pemToPkcs12Pass() got '%s', expected '%s'", out, expectedPfxCa)
}
}
type MockRandomReader struct{}
func (r MockRandomReader) Read(p []byte) (int, error) {
copy(p, `"0123456789"`)
return len(p), nil
}