2023-10-18 15:08:02 -05:00
|
|
|
package audit_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-04-22 11:52:52 +01:00
|
|
|
"github.com/google/uuid"
|
2023-10-18 15:08:02 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
"go.opentelemetry.io/otel/propagation"
|
|
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/coderd/audit"
|
2026-04-22 11:52:52 +01:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
2023-10-18 15:08:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestBaggage(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
prop := propagation.NewCompositeTextMapPropagator(
|
|
|
|
|
propagation.TraceContext{},
|
|
|
|
|
propagation.Baggage{},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expected := audit.WorkspaceBuildBaggage{
|
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, err := audit.BaggageToContext(context.Background(), expected)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
carrier := propagation.MapCarrier{}
|
|
|
|
|
prop.Inject(ctx, carrier)
|
|
|
|
|
bCtx := prop.Extract(ctx, carrier)
|
|
|
|
|
got := audit.BaggageFromContext(bCtx)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, expected, got)
|
|
|
|
|
}
|
2026-04-22 11:52:52 +01:00
|
|
|
|
|
|
|
|
func TestResourceTarget_ChatTitleNotLeaked(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
chat := database.Chat{
|
|
|
|
|
ID: uuid.UUID{1},
|
|
|
|
|
Title: "sensitive-project-name",
|
|
|
|
|
}
|
|
|
|
|
target := audit.ResourceTarget(chat)
|
|
|
|
|
require.NotContains(t, target, chat.Title,
|
|
|
|
|
"ResourceTarget for Chat must not contain the title; it should use a UUID prefix")
|
|
|
|
|
}
|
2026-08-19 21:11:52 +02:00
|
|
|
|
|
|
|
|
func TestResourceTarget_MCPServerConfigSlugFallback(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
config := database.MCPServerConfig{
|
|
|
|
|
ID: uuid.UUID{1},
|
|
|
|
|
DisplayName: "GitHub MCP",
|
|
|
|
|
Slug: "github",
|
|
|
|
|
}
|
|
|
|
|
require.Equal(t, "GitHub MCP", audit.ResourceTarget(config))
|
|
|
|
|
|
|
|
|
|
config.DisplayName = ""
|
|
|
|
|
require.Equal(t, "github", audit.ResourceTarget(config))
|
|
|
|
|
|
|
|
|
|
config.Slug = ""
|
|
|
|
|
require.Equal(t, config.ID.String(), audit.ResourceTarget(config))
|
|
|
|
|
}
|