Skip to content

Commit

Permalink
allows secure backend configuration from global
Browse files Browse the repository at this point in the history
Some configuration keys were not designed to be made from global due to
the lack of a namespace name. Secure backend needs a namespace to read
secrets for a CA bundle and client certificate. This update allows to
provide a secret in the global ConfigMap by adding the namespace along
with the name of the secret.
  • Loading branch information
jcmoraisjr committed May 4, 2024
1 parent a16c57d commit bdb25a8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 22 deletions.
34 changes: 22 additions & 12 deletions pkg/converters/ingress/annotations/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,15 +674,20 @@ func (c *updater) buildBackendProtocol(d *backData) {
return
}
if crt := d.mapper.Get(ingtypes.BackSecureCrtSecret); crt.Value != "" {
if crtFile, err := c.cache.GetTLSSecretPath(
crt.Source.Namespace,
crt.Value,
convtypes.TrackingTarget{Backend: d.backend.BackendID()},
); err == nil {
var crtFile convtypes.CrtFile
namespace, name, err := crt.NamespacedName()
if err == nil {
crtFile, err = c.cache.GetTLSSecretPath(
namespace,
name,
convtypes.TrackingTarget{Backend: d.backend.BackendID()},
)
}
if err == nil {
d.backend.Server.CrtFilename = crtFile.Filename
d.backend.Server.CrtHash = crtFile.SHA1Hash
} else {
c.logger.Warn("skipping client certificate on %v: %v", crt.Source, err)
c.logger.Warn("skipping client certificate on %s: %v", crt.Source.String(), err)
}
}
if sni := d.mapper.Get(ingtypes.BackSecureSNI); sni.Value != "" {
Expand All @@ -707,17 +712,22 @@ func (c *updater) buildBackendProtocol(d *backData) {
}
}
if ca := d.mapper.Get(ingtypes.BackSecureVerifyCASecret); ca.Value != "" {
if caFile, crlFile, err := c.cache.GetCASecretPath(
ca.Source.Namespace,
ca.Value,
convtypes.TrackingTarget{Backend: d.backend.BackendID()},
); err == nil {
var caFile, crlFile convtypes.File
namespace, name, err := ca.NamespacedName()
if err == nil {
caFile, crlFile, err = c.cache.GetCASecretPath(
namespace,
name,
convtypes.TrackingTarget{Backend: d.backend.BackendID()},
)
}
if err == nil {
d.backend.Server.CAFilename = caFile.Filename
d.backend.Server.CAHash = caFile.SHA1Hash
d.backend.Server.CRLFilename = crlFile.Filename
d.backend.Server.CRLHash = crlFile.SHA1Hash
} else {
c.logger.Warn("skipping CA on %v: %v", ca.Source, err)
c.logger.Warn("skipping CA on %s: %v", ca.Source.String(), err)
}
}
}
Expand Down
68 changes: 58 additions & 10 deletions pkg/converters/ingress/annotations/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ func TestBackendServerNaming(t *testing.T) {

func TestBackendProtocol(t *testing.T) {
testCase := []struct {
source Source
source *Source
useHTX bool
annDefault map[string]string
ann map[string]map[string]string
Expand Down Expand Up @@ -1644,7 +1644,7 @@ func TestBackendProtocol(t *testing.T) {
},
// 2
{
source: Source{Namespace: "default", Name: "app1", Type: "service"},
source: &Source{Namespace: "default", Name: "app1", Type: "service"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackSecureBackends: "true",
Expand All @@ -1663,7 +1663,7 @@ func TestBackendProtocol(t *testing.T) {
},
// 3
{
source: Source{Namespace: "default", Name: "app1", Type: "service"},
source: &Source{Namespace: "default", Name: "app1", Type: "service"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackSecureBackends: "true",
Expand All @@ -1688,7 +1688,7 @@ func TestBackendProtocol(t *testing.T) {
},
// 4
{
source: Source{Namespace: "default", Name: "app1", Type: "service"},
source: &Source{Namespace: "default", Name: "app1", Type: "service"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackSecureBackends: "true",
Expand Down Expand Up @@ -1769,7 +1769,7 @@ WARN skipping CA on service 'default/app1': secret not found: 'default/ca'`,
},
// 10
{
source: Source{Namespace: "default", Name: "app1", Type: "service"},
source: &Source{Namespace: "default", Name: "app1", Type: "service"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackBackendProtocol: "invalid-ssl",
Expand All @@ -1780,7 +1780,7 @@ WARN skipping CA on service 'default/app1': secret not found: 'default/ca'`,
},
// 11
{
source: Source{Namespace: "default", Name: "app1", Type: "service"},
source: &Source{Namespace: "default", Name: "app1", Type: "service"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackBackendProtocol: "h2",
Expand Down Expand Up @@ -1835,7 +1835,7 @@ WARN skipping CA on service 'default/app1': secret not found: 'default/ca'`,
},
// 15
{
source: Source{Namespace: "default", Name: "app", Type: "ingress"},
source: &Source{Namespace: "default", Name: "app", Type: "ingress"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackBackendProtocol: "h1-ssl",
Expand Down Expand Up @@ -1864,7 +1864,7 @@ WARN skipping CA on service 'default/app1': secret not found: 'default/ca'`,
},
// 17
{
source: Source{Namespace: "default", Name: "app", Type: "ingress"},
source: &Source{Namespace: "default", Name: "app", Type: "ingress"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackBackendProtocol: "h1-ssl",
Expand Down Expand Up @@ -1907,7 +1907,7 @@ WARN skipping CA on service 'default/app1': secret not found: 'default/ca'`,
},
// 20
{
source: Source{Namespace: "default", Name: "app", Type: "ingress"},
source: &Source{Namespace: "default", Name: "app", Type: "ingress"},
ann: map[string]map[string]string{
"/": {
ingtypes.BackBackendProtocol: "h1-ssl",
Expand All @@ -1920,10 +1920,58 @@ WARN skipping CA on service 'default/app1': secret not found: 'default/ca'`,
},
logging: `WARN skipping invalid domain (verify-hostname) on ingress 'default/app': invalid-domain`,
},
// 21
{
ann: map[string]map[string]string{
"/": {
ingtypes.BackSecureBackends: "true",
ingtypes.BackSecureCrtSecret: "cli",
ingtypes.BackSecureVerifyCASecret: "ca",
},
},
tlsSecrets: map[string]string{
"default/cli": "/var/haproxy/ssl/cli.pem",
},
caSecrets: map[string]string{
"default/ca": "/var/haproxy/ssl/ca.pem",
},
expected: hatypes.ServerConfig{
Protocol: "h1",
Secure: true,
},
logging: `
WARN skipping client certificate on <global>: a globally configured resource name is missing the namespace: cli
WARN skipping CA on <global>: a globally configured resource name is missing the namespace: ca
`,
},
// 22
{
ann: map[string]map[string]string{
"/": {
ingtypes.BackSecureBackends: "true",
ingtypes.BackSecureCrtSecret: "default/cli",
ingtypes.BackSecureVerifyCASecret: "default/ca",
},
},
tlsSecrets: map[string]string{
"default/cli": "/var/haproxy/ssl/cli.pem",
},
caSecrets: map[string]string{
"default/ca": "/var/haproxy/ssl/ca.pem",
},
expected: hatypes.ServerConfig{
Protocol: "h1",
Secure: true,
CAFilename: "/var/haproxy/ssl/ca.pem",
CAHash: "3be93154b1cddfd0e1279f4d76022221676d08c7",
CrtFilename: "/var/haproxy/ssl/cli.pem",
CrtHash: "f916dd295030e070f4d4aca4508571bc82f549af",
},
},
}
for i, test := range testCase {
c := setup(t)
d := c.createBackendMappingData("defualt/app", &test.source, test.annDefault, test.ann, test.paths)
d := c.createBackendMappingData("default/app", test.source, test.annDefault, test.ann, test.paths)
c.haproxy.Global().UseHTX = test.useHTX
c.cache.SecretTLSPath = test.tlsSecrets
c.cache.SecretCAPath = test.caSecrets
Expand Down
19 changes: 19 additions & 0 deletions pkg/converters/ingress/annotations/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package annotations
import (
"fmt"
"strconv"
"strings"

hatypes "github.com/jcmoraisjr/haproxy-ingress/pkg/haproxy/types"
"github.com/jcmoraisjr/haproxy-ingress/pkg/types"
Expand Down Expand Up @@ -201,6 +202,21 @@ func (cv *ConfigValue) String() string {
return cv.Value
}

// NamespacedName ...
func (cv *ConfigValue) NamespacedName() (namespace, name string, err error) {
value := strings.Split(cv.Value, "/")
if len(value) > 2 {
return "", "", fmt.Errorf("unpexpected format for resource name: %s", cv.Value)
}
if len(value) == 2 {
return value[0], value[1], nil
}
if s := cv.Source; s != nil {
return s.Namespace, value[0], nil
}
return "", "", fmt.Errorf("a globally configured resource name is missing the namespace: %s", cv.Value)
}

// Bool ...
func (cv *ConfigValue) Bool() bool {
value, _ := strconv.ParseBool(cv.Value)
Expand Down Expand Up @@ -231,5 +247,8 @@ func (m *PathConfig) String() string {

// String ...
func (s *Source) String() string {
if s == nil {
return "<global>"
}
return s.Type + " '" + s.FullName() + "'"
}

0 comments on commit bdb25a8

Please sign in to comment.