forked from bitnami-labs/sealed-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit fixed the unit tests such that "make test" passes. It does not add any new tests, and only modifies TestInitKey to test initKeyRegistry instead. It does not test any new functionality. This commit also removes the redundant newKey function since generatePrivateKeyAndCert does the same job.
- Loading branch information
Showing
8 changed files
with
144 additions
and
129 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
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,104 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"io" | ||
mathrand "math/rand" | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
certUtil "k8s.io/client-go/util/cert" | ||
) | ||
|
||
// This is omg-not safe for real crypto use! | ||
func testRand() io.Reader { | ||
return mathrand.New(mathrand.NewSource(42)) | ||
} | ||
|
||
func TestReadKey(t *testing.T) { | ||
rand := testRand() | ||
|
||
key, err := rsa.GenerateKey(rand, 512) | ||
if err != nil { | ||
t.Fatalf("Failed to generate test key: %v", err) | ||
} | ||
|
||
cert, err := signKey(rand, key) | ||
if err != nil { | ||
t.Fatalf("Failed to self-sign key: %v", err) | ||
} | ||
|
||
secret := v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "mykey", | ||
Namespace: "myns", | ||
}, | ||
Data: map[string][]byte{ | ||
v1.TLSPrivateKeyKey: certUtil.EncodePrivateKeyPEM(key), | ||
v1.TLSCertKey: certUtil.EncodeCertPEM(cert), | ||
}, | ||
Type: v1.SecretTypeTLS, | ||
} | ||
|
||
client := fake.NewSimpleClientset(&secret) | ||
|
||
key2, _, err := readKey(client, "myns", "mykey") | ||
if err != nil { | ||
t.Errorf("readKey() failed with: %v", err) | ||
} | ||
|
||
t.Logf("actions: %v", client.Actions()) | ||
|
||
if !reflect.DeepEqual(key, key2) { | ||
t.Errorf("Fetched key != original key: %v != %v", key, key2) | ||
} | ||
} | ||
|
||
func TestWriteKey(t *testing.T) { | ||
rand := testRand() | ||
key, err := rsa.GenerateKey(rand, 512) | ||
if err != nil { | ||
t.Fatalf("Failed to generate test key: %v", err) | ||
} | ||
|
||
cert, err := signKey(rand, key) | ||
if err != nil { | ||
t.Fatalf("signKey failed: %v", err) | ||
} | ||
|
||
client := fake.NewSimpleClientset() | ||
|
||
if err := writeKey(client, key, []*x509.Certificate{cert}, "myns", "mykey"); err != nil { | ||
t.Errorf("writeKey() failed with: %v", err) | ||
} | ||
|
||
t.Logf("actions: %v", client.Actions()) | ||
|
||
if a := findAction(client, "create", "secrets"); a == nil { | ||
t.Errorf("writeKey didn't create a secret") | ||
} else if a.GetNamespace() != "myns" { | ||
t.Errorf("writeKey() created key in wrong namespace!") | ||
} | ||
} | ||
|
||
func TestSignKey(t *testing.T) { | ||
rand := testRand() | ||
|
||
key, err := rsa.GenerateKey(rand, 512) | ||
if err != nil { | ||
t.Fatalf("Failed to generate test key: %v", err) | ||
} | ||
|
||
cert, err := signKey(rand, key) | ||
if err != nil { | ||
t.Errorf("signKey() returned error: %v", err) | ||
} | ||
|
||
if !reflect.DeepEqual(cert.PublicKey, &key.PublicKey) { | ||
t.Errorf("cert pubkey != original pubkey") | ||
} | ||
} |
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
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
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,24 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 | ||
.glide/ | ||
|
||
/controller | ||
/kubeseal | ||
/controller.image | ||
/*-static | ||
/controller.yaml | ||
/sealedsecret-crd.yaml | ||
/docker/controller | ||
*.iml | ||
.idea |