Skip to content

Commit

Permalink
[v9] Regenerate host UUID of node if host_uuid is empty (#12222)
Browse files Browse the repository at this point in the history
This change allows nodes to regenerate their UUID if the host_uuid file exists but is empty.
  • Loading branch information
atburke authored Apr 26, 2022
1 parent ee00eb0 commit d89371c
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 196 deletions.
57 changes: 57 additions & 0 deletions api/utils/slices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSliceContainsStr(t *testing.T) {
tests := []struct {
name string
slice []string
target string
wantContains bool
}{
{name: "does contain", slice: []string{"two", "one"}, target: "one", wantContains: true},
{name: "does not contain", slice: []string{"two", "one"}, target: "five", wantContains: false},
{name: "empty slice", slice: nil, target: "one", wantContains: false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.wantContains, SliceContainsStr(tc.slice, tc.target))
})
}
}

func TestDeduplicate(t *testing.T) {
tests := []struct {
name string
in, expected []string
}{
{name: "empty slice", in: []string{}, expected: []string{}},
{name: "slice with unique elements", in: []string{"a", "b"}, expected: []string{"a", "b"}},
{name: "slice with duplicate elements", in: []string{"a", "b", "b", "a", "c"}, expected: []string{"a", "b", "c"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, Deduplicate(tc.in))
})
}
}
13 changes: 12 additions & 1 deletion lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,18 @@ func NewTeleport(cfg *Config) (*TeleportProcess, error) {
} else {
switch cfg.JoinMethod {
case types.JoinMethodToken, types.JoinMethodUnspecified, types.JoinMethodIAM:
cfg.HostUUID = uuid.New().String()
// Checking error instead of the usual uuid.New() in case uuid generation
// fails due to not enough randomness. It's been known to happen happen when
// Teleport starts very early in the node initialization cycle and /dev/urandom
// isn't ready yet.
rawID, err := uuid.NewRandom()
if err != nil {
return nil, trace.BadParameter("" +
"Teleport failed to generate host UUID. " +
"This may happen if randomness source is not fully initialized when the node is starting up. " +
"Please try restarting Teleport again.")
}
cfg.HostUUID = rawID.String()
case types.JoinMethodEC2:
cfg.HostUUID, err = utils.GetEC2NodeID()
if err != nil {
Expand Down
23 changes: 11 additions & 12 deletions lib/utils/proxyjump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ limitations under the License.
package utils

import (
"gopkg.in/check.v1"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func (s *UtilsSuite) TestProxyJumpParsing(c *check.C) {
func TestProxyJumpParsing(t *testing.T) {
type tc struct {
in string
out []JumpHost
err error
}
testCases := []tc{
{
Expand Down Expand Up @@ -56,14 +58,11 @@ func (s *UtilsSuite) TestProxyJumpParsing(c *check.C) {
out: []JumpHost{{Username: "[email protected]", Addr: NetAddr{Addr: "[::1]:7777", AddrNetwork: "tcp"}}, {Username: "bob@localhost", Addr: NetAddr{Addr: "localhost", AddrNetwork: "tcp"}}},
},
}
for i, tc := range testCases {
comment := check.Commentf("Test case %v: %q", i, tc.in)
re, err := ParseProxyJump(tc.in)
if tc.err == nil {
c.Assert(err, check.IsNil, comment)
c.Assert(re, check.DeepEquals, tc.out)
} else {
c.Assert(err, check.FitsTypeOf, tc.err)
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%q", tc.in), func(t *testing.T) {
re, err := ParseProxyJump(tc.in)
require.NoError(t, err)
require.Equal(t, tc.out, re)
})
}
}
21 changes: 18 additions & 3 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,13 @@ func ReadHostUUID(dataDir string) (string, error) {
//do not convert to system error as this loses the ability to compare that it is a permission error
return "", err
}
return "", trace.Wrap(err)
return "", trace.ConvertSystemError(err)
}
id := strings.TrimSpace(string(out))
if id == "" {
return "", trace.NotFound("host uuid is empty")
}
return strings.TrimSpace(string(out)), nil
return id, nil
}

// WriteHostUUID writes host UUID into a file
Expand All @@ -475,7 +479,18 @@ func ReadOrMakeHostUUID(dataDir string) (string, error) {
if !trace.IsNotFound(err) {
return "", trace.Wrap(err)
}
id = uuid.New().String()
// Checking error instead of the usual uuid.New() in case uuid generation
// fails due to not enough randomness. It's been known to happen happen when
// Teleport starts very early in the node initialization cycle and /dev/urandom
// isn't ready yet.
rawID, err := uuid.NewRandom()
if err != nil {
return "", trace.BadParameter("" +
"Teleport failed to generate host UUID. " +
"This may happen if randomness source is not fully initialized when the node is starting up. " +
"Please try restarting Teleport again.")
}
id = rawID.String()
if err = WriteHostUUID(dataDir, id); err != nil {
return "", trace.Wrap(err)
}
Expand Down
Loading

0 comments on commit d89371c

Please sign in to comment.