-
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.
Remove default token from all service accounts
Instead of only removing the default token from a service account when it already exists in the cluster, always remove it. If the service account already exists, continue to do the merging logic. Signed-off-by: Andy Goldstein <[email protected]>
- Loading branch information
Showing
5 changed files
with
207 additions
and
25 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
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,79 @@ | ||
/* | ||
Copyright 2018 the Heptio Ark 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" | ||
|
||
api "github.com/heptio/ark/pkg/apis/ark/v1" | ||
"github.com/heptio/ark/pkg/util/kube" | ||
) | ||
|
||
type serviceAccountAction struct { | ||
logger logrus.FieldLogger | ||
} | ||
|
||
func NewServiceAccountAction(logger logrus.FieldLogger) ItemAction { | ||
return &serviceAccountAction{logger: logger} | ||
} | ||
|
||
func (a *serviceAccountAction) AppliesTo() (ResourceSelector, error) { | ||
return ResourceSelector{ | ||
IncludedResources: []string{"serviceaccounts"}, | ||
}, nil | ||
} | ||
|
||
func (a *serviceAccountAction) Execute(obj runtime.Unstructured, restore *api.Restore) (runtime.Unstructured, error, error) { | ||
a.logger.Info("Executing serviceAccountAction") | ||
defer a.logger.Info("Done executing serviceAccountAction") | ||
|
||
var serviceAccount corev1.ServiceAccount | ||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &serviceAccount); err != nil { | ||
return nil, nil, errors.Wrap(err, "unable to convert serviceaccount from runtime.Unstructured") | ||
} | ||
|
||
log := a.logger.WithField("serviceaccount", kube.NamespaceAndName(&serviceAccount)) | ||
|
||
log.Debug("Checking secrets") | ||
check := serviceAccount.Name + "-token-" | ||
for i := len(serviceAccount.Secrets) - 1; i >= 0; i-- { | ||
secret := &serviceAccount.Secrets[i] | ||
log.Debugf("Checking if secret %s matches %s", secret.Name, check) | ||
|
||
if strings.HasPrefix(secret.Name, check) { | ||
// Copy all secrets *except* -token- | ||
log.Debug("Match found - excluding this secret") | ||
serviceAccount.Secrets = append(serviceAccount.Secrets[:i], serviceAccount.Secrets[i+1:]...) | ||
break | ||
} else { | ||
log.Debug("No match found - including this secret") | ||
} | ||
} | ||
|
||
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&serviceAccount) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "unable to convert serviceaccount to runtime.Unstructured") | ||
} | ||
|
||
return &unstructured.Unstructured{Object: res}, nil, 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,110 @@ | ||
/* | ||
Copyright 2018 the Heptio Ark 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 ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/heptio/ark/pkg/util/test" | ||
"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" | ||
) | ||
|
||
func TestServiceAccountActionAppliesTo(t *testing.T) { | ||
action := NewServiceAccountAction(test.NewLogger()) | ||
actual, err := action.AppliesTo() | ||
require.NoError(t, err) | ||
assert.Equal(t, ResourceSelector{IncludedResources: []string{"serviceaccounts"}}, actual) | ||
} | ||
|
||
func TestServiceAccountActionExecute(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
secrets []string | ||
expected []string | ||
}{ | ||
{ | ||
name: "no secrets", | ||
secrets: []string{}, | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "no match", | ||
secrets: []string{"a", "bar-TOKN-nomatch", "baz"}, | ||
expected: []string{"a", "bar-TOKN-nomatch", "baz"}, | ||
}, | ||
{ | ||
name: "match - first", | ||
secrets: []string{"bar-token-a1b2c", "a", "baz"}, | ||
expected: []string{"a", "baz"}, | ||
}, | ||
{ | ||
name: "match - middle", | ||
secrets: []string{"a", "bar-token-a1b2c", "baz"}, | ||
expected: []string{"a", "baz"}, | ||
}, | ||
{ | ||
name: "match - end", | ||
secrets: []string{"a", "baz", "bar-token-a1b2c"}, | ||
expected: []string{"a", "baz"}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
sa := corev1.ServiceAccount{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "foo", | ||
Name: "bar", | ||
}, | ||
} | ||
|
||
for _, secret := range tc.secrets { | ||
sa.Secrets = append(sa.Secrets, corev1.ObjectReference{ | ||
Name: secret, | ||
}) | ||
} | ||
|
||
saUnstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&sa) | ||
require.NoError(t, err) | ||
|
||
action := NewServiceAccountAction(test.NewLogger()) | ||
res, warning, err := action.Execute(&unstructured.Unstructured{Object: saUnstructured}, nil) | ||
require.NoError(t, warning) | ||
require.NoError(t, err) | ||
|
||
var resSA *corev1.ServiceAccount | ||
err = runtime.DefaultUnstructuredConverter.FromUnstructured(res.UnstructuredContent(), &resSA) | ||
require.NoError(t, err) | ||
|
||
actual := []string{} | ||
for _, secret := range resSA.Secrets { | ||
actual = append(actual, secret.Name) | ||
} | ||
|
||
sort.Strings(tc.expected) | ||
sort.Strings(actual) | ||
|
||
assert.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} |