Skip to content

Commit ec135ee

Browse files
committed
daemon: Exec journalctl rather than using library
The way the MCD today chroots into the host is problematic for major OS version updates and creates a hidden dependency. Eventually we need to stop chrooting, see: #543
1 parent 5dc6cc3 commit ec135ee

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

pkg/daemon/daemon.go

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"io/ioutil"
910
"net/http"
1011
"os"
@@ -15,7 +16,6 @@ import (
1516

1617
imgref "github.com/containers/image/docker/reference"
1718
"github.com/coreos/go-systemd/login1"
18-
"github.com/coreos/go-systemd/sdjournal"
1919
ignv2 "github.com/coreos/ignition/config/v2_2"
2020
ignv2_2types "github.com/coreos/ignition/config/v2_2/types"
2121
"github.com/golang/glog"
@@ -422,34 +422,13 @@ const (
422422
sdMessageSessionStart = "8d45620c1a4348dbb17410da57c60c66"
423423
)
424424

425+
// detectEarlySSHAccessesFromBoot taints the node if we find a login before the daemon started up.
425426
func (dn *Daemon) detectEarlySSHAccessesFromBoot() error {
426-
journal, err := sdjournal.NewJournal()
427+
journalOutput, err := exec.Command("journalctl", "-b", "-o", "cat", "MESSAGE_ID="+sdMessageSessionStart).CombinedOutput()
427428
if err != nil {
428429
return err
429430
}
430-
defer journal.Close()
431-
if err := journal.AddMatch("MESSAGE_ID=" + sdMessageSessionStart); err != nil {
432-
return err
433-
}
434-
if err := journal.SeekHead(); err != nil {
435-
return err
436-
}
437-
r, err := journal.Next()
438-
if err != nil {
439-
return err
440-
}
441-
// journal EOF
442-
if r == 0 {
443-
return nil
444-
}
445-
// just one entry is enough to understand if someone jumped on the node
446-
// from the very first boot
447-
entry, err := journal.GetEntry()
448-
if err != nil {
449-
return err
450-
}
451-
// just sanity checking
452-
if entry != nil {
431+
if len(journalOutput) > 0 {
453432
glog.Info("Detected a login session before the daemon took over on first boot")
454433
glog.Infof("Applying annotation: %v", machineConfigDaemonSSHAccessAnnotationKey)
455434
if err := dn.applySSHAccessedAnnotation(); err != nil {
@@ -521,22 +500,47 @@ func (dn *Daemon) BindPodMounts() error {
521500
}
522501

523502
func (dn *Daemon) runLoginMonitor(stopCh <-chan struct{}, exitCh chan<- error) {
524-
sessionNewCh := dn.loginClient.Subscribe("SessionNew")
525-
for {
526-
select {
527-
case <-stopCh:
528-
return
529-
case msg, ok := <-sessionNewCh:
530-
if !ok {
531-
glog.V(4).Info("Not adding the ssh accessed annotation because the logind SessionNew channel is closed")
532-
return
533-
}
534-
glog.Infof("Detected a new login session: %v", msg)
535-
glog.Infof("Login access is discouraged! Applying annotation: %v", machineConfigDaemonSSHAccessAnnotationKey)
536-
if err := dn.applySSHAccessedAnnotation(); err != nil {
537-
exitCh <- err
538-
}
539-
}
503+
cmd := exec.Command("journalctl", "-b", "-f", "-o", "cat", "MESSAGE_ID="+sdMessageSessionStart)
504+
stdout, err := cmd.StdoutPipe()
505+
if err != nil {
506+
exitCh <- err
507+
return
508+
}
509+
if err := cmd.Start(); err != nil {
510+
exitCh <- err
511+
return
512+
}
513+
worker := make(chan struct{})
514+
go func() {
515+
for {
516+
select {
517+
case <-worker:
518+
return
519+
default:
520+
buf := make([]byte, 1024)
521+
l, err := stdout.Read(buf)
522+
if err != nil {
523+
if err == io.EOF {
524+
return
525+
}
526+
exitCh <- err
527+
return
528+
}
529+
if l > 0 {
530+
line := strings.Split(string(buf), "\n")[0]
531+
glog.Infof("Detected a new login session: %s", line)
532+
glog.Infof("Login access is discouraged! Applying annotation: %v", machineConfigDaemonSSHAccessAnnotationKey)
533+
if err := dn.applySSHAccessedAnnotation(); err != nil {
534+
exitCh <- err
535+
}
536+
}
537+
}
538+
}
539+
}()
540+
select {
541+
case <-stopCh:
542+
close(worker)
543+
cmd.Process.Kill()
540544
}
541545
}
542546

@@ -736,10 +740,8 @@ func (dn *Daemon) CheckStateOnBoot() error {
736740
if err != nil {
737741
return err
738742
}
739-
if state.bootstrapping {
740-
if err := dn.detectEarlySSHAccessesFromBoot(); err != nil {
741-
return fmt.Errorf("error detecting early SSH accesses: %v", err)
742-
}
743+
if err := dn.detectEarlySSHAccessesFromBoot(); err != nil {
744+
return fmt.Errorf("error detecting previous SSH accesses: %v", err)
743745
}
744746

745747
if state.bootstrapping {

0 commit comments

Comments
 (0)