mirror of https://github.com/docker/docs.git
254 lines
7.7 KiB
Go
254 lines
7.7 KiB
Go
package trustmanager
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAddKey(t *testing.T) {
|
|
testName := "docker.com/notary/root"
|
|
testExt := "key"
|
|
|
|
// Temporary directory where test files will be created
|
|
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
|
|
if err != nil {
|
|
t.Fatalf("failed to create a temporary directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempBaseDir)
|
|
|
|
// Since we're generating this manually we need to add the extension '.'
|
|
expectedFilePath := filepath.Join(tempBaseDir, testName+"."+testExt)
|
|
|
|
// Create our store
|
|
store, err := NewKeyFileStore(tempBaseDir)
|
|
if err != nil {
|
|
t.Fatalf("failed to create new key filestore: %v", err)
|
|
}
|
|
|
|
privKey, err := GenerateRSAKey(rand.Reader, 512)
|
|
if err != nil {
|
|
t.Fatalf("could not generate private key: %v", err)
|
|
}
|
|
|
|
// Call the AddKey function
|
|
err = store.AddKey(testName, privKey)
|
|
if err != nil {
|
|
t.Fatalf("failed to add file to store: %v", err)
|
|
}
|
|
|
|
// Check to see if file exists
|
|
b, err := ioutil.ReadFile(expectedFilePath)
|
|
if err != nil {
|
|
t.Fatalf("expected file not found: %v", err)
|
|
}
|
|
|
|
if !strings.Contains(string(b), "-----BEGIN RSA PRIVATE KEY-----") {
|
|
t.Fatalf("expected private key content in the file: %s", expectedFilePath)
|
|
}
|
|
}
|
|
|
|
func TestGet(t *testing.T) {
|
|
testData := []byte(`-----BEGIN RSA PRIVATE KEY-----
|
|
MIIEogIBAAKCAQEAyUIXjsrWRrvPa4Bzp3VJ6uOUGPay2fUpSV8XzNxZxIG/Opdr
|
|
+k3EQi1im6WOqF3Y5AS1UjYRxNuRN+cAZeo3uS1pOTuoSupBXuchVw8s4hZJ5vXn
|
|
TRmGb+x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V5qJj6arxe49NqhwdESrFohrKaIP
|
|
kM2l/o2QaWRuRT/Pvl2Xqsrhmh0QSOQjGCYVfOb10nAHVIRHLY22W4o1jk+piLBo
|
|
a+1+74NRaOGAnu1J6/fRKQKBgAF180+dmlzemjqFlFCxsR/4G8s2r4zxTMXdF+6O
|
|
3zKuj8MbsqgCZy7e8qNeARxwpCJmoYy7dITNqJ5SOGSzrb2Trn9ClP+uVhmR2SH6
|
|
AlGQlIhPn3JNzI0XVsLIloMNC13ezvDE/7qrDJ677EQQtNEKWiZh1/DrsmHr+irX
|
|
EkqpAoGAJWe8PC0XK2RE9VkbSPg9Ehr939mOLWiHGYTVWPttUcum/rTKu73/X/mj
|
|
WxnPWGtzM1pHWypSokW90SP4/xedMxludvBvmz+CTYkNJcBGCrJumy11qJhii9xp
|
|
EMl3eFOJXjIch/wIesRSN+2dGOsl7neercjMh1i9RvpCwHDx/E0=
|
|
-----END RSA PRIVATE KEY-----
|
|
`)
|
|
testName := "docker.com/notary/root"
|
|
testExt := "key"
|
|
perms := os.FileMode(0755)
|
|
|
|
// Temporary directory where test files will be created
|
|
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
|
|
if err != nil {
|
|
t.Fatalf("failed to create a temporary directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempBaseDir)
|
|
|
|
// Since we're generating this manually we need to add the extension '.'
|
|
filePath := filepath.Join(tempBaseDir, testName+"."+testExt)
|
|
|
|
os.MkdirAll(filepath.Dir(filePath), perms)
|
|
if err = ioutil.WriteFile(filePath, testData, perms); err != nil {
|
|
t.Fatalf("Failed to write test file: %v", err)
|
|
}
|
|
|
|
// Create our store
|
|
store, err := NewKeyFileStore(tempBaseDir)
|
|
if err != nil {
|
|
t.Fatalf("failed to create new key filestore: %v", err)
|
|
}
|
|
|
|
// Call the GetKey function
|
|
privKey, err := store.GetKey(testName)
|
|
if err != nil {
|
|
t.Fatalf("failed to get file from store: %v", err)
|
|
}
|
|
|
|
pemPrivKey, err := KeyToPEM(privKey)
|
|
if err != nil {
|
|
t.Fatalf("failed to convert key to PEM: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(testData, pemPrivKey) {
|
|
t.Fatalf("unexpected content in the file: %s", filePath)
|
|
}
|
|
}
|
|
|
|
func TestAddEncryptedAndGetDecrypted(t *testing.T) {
|
|
testExt := "key"
|
|
|
|
// Temporary directory where test files will be created
|
|
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
|
|
if err != nil {
|
|
t.Fatalf("failed to create a temporary directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempBaseDir)
|
|
|
|
// Create our FileStore
|
|
store, err := NewKeyFileStore(tempBaseDir)
|
|
if err != nil {
|
|
t.Fatalf("failed to create new key filestore: %v", err)
|
|
}
|
|
|
|
// Generate new PrivateKey
|
|
privKey, err := GenerateRSAKey(rand.Reader, 512)
|
|
if err != nil {
|
|
t.Fatalf("could not generate private key: %v", err)
|
|
}
|
|
|
|
// Call the AddEncryptedKey function
|
|
err = store.AddEncryptedKey(privKey.ID(), privKey, "diogomonica")
|
|
if err != nil {
|
|
t.Fatalf("failed to add file to store: %v", err)
|
|
}
|
|
|
|
// Since we're generating this manually we need to add the extension '.'
|
|
expectedFilePath := filepath.Join(tempBaseDir, privKey.ID()+"."+testExt)
|
|
|
|
// Check to see if file exists
|
|
_, err = ioutil.ReadFile(expectedFilePath)
|
|
if err != nil {
|
|
t.Fatalf("expected file not found: %v", err)
|
|
}
|
|
|
|
// Call the GetDecryptedKey function
|
|
readPrivKey, err := store.GetDecryptedKey(privKey.ID(), "diogomonica")
|
|
if err != nil {
|
|
t.Fatalf("could not decrypt private key: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(privKey.Private(), readPrivKey.Private()) {
|
|
t.Fatalf("written key and loaded key do not match")
|
|
}
|
|
}
|
|
|
|
func TestGetDecryptedWithTamperedCipherText(t *testing.T) {
|
|
testExt := "key"
|
|
|
|
// Temporary directory where test files will be created
|
|
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
|
|
if err != nil {
|
|
t.Fatalf("failed to create a temporary directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempBaseDir)
|
|
|
|
// Create our FileStore
|
|
store, err := NewKeyFileStore(tempBaseDir)
|
|
if err != nil {
|
|
t.Fatalf("failed to create new key filestore: %v", err)
|
|
}
|
|
|
|
// Generate a new Private Key
|
|
privKey, err := GenerateRSAKey(rand.Reader, 512)
|
|
if err != nil {
|
|
t.Fatalf("could not generate private key: %v", err)
|
|
}
|
|
|
|
// Call the AddEncryptedKey function
|
|
err = store.AddEncryptedKey(privKey.ID(), privKey, "diogomonica")
|
|
if err != nil {
|
|
t.Fatalf("failed to add file to store: %v", err)
|
|
}
|
|
|
|
// Since we're generating this manually we need to add the extension '.'
|
|
expectedFilePath := filepath.Join(tempBaseDir, privKey.ID()+"."+testExt)
|
|
|
|
// Get file description, open file
|
|
fp, err := os.OpenFile(expectedFilePath, os.O_WRONLY, 0600)
|
|
if err != nil {
|
|
t.Fatalf("expected file not found: %v", err)
|
|
}
|
|
|
|
// Tamper the file
|
|
fp.WriteAt([]byte("a"), int64(1))
|
|
|
|
// Try to decrypt the file
|
|
_, err = store.GetDecryptedKey(privKey.ID(), "diogomonica")
|
|
if err == nil {
|
|
t.Fatalf("expected error while decrypting the content due to invalid cipher text")
|
|
}
|
|
}
|
|
|
|
func TestGetDecryptedWithInvalidPassphrase(t *testing.T) {
|
|
testName := "docker.com/notary/root"
|
|
|
|
// Temporary directory where test files will be created
|
|
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
|
|
if err != nil {
|
|
t.Fatalf("failed to create a temporary directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempBaseDir)
|
|
|
|
// Create our FileStore
|
|
store, err := NewKeyFileStore(tempBaseDir)
|
|
if err != nil {
|
|
t.Fatalf("failed to create new key filestore: %v", err)
|
|
}
|
|
|
|
// Generate a new random RSA Key
|
|
privKey, err := GenerateRSAKey(rand.Reader, 512)
|
|
if err != nil {
|
|
t.Fatalf("could not generate private key: %v", err)
|
|
}
|
|
|
|
// Call the AddEncryptedKey function
|
|
err = store.AddEncryptedKey(privKey.ID(), privKey, "diogomonica")
|
|
if err != nil {
|
|
t.Fatalf("failed to add file to stoAFre: %v", err)
|
|
}
|
|
|
|
// Try to decrypt the file with an invalid passphrase
|
|
_, err = store.GetDecryptedKey(testName, "diegomonica")
|
|
if err == nil {
|
|
t.Fatalf("expected error while decrypting the content due to invalid passphrase")
|
|
}
|
|
}
|