Skip to content

Commit

Permalink
Introduce TokenCleaner to clean out expired bootstrap tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeda committed Nov 2, 2016
1 parent be68323 commit 0b6ea76
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/kube-controller-manager/app/BUILD
Expand Up @@ -40,6 +40,7 @@ go_library(
"//pkg/cloudprovider/providers/openstack:go_default_library",
"//pkg/cloudprovider/providers/vsphere:go_default_library",
"//pkg/controller:go_default_library",
"//pkg/controller/bootstrap:go_default_library",
"//pkg/controller/certificates:go_default_library",
"//pkg/controller/daemon:go_default_library",
"//pkg/controller/deployment:go_default_library",
Expand Down
55 changes: 55 additions & 0 deletions pkg/controller/bootstrap/BUILD
@@ -0,0 +1,55 @@
package(default_visibility = ["//visibility:public"])

licenses(["notice"])

load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
"go_test",
"cgo_library",
)

go_library(
name = "go_default_library",
srcs = [
"bootstrapsigner.go",
"doc.go",
"jws.go",
"tokencleaner.go",
"util.go",
],
tags = ["automanaged"],
deps = [
"//pkg/api:go_default_library",
"//pkg/api/errors:go_default_library",
"//pkg/client/cache:go_default_library",
"//pkg/client/clientset_generated/internalclientset:go_default_library",
"//pkg/fields:go_default_library",
"//pkg/runtime:go_default_library",
"//pkg/util/metrics:go_default_library",
"//pkg/util/runtime:go_default_library",
"//pkg/watch:go_default_library",
"//vendor:github.com/golang/glog",
"//vendor:github.com/square/go-jose",
],
)

go_test(
name = "go_default_test",
srcs = [
"bootstrapsigner_test.go",
"jws_test.go",
"tokencleaner_test.go",
"util_test.go",
],
library = "go_default_library",
tags = ["automanaged"],
deps = [
"//pkg/api:go_default_library",
"//pkg/api/unversioned:go_default_library",
"//pkg/client/clientset_generated/internalclientset/fake:go_default_library",
"//pkg/client/testing/core:go_default_library",
"//vendor:github.com/davecgh/go-spew/spew",
],
)
118 changes: 118 additions & 0 deletions pkg/controller/bootstrap/tokencleaner.go
@@ -0,0 +1,118 @@
/*
Copyright 2014 The Kubernetes Authors.
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 bootstrap

import (
"time"

"github.com/golang/glog"

"k8s.io/kubernetes/pkg/api"
apierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/metrics"
"k8s.io/kubernetes/pkg/watch"
)

// TokenCleanerOptions contains options for the TokenCleaner
type TokenCleanerOptions struct {

// SecretResync is the time.Duration at which to fully re-list secrets.
// If zero, re-list will be delayed as long as possible
SecretResync time.Duration

// MaxRetries controls the maximum number of times a particular key is retried before giving up
// If zero, a default max is used
MaxRetries int
}

// DefaultBootstrapSignerOptions returns a set of default options for creating a
// TokenCleaner
func DefaultTokenCleanerOptions() TokenCleanerOptions {
return TokenCleanerOptions{}
}

// TokenCleaner is a controller that deletes expired tokens
type TokenCleaner struct {
stopChan chan struct{}

client clientset.Interface

secrets cache.Store
secretsController *cache.Controller

maxRetries int
}

func NewTokenCleaner(cl clientset.Interface, options TokenCleanerOptions) *TokenCleaner {
maxRetries := options.MaxRetries
if maxRetries == 0 {
maxRetries = 10
}

e := &TokenCleaner{
client: cl,
maxRetries: maxRetries,
}
if cl != nil && cl.Core().RESTClient().GetRateLimiter() != nil {
metrics.RegisterMetricAndTrackRateLimiterUsage("token_cleaner", cl.Core().RESTClient().GetRateLimiter())
}

secretSelector := fields.SelectorFromSet(map[string]string{api.SecretTypeField: string(api.SecretTypeBootstrapToken)})
e.secrets, e.secretsController = cache.NewInformer(
&cache.ListWatch{
ListFunc: func(lo api.ListOptions) (runtime.Object, error) {
lo.FieldSelector = secretSelector
return e.client.Core().Secrets(api.NamespaceSystem).List(lo)
},
WatchFunc: func(lo api.ListOptions) (watch.Interface, error) {
lo.FieldSelector = secretSelector
return e.client.Core().Secrets(api.NamespaceSystem).Watch(lo)
},
},
&api.Secret{},
options.SecretResync,
cache.ResourceEventHandlerFuncs{
AddFunc: e.evalSecret,
UpdateFunc: func(old, new interface{}) { e.evalSecret(new) },
},
)
return e
}

func (e *TokenCleaner) evalSecret(o interface{}) {
secret := o.(*api.Secret)
if isSecretExpired(secret) {
glog.V(3).Infof("Deleting expired secret %s/%s", secret.Namespace, secret.Name)
for i := 0; i < e.maxRetries; i++ {
var options *api.DeleteOptions
if len(secret.UID) > 0 {
options = &api.DeleteOptions{Preconditions: &api.Preconditions{UID: &secret.UID}}
}
err := e.client.Core().Secrets(secret.Namespace).Delete(secret.Name, options)
// NotFound doesn't need a retry (it's already been deleted)
// Conflict doesn't need a retry (the UID precondition failed)
if err == nil || apierrors.IsNotFound(err) || apierrors.IsConflict(err) {
return
}
glog.Errorf("Error deleting secret. Try %d/%d. Error: %v", i+1, e.maxRetries, err)
}
}
}
154 changes: 154 additions & 0 deletions pkg/controller/bootstrap/tokencleaner_test.go
@@ -0,0 +1,154 @@
/*
Copyright 2014 The Kubernetes Authors.
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 bootstrap

import (
"testing"

"github.com/davecgh/go-spew/spew"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/client/testing/core"
)

func init() {
spew.Config.DisableMethods = true
}

func newTokenCleaner() (*TokenCleaner, *fake.Clientset) {
options := DefaultTokenCleanerOptions()
cl := fake.NewSimpleClientset()
return NewTokenCleaner(cl, options), cl
}

func newTokenSecret(tokenID, tokenSecret, expiration string) *api.Secret {
return &api.Secret{
ObjectMeta: api.ObjectMeta{
Namespace: api.NamespaceSystem,
Name: "secretName",
ResourceVersion: "1",
},
Type: api.SecretTypeBootstrapToken,
Data: map[string][]byte{
api.BootstrapTokenIdKey: []byte(tokenID),
api.BootstrapTokenSecretKey: []byte(tokenSecret),
api.BootstrapTokenExpirationKey: []byte(expiration),
},
}
}

func verifyActions(t *testing.T, expected, actual []core.Action) {
for i, a := range actual {
if len(expected) < i+1 {
t.Errorf("%d unexpected actions: %s", len(actual)-len(expected), spew.Sdump(actual[i:]))
break
}

e := expected[i]
if !api.Semantic.DeepEqual(e, a) {
t.Errorf("Expected\n\t%s\ngot\n\t%s", spew.Sdump(e), spew.Sdump(a))
continue
}
}

if len(expected) > len(actual) {
t.Errorf("%d additional expected actions", len(expected)-len(actual))
for _, a := range expected[len(actual):] {
t.Logf(" %s", spew.Sdump(a))
}
}

}

func TestNoConfigMap(t *testing.T) {
signer, cl := newBootstrapSigner()
signer.signConfigMap()
verifyActions(t, []core.Action{}, cl.Actions())
}

func TestSimpleSign(t *testing.T) {
signer, cl := newBootstrapSigner()

cm := newConfigMap("", "")
signer.configMaps.Add(cm)

secret := newTokenSecret("tokenID", "tokenSecret")
signer.secrets.Add(secret)

signer.signConfigMap()

expected := []core.Action{
core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "configmaps"},
api.NamespacePublic,
newConfigMap("tokenID", "eyJhbGciOiJIUzI1NiIsImtpZCI6InRva2VuSUQifQ..QAvK9DAjF0hSyASEkH1MOTB5rJMmbWEY9j-z1NSYILE")),
}

verifyActions(t, expected, cl.Actions())
}

func TestNoSignNeeded(t *testing.T) {
signer, cl := newBootstrapSigner()

cm := newConfigMap("tokenID", "eyJhbGciOiJIUzI1NiIsImtpZCI6InRva2VuSUQifQ..QAvK9DAjF0hSyASEkH1MOTB5rJMmbWEY9j-z1NSYILE")
signer.configMaps.Add(cm)

secret := newTokenSecret("tokenID", "tokenSecret")
signer.secrets.Add(secret)

signer.signConfigMap()

verifyActions(t, []core.Action{}, cl.Actions())
}

func TestUpdateSignature(t *testing.T) {
signer, cl := newBootstrapSigner()

cm := newConfigMap("tokenID", "old signature")
signer.configMaps.Add(cm)

secret := newTokenSecret("tokenID", "tokenSecret")
signer.secrets.Add(secret)

signer.signConfigMap()

expected := []core.Action{
core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "configmaps"},
api.NamespacePublic,
newConfigMap("tokenID", "eyJhbGciOiJIUzI1NiIsImtpZCI6InRva2VuSUQifQ..QAvK9DAjF0hSyASEkH1MOTB5rJMmbWEY9j-z1NSYILE")),
}

verifyActions(t, expected, cl.Actions())
}

func TestRemoveSignature(t *testing.T) {
signer, cl := newBootstrapSigner()

cm := newConfigMap("tokenID", "old signature")
signer.configMaps.Add(cm)

signer.signConfigMap()

expected := []core.Action{
core.NewUpdateAction(unversioned.GroupVersionResource{Resource: "configmaps"},
api.NamespacePublic,
newConfigMap("", "")),
}

verifyActions(t, expected, cl.Actions())
}

0 comments on commit 0b6ea76

Please sign in to comment.