Skip to content

Commit

Permalink
updated admission-controller to have adjustable --min-tls-version and…
Browse files Browse the repository at this point in the history
… --tls-ciphers
  • Loading branch information
allenmun197 committed Mar 18, 2024
1 parent 75e7df6 commit 2cd5884
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions vertical-pod-autoscaler/pkg/admission-controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ up the changes: ```sudo systemctl restart kubelet.service```
for pods on their creation & updates.
1. You can specify a path for it to register as a part of the installation process
by setting `--register-by-url=true` and passing `--webhook-address` and `--webhook-port`.
1. You can specify a minimum TLS version with `--min-tls-version` with acceptable values being `tls1_2` (default), or `tls1_3`.
1. You can also specify a comma or colon separated list of ciphers for the server to use with `--tls-ciphers` if `--min-tls-version` is set to `tls1_2`.

## Implementation

Expand Down
35 changes: 33 additions & 2 deletions vertical-pod-autoscaler/pkg/admission-controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package main
import (
"context"
"crypto/tls"
"fmt"
"strings"
"time"

admissionregistration "k8s.io/api/admissionregistration/v1"
Expand All @@ -31,14 +33,43 @@ const (
webhookConfigName = "vpa-webhook-config"
)

func configTLS(serverCert, serverKey []byte) *tls.Config {
func configTLS(serverCert, serverKey []byte, minTlsVersion, ciphers string) *tls.Config {
var tlsVersion uint16
var ciphersuites []uint16
reverseCipherMap := make(map[string]uint16)
sCert, err := tls.X509KeyPair(serverCert, serverKey)
if err != nil {
klog.Fatal(err)
}

for _, c := range tls.CipherSuites() {
reverseCipherMap[c.Name] = c.ID
}
for _, c := range strings.Split(strings.ReplaceAll(ciphers, ",", ":"), ":") {
cipher, ok := reverseCipherMap[c]
if ok {
ciphersuites = append(ciphersuites, cipher)
}
}
if len(ciphersuites) == 0 {
ciphersuites = nil
}

switch minTlsVersion {
case "":
fallthrough
case "tls1_2":
tlsVersion = tls.VersionTLS12
case "tls1_3":
tlsVersion = tls.VersionTLS13
default:
klog.Fatal(fmt.Errorf("Unable to determine value for --min-tls-version (%s), must be either tls1_2 or tls1_3", minTlsVersion))
}

return &tls.Config{
MinVersion: tls.VersionTLS12,
MinVersion: tlsVersion,
Certificates: []tls.Certificate{sCert},
CipherSuites: ciphersuites,
}
}

Expand Down
4 changes: 3 additions & 1 deletion vertical-pod-autoscaler/pkg/admission-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ var (
tlsCertFile: flag.String("tls-cert-file", "/etc/tls-certs/serverCert.pem", "Path to server certificate PEM file."),
tlsPrivateKey: flag.String("tls-private-key", "/etc/tls-certs/serverKey.pem", "Path to server certificate key PEM file."),
}
ciphers = flag.String("tls-ciphers", "", "A comma-separated or colon-separated list of ciphers to accept. Only works when min-tls-version is set to tls1_2.")
minTlsVersion = flag.String("min-tls-version", "tls1_2", "The minimum TLS version to accept. Must be set to either tls1_2 (default) or tls1_3.")

port = flag.Int("port", 8000, "The port to listen on.")
address = flag.String("address", ":8944", "The address to expose Prometheus metrics.")
Expand Down Expand Up @@ -131,7 +133,7 @@ func main() {
})
server := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
TLSConfig: configTLS(certs.serverCert, certs.serverKey),
TLSConfig: configTLS(certs.serverCert, certs.serverKey, *minTlsVersion, *ciphers),
}
url := fmt.Sprintf("%v:%v", *webhookAddress, *webhookPort)
go func() {
Expand Down

0 comments on commit 2cd5884

Please sign in to comment.