Skip to content

Commit

Permalink
UPSTREAM: <carry>: Ensure service ca is mounted for projected tokens
Browse files Browse the repository at this point in the history
OpenShift since 3.x has injected the service serving certificate
ca (service ca) bundle into service account token secrets. This was
intended to ensure that all pods would be able to easily verify
connections to endpoints secured with service serving
certificates. Since breaking customer workloads is not an option, and
there is no way to ensure that customers are not relying on the
service ca bundle being mounted at
/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt, it is
necessary to continue mounting the service ca bundle in the same
location in the bound token projected volumes enabled by the
BoundServiceAccountTokenVolume feature (enabled by default in 1.21).

A new controller is added to create a configmap per namespace that is
annotated for service ca injection. The controller is derived from the
controller that creates configmaps for the root ca. The service
account admission controller is updated to include a source for the
new configmap in the default projected volume definition.

UPSTREAM: <carry>: <squash> Add unit testing for service ca configmap publishing

This commit should be squashed with:

UPSTREAM: <carry>: Ensure service ca is mounted for projected tokens

OpenShift-Rebase-Source: d69d054
  • Loading branch information
marun authored and bertinatto committed Apr 11, 2023
1 parent 311a104 commit 6c491f7
Show file tree
Hide file tree
Showing 11 changed files with 593 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/kube-controller-manager/app/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"k8s.io/controller-manager/controller"
"k8s.io/klog/v2"
"k8s.io/kubernetes/openshift-kube-controller-manager/servicecacertpublisher"
"k8s.io/kubernetes/pkg/controller/certificates/approver"
"k8s.io/kubernetes/pkg/controller/certificates/cleaner"
"k8s.io/kubernetes/pkg/controller/certificates/rootcacertpublisher"
Expand Down Expand Up @@ -192,3 +193,16 @@ func startRootCACertPublisher(ctx context.Context, controllerContext ControllerC
go sac.Run(ctx, 1)
return nil, true, nil
}

func startServiceCACertPublisher(ctx context.Context, controllerContext ControllerContext) (controller.Interface, bool, error) {
sac, err := servicecacertpublisher.NewPublisher(
controllerContext.InformerFactory.Core().V1().ConfigMaps(),
controllerContext.InformerFactory.Core().V1().Namespaces(),
controllerContext.ClientBuilder.ClientOrDie("service-ca-cert-publisher"),
)
if err != nil {
return nil, true, fmt.Errorf("error creating service CA certificate publisher: %v", err)
}
go sac.Run(1, ctx.Done())
return nil, true, nil
}
1 change: 1 addition & 0 deletions cmd/kube-controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ func NewControllerInitializers(loopMode ControllerLoopMode) map[string]InitFunc
register("pv-protection", startPVProtectionController)
register("ttl-after-finished", startTTLAfterFinishedController)
register("root-ca-cert-publisher", startRootCACertPublisher)
register("service-ca-cert-publisher", startServiceCACertPublisher)
register("ephemeral-volume", startEphemeralVolumeController)
if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.APIServerIdentity) &&
utilfeature.DefaultFeatureGate.Enabled(genericfeatures.StorageVersionAPI) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package servicecacertpublisher

import (
"strconv"
"sync"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)

// ServiceCACertPublisher - subsystem name used by service_ca_cert_publisher
const ServiceCACertPublisher = "service_ca_cert_publisher"

var (
syncCounter = metrics.NewCounterVec(
&metrics.CounterOpts{
Subsystem: ServiceCACertPublisher,
Name: "sync_total",
Help: "Number of namespace syncs happened in service ca cert publisher.",
StabilityLevel: metrics.ALPHA,
},
[]string{"code"},
)
syncLatency = metrics.NewHistogramVec(
&metrics.HistogramOpts{
Subsystem: ServiceCACertPublisher,
Name: "sync_duration_seconds",
Help: "Number of namespace syncs happened in service ca cert publisher.",
Buckets: metrics.ExponentialBuckets(0.001, 2, 15),
StabilityLevel: metrics.ALPHA,
},
[]string{"code"},
)
)

func recordMetrics(start time.Time, ns string, err error) {
code := "500"
if err == nil {
code = "200"
} else if se, ok := err.(*apierrors.StatusError); ok && se.Status().Code != 0 {
code = strconv.Itoa(int(se.Status().Code))
}
syncLatency.WithLabelValues(code).Observe(time.Since(start).Seconds())
syncCounter.WithLabelValues(code).Inc()
}

var once sync.Once

func registerMetrics() {
once.Do(func() {
legacyregistry.MustRegister(syncCounter)
legacyregistry.MustRegister(syncLatency)
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package servicecacertpublisher

import (
"errors"
"strings"
"testing"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/component-base/metrics/testutil"
)

func TestSyncCounter(t *testing.T) {
testCases := []struct {
desc string
err error
metrics []string
want string
}{
{
desc: "nil error",
err: nil,
metrics: []string{
"service_ca_cert_publisher_sync_total",
},
want: `
# HELP service_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in service ca cert publisher.
# TYPE service_ca_cert_publisher_sync_total counter
service_ca_cert_publisher_sync_total{code="200"} 1
`,
},
{
desc: "kube api error",
err: apierrors.NewNotFound(corev1.Resource("configmap"), "test-configmap"),
metrics: []string{
"service_ca_cert_publisher_sync_total",
},
want: `
# HELP service_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in service ca cert publisher.
# TYPE service_ca_cert_publisher_sync_total counter
service_ca_cert_publisher_sync_total{code="404"} 1
`,
},
{
desc: "kube api error without code",
err: &apierrors.StatusError{},
metrics: []string{
"service_ca_cert_publisher_sync_total",
},
want: `
# HELP service_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in service ca cert publisher.
# TYPE service_ca_cert_publisher_sync_total counter
service_ca_cert_publisher_sync_total{code="500"} 1
`,
},
{
desc: "general error",
err: errors.New("test"),
metrics: []string{
"service_ca_cert_publisher_sync_total",
},
want: `
# HELP service_ca_cert_publisher_sync_total [ALPHA] Number of namespace syncs happened in service ca cert publisher.
# TYPE service_ca_cert_publisher_sync_total counter
service_ca_cert_publisher_sync_total{code="500"} 1
`,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
recordMetrics(time.Now(), "test-ns", tc.err)
defer syncCounter.Reset()
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tc.want), tc.metrics...); err != nil {
t.Fatal(err)
}
})
}
}
216 changes: 216 additions & 0 deletions openshift-kube-controller-manager/servicecacertpublisher/publisher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package servicecacertpublisher

import (
"context"
"fmt"
"reflect"
"time"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
coreinformers "k8s.io/client-go/informers/core/v1"
clientset "k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
)

// ServiceCACertConfigMapName is name of the configmap which stores certificates
// to validate service serving certificates issued by the service ca operator.
const ServiceCACertConfigMapName = "openshift-service-ca.crt"

func init() {
registerMetrics()
}

// NewPublisher construct a new controller which would manage the configmap
// which stores certificates in each namespace. It will make sure certificate
// configmap exists in each namespace.
func NewPublisher(cmInformer coreinformers.ConfigMapInformer, nsInformer coreinformers.NamespaceInformer, cl clientset.Interface) (*Publisher, error) {
e := &Publisher{
client: cl,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "service_ca_cert_publisher"),
}

cmInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
DeleteFunc: e.configMapDeleted,
UpdateFunc: e.configMapUpdated,
})
e.cmLister = cmInformer.Lister()
e.cmListerSynced = cmInformer.Informer().HasSynced

nsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: e.namespaceAdded,
UpdateFunc: e.namespaceUpdated,
})
e.nsListerSynced = nsInformer.Informer().HasSynced

e.syncHandler = e.syncNamespace

return e, nil
}

// Publisher manages certificate ConfigMap objects inside Namespaces
type Publisher struct {
client clientset.Interface

// To allow injection for testing.
syncHandler func(key string) error

cmLister corelisters.ConfigMapLister
cmListerSynced cache.InformerSynced

nsListerSynced cache.InformerSynced

queue workqueue.RateLimitingInterface
}

// Run starts process
func (c *Publisher) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()

klog.Infof("Starting service CA certificate configmap publisher")
defer klog.Infof("Shutting down service CA certificate configmap publisher")

if !cache.WaitForNamedCacheSync("crt configmap", stopCh, c.cmListerSynced) {
return
}

for i := 0; i < workers; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}

<-stopCh
}

func (c *Publisher) configMapDeleted(obj interface{}) {
cm, err := convertToCM(obj)
if err != nil {
utilruntime.HandleError(err)
return
}
if cm.Name != ServiceCACertConfigMapName {
return
}
c.queue.Add(cm.Namespace)
}

func (c *Publisher) configMapUpdated(_, newObj interface{}) {
cm, err := convertToCM(newObj)
if err != nil {
utilruntime.HandleError(err)
return
}
if cm.Name != ServiceCACertConfigMapName {
return
}
c.queue.Add(cm.Namespace)
}

func (c *Publisher) namespaceAdded(obj interface{}) {
namespace := obj.(*v1.Namespace)
c.queue.Add(namespace.Name)
}

func (c *Publisher) namespaceUpdated(oldObj interface{}, newObj interface{}) {
newNamespace := newObj.(*v1.Namespace)
if newNamespace.Status.Phase != v1.NamespaceActive {
return
}
c.queue.Add(newNamespace.Name)
}

func (c *Publisher) runWorker() {
for c.processNextWorkItem() {
}
}

// processNextWorkItem deals with one key off the queue. It returns false when
// it's time to quit.
func (c *Publisher) processNextWorkItem() bool {
key, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(key)

if err := c.syncHandler(key.(string)); err != nil {
utilruntime.HandleError(fmt.Errorf("syncing %q failed: %v", key, err))
c.queue.AddRateLimited(key)
return true
}

c.queue.Forget(key)
return true
}

func (c *Publisher) syncNamespace(ns string) (err error) {
startTime := time.Now()
defer func() {
recordMetrics(startTime, ns, err)
klog.V(4).Infof("Finished syncing namespace %q (%v)", ns, time.Since(startTime))
}()

annotations := map[string]string{
// This annotation prompts the service ca operator to inject
// the service ca bundle into the configmap.
"service.beta.openshift.io/inject-cabundle": "true",
}

cm, err := c.cmLister.ConfigMaps(ns).Get(ServiceCACertConfigMapName)
switch {
case apierrors.IsNotFound(err):
_, err = c.client.CoreV1().ConfigMaps(ns).Create(context.TODO(), &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ServiceCACertConfigMapName,
Annotations: annotations,
},
// Create new configmaps with the field referenced by the default
// projected volume. This ensures that pods - including the pod for
// service ca operator - will be able to start during initial
// deployment before the service ca operator has responded to the
// injection annotation.
Data: map[string]string{
"service-ca.crt": "",
},
}, metav1.CreateOptions{})
// don't retry a create if the namespace doesn't exist or is terminating
if apierrors.IsNotFound(err) || apierrors.HasStatusCause(err, v1.NamespaceTerminatingCause) {
return nil
}
return err
case err != nil:
return err
}

if reflect.DeepEqual(cm.Annotations, annotations) {
return nil
}

// copy so we don't modify the cache's instance of the configmap
cm = cm.DeepCopy()
cm.Annotations = annotations

_, err = c.client.CoreV1().ConfigMaps(ns).Update(context.TODO(), cm, metav1.UpdateOptions{})
return err
}

func convertToCM(obj interface{}) (*v1.ConfigMap, error) {
cm, ok := obj.(*v1.ConfigMap)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
return nil, fmt.Errorf("couldn't get object from tombstone %#v", obj)
}
cm, ok = tombstone.Obj.(*v1.ConfigMap)
if !ok {
return nil, fmt.Errorf("tombstone contained object that is not a ConfigMap %#v", obj)
}
}
return cm, nil
}

0 comments on commit 6c491f7

Please sign in to comment.