Skip to content

Commit

Permalink
Add secret restore item action to handle service account token secret
Browse files Browse the repository at this point in the history
Add secret restore item action to handle service account token secret:
1. Skip the restoration for the default service account token secret
2. Remove several fields for non-default service account token secret to make sure the secret can be restored

Signed-off-by: Wenkai Yin(尹文开) <[email protected]>
  • Loading branch information
ywk253100 committed Feb 8, 2023
1 parent 7139daf commit a60ef4e
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelogs/unreleased/5843-ywk253100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add secret restore item action to handle service account token secret
7 changes: 6 additions & 1 deletion pkg/cmd/server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func NewCommand(f client.Factory) *cobra.Command {
RegisterRestoreItemAction("velero.io/crd-preserve-fields", newCRDV1PreserveUnknownFieldsItemAction).
RegisterRestoreItemAction("velero.io/change-pvc-node-selector", newChangePVCNodeSelectorItemAction(f)).
RegisterRestoreItemAction("velero.io/apiservice", newAPIServiceRestoreItemAction).
RegisterRestoreItemAction("velero.io/admission-webhook-configuration", newAdmissionWebhookConfigurationAction)
RegisterRestoreItemAction("velero.io/admission-webhook-configuration", newAdmissionWebhookConfigurationAction).
RegisterRestoreItemAction("velero.io/secret", newSecretRestoreItemAction)
if !features.IsEnabled(velerov1api.APIGroupVersionsFeatureFlag) {
// Do not register crd-remap-version BIA if the API Group feature flag is enabled, so that the v1 CRD can be backed up
pluginServer = pluginServer.RegisterBackupItemAction("velero.io/crd-remap-version", newRemapCRDVersionAction(f))
Expand Down Expand Up @@ -234,3 +235,7 @@ func newAPIServiceRestoreItemAction(logger logrus.FieldLogger) (interface{}, err
func newAdmissionWebhookConfigurationAction(logger logrus.FieldLogger) (interface{}, error) {
return restore.NewAdmissionWebhookConfigurationAction(logger), nil
}

func newSecretRestoreItemAction(logger logrus.FieldLogger) (interface{}, error) {
return restore.NewSecretAction(logger), nil
}
94 changes: 94 additions & 0 deletions pkg/restore/secret_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright The Velero Contributors.
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 restore

import (
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"github.com/vmware-tanzu/velero/pkg/plugin/velero"
"github.com/vmware-tanzu/velero/pkg/util/kube"
)

// SecretAction is a restore item action for secrets
type SecretAction struct {
logger logrus.FieldLogger
}

// NewSecretAction creates a new SecretAction instance
func NewSecretAction(logger logrus.FieldLogger) *SecretAction {
return &SecretAction{logger: logger}
}

// AppliesTo indicates which resources this action applies
func (s *SecretAction) AppliesTo() (velero.ResourceSelector, error) {
return velero.ResourceSelector{
IncludedResources: []string{"secrets"},
}, nil
}

// Execute the action
func (s *SecretAction) Execute(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) {
s.logger.Info("Executing SecretAction")
defer s.logger.Info("Done executing SecretAction")

var secret corev1.Secret
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(input.Item.UnstructuredContent(), &secret); err != nil {
return nil, errors.Wrap(err, "unable to convert secret from runtime.Unstructured")
}

log := s.logger.WithField("secret", kube.NamespaceAndName(&secret))
if secret.Type != corev1.SecretTypeServiceAccountToken {
log.Debug("No match found - including this secret")
return &velero.RestoreItemActionExecuteOutput{
UpdatedItem: input.Item,
}, nil
}

// The default service account token secret will be created automatically(before Kubernetes v1.22), no need to restore.
// This will cause the patch operation of managedFields failed if we restore it as the secret is removed immediately
// after restoration and the patch operation reports not found error.
if strings.HasPrefix(secret.Name, "default-token-") {
log.Debug("default service account token secret found - excluding this secret")
return &velero.RestoreItemActionExecuteOutput{
UpdatedItem: input.Item,
SkipRestore: true,
}, nil
}

log.Debug("service account token secret(not default) found - remove some fields from this secret")
// If the annotation and data are not removed, the secret cannot be restored successfully.
// The kube controller will fill the annotation and data with new value automatically:
// https://kubernetes.io/docs/concepts/configuration/secret/#service-account-token-secrets
delete(secret.Annotations, "kubernetes.io/service-account.uid")
delete(secret.Data, "token")
delete(secret.Data, "ca.crt")

res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&secret)
if err != nil {
return nil, errors.Wrap(err, "unable to convert secret to runtime.Unstructured")
}

return &velero.RestoreItemActionExecuteOutput{
UpdatedItem: &unstructured.Unstructured{Object: res},
}, nil
}
129 changes: 129 additions & 0 deletions pkg/restore/secret_action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright The Velero Contributors.
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 restore

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"github.com/vmware-tanzu/velero/pkg/plugin/velero"
"github.com/vmware-tanzu/velero/pkg/test"
)

func TestSecretActionAppliesTo(t *testing.T) {
action := NewSecretAction(test.NewLogger())
actual, err := action.AppliesTo()
require.NoError(t, err)
assert.Equal(t, velero.ResourceSelector{IncludedResources: []string{"secrets"}}, actual)
}

func TestSecretActionExecute(t *testing.T) {
tests := []struct {
name string
input *corev1.Secret
skipped bool
output *corev1.Secret
}{
{
name: "not service account token secret",
input: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "default-token-sfafa",
},
Type: corev1.SecretTypeOpaque,
},
skipped: false,
output: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "default-token-sfafa",
},
Type: corev1.SecretTypeOpaque,
},
},
{
name: "default service account token",
input: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "default-token-sfafa",
},
Type: corev1.SecretTypeServiceAccountToken,
},
skipped: true,
},
{
name: "not default service account token",
input: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "my-token",
Annotations: map[string]string{
"kubernetes.io/service-account.uid": "uid",
"key": "value",
},
},
Type: corev1.SecretTypeServiceAccountToken,
Data: map[string][]byte{
"token": []byte("token"),
"ca.crt": []byte("ca"),
"key": []byte("value"),
},
},
skipped: false,
output: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "foo",
Name: "my-token",
Annotations: map[string]string{
"key": "value",
},
},
Type: corev1.SecretTypeServiceAccountToken,
Data: map[string][]byte{
"key": []byte("value"),
},
},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
secretUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tc.input)
require.NoError(t, err)

action := NewSecretAction(test.NewLogger())
res, err := action.Execute(&velero.RestoreItemActionExecuteInput{
Item: &unstructured.Unstructured{Object: secretUnstructured},
})
require.NoError(t, err)
assert.Equal(t, tc.skipped, res.SkipRestore)
if !tc.skipped {
r, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tc.output)
require.NoError(t, err)
assert.EqualValues(t, &unstructured.Unstructured{Object: r}, res.UpdatedItem)
}
})
}
}

0 comments on commit a60ef4e

Please sign in to comment.