-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: mittachaitu <[email protected]>
- Loading branch information
mittachaitu
committed
Nov 16, 2021
1 parent
82a18e0
commit 7dcfbb8
Showing
5 changed files
with
300 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,95 @@ | ||
/* | ||
Copyright © 2021 The MayaData Authors | ||
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 rsa | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"encoding/json" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func fakeCreateRSAKeys(size int) (*PrivateKey, *PublicKey, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, size) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return &PrivateKey{PrivateKey: privateKey}, &PublicKey{PublicKey: &privateKey.PublicKey}, nil | ||
} | ||
|
||
func TestSign(t *testing.T) { | ||
privateKey, publicKey, err := fakeCreateRSAKeys(2048) | ||
if err != nil { | ||
t.Fatalf("failed to get test1 RSA keys error: %v", err) | ||
} | ||
mismatchedPrivateKey, _, err := fakeCreateRSAKeys(4096) | ||
if err != nil { | ||
t.Fatalf("failed to get test2 RSA keys error: %v", err) | ||
} | ||
tests := map[string]struct { | ||
data interface{} | ||
privateKey *PrivateKey | ||
publicKey *PublicKey | ||
isErrExpected bool | ||
isUnsignErrExpected bool | ||
}{ | ||
"When valid data is given to sign": { | ||
data: &corev1.PersistentVolume{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "pv1"}, | ||
}, | ||
privateKey: privateKey, | ||
publicKey: publicKey, | ||
}, | ||
"When empty data is given to sign": { | ||
privateKey: privateKey, | ||
publicKey: publicKey, | ||
isErrExpected: false, | ||
}, | ||
"When different public key is used to verify signing": { | ||
data: &corev1.PersistentVolume{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "pv2"}, | ||
}, | ||
privateKey: mismatchedPrivateKey, | ||
publicKey: publicKey, | ||
isUnsignErrExpected: true, | ||
}, | ||
} | ||
for name, test := range tests { | ||
signedBytes, err := test.privateKey.Sign(test.data) | ||
if err != nil && !test.isErrExpected { | ||
t.Fatalf("%q test failed expected error not to occur but got error %v", name, err) | ||
} | ||
if err == nil && test.isErrExpected { | ||
t.Fatalf("%q test failed expected error to occur", name) | ||
} | ||
if signedBytes != nil { | ||
dataInBytes, err := json.Marshal(test.data) | ||
if err != nil { | ||
t.Fatalf("%q test failed while marshaling the data", name) | ||
} | ||
unsignErr := test.publicKey.Unsign(dataInBytes, signedBytes) | ||
if unsignErr != nil && !test.isUnsignErrExpected { | ||
t.Fatalf("%q test failed expected error not to occur while unsigning error %v", name, unsignErr) | ||
} | ||
if unsignErr == nil && test.isUnsignErrExpected { | ||
t.Fatalf("%q test failed expected error to occur while unsigning", name) | ||
} | ||
} | ||
} | ||
} |
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,86 @@ | ||
/* | ||
Copyright © 2021 The MayaData Authors | ||
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 sign | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func fakeCreateRSAPrivateKey(path string) (*rsa.PrivateKey, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if path != "" { | ||
privDer := x509.MarshalPKCS1PrivateKey(privateKey) | ||
privBlock := pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Headers: nil, | ||
Bytes: privDer, | ||
} | ||
privKeyBytes := pem.EncodeToMemory(&privBlock) | ||
err = ioutil.WriteFile(path, privKeyBytes, 0600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return privateKey, nil | ||
} | ||
|
||
func TestLoadPrivateKeyFromPath(t *testing.T) { | ||
testDir, err := os.MkdirTemp(os.TempDir(), "rsa-keys") | ||
if err != nil { | ||
t.Fatalf("failed to create temporary directory, error: %v", err) | ||
} | ||
tests := map[string]struct { | ||
path string | ||
isErrExpected bool | ||
}{ | ||
"When private key exist in RSA PEM format": { | ||
path: func(path string) string { | ||
_, err = fakeCreateRSAPrivateKey(path) | ||
if err != nil { | ||
t.Fatalf("failed to create RSA public key") | ||
} | ||
return path | ||
}(filepath.Join(testDir, "valid_public_rsa")), | ||
}, | ||
"When invalid path is given to load private key": { | ||
path: filepath.Join(testDir, "invalid_public_key"), | ||
isErrExpected: true, | ||
}, | ||
"When path is empty to load private key": { | ||
path: "", | ||
}, | ||
} | ||
for name, test := range tests { | ||
_, err := LoadPrivateKeyFromPath(test.path) | ||
if err != nil && !test.isErrExpected { | ||
t.Fatalf("%s test failed expected error not to occur but got error %v", name, err) | ||
} | ||
if err == nil && test.isErrExpected { | ||
t.Fatalf("%s test failed expected error to occur", name) | ||
} | ||
} | ||
os.RemoveAll(testDir) | ||
} |
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,117 @@ | ||
/* | ||
Copyright © 2021 The MayaData Authors | ||
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 sign | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
func fakeCreateRSAPublicKey(path string) (*rsa.PublicKey, error) { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if path != "" { | ||
publicDer := x509.MarshalPKCS1PublicKey(&privateKey.PublicKey) | ||
pubBlock := pem.Block{ | ||
Type: "PUBLIC KEY", | ||
Headers: nil, | ||
Bytes: publicDer, | ||
} | ||
publicKeyBytes := pem.EncodeToMemory(&pubBlock) | ||
err = ioutil.WriteFile(path, publicKeyBytes, 0600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &privateKey.PublicKey, nil | ||
} | ||
|
||
func fakeCreateSSHPublicKey(path string) error { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
publicRsaKey, err := ssh.NewPublicKey(&privateKey.PublicKey) | ||
if err != nil { | ||
return err | ||
} | ||
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey) | ||
err = ioutil.WriteFile(path, pubKeyBytes, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func TestLoadPublicKeyFromPath(t *testing.T) { | ||
testDir, err := os.MkdirTemp(os.TempDir(), "rsa-keys") | ||
if err != nil { | ||
t.Fatalf("failed to create temporary directory, error: %v", err) | ||
} | ||
tests := map[string]struct { | ||
path string | ||
isErrExpected bool | ||
}{ | ||
"When public key exist in RSA PEM format": { | ||
path: func(path string) string { | ||
_, err = fakeCreateRSAPublicKey(path) | ||
if err != nil { | ||
t.Fatalf("failed to create RSA public key") | ||
} | ||
return path | ||
}(filepath.Join(testDir, "valid_public_rsa")), | ||
}, | ||
"When invalid path is given to load public key": { | ||
path: filepath.Join(testDir, "invalid_public_key"), | ||
isErrExpected: true, | ||
}, | ||
"When path is not provided": { | ||
path: "", | ||
}, | ||
"When public key exist in SSH format": { | ||
path: func(path string) string { | ||
err = fakeCreateSSHPublicKey(path) | ||
if err != nil { | ||
t.Fatalf("failed to create SSH public key") | ||
} | ||
return path | ||
}(filepath.Join(testDir, "valid_public_ssh_rsa")), | ||
isErrExpected: true, | ||
}, | ||
} | ||
for name, test := range tests { | ||
_, err := LoadPublicKeyFromPath(test.path) | ||
if err != nil && !test.isErrExpected { | ||
t.Fatalf("%s test failed expected error not to occur but got error %v", name, err) | ||
} | ||
if err == nil && test.isErrExpected { | ||
t.Fatalf("%s test failed expected error to occur", name) | ||
} | ||
} | ||
os.RemoveAll(testDir) | ||
} |
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