-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add secret restore item action to handle service account token secret
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
Showing
4 changed files
with
230 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add secret restore item action to handle service account token secret |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |