Skip to content

Commit

Permalink
Provide TLS Certificate to kubelet service status request
Browse files Browse the repository at this point in the history
This change enhances the security of the kubelet service request in the
control plane manager by providing a TLS certificate when making the
request.

- Provide a CertStorageReader to the Manager
- Re-use PrepareCredentials function from certificates package to create
  the proper certificates
- Change TLSConfig providing Certificates and setting InsecureSkipVerify
  to false

see https://issues.redhat.com/browse/ECOPROJECT-1421

Signed-off-by: Carlo Lobrano <[email protected]>
  • Loading branch information
clobrano committed Oct 13, 2023
1 parent eea79f7 commit e223ee7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func initSelfNodeRemediationAgent(mgr manager.Manager) {
MaxTimeForNoPeersResponse: reboot.MaxTimeForNoPeersResponse,
}

controlPlaneManager := controlplane.NewManager(myNodeName, mgr.GetClient())
controlPlaneManager := controlplane.NewManager(myNodeName, mgr.GetClient(), certReader)

if err = mgr.Add(controlPlaneManager); err != nil {
setupLog.Error(err, "failed to add controlPlane remediation manager to setup manager")
Expand Down
6 changes: 3 additions & 3 deletions pkg/certificates/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TLSMinVersion = tls.VersionTLS13

func GetServerCredentialsFromCerts(certReader CertStorageReader) (credentials.TransportCredentials, error) {

keyPair, pool, err := prepareCredentials(certReader)
keyPair, pool, err := PrepareCredentials(certReader)
if err != nil {
return nil, err
}
Expand All @@ -27,7 +27,7 @@ func GetServerCredentialsFromCerts(certReader CertStorageReader) (credentials.Tr

func GetClientCredentialsFromCerts(certReader CertStorageReader) (credentials.TransportCredentials, error) {

keyPair, pool, err := prepareCredentials(certReader)
keyPair, pool, err := PrepareCredentials(certReader)
if err != nil {
return nil, err
}
Expand All @@ -40,7 +40,7 @@ func GetClientCredentialsFromCerts(certReader CertStorageReader) (credentials.Tr
}), nil
}

func prepareCredentials(certReader CertStorageReader) (*tls.Certificate, *x509.CertPool, error) {
func PrepareCredentials(certReader CertStorageReader) (*tls.Certificate, *x509.CertPool, error) {
caPem, certPem, keyPem, err := certReader.GetCerts()
if err != nil {
return nil, nil, err
Expand Down
14 changes: 11 additions & 3 deletions pkg/controlplane/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ type Manager struct {
wasEndpointAccessibleAtStart bool
client client.Client
log logr.Logger
certReader certificates.CertStorageReader
}

// NewManager inits a new Manager return nil if init fails
func NewManager(nodeName string, myClient client.Client) *Manager {
func NewManager(nodeName string, myClient client.Client, certReader *certificates.SecretCertStorage) *Manager {
return &Manager{
nodeName: nodeName,
endpointHealthCheckUrl: os.Getenv("END_POINT_HEALTH_CHECK_URL"),
client: myClient,
wasEndpointAccessibleAtStart: false,
log: ctrl.Log.WithName("controlPlane").WithName("Manager"),
certReader: certReader,
}
}

Expand Down Expand Up @@ -149,15 +151,21 @@ func (manager *Manager) isEndpointAccessible() bool {
}

func (manager *Manager) isKubeletServiceRunning() bool {
url := fmt.Sprintf("https://%s:%s/pods", manager.nodeName, kubeletPort)
keyPair, _, err := certificates.PrepareCredentials(manager.certReader)
if err != nil {
manager.log.Error(err, "failed to prepare credentials", "node name", manager.nodeName)
return false
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{*keyPair},
InsecureSkipVerify: false,
MinVersion: certificates.TLSMinVersion,
},
}
httpClient := &http.Client{Transport: tr}

url := fmt.Sprintf("https://%s:%s/pods", manager.nodeName, kubeletPort)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
manager.log.Error(err, "failed to create a kubelet service request", "node name", manager.nodeName)
Expand Down

0 comments on commit e223ee7

Please sign in to comment.