Skip to content
Open
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
2 changes: 1 addition & 1 deletion pkg/apis/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func setDefaultIfEmpty(attr map[string]string, k, v string) {
// in the attributes at all. If the other attributes are present, then a
// validation error will be picked up by validation.
func setDefaultKeyStorePKCS12(attr map[string]string) {
if _, ok := attr[csiapi.KeyStorePKCS12EnableKey]; ok {
if attr[csiapi.KeyStorePKCS12EnableKey] == "true" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: this is a sharp edge if anyone set the value to "TRUE" or similar. Could we lowercase the attribute?

Suggested change
if attr[csiapi.KeyStorePKCS12EnableKey] == "true" {
if strings.ToLower(attr[csiapi.KeyStorePKCS12EnableKey]) == "true" {

setDefaultIfEmpty(attr, csiapi.KeyStorePKCS12FileKey, "keystore.p12")
}
}
22 changes: 19 additions & 3 deletions pkg/apis/defaults/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,31 @@ func Test_pkcs12Values(t *testing.T) {
input: map[string]string{},
expOutput: map[string]string{},
},
"if PKCS12 enable attribute present, expect PKCS12 attributes present": {
"if PKCS12 is enabled, expect PKCS12 file attribute present": {
input: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "foo",
"csi.cert-manager.io/pkcs12-enable": "true",
},
expOutput: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "foo",
"csi.cert-manager.io/pkcs12-enable": "true",
"csi.cert-manager.io/pkcs12-filename": "keystore.p12",
},
},
"if PKCS12 is disabled, expect no PKCS12 file attribute": {
input: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "false",
},
expOutput: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "false",
},
},
"if PKCS12 enable attribute is invalid, expect no PKCS12 file attribute": {
input: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "foo",
},
expOutput: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "foo",
},
},
}

for name, test := range tests {
Expand Down
17 changes: 8 additions & 9 deletions pkg/apis/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,18 @@ func pkcs12Values(path *field.Path, attr map[string]string) field.ErrorList {
var el field.ErrorList

if enable := attr[csiapi.KeyStorePKCS12EnableKey]; len(enable) > 0 {
if file := attr[csiapi.KeyStorePKCS12FileKey]; len(file) == 0 {
el = append(el, field.Required(path.Child(csiapi.KeyStorePKCS12FileKey), "required attribute when PKCS12 KeyStore is enabled"))
}
if password := attr[csiapi.KeyStorePKCS12PasswordKey]; len(password) == 0 {
el = append(el, field.Required(path.Child(csiapi.KeyStorePKCS12PasswordKey), "required attribute when PKCS12 KeyStore is enabled"))
}

switch enable {
case "false", "true":
case "true":
if file := attr[csiapi.KeyStorePKCS12FileKey]; len(file) == 0 {
el = append(el, field.Required(path.Child(csiapi.KeyStorePKCS12FileKey), "required attribute when PKCS12 KeyStore is enabled"))
}
if password := attr[csiapi.KeyStorePKCS12PasswordKey]; len(password) == 0 {
el = append(el, field.Required(path.Child(csiapi.KeyStorePKCS12PasswordKey), "required attribute when PKCS12 KeyStore is enabled"))
}
case "false":
default:
el = append(el, field.NotSupported(path.Child(csiapi.KeyStorePKCS12EnableKey), enable, []string{"true", "false"}))
}

} else {
// No PKCS12 attributes should be defined when PKCS12 is not defined.

Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ func Test_PKCS12Values(t *testing.T) {
},
expErr: nil,
},
"if key and password are not defined, and enabled is defined as false, expect no error": {
attr: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "false",
},
expErr: nil,
},
"if key and password is defined, but enabled is defined as foo, expect error": {
attr: map[string]string{
"csi.cert-manager.io/pkcs12-enable": "foo",
Expand Down
21 changes: 21 additions & 0 deletions pkg/filestore/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,27 @@ func Test_WriteKeypair(t *testing.T) {
},
expErr: false,
},
"keystore PKCS12 explicitly disabled should not require password": {
testBundle: pkcs8Bundle,
meta: metadata.Metadata{
VolumeID: "vol-id",
TargetPath: "/target-path",
VolumeContext: map[string]string{
"csi.cert-manager.io/issuer-name": "ca-issuer",
"csi.cert-manager.io/key-encoding": "PKCS8",
"csi.cert-manager.io/pkcs12-enable": "false",
},
},
expFiles: map[string][]byte{
"ca.crt": pkcs8Bundle.caPEM,
"tls.crt": pkcs8Bundle.certPEM,
"tls.key": pkcs8Bundle.pkPEM,
"metadata.json": []byte(
`{"volumeID":"vol-id","targetPath":"/target-path","nextIssuanceTime":"1970-01-03T00:00:00Z","volumeContext":{"csi.cert-manager.io/issuer-name":"ca-issuer","csi.cert-manager.io/key-encoding":"PKCS8","csi.cert-manager.io/pkcs12-enable":"false"}}`,
),
},
expErr: false,
},
"keystore PKCS12 with no password and breakout file should error": {
testBundle: pkcs8Bundle,
meta: metadata.Metadata{
Expand Down