Skip to content

Commit

Permalink
[v15] Machine ID: Render kubernetes template without exec plugin when…
Browse files Browse the repository at this point in the history
… using non-directory destination (#43656)

* Render kubernetes template without exec plugin when using non-directory destination

* Switch to info level for warning

* Update lib/tbot/config/template_kubernetes.go

Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>

* Address review feedback

---------

Co-authored-by: Edoardo Spadolini <edoardo.spadolini@goteleport.com>
  • Loading branch information
strideynet and espadolini authored Jun 28, 2024
1 parent 2146bfd commit bdec5b2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
9 changes: 8 additions & 1 deletion lib/tbot/bot/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package bot

import "context"
import (
"context"
"fmt"
)

// Destination can persist renewable certificates.
type Destination interface {
Expand Down Expand Up @@ -53,4 +56,8 @@ type Destination interface {
// MarshalYAML enables the yaml package to correctly marshal the Destination
// as YAML including the type header.
MarshalYAML() (interface{}, error)

// Stringer so that Destination's implements fmt.Stringer which allows for
// better logging.
fmt.Stringer
}
31 changes: 17 additions & 14 deletions lib/tbot/config/template_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/gravitational/teleport/lib/kube/kubeconfig"
"github.com/gravitational/teleport/lib/tbot/bot"
"github.com/gravitational/teleport/lib/tbot/identity"
logutils "github.com/gravitational/teleport/lib/utils/log"
)

const defaultKubeconfigPath = "kubeconfig.yaml"
Expand Down Expand Up @@ -201,6 +202,22 @@ func (t *templateKubernetes) render(
kubernetesClusterName: t.clusterName,
}

// In exec plugin mode, we write the credentials to disk and write a
// kubeconfig that execs `tbot` to load those credentials.

// We only support directory mode for this since the exec plugin needs
// to know the path to read the credentials from, and this is
// unpredictable with other types of destination.
destinationDir, isDirectoryDest := destination.(*DestinationDirectory)
if !t.disableExecPlugin {
if !isDirectoryDest {
log.InfoContext(
ctx,
"Kubernetes template will be rendered without exec plugin because destination is not a directory. Explicitly set `disable_exec_plugin: true` in the output to suppress this message",
"destination", logutils.StringerAttr(destination))
t.disableExecPlugin = true
}
}
var cfg *clientcmdapi.Config
if t.disableExecPlugin {
// If they've disabled the exec plugin, we just write the credentials
Expand All @@ -210,20 +227,6 @@ func (t *templateKubernetes) render(
return trace.Wrap(err)
}
} else {
// In exec plugin mode, we write the credentials to disk and write a
// kubeconfig that execs `tbot` to load those credentials.

// We only support directory mode for this since the exec plugin needs
// to know the path to read the credentials from, and this is
// unpredictable with other types of destination.
destinationDir, ok := destination.(*DestinationDirectory)
if !ok {
return trace.BadParameter(
"Destination %s must be a directory in exec plugin mode",
destination,
)
}

executablePath, err := t.executablePathGetter()
if err != nil {
return trace.Wrap(err)
Expand Down

0 comments on commit bdec5b2

Please sign in to comment.