Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept and seal stringData into secret #221

Merged
merged 1 commit into from
Aug 26, 2019
Merged
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 cmd/kubeseal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func seal(in io.Reader, out io.Writer, codecs runtimeserializer.CodecFactory, pu
return err
}

if len(secret.Data) == 0 {
if len(secret.Data) == 0 && len(secret.StringData) == 0 {
// No data. This is _theoretically_ just fine, but
// almost certainly indicates a misuse of the tools.
// If you _really_ want to encrypt an empty secret,
Expand Down
6 changes: 6 additions & 0 deletions cmd/kubeseal/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func TestSeal(t *testing.T) {
Data: map[string][]byte{
"foo": []byte("sekret"),
},
StringData: map[string]string{
"foos": string("stringsekret"),
},
}

info, ok := runtime.SerializerInfoForMediaType(scheme.Codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
Expand Down Expand Up @@ -170,5 +173,8 @@ func TestSeal(t *testing.T) {
if len(result.Spec.EncryptedData["foo"]) < 100 {
t.Errorf("Encrypted data is implausibly short: %v", result.Spec.EncryptedData)
}
if len(result.Spec.EncryptedData["foos"]) < 100 {
t.Errorf("Encrypted data is implausibly short: %v", result.Spec.EncryptedData)
}
// NB: See sealedsecret_test.go for e2e crypto test
}
8 changes: 8 additions & 0 deletions pkg/apis/sealed-secrets/v1alpha1/sealedsecret_expansion.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ func NewSealedSecret(codecs runtimeserializer.CodecFactory, pubKey *rsa.PublicKe
s.Spec.EncryptedData[key] = base64.StdEncoding.EncodeToString(ciphertext)
}

for key, value := range secret.StringData {
ciphertext, err := crypto.HybridEncrypt(rand.Reader, pubKey, []byte(value), label)
if err != nil {
return nil, err
}
s.Spec.EncryptedData[key] = base64.StdEncoding.EncodeToString(ciphertext)
}

if clusterWide {
if s.Annotations == nil {
s.Annotations = map[string]string{}
Expand Down
53 changes: 53 additions & 0 deletions pkg/apis/sealed-secrets/v1alpha1/sealedsecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,59 @@ func TestSealRoundTrip(t *testing.T) {
}
}

func TestSealRoundTripStringDataConversion(t *testing.T) {
scheme := runtime.NewScheme()
codecs := serializer.NewCodecFactory(scheme)

SchemeBuilder.AddToScheme(scheme)
v1.SchemeBuilder.AddToScheme(scheme)

rand := testRand()
key, err := rsa.GenerateKey(rand, 2048)
if err != nil {
t.Fatalf("Failed to generate test key: %v", err)
}

secret := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "myname",
Namespace: "myns",
},
Data: map[string][]byte{
"foo": []byte("bar"),
"fss": []byte("brr"),
},
StringData: map[string]string{
"fss": "baa",
},
}

unsealed := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "myname",
Namespace: "myns",
},
Data: map[string][]byte{
"foo": []byte("bar"),
"fss": []byte("baa"),
},
}

ssecret, err := NewSealedSecret(codecs, &key.PublicKey, &secret)
if err != nil {
t.Fatalf("NewSealedSecret returned error: %v", err)
}

secret2, err := ssecret.Unseal(codecs, key)
if err != nil {
t.Fatalf("Unseal returned error: %v", err)
}

if !reflect.DeepEqual(unsealed.Data, secret2.Data) {
t.Errorf("Unsealed secret != original secret: %v != %v", unsealed, secret2)
}
}

func TestSealRoundTripWithClusterWide(t *testing.T) {
scheme := runtime.NewScheme()
codecs := serializer.NewCodecFactory(scheme)
Expand Down