From c6411a4e1dfe3564bdfd59c4ab7f594ea8fc3771 Mon Sep 17 00:00:00 2001 From: Francesco Giudici Date: Thu, 8 Aug 2024 10:58:44 +0200 Subject: [PATCH] register: always register when called (#816) Remove the static check to re-register only after 24 hours. The re-registration will then happen every time the elemental-register client is called. This static timer was introduced to limit as much as possible the communication form the host to Rancher and save communication bandwidth for remote clients. Anyway, this makes not much sense as long as the elemental-system-agent is running, which will in any case keep connecting to Rancher. The call to the elemental-register binary is performed on official Elemental SLE Micro images every 30 minutes and at each boot. Fixes https://github.com/rancher/elemental-operator/issues/811 Backport of #813 Signed-off-by: Francesco Giudici (cherry picked from commit 36468aba429ec2e4472b5762b8a400f9c2c363a4) --- cmd/register/main.go | 5 ----- cmd/register/main_test.go | 10 +++++----- pkg/install/install_test.go | 1 - pkg/register/register.go | 1 - pkg/register/state.go | 5 ----- pkg/register/state_test.go | 15 --------------- 6 files changed, 5 insertions(+), 32 deletions(-) diff --git a/cmd/register/main.go b/cmd/register/main.go index f748e067d..7db816310 100644 --- a/cmd/register/main.go +++ b/cmd/register/main.go @@ -100,11 +100,6 @@ func newCommand(fs vfs.FS, client register.Client, stateHandler register.StateHa if err != nil { return fmt.Errorf("getting registration state: %w", err) } - // Determine if registration should execute or skip a cycle - if !installation && !reset && !registrationState.HasLastUpdateElapsed(registrationUpdateSuppressTimer) { - log.Info("Nothing to do") - return nil - } // Validate CA caCert, err := getRegistrationCA(fs, cfg) if err != nil { diff --git a/cmd/register/main_test.go b/cmd/register/main_test.go index 333b55731..7d248327e 100644 --- a/cmd/register/main_test.go +++ b/cmd/register/main_test.go @@ -107,7 +107,6 @@ var ( } stateFixture = register.State{ InitialRegistration: time.Now(), - LastUpdate: time.Time{}, EmulatedTPM: true, EmulatedTPMSeed: 987654321, } @@ -206,18 +205,19 @@ var _ = Describe("elemental-register state", Label("registration", "cli", "state cmd.SetArgs([]string{"--state-path", newPath}) registrationState := register.State{ InitialRegistration: time.Now(), - LastUpdate: time.Now(), } stateHandler.EXPECT().Init(newPath).Return(nil) stateHandler.EXPECT().Load().Return(registrationState, nil) - client.EXPECT().Register(gomock.Any(), gomock.Any(), gomock.Any()).Times(0) + stateHandler.EXPECT().Save(registrationState).Return(nil) + client.EXPECT(). + Register(baseConfigFixture.Elemental.Registration, []byte(baseConfigFixture.Elemental.Registration.CACert), ®istrationState). + Return(marshalToBytes(baseConfigFixture), nil) Expect(cmd.Execute()).ToNot(HaveOccurred()) }) - It("should not skip registration if lastUpdate is stale", func() { + It("should return the registration data", func() { cmd.SetArgs([]string{}) registrationState := register.State{ InitialRegistration: time.Now(), - LastUpdate: time.Now().Add(-25 * time.Hour), } stateHandler.EXPECT().Init(defaultStatePath).Return(nil) stateHandler.EXPECT().Load().Return(registrationState, nil) diff --git a/pkg/install/install_test.go b/pkg/install/install_test.go index ae9afc2bc..27080aaeb 100644 --- a/pkg/install/install_test.go +++ b/pkg/install/install_test.go @@ -86,7 +86,6 @@ var ( } stateFixture = register.State{ InitialRegistration: time.Date(2023, time.August, 2, 12, 35, 10, 3, time.UTC), - LastUpdate: time.Time{}, EmulatedTPM: true, EmulatedTPMSeed: 987654321, } diff --git a/pkg/register/register.go b/pkg/register/register.go index 05ef74b2c..76479e201 100644 --- a/pkg/register/register.go +++ b/pkg/register/register.go @@ -97,7 +97,6 @@ func (r *client) Register(reg elementalv1.Registration, caCert []byte, state *St if err := sendUpdateData(conn); err != nil { return nil, fmt.Errorf("failed to send update data: %w", err) } - state.LastUpdate = time.Now() } else { state.InitialRegistration = time.Now() } diff --git a/pkg/register/state.go b/pkg/register/state.go index f94bb4e24..337a81ed5 100644 --- a/pkg/register/state.go +++ b/pkg/register/state.go @@ -31,7 +31,6 @@ import ( type State struct { InitialRegistration time.Time `yaml:"initialRegistration,omitempty"` - LastUpdate time.Time `yaml:"lastUpdate,omitempty"` EmulatedTPM bool `yaml:"emulatedTPM,omitempty"` EmulatedTPMSeed int64 `yaml:"emulatedTPMSeed,omitempty"` } @@ -40,10 +39,6 @@ func (s *State) IsUpdatable() bool { return !s.InitialRegistration.IsZero() } -func (s *State) HasLastUpdateElapsed(suppress time.Duration) bool { - return time.Now().After(s.LastUpdate.Add(suppress)) -} - type StateHandler interface { Init(location string) error Load() (State, error) diff --git a/pkg/register/state_test.go b/pkg/register/state_test.go index 9b750f35e..564cf35de 100644 --- a/pkg/register/state_test.go +++ b/pkg/register/state_test.go @@ -37,10 +37,8 @@ func TestRegister(t *testing.T) { var ( testStateDir = "/test/register/state" testStatePath = fmt.Sprintf("%s/%s", testStateDir, "state.yaml") - loc, _ = time.LoadLocation("Europe/Berlin") stateFixture = State{ InitialRegistration: time.Now().UTC(), - LastUpdate: time.Now().UTC(), EmulatedTPM: true, EmulatedTPMSeed: 123456789, } @@ -59,19 +57,6 @@ var _ = Describe("is state updatable", Label("registration", "state"), func() { }) }) -var _ = Describe("has state update elapsed", Label("registration", "state"), func() { - It("returns false if the state is new", func() { - state := State{} - Expect(state.HasLastUpdateElapsed(-1 * time.Hour)).To(BeTrue()) - }) - It("returns true last update time is more than suppress timer ago", func() { - state := State{ - LastUpdate: time.Now().Add(-10 * time.Hour), - } - Expect(state.HasLastUpdateElapsed(1 * time.Hour)).To(BeTrue()) - }) -}) - var _ = Describe("init file state handler", Label("registration", "state"), func() { var fs vfs.FS var handler StateHandler