core: read mon secret from file instead of env var
Environment variables are not recommended for secrets in pods since
they can be easily leaked if the environemnt variables are logged.
By mounting the mon secret as a file, the mgr and osd prepare pods
can read the mon secret from a file for better security.
Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
(cherry picked from commit 403335177a)
This commit is contained in:
9 files changed
+119
-13
No files matched your search
@@ -73,7 +73,6 @@ func addCephFlags(command *cobra.Command) {
|
||||
command.Flags().StringVar(&clusterInfo.FSID, "fsid", "", "the cluster uuid")
|
||||
command.Flags().StringVar(&clusterInfo.MonitorSecret, "mon-secret", "", "the cephx keyring for monitors")
|
||||
command.Flags().StringVar(&clusterInfo.CephCred.Username, "ceph-username", "", "ceph username")
|
||||
command.Flags().StringVar(&clusterInfo.CephCred.Secret, "ceph-secret", "", "secret for the ceph user (random if not specified)")
|
||||
command.Flags().StringVar(&cfg.monEndpoints, "mon-endpoints", "", "ceph mon endpoints")
|
||||
command.Flags().StringVar(&cfg.dataDir, "config-dir", "/var/lib/rook", "directory for storing configuration")
|
||||
command.Flags().StringVar(&cfg.cephConfigOverride, "ceph-config-override", "", "optional path to a ceph config file that will be appended to the config files that rook generates")
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package ceph
|
||||
|
||||
import (
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/rook/rook/cmd/rook/rook"
|
||||
@@ -68,6 +69,10 @@ func runMgrSidecar(cmd *cobra.Command, args []string) error {
|
||||
rook.SetLogLevel()
|
||||
clusterInfo.Context = cmd.Context()
|
||||
|
||||
if err := readCephSecret(path.Join(mon.CephSecretMountPath, mon.CephSecretFilename)); err != nil {
|
||||
rook.TerminateFatal(err)
|
||||
}
|
||||
|
||||
context := createContext()
|
||||
clusterInfo.Monitors = mon.ParseMonEndpoints(cfg.monEndpoints)
|
||||
rook.LogStartupInfo(mgrSidecarCmd.Flags())
|
||||
|
||||
+39
-2
@@ -20,6 +20,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -76,6 +77,11 @@ var (
|
||||
forceOSDRemoval string
|
||||
)
|
||||
|
||||
const (
|
||||
//#nosec G101 -- This is only an env var name
|
||||
fallbackCephSecretEnvVar = "ROOK_CEPH_SECRET"
|
||||
)
|
||||
|
||||
func addOSDFlags(command *cobra.Command) {
|
||||
addOSDConfigFlags(osdConfigCmd)
|
||||
addOSDConfigFlags(provisionCmd)
|
||||
@@ -166,7 +172,7 @@ func verifyConfigFlags(configCmd *cobra.Command) error {
|
||||
if err := flags.VerifyRequiredFlags(configCmd, required); err != nil {
|
||||
return err
|
||||
}
|
||||
required = []string{"mon-endpoints", "ceph-username", "ceph-secret"}
|
||||
required = []string{"mon-endpoints", "ceph-username"}
|
||||
if err := flags.VerifyRequiredFlags(osdCmd, required); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -192,6 +198,10 @@ func prepareOSD(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := readCephSecret(path.Join(mon.CephSecretMountPath, mon.CephSecretFilename)); err != nil {
|
||||
rook.TerminateFatal(err)
|
||||
}
|
||||
|
||||
var dataDevices []osddaemon.DesiredDevice
|
||||
if osdDataDeviceFilter != "" {
|
||||
if cfg.devices != "" || osdDataDevicePathFilter != "" {
|
||||
@@ -257,11 +267,15 @@ func removeOSDs(cmd *cobra.Command, args []string) error {
|
||||
if err := flags.VerifyRequiredFlags(osdRemoveCmd, required); err != nil {
|
||||
return err
|
||||
}
|
||||
required = []string{"mon-endpoints", "ceph-username", "ceph-secret"}
|
||||
required = []string{"mon-endpoints", "ceph-username"}
|
||||
if err := flags.VerifyRequiredFlags(osdCmd, required); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := readCephSecret(path.Join(mon.CephSecretMountPath, mon.CephSecretFilename)); err != nil {
|
||||
rook.TerminateFatal(err)
|
||||
}
|
||||
|
||||
commonOSDInit(osdRemoveCmd)
|
||||
|
||||
context := createContext()
|
||||
@@ -348,3 +362,26 @@ func parseDevices(devices string) ([]osddaemon.DesiredDevice, error) {
|
||||
logger.Infof("desired devices to configure osds: %+v", result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Populate the ceph admin secret from a file
|
||||
// This is more secret than using an environment variable for the secret
|
||||
// since environment variables are easier to access than a file inside the container.
|
||||
func readCephSecret(path string) error {
|
||||
secret, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
// For backward compatibility we need to check if the env var is still set
|
||||
adminSecretEnv := os.Getenv(fallbackCephSecretEnvVar)
|
||||
if adminSecretEnv == "" {
|
||||
// Go ahead and fail since neither the file could be loaded nor is the env var set
|
||||
return errors.Wrapf(err, "failed to read ceph secret file from %q", mon.CephSecretMountPath)
|
||||
}
|
||||
logger.Warningf("loaded admin secret from env var %s instead of from file", fallbackCephSecretEnvVar)
|
||||
secret = []byte(adminSecretEnv)
|
||||
}
|
||||
|
||||
clusterInfo.CephCred.Secret = string(secret)
|
||||
if clusterInfo.CephCred.Secret == "" {
|
||||
return errors.New("ceph admin secret not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package ceph
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
osddaemon "github.com/rook/rook/pkg/daemon/ceph/osd"
|
||||
@@ -173,3 +174,37 @@ func TestParseDesiredDevices(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []osddaemon.DesiredDevice{}, result)
|
||||
}
|
||||
|
||||
func TestReadSecretFile(t *testing.T) {
|
||||
// Fail if the file does not exist
|
||||
badPath := "/tmp/badpath"
|
||||
err := readCephSecret(badPath)
|
||||
assert.Error(t, err)
|
||||
|
||||
// Fall back to the env var if the file is not found
|
||||
assert.NoError(t, os.Setenv(fallbackCephSecretEnvVar, "env-secret"))
|
||||
err = readCephSecret(badPath)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "env-secret", clusterInfo.CephCred.Secret)
|
||||
os.Unsetenv(fallbackCephSecretEnvVar)
|
||||
|
||||
// Create a temp file, but leave it empty
|
||||
path, err := os.CreateTemp("", "")
|
||||
assert.NoError(t, err)
|
||||
defer os.Remove(path.Name())
|
||||
|
||||
// Fail if the secret file is empty
|
||||
err = readCephSecret(path.Name())
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "", clusterInfo.CephCred.Secret)
|
||||
|
||||
// Write a test keyring
|
||||
testSecret := "testkeyring"
|
||||
err = os.WriteFile(path.Name(), []byte(testSecret), 0600)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Read the secret from the file
|
||||
err = readCephSecret(path.Name())
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testSecret, clusterInfo.CephCred.Secret)
|
||||
}
|
||||
@@ -74,6 +74,7 @@ func (c *Cluster) makeDeployment(mgrConfig *mgrConfig) (*apps.Deployment, error)
|
||||
if c.spec.Mgr.Count > 1 {
|
||||
podSpec.Spec.Containers = append(podSpec.Spec.Containers, c.makeMgrSidecarContainer(mgrConfig))
|
||||
matchLabels := controller.AppLabels(AppName, c.clusterInfo.Namespace)
|
||||
podSpec.Spec.Volumes = append(podSpec.Spec.Volumes, mon.CephSecretVolume())
|
||||
|
||||
// Stretch the mgrs across hosts by default, or across a bigger failure domain for stretch clusters
|
||||
topologyKey := v1.LabelHostname
|
||||
@@ -215,7 +216,6 @@ func (c *Cluster) makeMgrSidecarContainer(mgrConfig *mgrConfig) v1.Container {
|
||||
mon.PodNamespaceEnvVar(c.clusterInfo.Namespace),
|
||||
mon.EndpointEnvVar(),
|
||||
mon.CephUsernameEnvVar(),
|
||||
mon.CephSecretEnvVar(),
|
||||
k8sutil.ConfigOverrideEnvVar(),
|
||||
{Name: "ROOK_DASHBOARD_ENABLED", Value: strconv.FormatBool(c.spec.Dashboard.Enabled)},
|
||||
{Name: "ROOK_MONITORING_ENABLED", Value: strconv.FormatBool(c.spec.Monitoring.Enabled)},
|
||||
@@ -232,6 +232,7 @@ func (c *Cluster) makeMgrSidecarContainer(mgrConfig *mgrConfig) v1.Container {
|
||||
Env: envVars,
|
||||
Resources: cephv1.GetMgrSidecarResources(c.spec.Resources),
|
||||
SecurityContext: controller.PrivilegedContext(true),
|
||||
VolumeMounts: []v1.VolumeMount{mon.CephSecretVolumeMount()},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,9 +24,18 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rook/rook/pkg/clusterd"
|
||||
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
|
||||
opcontroller "github.com/rook/rook/pkg/operator/ceph/controller"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
//#nosec G101 -- This is only a path name
|
||||
CephSecretMountPath = "/var/lib/rook-ceph-mon"
|
||||
//#nosec G101 -- This is only a filename
|
||||
CephSecretFilename = "secret.keyring"
|
||||
//#nosec G101 -- This is only a volume name
|
||||
cephSecretVolumeName = "ceph-admin-secret"
|
||||
|
||||
// All mons share the same keyring
|
||||
keyringStoreName = "rook-ceph-mons"
|
||||
|
||||
@@ -67,3 +76,25 @@ func WriteConnectionConfig(context *clusterd.Context, clusterInfo *cephclient.Cl
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CephSecretVolume is a volume for the ceph admin secret
|
||||
func CephSecretVolume() v1.Volume {
|
||||
return v1.Volume{
|
||||
Name: cephSecretVolumeName,
|
||||
VolumeSource: v1.VolumeSource{
|
||||
Secret: &v1.SecretVolumeSource{
|
||||
SecretName: AppName,
|
||||
Items: []v1.KeyToPath{{Key: opcontroller.CephUserSecretKey, Path: CephSecretFilename}},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// CephSecretVolumeMount is a mount for the ceph admin secret
|
||||
func CephSecretVolumeMount() v1.VolumeMount {
|
||||
return v1.VolumeMount{
|
||||
Name: cephSecretVolumeName,
|
||||
MountPath: CephSecretMountPath,
|
||||
ReadOnly: true,
|
||||
}
|
||||
}
|
||||
@@ -38,9 +38,3 @@ func CephUsernameEnvVar() v1.EnvVar {
|
||||
ref := &v1.SecretKeySelector{LocalObjectReference: v1.LocalObjectReference{Name: AppName}, Key: opcontroller.CephUsernameKey}
|
||||
return v1.EnvVar{Name: "ROOK_CEPH_USERNAME", ValueFrom: &v1.EnvVarSource{SecretKeyRef: ref}}
|
||||
}
|
||||
|
||||
// CephSecretEnvVar is the ceph secret environment var
|
||||
func CephSecretEnvVar() v1.EnvVar {
|
||||
ref := &v1.SecretKeySelector{LocalObjectReference: v1.LocalObjectReference{Name: AppName}, Key: opcontroller.CephUserSecretKey}
|
||||
return v1.EnvVar{Name: "ROOK_CEPH_SECRET", ValueFrom: &v1.EnvVarSource{SecretKeyRef: ref}}
|
||||
}
|
||||
@@ -73,7 +73,6 @@ func (c *Cluster) getConfigEnvVars(osdProps osdProperties, dataDir string, prepa
|
||||
if prepare {
|
||||
envVars = append(envVars, []v1.EnvVar{
|
||||
opmon.CephUsernameEnvVar(),
|
||||
opmon.CephSecretEnvVar(),
|
||||
{Name: "ROOK_FSID", ValueFrom: &v1.EnvVarSource{
|
||||
SecretKeyRef: &v1.SecretKeySelector{
|
||||
LocalObjectReference: v1.LocalObjectReference{Name: "rook-ceph-mon"},
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
||||
kms "github.com/rook/rook/pkg/daemon/ceph/osd/kms"
|
||||
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
|
||||
"github.com/rook/rook/pkg/operator/ceph/cluster/osd/config"
|
||||
"github.com/rook/rook/pkg/operator/ceph/controller"
|
||||
"github.com/rook/rook/pkg/operator/k8sutil"
|
||||
@@ -104,9 +105,12 @@ func (c *Cluster) provisionPodTemplateSpec(osdProps osdProperties, restart v1.Re
|
||||
|
||||
// create a volume on /dev so the pod can access devices on the host
|
||||
devVolume := v1.Volume{Name: "devices", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/dev"}}}
|
||||
volumes = append(volumes, devVolume)
|
||||
udevVolume := v1.Volume{Name: "udev", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/run/udev"}}}
|
||||
volumes = append(volumes, udevVolume)
|
||||
volumes = append(volumes, []v1.Volume{
|
||||
udevVolume,
|
||||
devVolume,
|
||||
mon.CephSecretVolume(),
|
||||
}...)
|
||||
|
||||
if osdProps.onPVC() {
|
||||
// Create volume config for PVCs
|
||||
@@ -235,6 +239,7 @@ func (c *Cluster) provisionOSDContainer(osdProps osdProperties, copyBinariesMoun
|
||||
{Name: "devices", MountPath: "/dev"},
|
||||
{Name: "udev", MountPath: "/run/udev"},
|
||||
copyBinariesMount,
|
||||
mon.CephSecretVolumeMount(),
|
||||
}...)
|
||||
|
||||
if controller.LoopDevicesAllowed() {
|
||||
|
||||
Reference in new issue
Block a user