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

Non root etcd #331

Merged
merged 2 commits into from
Oct 29, 2020
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
11 changes: 8 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"syscall"
"time"

"github.com/Mirantis/mke/pkg/performance"

"github.com/avast/retry-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -36,6 +34,7 @@ import (
"github.com/Mirantis/mke/pkg/component/server"
"github.com/Mirantis/mke/pkg/component/worker"
"github.com/Mirantis/mke/pkg/constant"
"github.com/Mirantis/mke/pkg/performance"
"github.com/Mirantis/mke/pkg/util"

"github.com/Mirantis/mke/pkg/apis/v1beta1"
Expand Down Expand Up @@ -89,10 +88,16 @@ func startServer(ctx *cli.Context) error {
if err != nil {
return err
}
componentManager := component.NewManager()

// create directories early with the proper permissions
if err = util.InitDirectory(constant.DataDir, constant.DataDirMode); err != nil {
return err
}
if err := util.InitDirectory(constant.CertRootDir, constant.CertRootDirMode); err != nil {
return err
}

componentManager := component.NewManager()
certificateManager := certificate.Manager{}

var join = false
Expand Down
2 changes: 2 additions & 0 deletions inttest/footloose-alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ RUN echo "#!/bin/sh" > /etc/local.d/machine-id.start \
&& echo "fi" >> /etc/local.d/machine-id.start \
&& chmod +x /etc/local.d/machine-id.start

RUN adduser -H -S -s /sbin/nologin etcd

# Put kubectl into place to ease up debugging
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.19.0/bin/linux/amd64/kubectl \
&& chmod +x ./kubectl \
Expand Down
4 changes: 0 additions & 4 deletions pkg/certificate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (m *Manager) EnsureCA(name, cn string) error {
return nil
}

if err := util.InitDirectory(filepath.Dir(keyFile), constant.CertRootDirMode); err != nil {
return errors.Wrapf(err, "failed to create pki dir")
}

req := new(csr.CertificateRequest)
req.KeyRequest = csr.NewKeyRequest()
req.KeyRequest.A = "rsa"
Expand Down
22 changes: 17 additions & 5 deletions pkg/component/server/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,24 @@ func (e *Etcd) Init() error {
if err != nil {
logrus.Warning(errors.Wrap(err, "Running etcd as root"))
}
e.gid, _ = util.GetGID(constant.Group)

err = util.InitDirectory(constant.EtcdDataDir, constant.EtcdDataDirMode) // https://docs.datadoghq.com/security_monitoring/default_rules/cis-kubernetes-1.5.1-1.1.11/
if err != nil {
return errors.Wrapf(err, "failed to create %s", constant.EtcdDataDir)
}

e.gid, _ = util.GetGID(constant.Group)
err = util.InitDirectory(constant.EtcdCertDir, constant.EtcdCertDirMode) // https://docs.datadoghq.com/security_monitoring/default_rules/cis-kubernetes-1.5.1-4.1.7/
if err != nil {
return errors.Wrapf(err, "failed to create etcd cert dir")
}

for _, f := range []string{constant.EtcdDataDir, constant.EtcdCertDir} {
err = os.Chown(f, e.uid, e.gid)
if err != nil {
return err
}
}

for _, f := range []string{
"ca.crt",
Expand Down Expand Up @@ -130,10 +141,6 @@ func (e *Etcd) Run() error {
if util.FileExists(etcdCaCert) && util.FileExists(etcdCaCertKey) {
logrus.Warnf("etcd ca certs already exists, not gonna overwrite. If you wish to re-sync them, delete the existing ones.")
} else {
err := util.InitDirectory(filepath.Dir(etcdCaCertKey), constant.CertSecureMode) // https://docs.datadoghq.com/security_monitoring/default_rules/cis-kubernetes-1.5.1-4.1.7/
if err != nil {
return errors.Wrapf(err, "failed to create etcd cert dir")
}
err = ioutil.WriteFile(etcdCaCertKey, etcdResponse.CA.Key, constant.CertSecureMode)
if err != nil {
return err
Expand All @@ -143,6 +150,11 @@ func (e *Etcd) Run() error {
if err != nil {
return err
}
for _, f := range []string{filepath.Dir(etcdCaCertKey), etcdCaCertKey, etcdCaCert} {
if err := os.Chown(f, e.uid, e.gid); err != nil {
return err
}
}
}

args = append(args, fmt.Sprintf("--initial-cluster=%s", strings.Join(etcdResponse.InitialCluster, ",")))
Expand Down
2 changes: 1 addition & 1 deletion pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// CertRootDir defines the root location for all pki related artifacts
CertRootDir = "/var/lib/mke/pki"
// CertRootDirMode is the expected directory permissions for CertRootDir.
CertRootDirMode = 0750
CertRootDirMode = 0751
//EtcdCertDir contains etcd certificates
EtcdCertDir = "/var/lib/mke/pki/etcd"
// EtcdCertDirMode is the expected directory permissions for EtcdCertDir
Expand Down