Skip to content

Commit 5852629

Browse files
committed
Inject payload's system store with proxy CA when specified
1 parent b8280b8 commit 5852629

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

pkg/operator2/deployment.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
func defaultDeployment(
2222
operatorConfig *operatorv1.Authentication,
2323
syncData *configSyncData,
24+
combinedCA *corev1.ConfigMap,
2425
routerSecret *corev1.Secret,
2526
proxyConfig *configv1.Proxy,
2627
operatorDeployment *appsv1.Deployment,
@@ -80,6 +81,7 @@ func defaultDeployment(
8081
volumes, mounts = toVolumesAndMounts(syncData.idpConfigMaps, volumes, mounts)
8182
volumes, mounts = toVolumesAndMounts(syncData.idpSecrets, volumes, mounts)
8283
volumes, mounts = toVolumesAndMounts(syncData.tplSecrets, volumes, mounts)
84+
volumes, mounts = appendCombinedCAToVolumesAndMounts(volumes, mounts, combinedCA)
8385

8486
// force redeploy when any associated resource changes
8587
// we use a hash to prevent this value from growing indefinitely

pkg/operator2/operator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ const (
9090
// root path for template data
9191
userConfigPathPrefixTemplate = userConfigPath + "/" + "template"
9292

93+
// system-wide tls cert store to mount the combined CA if Proxy has config specified
94+
systemTrustStorePath = "/etc/pki/ca-trust/extracted/pem"
95+
9396
sessionNameAndKey = systemConfigPrefix + "session"
9497
sessionMount = systemConfigPathSecrets + "/" + sessionNameAndKey
9598
sessionPath = sessionMount + "/" + sessionNameAndKey
@@ -105,6 +108,11 @@ const (
105108
servingCertPathCert = servingCertMount + "/" + corev1.TLSCertKey
106109
servingCertPathKey = servingCertMount + "/" + corev1.TLSPrivateKeyKey
107110

111+
proxyCAName = systemConfigPrefix + "combined-ca"
112+
proxyCAKey = "ca-bundle.crt"
113+
proxyCAVolumePath = "tls-ca-bundle.pem"
114+
proxyCAMount = systemTrustStorePath
115+
108116
consoleConfigMapSharedName = "console-config"
109117
consoleConfigMapLocalName = systemConfigPrefix + consoleConfigMapSharedName
110118
consoleConfigKey = consoleConfigMapSharedName + ".yaml"
@@ -387,6 +395,11 @@ func (c *authOperator) handleSync(operatorConfig *operatorv1.Authentication) err
387395
proxyConfig := c.handleProxyConfig()
388396
resourceVersions = append(resourceVersions, proxyConfig.ResourceVersion)
389397

398+
combinedCA, err := c.handleProxyCA(proxyConfig)
399+
if err != nil {
400+
return fmt.Errorf("failed handling the combined CA: %v", err)
401+
}
402+
390403
operatorDeployment, err := c.deployments.Deployments(targetNamespaceOperator).Get(targetNameOperator, metav1.GetOptions{})
391404
if err != nil {
392405
return err
@@ -404,6 +417,7 @@ func (c *authOperator) handleSync(operatorConfig *operatorv1.Authentication) err
404417
expectedDeployment := defaultDeployment(
405418
operatorConfig,
406419
syncData,
420+
combinedCA,
407421
routerSecret,
408422
proxyConfig,
409423
operatorDeployment,

pkg/operator2/proxy.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package operator2
22

33
import (
4+
"fmt"
5+
6+
corev1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/api/errors"
48
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
59
"k8s.io/klog"
610

711
configv1 "github.com/openshift/api/config/v1"
812
)
913

14+
const (
15+
injectProxyCAName = "config.openshift.io/inject-trusted-cabundle"
16+
injectProxyCAValue = "true"
17+
)
18+
1019
func (c *authOperator) handleProxyConfig() *configv1.Proxy {
1120
proxyConfig, err := c.proxy.Get(globalConfigName, metav1.GetOptions{})
1221
if err != nil {
@@ -15,3 +24,80 @@ func (c *authOperator) handleProxyConfig() *configv1.Proxy {
1524
}
1625
return proxyConfig
1726
}
27+
28+
func (c *authOperator) handleProxyCA(proxy *configv1.Proxy) (*corev1.ConfigMap, error) {
29+
cmClient := c.configMaps.ConfigMaps(targetNamespace)
30+
shouldHaveProxyCA := proxy != nil && len(proxy.Spec.TrustedCA.Name) > 0
31+
32+
proxyCACM, err := cmClient.Get(proxyCAName, metav1.GetOptions{})
33+
if errors.IsNotFound(err) {
34+
if !shouldHaveProxyCA {
35+
return nil, nil
36+
}
37+
proxyCACM, err = cmClient.Create(defaultProxyCACM())
38+
}
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
if !shouldHaveProxyCA {
44+
return nil, cmClient.Delete(proxyCAName, &metav1.DeleteOptions{})
45+
}
46+
47+
if err := isValidProxyCACM(proxyCACM); err != nil {
48+
// delete the proxy CA config map so that it is replaced with the proper one in next reconcile loop
49+
klog.Infof("deleting invalid proxy CA config map: %#v", proxyCACM)
50+
opts := &metav1.DeleteOptions{Preconditions: &metav1.Preconditions{UID: &proxyCACM.UID}}
51+
if err := cmClient.Delete(proxyCACM.Name, opts); err != nil && !errors.IsNotFound(err) {
52+
klog.Infof("failed to delete invalid proxy CA config map: %v", err)
53+
}
54+
return nil, err
55+
}
56+
57+
return proxyCACM, nil
58+
}
59+
60+
func isValidProxyCACM(cm *corev1.ConfigMap) error {
61+
if cm.Annotations[injectProxyCAName] != injectProxyCAValue {
62+
return fmt.Errorf("config map missing injection annotation: %#v", cm)
63+
}
64+
return nil
65+
}
66+
67+
func defaultProxyCACM() *corev1.ConfigMap {
68+
meta := defaultMeta()
69+
meta.Name = proxyCAName
70+
meta.Annotations[injectProxyCAName] = injectProxyCAValue
71+
return &corev1.ConfigMap{
72+
ObjectMeta: meta,
73+
}
74+
}
75+
76+
func appendCombinedCAToVolumesAndMounts(volumes []corev1.Volume, mounts []corev1.VolumeMount, combinedCACM *corev1.ConfigMap) ([]corev1.Volume, []corev1.VolumeMount) {
77+
// nothing got injected in that CM, we shouldn't mount it to trust store
78+
if combinedCACM == nil || len(combinedCACM.Data) == 0 {
79+
return volumes, mounts
80+
}
81+
82+
combinedCAVolume := corev1.Volume{
83+
Name: proxyCAName,
84+
VolumeSource: corev1.VolumeSource{
85+
ConfigMap: &corev1.ConfigMapVolumeSource{
86+
LocalObjectReference: corev1.LocalObjectReference{
87+
Name: proxyCAName,
88+
},
89+
Items: []corev1.KeyToPath{{Key: proxyCAKey, Path: proxyCAVolumePath}},
90+
},
91+
},
92+
}
93+
volumes = append(volumes, combinedCAVolume)
94+
95+
combinedCAMount := corev1.VolumeMount{
96+
Name: proxyCAName,
97+
ReadOnly: true,
98+
MountPath: proxyCAMount,
99+
}
100+
mounts = append(mounts, combinedCAMount)
101+
102+
return volumes, mounts
103+
}

0 commit comments

Comments
 (0)