Skip to content

Commit

Permalink
Merge pull request #339 from aramase/fix-secrets
Browse files Browse the repository at this point in the history
fix: marshal secrets for non node publish secret ref
  • Loading branch information
k8s-ci-robot authored Oct 9, 2020
2 parents 7aec51d + e88b11f commit 6c0187b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
45 changes: 31 additions & 14 deletions pkg/rotation/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ const (
mountRotationCompleteReason = "MountRotationComplete"
k8sSecretRotationFailedReason = "SecretRotationFailed"
k8sSecretRotationCompleteReason = "SecretRotationComplete"

csipodname = "csi.storage.k8s.io/pod.name"
csipodnamespace = "csi.storage.k8s.io/pod.namespace"
csipoduid = "csi.storage.k8s.io/pod.uid"
csipodsa = "csi.storage.k8s.io/serviceAccount.name"
)

// Reconciler reconciles and rotates contents in the pod
Expand Down Expand Up @@ -183,14 +188,6 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid
errorReason = internalerrors.SecretProviderClassNotFound
return fmt.Errorf("failed to get secret provider class %s/%s, err: %+v", spcNamespace, spcName, err)
}
paramsJSON, err := json.Marshal(spc.Spec.Parameters)
if err != nil {
return fmt.Errorf("failed to marshal parameters, err: %+v", err)
}
permissionJSON, err := json.Marshal(permission)
if err != nil {
return fmt.Errorf("failed to marshal permission, err: %+v", err)
}
// get pod from informer cache
podName, podNamespace := spcps.Status.PodName, spcps.Namespace
pod, err := r.store.GetPod(podName, podNamespace)
Expand All @@ -199,6 +196,25 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid
return fmt.Errorf("failed to get pod %s/%s, err: %+v", podNamespace, podName, err)
}

parameters := make(map[string]string)
if spc.Spec.Parameters != nil {
parameters = spc.Spec.Parameters
}
// Set these parameters to mimic the exact same attributes we get as part of NodePublishVolumeRequest
parameters[csipodname] = podName
parameters[csipodnamespace] = podNamespace
parameters[csipoduid] = string(pod.UID)
parameters[csipodsa] = pod.Spec.ServiceAccountName

paramsJSON, err := json.Marshal(parameters)
if err != nil {
return fmt.Errorf("failed to marshal parameters, err: %+v", err)
}
permissionJSON, err := json.Marshal(permission)
if err != nil {
return fmt.Errorf("failed to marshal permission, err: %+v", err)
}

// check if the volume pertaining to the current spc is using nodePublishSecretRef for
// accessing external secrets store
var nodePublishSecretRef *v1.LocalObjectReference
Expand All @@ -217,6 +233,7 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid
}

var secretsJSON []byte
nodePublishSecretData := make(map[string]string)
// read the Kubernetes secret referenced in NodePublishSecretRef and marshal it
// This comprises the secret parameter in the MountRequest to the provider
if nodePublishSecretRef != nil {
Expand All @@ -231,15 +248,15 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid
return fmt.Errorf("failed to get node publish secret %s/%s, err: %+v", secretNamespace, secretName, err)
}

nodePublishSecretData := make(map[string]string)
for k, v := range secret.Data {
nodePublishSecretData[k] = string(v)
}
secretsJSON, err = json.Marshal(nodePublishSecretData)
if err != nil {
r.generateEvent(pod, v1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("failed to marshal node publish secret data, err: %+v", err))
return fmt.Errorf("failed to marshal node publish secret data, err: %+v", err)
}
}

secretsJSON, err = json.Marshal(nodePublishSecretData)
if err != nil {
r.generateEvent(pod, v1.EventTypeWarning, mountRotationFailedReason, fmt.Sprintf("failed to marshal node publish secret data, err: %+v", err))
return fmt.Errorf("failed to marshal node publish secret data, err: %+v", err)
}

// generate a map with the current object versions stored in spc pod status
Expand Down
6 changes: 4 additions & 2 deletions pkg/secrets-store/provider_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func TestMountContent(t *testing.T) {
socketPath: getTempTestDir(t),
attributes: "{}",
targetPath: "/var/lib/kubelet/pods/d448c6a2-cda8-42e3-84fb-3cf75faa8399/volumes/kubernetes.io~csi/secrets-store-inline/mount",
permission: "0644",
permission: "420",
secrets: "{}",
expectedObjectVersion: map[string]string{"secret/secret1": "v1", "secret/secret2": "v2"},
},
}
Expand Down Expand Up @@ -160,7 +161,8 @@ func TestMountContentError(t *testing.T) {
socketPath: getTempTestDir(t),
attributes: "{}",
targetPath: "/var/lib/kubelet/pods/d448c6a2-cda8-42e3-84fb-3cf75faa8399/volumes/kubernetes.io~csi/secrets-store-inline/mount",
permission: "0644",
permission: "420",
secrets: "{}",
expectedErrorCode: "AuthenticationFailed",
},
}
Expand Down
19 changes: 14 additions & 5 deletions provider/fake/fake_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package fake

import (
"context"
"encoding/json"
"fmt"
"net"
"os"

"google.golang.org/grpc"

Expand Down Expand Up @@ -77,18 +79,25 @@ func (m *MockCSIProviderServer) Start() error {

// Mount implements provider csi-provider method
func (m *MockCSIProviderServer) Mount(ctx context.Context, req *v1alpha1.MountRequest) (*v1alpha1.MountResponse, error) {
var attrib, secret map[string]string
var filePermission os.FileMode
var err error

if m.returnErr != nil {
return &v1alpha1.MountResponse{}, m.returnErr
}
if len(req.GetAttributes()) == 0 {
return nil, fmt.Errorf("missing attributes")
if err = json.Unmarshal([]byte(req.GetAttributes()), &attrib); err != nil {
return nil, fmt.Errorf("failed to unmarshal attributes, error: %+v", err)
}
if err = json.Unmarshal([]byte(req.GetSecrets()), &secret); err != nil {
return nil, fmt.Errorf("failed to unmarshal secrets, error: %+v", err)
}
if err = json.Unmarshal([]byte(req.GetPermission()), &filePermission); err != nil {
return nil, fmt.Errorf("failed to unmarshal file permission, error: %+v", err)
}
if len(req.GetTargetPath()) == 0 {
return nil, fmt.Errorf("missing target path")
}
if len(req.GetPermission()) == 0 {
return nil, fmt.Errorf("missing permissions")
}
return &v1alpha1.MountResponse{
ObjectVersion: m.objects,
Error: &v1alpha1.Error{
Expand Down

0 comments on commit 6c0187b

Please sign in to comment.