teleport/tool/tbot/init_test.go

272 lines
7.5 KiB
Go
Raw Normal View History

Implement `tbot init` subcommand and ACL management (#10289) * Add certificate renewal bot This adds a new `tbot` tool to continuously renew a set of certificates after registering with a Teleport cluster using a similar process to standard node joining. This makes some modifications to user certificate generation to allow for certificates that can be renewed beyond their original TTL, and exposes new gRPC endpoints: * `CreateBotJoinToken` creates a join token for a bot user * `GenerateInitialRenewableUserCerts` exchanges a token for a set of certificates with a new `renewable` flag set A new `tctl` command, `tctl bots add`, creates a bot user and calls `CreateBotJoinToken` to issue a token. A bot instance can then be started using a provided command. * Cert bot refactoring pass * Use role requests to split renewable certs from end-user certs * Add bot configuration file * Use `teleport.dev/bot` label * Remove `impersonator` flag on initial bot certs * Remove unnecessary `renew` package * Misc other cleanup * Do not pass through `renewable` flag when role requests are set This adds additional restrictions on when a certificate's `renewable` flag is carried over to a new certificate. In particular, it now also denies the flag when either role requests are present, or the `disallowReissue` flag has been previously set. In practice `disallow-reissue` would have prevented any undesired behavior but this improves consistency and resolves a TODO. * Various tbot UX improvements; render SSH config * Fully flesh out config template rendering * Fix rendering for SSH configuration templates * Added `String()` impls for destination types * Improve certificate renewal logging; show more detail * Properly fall back to default (all) roles * Add mode hints for files * Add/update copyright headers * Add stubs for tbot init and watch commands * Add gRPC endpoints for managing bots * Add `CreateBot`, `DeleteBot`, and `GetBotUsers` gRPC endpoints * Replace `tctl bot (add|rm|ls)` implementations with gRPC calls * Define a few new constants, `DefaultBotJoinTTL`, `BotLabel`, `BotGenerationLabel` * Fix outdated destination flag in example tbot command * Bugfix pass for demo * Fixed a few nil pointer derefs when using config from CLI args * Properly create destination if `--destination-dir` flag is used * Remove improper default on CLI flag * `DestinationConfig` is now a list of pointers * Address first wave of review feedback Fixes the majority of smaller issues caught by reviewers, thanks all! * Add doc comments for bot.go functions * Return the token TTL from CreateBot * Split initial user cert issuance from `generateUserCerts()` Issuing initial renewable certificate ended up requiring a lot of hacks to skip checks that prevented anonymous bots from getting certs even though we'd verified their identity elsewhere (via token). This reverts all those hacks and splits initial bot cert logic into a dedicated `generateInitialRenewableUserCerts()` function which should make the whole process much easier to follow. * Set bot traits to silence log messages * tbot log message consistency pass * Implement `tbot init` subcommand This adds a new CLI subcommand to initialize a tbot destination directory by creating required files ahead of time and assigning proper permissions (and ACLs, where possible). * Resolve lints * Add config tests * Remove CreateBotJoinToken endpoint Users should instead use the CreateBot/DeleteBot endpoints. * Create a fresh private key for every impersonated identity renewal * Hide `config` subcommand * Rename bot label prefix to `teleport.internal/` * Use types.NewRole() to create bot roles * Clean up error handling in custom YAML unmarshallers Also, add notes about the supported YAML shapes. * Fetch proxy host via gRPC Ping() instead of GetProxies() * Update lib/auth/bot.go Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> * Fix some review comments * Add renewable certificate generation checks (#10098) * Add renewable certificate generation checks This adds a new validation check for renewable certificates that maintains a renewal counter as both a certificate extension and a user label. This counter is used to ensure only a single certificate lineage can exist: for example, if a renewable certificate is stolen, only one copy of the certificate can be renewed as the generation counter will not match When renewing a certificate, first the generation counter presented by the user (via their TLS identity) is compared to a value stored with the associated user (in a new `teleport.dev/bot-generation` label field). If they aren't equal, the renewal attempt fails. Otherwise, the generation counter is incremented by 1, stored to the database using a `CompareAndSwap()` to ensure atomicity, and set on the generated certificate for use in future renewals. * Add unit tests for the generation counter This adds new unit tests to exercise the generation counter checks. Additionally, it fixes two other renewable cert tests that were failing. * Remove certRequestGeneration() function * Emit audit event when cert generations don't match * Fully implement `tctl bots lock` * Show bot name in `tctl bots ls` * Lock bots when a cert generation mismatch is found * Make CompareFailed respones from validateGenerationLabel() more actionable * Update lib/services/local/users.go Co-authored-by: Nic Klaassen <nic@goteleport.com> * Backend changes for tbot IoT and AWS joining (#10360) * backend changes * add token permission check * pass ctx from caller Co-authored-by: Roman Tkachenko <roman@goteleport.com> * fix comment typo Co-authored-by: Roman Tkachenko <roman@goteleport.com> * use UserMetadata instead of Identity in RenewableCertificateGenerationMismatch event * Client changes for tbot IoT joining (#10397) * client changes * delete replaced APIs * delete unused tbot/auth.go * add license header * don't unecessarily fetch host CA * log fixes * s/tunnelling/tunneling/ Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> * auth server addresses may be proxies Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> * comment typo fix Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> * move *Server methods out of auth_with_roles.go (#10416) Co-authored-by: Tim Buckley <tim@goteleport.com> Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> Co-authored-by: Tim Buckley <tim@goteleport.com> Co-authored-by: Roman Tkachenko <roman@goteleport.com> Co-authored-by: Tim Buckley <tim@goteleport.com> Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> Co-authored-by: Nic Klaassen <nic@goteleport.com> Co-authored-by: Roman Tkachenko <roman@goteleport.com> Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> * Address another batch of review feedback * Addres another batch of review feedback Add `Role.SetMetadata()`, simplify more `trace.WrapWithMessage()` calls, clear some TODOs and lints, and address other misc feedback items. * Fix lint * Add missing doc comments to SaveIdentity / LoadIdentity * Remove pam tag from tbot build * Update note about bot lock deletion * Another pass of review feedback Ensure all requestable roles exist when creating a bot, adjust the default renewable cert TTL down to 1 hour, and check types during `CompareAndSwapUser()` * Remove ModeHint * Rename Identity.Cert and Identity.XCert * Add `symlinks` flag to tbot config The optional symlinks flag for directory destinations allows users to opt in / out of whichever symlink attack hardening mode is selected by default. * Add mostly-working secure implementation of botfs.Create/Write This adds symlink mode selection (secure, try-secure, insecure) and Linux `Create()`/`Write()` implementations to open files safely. * Add configurable ACL modes and verify ACL support in tbot init * Initialize destinations at startup and test before renewal This initializes destinations at startup (to create directories if not using `tbot init`) and tests them to ensure the bot can write _before_ attempting to renew certificates; this should prevent most accidental generation counter locks. * Hide watch for now * Issue a new identity if a token change is detected * Warn if identity appears to be expired on startup * Fully implement ACL Verify and Configure - Fully implements ACL support for Linux - Adds bot-side verification support to ensure ACLs are configured properly at runtime. - Gracefully falls back to no ACLs if the platform / filesystem doesn't support them - Clear up outstanding lints * Make `tbot init` work without a config file * Show init instructions in tctl bots add Also: - Make --bot-user a flag in init (the tctl instructions were confusing otherwise) - Handle IsOwnedBy sanely on unsupported platforms - Add Bold colorizing support * Clear some TODOs and rephrase tctl help * Fix typo * Fix token hash detection bug * Actually read and write certs with symlink enforcement Also, fix a config loading bug where CheckAndSetDefaults() wasn't being called in all cases with CLI destinations. * Add workaround for OpenSSH permissions check with ACLs OpenSSH has an overly-paranoid permissions check that forces key files to be exclusively owner-readable. Unfortunately, for POSIX compatibility purposes, when ACLs are set, the ACL mask is set as the group permissions. This effectively makes any ACL incompatible with OpenSSH. However, OpenSSH's check does have an escape hatch: it only applies if the current user is the owner of the file. Therefore, this change tweaks the `tbot init` flow to create files as root, owned by a separate user (either `nobody` or even the bot user), with ACL permissions granting both the bot and reader user access to the certificates. This effectively bypasses OpenSSH's permissions check and should preserve our security boundaries. * Fix lints * Fix an improper directory chmod to 0600 if ACL test fails * First pass of tbot init unit tests * Add symlink tests and fix bug with resolving the default owner * Fix err misuse * Fix an ACL error if the bot or reader user is the owner. * Fix typo * Fix missing error case in VerifyACL causing unreadable directories * Address review feedback - Rename ACLOn -> ACLRequired - Simplify fs_linux.Read() - Add missing fs_other.Read() - Hoist renewal loop logic into its own function - A few misc bugfixes * Apply suggestions from code review Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com> * Address review feedback - Only log syscall warning once - Formatting and wording changes - Improve error handling for `--clean` * Fix lint error * Fix imports in fs_other * Fix possible nil pointer deref if storage is unset * Use the bot user as default owner This is more likely to be a safe owner choice than `nobody:nobody`. * Apply suggestions from code review Co-authored-by: Roman Tkachenko <roman@goteleport.com> * Code review fixes Co-authored-by: Zac Bergquist <zmb3@users.noreply.github.com> Co-authored-by: Nic Klaassen <nic@goteleport.com> Co-authored-by: Roman Tkachenko <roman@goteleport.com> Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
2022-03-10 06:09:01 +00:00
/*
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 main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/tool/tbot/botfs"
"github.com/gravitational/teleport/tool/tbot/config"
"github.com/gravitational/teleport/tool/tbot/identity"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
)
// usernamesToTry contains a list of usernames we can use as ACL targets in
// testing.
var usernamesToTry = []string{"nobody", "ci", "root"}
func contains(entries []string, entry string) bool {
for _, e := range entries {
if e == entry {
return true
}
}
return false
}
// filterUsers returns the input list of usernames except for those in the
// exclude list.
func filterUsers(usernames, exclude []string) []string {
ret := []string{}
for _, username := range usernames {
if !contains(exclude, username) {
ret = append(ret, username)
}
}
return ret
}
// findUser attempts to find a usable user on the local system from the given
// list of usernames and returns the first match found.
func findUser(usernamesToTry, usernamesToExclude []string) (*user.User, error) {
filtered := filterUsers(usernamesToTry, usernamesToExclude)
for _, username := range filtered {
u, err := user.Lookup(username)
if err == nil {
return u, nil
}
}
return nil, trace.NotFound("No users found matching %+v (excluding %+v)", usernamesToTry, usernamesToExclude)
}
// getACLOptions returns sane ACLOptions for this platform.
func getACLOptions() (*botfs.ACLOptions, error) {
if runtime.GOOS != constants.LinuxOS {
return nil, trace.NotImplemented("Unsupported platform")
}
user, err := user.Current()
if err != nil {
return nil, trace.Wrap(err)
}
exclude := []string{user.Name}
// Find a set of users we can test against.
readerUser, err := findUser(usernamesToTry, exclude)
if trace.IsNotFound(err) {
return nil, trace.NotFound("Not enough usable users for testing ACLs")
} else if err != nil {
return nil, trace.Wrap(err)
}
exclude = append(exclude, readerUser.Name)
botUser, err := findUser(usernamesToTry, exclude)
if trace.IsNotFound(err) {
return nil, trace.NotFound("Not enough suitable users found for testing ACLs.")
} else if err != nil {
return nil, trace.Wrap(err)
}
return &botfs.ACLOptions{
ReaderUser: readerUser,
BotUser: botUser,
}, nil
}
// testConfigFromCLI creates a BotConfig from the given CLI config.
func testConfigFromCLI(t *testing.T, cf *config.CLIConf) *config.BotConfig {
cfg, err := config.FromCLIConf(cf)
require.NoError(t, err)
return cfg
}
// testConfigFromString parses a YAML config file from a string.
func testConfigFromString(t *testing.T, yaml string) *config.BotConfig {
cfg, err := config.ReadConfig(strings.NewReader(yaml))
require.NoError(t, err)
return cfg
}
// validateFileDestinations ensures all files in a destination exist on disk as
// expected, and returns the destination.
func validateFileDestination(t *testing.T, dest *config.DestinationConfig) *config.DestinationDirectory {
destImpl, err := dest.GetDestination()
require.NoError(t, err)
destDir, ok := destImpl.(*config.DestinationDirectory)
require.True(t, ok)
for _, art := range identity.GetArtifacts() {
if !art.Matches(dest.Kinds...) {
continue
}
require.FileExists(t, filepath.Join(destDir.Path, art.Key))
}
return destDir
}
// TestInit ensures defaults work regardless of host platform. With no bot user
// specified, this never tries to use ACLs.
func TestInit(t *testing.T) {
dir := t.TempDir()
cf := &config.CLIConf{
AuthServer: "example.com",
DestinationDir: dir,
}
cfg := testConfigFromCLI(t, cf)
// Run init.
require.NoError(t, onInit(cfg, cf))
// Make sure everything was created.
_ = validateFileDestination(t, cfg.Destinations[0])
}
// TestInitMaybeACLs tests defaults with ACLs possibly enabled, by supplying
// bot and reader users.
func TestInitMaybeACLs(t *testing.T) {
opts, err := getACLOptions()
if trace.IsNotImplemented(err) {
t.Skipf("%+v", err)
} else if trace.IsNotFound(err) {
t.Skipf("%+v", err)
}
require.NoError(t, err)
hasACLSupport, err := botfs.HasACLSupport()
require.NoError(t, err)
currentUser, err := user.Current()
require.NoError(t, err)
currentGroup, err := user.LookupGroupId(currentUser.Gid)
require.NoError(t, err)
// Determine if we expect init to use ACLs.
expectACLs := false
if hasACLSupport {
if err := testACL(t.TempDir(), currentUser, opts); err == nil {
expectACLs = true
}
}
// Note: we'll use the current user as owner as that's the only way to
// guarantee ACL write access.
dir := t.TempDir()
cf := &config.CLIConf{
AuthServer: "example.com",
DestinationDir: dir,
BotUser: opts.BotUser.Username,
ReaderUser: opts.ReaderUser.Username,
// This isn't a default, but unfortunately we need to specify a
// non-nobody owner for CI purposes.
Owner: fmt.Sprintf("%s:%s", currentUser.Name, currentGroup.Name),
}
cfg := testConfigFromCLI(t, cf)
// Run init.
require.NoError(t, onInit(cfg, cf))
// Make sure everything was created.
destDir := validateFileDestination(t, cfg.Destinations[0])
// If we expect ACLs, verify them.
if expectACLs {
require.NoError(t, destDir.Verify(identity.ListKeys(cfg.Destinations[0].Kinds...)))
} else {
t.Logf("Skipping ACL check on %q as they should not be supported.", dir)
}
}
// testInitSymlinksTemplate is a config template with a configurable symlinks
// mode and ACLs disabled.
const testInitSymlinksTemplate = `
auth_server: example.com
destinations:
- directory:
path: %s
acls: off
symlinks: %s
`
// TestInitSymlink tests tbot init with a symlink in the path.
func TestInitSymlink(t *testing.T) {
secureWriteSupported, err := botfs.HasSecureWriteSupport()
require.NoError(t, err)
if !secureWriteSupported {
t.Skip("Secure write not supported on this system.")
}
dir := t.TempDir()
realPath := filepath.Join(dir, "data")
dataDir := filepath.Join(dir, "data-symlink")
require.NoError(t, os.Symlink(realPath, dataDir))
// Should fail due to symlink in path.
cfg := testConfigFromString(t, fmt.Sprintf(testInitSymlinksTemplate, dataDir, botfs.SymlinksSecure))
require.Error(t, onInit(cfg, &config.CLIConf{}))
// Should succeed when writing to the dir directly.
cfg = testConfigFromString(t, fmt.Sprintf(testInitSymlinksTemplate, realPath, botfs.SymlinksSecure))
require.NoError(t, onInit(cfg, &config.CLIConf{}))
// Make sure everything was created.
_ = validateFileDestination(t, cfg.Destinations[0])
}
// TestInitSymlinksInsecure should work on all platforms.
func TestInitSymlinkInsecure(t *testing.T) {
dir := t.TempDir()
realPath := filepath.Join(dir, "data")
dataDir := filepath.Join(dir, "data-symlink")
require.NoError(t, os.Symlink(realPath, dataDir))
// Should succeed due to SymlinksInsecure
cfg := testConfigFromString(t, fmt.Sprintf(testInitSymlinksTemplate, dataDir, botfs.SymlinksInsecure))
require.Error(t, onInit(cfg, &config.CLIConf{}))
}