Skip to content

Commit 46ad7f3

Browse files
robo-capInbaraj-S
andauthored
Enable TLS termination on LB. (#44)
* Add support to disable TLS on the backend via annotation: `oci-native-ingress.oraclecloud.com/backend-tls-enabled` --------- Co-authored-by: Inbaraj S <[email protected]>
1 parent 913ccdd commit 46ad7f3

File tree

5 files changed

+123
-3
lines changed

5 files changed

+123
-3
lines changed

pkg/state/ingressstate.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (s *StateStore) BuildState(ingressClass *networkingv1.IngressClass) error {
150150
if err != nil {
151151
return err
152152
}
153-
153+
bsTLSEnabled := util.GetBackendTlsEnabled(ing)
154154
certificateId := util.GetListenerTlsCertificateOcid(ing)
155155
if certificateId != nil {
156156
tlsPortDetail, ok := listenerTLSConfigMap[servicePort]
@@ -165,11 +165,12 @@ func (s *StateStore) BuildState(ingressClass *networkingv1.IngressClass) error {
165165
Artifact: *certificateId,
166166
}
167167
listenerTLSConfigMap[servicePort] = config
168-
bsTLSConfigMap[bsName] = config
168+
updateBackendTlsStatus(bsTLSEnabled, bsTLSConfigMap, bsName, config)
169169
}
170170

171171
if rule.Host != "" {
172172
secretName, ok := hostSecretMap[rule.Host]
173+
173174
if ok && secretName != "" {
174175
tlsPortDetail, ok := listenerTLSConfigMap[servicePort]
175176
if ok {
@@ -183,7 +184,7 @@ func (s *StateStore) BuildState(ingressClass *networkingv1.IngressClass) error {
183184
Artifact: secretName,
184185
}
185186
listenerTLSConfigMap[servicePort] = config
186-
bsTLSConfigMap[bsName] = config
187+
updateBackendTlsStatus(bsTLSEnabled, bsTLSConfigMap, bsName, config)
187188
}
188189
}
189190
}
@@ -214,6 +215,18 @@ func (s *StateStore) BuildState(ingressClass *networkingv1.IngressClass) error {
214215
return nil
215216
}
216217

218+
func updateBackendTlsStatus(bsTLSEnabled bool, bsTLSConfigMap map[string]TlsConfig, bsName string, config TlsConfig) {
219+
if bsTLSEnabled {
220+
bsTLSConfigMap[bsName] = config
221+
} else {
222+
config := TlsConfig{
223+
Type: "",
224+
Artifact: "",
225+
}
226+
bsTLSConfigMap[bsName] = config
227+
}
228+
}
229+
217230
func validateBackendSetHealthChecker(ingressResource *networkingv1.Ingress,
218231
bsHealthCheckerMap map[string]*ociloadbalancer.HealthCheckerDetails, bsName string) error {
219232
defaultHealthChecker := util.GetDefaultHeathChecker()

pkg/state/ingressstate_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535
TestIngressStateFilePath = "test-ingress-state.yaml"
3636
TestIngressStateWithPortNameFilePath = "test-ingress-state_withportname.yaml"
3737
TestIngressStateWithNamedClassesFilePath = "test-ingress-state_withnamedclasses.yaml"
38+
TestSslTerminationAtLb = "test-ssl-termination-lb.yaml"
3839
)
3940

4041
func setUp(ctx context.Context, ingressClassList *networkingv1.IngressClassList, ingressList *networkingv1.IngressList, testService *v1.ServiceList) (networkinglisters.IngressClassLister, networkinglisters.IngressLister, corelisters.ServiceLister) {
@@ -418,3 +419,33 @@ func TestValidateProtocolConfigWithConflict(t *testing.T) {
418419

419420
Expect(err.Error()).Should(ContainSubstring(fmt.Sprintf(ProtocolConflictMessage, 900)))
420421
}
422+
423+
func TestSslTerminationAtLB(t *testing.T) {
424+
RegisterTestingT(t)
425+
ctx, cancel := context.WithCancel(context.Background())
426+
defer cancel()
427+
428+
ingressClassList := testutil.GetIngressClassList()
429+
430+
ingressList := testutil.ReadResourceAsIngressList(TestSslTerminationAtLb)
431+
432+
certificateId := "certificateId"
433+
ingressList.Items[0].Spec.TLS = []networkingv1.IngressTLS{}
434+
ingressList.Items[0].Annotations = map[string]string{util.IngressListenerTlsCertificateAnnotation: certificateId}
435+
436+
testService := testutil.GetServiceListResource("default", "tls-test", 443)
437+
ingressClassLister, ingressLister, serviceLister := setUp(ctx, ingressClassList, ingressList, testService)
438+
439+
stateStore := NewStateStore(ingressClassLister, ingressLister, serviceLister, nil)
440+
err := stateStore.BuildState(&ingressClassList.Items[0])
441+
Expect(err).NotTo(HaveOccurred())
442+
443+
bsName := util.GenerateBackendSetName("default", "tls-test", 443)
444+
bsTlsConfig := stateStore.IngressGroupState.BackendSetTLSConfigMap[bsName]
445+
Expect(bsTlsConfig.Artifact).Should(Equal(""))
446+
Expect(bsTlsConfig.Type).Should(Equal(""))
447+
448+
lstTlsConfig := stateStore.IngressGroupState.ListenerTLSConfigMap[443]
449+
Expect(lstTlsConfig.Artifact).Should(Equal(certificateId))
450+
Expect(lstTlsConfig.Type).Should(Equal(ArtifactTypeCertificate))
451+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: ingress-tls
5+
annotations:
6+
oci-native-ingress.oraclecloud.com/protocol: HTTP2
7+
oci-native-ingress.oraclecloud.com/backend-tls-enabled: "false"
8+
spec:
9+
tls:
10+
- hosts:
11+
- foo.bar.com
12+
secretName: secret_name
13+
rules:
14+
- host: "foo.bar.com"
15+
http:
16+
paths:
17+
- pathType: Prefix
18+
path: "/TLSPath"
19+
backend:
20+
service:
21+
name: tls-test
22+
port:
23+
number: 443

pkg/util/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
IngressControllerFinalizer = "oci.oraclecloud.com/ingress-controller-protection"
5151

5252
IngressListenerTlsCertificateAnnotation = "oci-native-ingress.oraclecloud.com/certificate-ocid"
53+
IngressBackendTlsEnabledAnnotation = "oci-native-ingress.oraclecloud.com/backend-tls-enabled"
5354

5455
// IngressProtocolAnntoation - HTTP only for now
5556
// HTTP, HTTP2 - accepted.
@@ -164,6 +165,23 @@ func GetListenerTlsCertificateOcid(i *networkingv1.Ingress) *string {
164165
return &value
165166
}
166167

168+
func GetBackendTlsEnabled(i *networkingv1.Ingress) bool {
169+
annotation := IngressBackendTlsEnabledAnnotation
170+
value, ok := i.Annotations[annotation]
171+
172+
if !ok || strings.TrimSpace(value) == "" {
173+
return true
174+
}
175+
176+
result, err := strconv.ParseBool(value)
177+
if err != nil {
178+
klog.Errorf("Error parsing value %s for flag %s as boolean. Setting the default value as 'true'", value, annotation)
179+
return true
180+
}
181+
182+
return result
183+
}
184+
167185
func GetIngressHealthCheckProtocol(i *networkingv1.Ingress) string {
168186
protocol, ok := i.Annotations[IngressHealthCheckProtocolAnnotation]
169187
if !ok {

pkg/util/util_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,41 @@ func TestGetListenerTlsCertificateOcid(t *testing.T) {
181181
Expect(result).To(BeNil())
182182
}
183183

184+
func TestGetBackendTlsEnabled(t *testing.T) {
185+
RegisterTestingT(t)
186+
i := networkingv1.Ingress{
187+
ObjectMeta: metav1.ObjectMeta{
188+
Annotations: map[string]string{IngressBackendTlsEnabledAnnotation: "true"},
189+
},
190+
}
191+
result := GetBackendTlsEnabled(&i)
192+
Expect(result).Should(Equal(true))
193+
194+
i = networkingv1.Ingress{
195+
ObjectMeta: metav1.ObjectMeta{
196+
Annotations: map[string]string{IngressBackendTlsEnabledAnnotation: "false"},
197+
},
198+
}
199+
result = GetBackendTlsEnabled(&i)
200+
Expect(result).Should(Equal(false))
201+
202+
i = networkingv1.Ingress{
203+
ObjectMeta: metav1.ObjectMeta{
204+
Annotations: map[string]string{IngressBackendTlsEnabledAnnotation: "scam"},
205+
},
206+
}
207+
result = GetBackendTlsEnabled(&i)
208+
Expect(result).Should(Equal(true))
209+
210+
i = networkingv1.Ingress{
211+
ObjectMeta: metav1.ObjectMeta{
212+
Annotations: map[string]string{IngressBackendTlsEnabledAnnotation: ""},
213+
},
214+
}
215+
result = GetBackendTlsEnabled(&i)
216+
Expect(result).Should(Equal(true))
217+
}
218+
184219
func TestGetIngressHealthCheckProtocol(t *testing.T) {
185220
RegisterTestingT(t)
186221
protocol := "http"

0 commit comments

Comments
 (0)