-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling in
tbot start
(#11756)
* Improve error handling in `tbot start` This attempts to improve a number of error handling issues while loading the bot identity from storage in `tbot start`: 1. Identity loading errors are silently ignored and the bot always attempts to generate a new identity from token. This isn't always correct and is impossible to debug as the true error is never logged. We now debug log these errors. 2. `LoadIdentity()` doesn't properly account for existing-but-empty identity files and happily tries to load empty identities from `tbot init`. This isn't hugely harmful, but produces nonsensical error logs once #1 is fixed. * Use `O_RDWR` instead of `O_WRONLY` in `botfs.openStandard()` This behaves the same as the fs_linux secure implementation in all cases, and moves the open mode to a shared constant for good measure. * Add a small unit test for symlinks mode read/write. * Fail on non-NotFound errors while reading an Identity. * Add small unit test for empty identities. * Remove outdated TODO comment * Apply suggestions from code review Co-authored-by: Zac Bergquist <[email protected]> * Address review feedback Co-authored-by: Zac Bergquist <[email protected]>
- Loading branch information
1 parent
cabfcdb
commit c90d59a
Showing
7 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
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 botfs | ||
|
||
import ( | ||
"bytes" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestReadWrite attempts to test read/write against all possible symlink | ||
// modes. | ||
func TestReadWrite(t *testing.T) { | ||
dir := t.TempDir() | ||
|
||
secureWriteExpected, err := HasSecureWriteSupport() | ||
require.NoError(t, err) | ||
|
||
expectedData := []byte{1, 2, 3, 4} | ||
|
||
for _, mode := range []SymlinksMode{SymlinksInsecure, SymlinksTrySecure, SymlinksSecure} { | ||
if mode == SymlinksSecure && !secureWriteExpected { | ||
t.Logf("skipping secure read/write test due to lack of platform support") | ||
continue | ||
} | ||
|
||
path := filepath.Join(dir, string(mode)) | ||
|
||
err := Create(path, false, mode) | ||
require.NoError(t, err) | ||
|
||
err = Write(path, expectedData, mode) | ||
require.NoError(t, err) | ||
|
||
data, err := Read(path, mode) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, 0, bytes.Compare(data, expectedData), "read bytes must be equal to those written") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
// Note: this lives in main to avoid import cycles since this depends on the | ||
// config/identity/destinations packages. | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gravitational/teleport/tool/tbot/config" | ||
"github.com/gravitational/teleport/tool/tbot/identity" | ||
"github.com/gravitational/trace" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadEmptyIdentity(t *testing.T) { | ||
dir := t.TempDir() | ||
dest := config.DestinationDirectory{ | ||
Path: dir, | ||
} | ||
require.NoError(t, dest.CheckAndSetDefaults()) | ||
|
||
_, err := identity.LoadIdentity(&dest, identity.BotKinds()...) | ||
require.Error(t, err) | ||
|
||
require.True(t, trace.IsNotFound(err)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters