-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add certwatcher for TLS cert and key from controller-runtime
Also adds error for missing either tls-key or tls-cert arguments. Signed-off-by: Tayler Geiger <tayler@redhat.com>
- Loading branch information
Showing
2 changed files
with
53 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package serverutil | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
Check failure on line 9 in internal/serverutil/serverutil.go GitHub Actions / lint
|
||
"sigs.k8s.io/controller-runtime/pkg/certwatcher" | ||
) | ||
|
||
func ConfigureListener(cert string, key string, addr string, catalogAddr string, mgr ctrl.Manager) (net.Listener, string, error) { | ||
switch { | ||
case cert == "" && key == "": | ||
listener, err := net.Listen("tcp", catalogAddr) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("unable to create HTTP server listener: %w", err) | ||
} | ||
addr = "http://" + addr | ||
return listener, addr, nil | ||
case cert != "" && key != "": | ||
tlsFileWatcher, err := certwatcher.New(cert, key) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
config := &tls.Config{ | ||
GetCertificate: tlsFileWatcher.GetCertificate, | ||
MinVersion: tls.VersionTLS13, | ||
} | ||
listener, err := tls.Listen("tcp", catalogAddr, config) | ||
if err != nil { | ||
return nil, "", fmt.Errorf("unable to create HTTPS server listener: %w", err) | ||
} | ||
if err := mgr.Add(tlsFileWatcher); err != nil { | ||
return nil, "", fmt.Errorf("unable to add TLS file watcher to manager: %w", err) | ||
} | ||
addr = "https://" + addr | ||
return listener, addr, nil | ||
default: | ||
return nil, "", fmt.Errorf("unable to configure TLS certificates, tls-cert and tls-key flags must be used together") | ||
} | ||
} |