Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend strict TLS mode to fleet-agent container #2556

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions internal/cmd/agent/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ func Get(ctx context.Context, namespace string, cfg *rest.Config) (*AgentInfo, e
return nil, err
}

agentConfig, err := config.Lookup(ctx, namespace, config.AgentConfigName, k8s.Core().V1().ConfigMap())
if err != nil {
return nil, fmt.Errorf(
"failed to look up client config %s/%s: %w",
namespace,
config.AgentConfigName,
err,
)
}

if agentConfig.AgentTLSMode == config.AgentTLSModeStrict {
config.BypassSystemCAStore()
}

secret, err := k8s.Core().V1().Secret().Get(namespace, CredName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
logrus.Warn("Cannot find fleet-agent secret")
Expand Down Expand Up @@ -330,18 +344,7 @@ func createClientConfigFromSecret(secret *corev1.Secret, trustSystemStoreCAs boo
apiServerCA = nil
}
} else {
// Bypass the OS trust store through env vars, see https://pkg.go.dev/crypto/x509#SystemCertPool
// We set values to paths belonging to the root filesystem, which is read-only, to prevent tampering.
// Note: this will not work on Windows nor Mac OS. Agent are expected to run on Linux nodes.
err := os.Setenv("SSL_CERT_FILE", "/dev/null")
if err != nil {
logrus.Errorf("failed to set env var SSL_CERT_FILE: %s", err.Error())
}

err = os.Setenv("SSL_CERT_DIR", "/dev/null")
if err != nil {
logrus.Errorf("failed to set env var SSL_CERT_DIR: %s", err.Error())
}
config.BypassSystemCAStore()
}

cfg := clientcmdapi.Config{
Expand Down
25 changes: 25 additions & 0 deletions internal/config/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"os"

"github.com/sirupsen/logrus"
)

// BypassSystemCAStore is used to bypass the OS trust store in agents through env vars, see
// https://pkg.go.dev/crypto/x509#SystemCertPool for more info.
// We set values to paths belonging to the root filesystem, which is read-only, to prevent tampering.
// Eventually, this should not be necessary, if/when we find a way to set client-go's API Config to achieve similar
// effects.
// Note: this will not work on Windows nor Mac OS. Agents are expected to run on Linux nodes.
func BypassSystemCAStore() {
err := os.Setenv("SSL_CERT_FILE", "/dev/null")
if err != nil {
logrus.Errorf("failed to set env var SSL_CERT_FILE: %s", err.Error())
}

err = os.Setenv("SSL_CERT_DIR", "/dev/null")
if err != nil {
logrus.Errorf("failed to set env var SSL_CERT_DIR: %s", err.Error())
}
}
Loading