-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validation for tls private key and cert file values (#771)
* add validation for tls private key and cert file values
- Loading branch information
Devang Gaur
authored
May 18, 2021
1 parent
1d7e5b9
commit dc0b428
Showing
3 changed files
with
119 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
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 httpserver | ||
|
||
import "fmt" | ||
|
||
func (g *APIServer) validateFiles(privateKeyFile, certFile string) error { | ||
keylength := len(privateKeyFile) | ||
certlength := len(certFile) | ||
|
||
if keylength > 0 && certlength == 0 { | ||
return fmt.Errorf("private key file provided but certficate file missing") | ||
} else if keylength == 0 && certlength > 0 { | ||
return fmt.Errorf("certificate file provided but private key file missing") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright (C) 2020 Accurics, Inc. | ||
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 httpserver | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestValidateFiles(t *testing.T) { | ||
server := APIServer{} | ||
table := []struct { | ||
name string | ||
privateKeyFile string | ||
certFile string | ||
wantOutput interface{} | ||
wantErr error | ||
}{ | ||
{ | ||
name: "both file names provided", | ||
privateKeyFile: "key", | ||
certFile: "cert", | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "privatekey filename absent", | ||
privateKeyFile: "", | ||
certFile: "server.crt", | ||
wantErr: fmt.Errorf("certificate file provided but private key file missing"), | ||
}, | ||
{ | ||
name: "both file names blank", | ||
privateKeyFile: "", | ||
certFile: "", | ||
wantErr: nil, | ||
}, | ||
{ | ||
name: "cert filename absent", | ||
privateKeyFile: "keyfile", | ||
certFile: "", | ||
wantErr: fmt.Errorf("private key file provided but certficate file missing"), | ||
}, | ||
} | ||
|
||
for _, tt := range table { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotErr := server.validateFiles(tt.privateKeyFile, tt.certFile) | ||
if !reflect.DeepEqual(gotErr, tt.wantErr) { | ||
if tt.wantErr != nil && gotErr != nil && tt.wantErr.Error() != gotErr.Error() { | ||
t.Errorf("error got: '%v', want: '%v'", gotErr, tt.wantErr) | ||
} | ||
} | ||
}) | ||
} | ||
} |