Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v16] [kube] add namespace/podName as SessionTracker hostname #44649

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/kube/proxy/sess.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"net/http"
"path"
"reflect"
"slices"
"strings"
Expand Down Expand Up @@ -1325,7 +1326,7 @@ func (s *session) trackSession(p *party, policySet []*types.SessionTrackerPolicy
SessionID: s.id.String(),
Kind: string(types.KubernetesSessionKind),
State: types.SessionState_SessionStatePending,
Hostname: s.podName,
Hostname: path.Join(s.podNamespace, s.podName),
ClusterName: s.ctx.teleportCluster.name,
KubernetesCluster: s.ctx.kubeClusterName,
HostUser: p.Ctx.User.GetName(),
Expand Down Expand Up @@ -1362,7 +1363,7 @@ func (s *session) trackSession(p *party, policySet []*types.SessionTrackerPolicy
case err != nil:
return trace.Wrap(err)
// the tracker was created successfully
case err == nil:
default:
s.tracker = tracker
}

Expand Down
22 changes: 19 additions & 3 deletions lib/kube/proxy/sess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,18 @@ func Test_session_trackSession(t *testing.T) {
assertErr: require.NoError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sess := &session{
log: logrus.New().WithField(teleport.ComponentKey, "test"),
id: uuid.New(),
req: &http.Request{
URL: &url.URL{},
URL: &url.URL{
RawQuery: "command=command&command=arg1&command=arg2",
},
},
podName: "podName",
podNamespace: "podNamespace",
accessEvaluator: auth.NewSessionAccessEvaluator(tt.args.policies, types.KubernetesSessionKind, "username"),
ctx: authContext{
Context: authz.Context{
Expand Down Expand Up @@ -319,16 +321,30 @@ func Test_session_trackSession(t *testing.T) {
}
err := sess.trackSession(p, tt.args.policies)
tt.assertErr(t, err)
if err != nil {
return
}
tracker := tt.args.authClient.(*mockSessionTrackerService).tracker
require.Equal(t, "username", tracker.GetHostUser())
require.Equal(t, "name", tracker.GetClusterName())
require.Equal(t, "kubeClusterName", tracker.GetKubeCluster())
require.Equal(t, sess.id.String(), tracker.GetSessionID())
require.Equal(t, []string{"command", "arg1", "arg2"}, tracker.GetCommand())
require.Equal(t, "podNamespace/podName", tracker.GetHostname())
require.Equal(t, types.KubernetesSessionKind, tracker.GetSessionKind())

})
}
}

type mockSessionTrackerService struct {
authclient.ClientI
returnErr bool
tracker types.SessionTracker
}

func (m *mockSessionTrackerService) CreateSessionTracker(ctx context.Context, tracker types.SessionTracker) (types.SessionTracker, error) {
func (m *mockSessionTrackerService) CreateSessionTracker(_ context.Context, tracker types.SessionTracker) (types.SessionTracker, error) {
m.tracker = tracker
if m.returnErr {
return nil, trace.ConnectionProblem(nil, "mock error")
}
Expand Down
Loading