Skip to content
Draft
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
22 changes: 20 additions & 2 deletions server/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ linters:
linters:
- exhaustruct
- gosec
# The guardian package is the only package that is allowed to use
# http.Client directly since it is used to construct the safe HTTP clients
# that are used throughout the codebase. This is tracked by GG002.
- path: internal/guardian/policy\.go
linters:
- forbidigo
text: GG002
# The gateway package contains an HTTP reverse proxy that needs finer
# grained control over the HTTP client it uses, so we allow direct use of
# http.Client there as well. It is manually decorating the client with
# SSRF protection from the guardian module and OTel instrumentation.
- path: internal/gateway/proxy\.go
linters:
- forbidigo
text: GG002
settings:
gosec:
excludes:
Expand All @@ -82,10 +97,13 @@ linters:
forbid:
- pattern: ^os\.Getenv$
pkg: ^os$
msg: "Direct environment variable access is forbidden because it makes testing harder. Access to environment variables is only allowed in the cmd/."
msg: "GG001: Direct environment variable access is forbidden because it makes testing harder. Access to environment variables is only allowed in the cmd/."
- pattern: ^http\.Client$
pkg: ^net/http$
msg: "GG002: Use `(*github.com/speakeasy-api/gram/server/internal/guardian.Policy).Client(...)` (or .PooledClient(...)) and the HTTPClient from the same package."
- pattern: ^otel\.Tracer$
pkg: ^go\.opentelemetry\.io/otel$
msg: "GG003: pass a trace.TracerProvider to functions instead of using otel.Tracer directly."
msg: "GG003: Pass a trace.TracerProvider to functions instead of using otel.Tracer directly."
sloglint:
no-mixed-args: true
attr-only: true
Expand Down
31 changes: 28 additions & 3 deletions server/cmd/gram/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/speakeasy-api/gram/server/internal/externalmcp"
"github.com/speakeasy-api/gram/server/internal/feature"
"github.com/speakeasy-api/gram/server/internal/functions"
"github.com/speakeasy-api/gram/server/internal/guardian"
"github.com/speakeasy-api/gram/server/internal/inv"
"github.com/speakeasy-api/gram/server/internal/must"
"github.com/speakeasy-api/gram/server/internal/o11y"
Expand All @@ -70,6 +71,27 @@ func loadConfigFromFile(c *cli.Context, flags []cli.Flag) error {
return cfgLoader(c)
}

func newGuardianPolicy(c *cli.Context, tracerProvider trace.TracerProvider) (policy *guardian.Policy, err error) {
// In local development, allow loopback addresses for internal tool-to-tool communication
if c.String("environment") == "local" {
policy, err = guardian.NewUnsafePolicy(tracerProvider, []string{}) // Allow all traffic for local development
if err != nil {
return nil, fmt.Errorf("failed to create unsafe http guardian policy: %w", err)
}
} else {
policy = guardian.NewDefaultPolicy(tracerProvider)
}
if s := c.StringSlice("disallowed-cidr-blocks"); s != nil {
var err error
policy, err = guardian.NewUnsafePolicy(tracerProvider, s)
if err != nil {
return nil, fmt.Errorf("failed to create unsafe http guardian policy: %w", err)
}
}

return policy, nil
}

func newClickhouseClient(ctx context.Context, logger *slog.Logger, c *cli.Context) (clickhouse.Conn, func(context.Context) error, error) {
logger = logger.With(attr.SlogComponent("clickhouse"))
nilFunc := func(context.Context) error { return nil }
Expand Down Expand Up @@ -377,6 +399,7 @@ func newBillingProvider(
ctx context.Context,
logger *slog.Logger,
tracerProvider trace.TracerProvider,
guardianPolicy *guardian.Policy,
redisClient *redis.Client,
posthogClient *posthog.Posthog,
c *cli.Context,
Expand All @@ -395,7 +418,7 @@ func newBillingProvider(
}
polarAPIKey := c.String("polar-api-key")
polarsdk := polargo.New(polargo.WithSecurity(polarAPIKey), polargo.WithTimeout(30*time.Second)) // Shouldn't take this long, but just in case
pclient := polar.NewClient(polarsdk, polarAPIKey, logger, tracerProvider, redisClient, catalog, c.String("polar-webhook-secret"))
pclient := polar.NewClient(guardianPolicy, polarsdk, polarAPIKey, logger, tracerProvider, redisClient, catalog, c.String("polar-webhook-secret"))
tracker := tracking.New(pclient, posthogClient, logger)
return pclient, tracker, nil
case c.String("environment") == "local":
Expand Down Expand Up @@ -484,6 +507,7 @@ func newFunctionOrchestrator(
c *cli.Context,
logger *slog.Logger,
tracerProvider trace.TracerProvider,
guardianPolicy *guardian.Policy,
db *pgxpool.Pool,
assetStore assets.BlobStore,
tigrisStore *assets.TigrisStore,
Expand Down Expand Up @@ -544,6 +568,7 @@ func newFunctionOrchestrator(
return functions.NewFlyRunner(
logger,
tracerProvider,
guardianPolicy,
serverURL,
db,
assetStore,
Expand Down Expand Up @@ -571,15 +596,15 @@ type mcpRegistryClientOptions struct {
cacheImpl cache.Cache
}

func newMCPRegistryClient(logger *slog.Logger, tracerProvider trace.TracerProvider, opts mcpRegistryClientOptions) (*externalmcp.RegistryClient, error) {
func newMCPRegistryClient(logger *slog.Logger, tracerProvider trace.TracerProvider, guardianProxy *guardian.Policy, opts mcpRegistryClientOptions) (*externalmcp.RegistryClient, error) {
pulseURL, err := url.Parse("https://api.pulsemcp.com")
if err != nil {
return nil, fmt.Errorf("parse pulse registry url: %w", err)
}

backend := externalmcp.NewPulseBackend(pulseURL, opts.pulseTenantID, opts.pulseAPIKey)

return externalmcp.NewRegistryClient(logger, tracerProvider, backend, opts.cacheImpl), nil
return externalmcp.NewRegistryClient(logger, tracerProvider, guardianProxy, backend, opts.cacheImpl), nil
}

func newFeatureChecker(logger *slog.Logger, pf *productfeatures.Client, feat productfeatures.Feature) telemetry.FeatureChecker {
Expand Down
38 changes: 14 additions & 24 deletions server/cmd/gram/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (
"github.com/speakeasy-api/gram/server/internal/externalmcp"
"github.com/speakeasy-api/gram/server/internal/feature"
"github.com/speakeasy-api/gram/server/internal/functions"
"github.com/speakeasy-api/gram/server/internal/guardian"
"github.com/speakeasy-api/gram/server/internal/hooks"
"github.com/speakeasy-api/gram/server/internal/instances"
"github.com/speakeasy-api/gram/server/internal/integrations"
Expand Down Expand Up @@ -401,6 +400,11 @@ func newStartCommand() *cli.Command {
}
shutdownFuncs = append(shutdownFuncs, shutdown)

guardianPolicy, err := newGuardianPolicy(c, tracerProvider)
if err != nil {
return err
}

db, err := newDBClient(ctx, logger, meterProvider, c.String("database-url"), dbClientOptions{
enableUnsafeLogging: c.Bool("unsafe-db-log"),
})
Expand Down Expand Up @@ -455,14 +459,15 @@ func newStartCommand() *cli.Command {

workosClient := workos.New(logger, c.String("workos-api-key"))

billingRepo, billingTracker, err := newBillingProvider(ctx, logger, tracerProvider, redisClient, posthogClient, c)
billingRepo, billingTracker, err := newBillingProvider(ctx, logger, tracerProvider, guardianPolicy, redisClient, posthogClient, c)
if err != nil {
return fmt.Errorf("failed to create billing provider: %w", err)
}

sessionManager := sessions.NewManager(
logger,
tracerProvider,
guardianPolicy,
db,
redisClient,
cache.SuffixNone,
Expand Down Expand Up @@ -514,6 +519,8 @@ func newStartCommand() *cli.Command {
} else {
openRouter = openrouter.New(
logger,
tracerProvider,
guardianPolicy,
db,
c.String("environment"),
c.String("openrouter-provisioning-key"),
Expand Down Expand Up @@ -558,38 +565,20 @@ func newStartCommand() *cli.Command {
return fmt.Errorf("failed to parse site url: %w", err)
}

// In local development, allow loopback addresses for internal tool-to-tool communication
var guardianPolicy *guardian.Policy
if c.String("environment") == "local" {
guardianPolicy, err = guardian.NewUnsafePolicy([]string{}) // Allow all traffic for local development
if err != nil {
return fmt.Errorf("failed to create unsafe http guardian policy: %w", err)
}
} else {
guardianPolicy = guardian.NewDefaultPolicy()
}
blockedCIDRs := c.StringSlice("disallowed-cidr-blocks")
if blockedCIDRs != nil {
guardianPolicy, err = guardian.NewUnsafePolicy(blockedCIDRs)
if err != nil {
return fmt.Errorf("failed to create unsafe http guardian policy: %w", err)
}
}

tigrisStore, shutdown, err := newTigrisStore(ctx, c, logger)
if err != nil {
return fmt.Errorf("failed to create tigris asset store: %w", err)
}
shutdownFuncs = append(shutdownFuncs, shutdown)

functionsOrchestrator, shutdown, err := newFunctionOrchestrator(c, logger, tracerProvider, db, assetStorage, tigrisStore, encryptionClient)
functionsOrchestrator, shutdown, err := newFunctionOrchestrator(c, logger, tracerProvider, guardianPolicy, db, assetStorage, tigrisStore, encryptionClient)
if err != nil {
return fmt.Errorf("failed to create functions orchestrator: %w", err)
}
shutdownFuncs = append(shutdownFuncs, shutdown)
runnerVersion := functions.RunnerVersion(conv.Default(strings.TrimPrefix(c.String("functions-runner-version"), "sha-"), GitSHA))

slackClient := slack_client.NewSlackClient("", "", db, encryptionClient)
slackClient := slack_client.NewSlackClient(guardianPolicy, "", "", db, encryptionClient)

logsEnabled := newFeatureChecker(logger, productFeatures, productfeatures.FeatureLogs)
toolIOLogsEnabled := newFeatureChecker(logger, productFeatures, productfeatures.FeatureToolIOLogs)
Expand All @@ -598,6 +587,7 @@ func newStartCommand() *cli.Command {

completionsClient := openrouter.NewUnifiedClient(
logger,
guardianPolicy,
openRouter,
chat.NewChatMessageCaptureStrategy(logger, db, assetStorage),
chat.NewDefaultUsageTrackingStrategy(db, logger, openRouter, billingTracker, &background.FallbackModelUsageTracker{TemporalEnv: temporalEnv}),
Expand All @@ -607,7 +597,7 @@ func newStartCommand() *cli.Command {
)

ragService := rag.NewToolsetVectorStore(logger, tracerProvider, db, completionsClient)
mcpRegistryClient, err := newMCPRegistryClient(logger, tracerProvider, mcpRegistryClientOptions{
mcpRegistryClient, err := newMCPRegistryClient(logger, tracerProvider, guardianPolicy, mcpRegistryClientOptions{
pulseTenantID: c.String("pulse-registry-tenant"),
pulseAPIKey: conv.NewSecret([]byte(c.String("pulse-registry-api-key"))),
cacheImpl: cache.NewRedisCacheAdapter(redisClient),
Expand Down Expand Up @@ -703,7 +693,7 @@ func newStartCommand() *cli.Command {
toolsets.Attach(mux, toolsetsSvc)
integrations.Attach(mux, integrations.NewService(logger, tracerProvider, db, sessionManager))
templates.Attach(mux, templates.NewService(logger, tracerProvider, db, sessionManager, toolsetsSvc))
assets.Attach(mux, assets.NewService(logger, tracerProvider, db, sessionManager, chatSessionsManager, assetStorage, c.String("jwt-signing-key")))
assets.Attach(mux, assets.NewService(logger, tracerProvider, guardianPolicy, db, sessionManager, chatSessionsManager, assetStorage, c.String("jwt-signing-key")))
deployments.Attach(mux, deployments.NewService(logger, tracerProvider, db, temporalEnv, sessionManager, assetStorage, posthogClient, siteURL, mcpRegistryClient))
keys.Attach(mux, keys.NewService(logger, tracerProvider, db, sessionManager, c.String("environment")))
chatsessionssvc.Attach(mux, chatsessionssvc.NewService(logger, tracerProvider, db, sessionManager, chatSessionsManager))
Expand Down
37 changes: 12 additions & 25 deletions server/cmd/gram/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/speakeasy-api/gram/server/internal/environments"
"github.com/speakeasy-api/gram/server/internal/feature"
"github.com/speakeasy-api/gram/server/internal/functions"
"github.com/speakeasy-api/gram/server/internal/guardian"
"github.com/speakeasy-api/gram/server/internal/k8s"
"github.com/speakeasy-api/gram/server/internal/mcp"
mcpmetadata_repo "github.com/speakeasy-api/gram/server/internal/mcpmetadata/repo"
Expand Down Expand Up @@ -327,6 +326,11 @@ func newWorkerCommand() *cli.Command {
}
shutdownFuncs = append(shutdownFuncs, shutdown)

guardianPolicy, err := newGuardianPolicy(c, tracerProvider)
if err != nil {
return err
}

db, err := newDBClient(ctx, logger, meterProvider, c.String("database-url"), dbClientOptions{
enableUnsafeLogging: c.Bool("unsafe-db-log"),
})
Expand Down Expand Up @@ -398,7 +402,7 @@ func newWorkerCommand() *cli.Command {

productFeatures := productfeatures.NewClient(logger, tracerProvider, db, redisClient)

billingRepo, billingTracker, err := newBillingProvider(ctx, logger, tracerProvider, redisClient, posthogClient, c)
billingRepo, billingTracker, err := newBillingProvider(ctx, logger, tracerProvider, guardianPolicy, redisClient, posthogClient, c)
if err != nil {
return fmt.Errorf("failed to create billing provider: %w", err)
}
Expand All @@ -407,25 +411,7 @@ func newWorkerCommand() *cli.Command {
if c.String("environment") == "local" {
openRouter = openrouter.NewDevelopment(c.String("openrouter-dev-key"))
} else {
openRouter = openrouter.New(logger, db, c.String("environment"), c.String("openrouter-provisioning-key"), &background.OpenRouterKeyRefresher{TemporalEnv: temporalEnv}, productFeatures, billingTracker)
}

// In local development, allow loopback addresses for internal tool-to-tool communication
var guardianPolicy *guardian.Policy
if c.String("environment") == "local" {
guardianPolicy, err = guardian.NewUnsafePolicy([]string{}) // Allow all traffic for local development
if err != nil {
return fmt.Errorf("failed to create unsafe http guardian policy: %w", err)
}
} else {
guardianPolicy = guardian.NewDefaultPolicy()
}
if s := c.StringSlice("disallowed-cidr-blocks"); s != nil {
var err error
guardianPolicy, err = guardian.NewUnsafePolicy(s)
if err != nil {
return fmt.Errorf("failed to create unsafe http guardian policy: %w", err)
}
openRouter = openrouter.New(logger, tracerProvider, guardianPolicy, db, c.String("environment"), c.String("openrouter-provisioning-key"), &background.OpenRouterKeyRefresher{TemporalEnv: temporalEnv}, productFeatures, billingTracker)
}

tigrisStore, shutdown, err := newTigrisStore(ctx, c, logger)
Expand All @@ -434,15 +420,15 @@ func newWorkerCommand() *cli.Command {
}
shutdownFuncs = append(shutdownFuncs, shutdown)

functionsOrchestrator, shutdown, err := newFunctionOrchestrator(c, logger, tracerProvider, db, assetStorage, tigrisStore, encryptionClient)
functionsOrchestrator, shutdown, err := newFunctionOrchestrator(c, logger, tracerProvider, guardianPolicy, db, assetStorage, tigrisStore, encryptionClient)
if err != nil {
return fmt.Errorf("failed to create functions orchestrator: %w", err)
}
shutdownFuncs = append(shutdownFuncs, shutdown)

runnerVersion := functions.RunnerVersion(conv.Default(strings.TrimPrefix(c.String("functions-runner-version"), "sha-"), GitSHA))

slackClient := slack_client.NewSlackClient("", "", db, encryptionClient)
slackClient := slack_client.NewSlackClient(guardianPolicy, "", "", db, encryptionClient)

logsEnabled := newFeatureChecker(logger, productFeatures, productfeatures.FeatureLogs)
toolIOLogsEnabled := newFeatureChecker(logger, productFeatures, productfeatures.FeatureToolIOLogs)
Expand All @@ -462,6 +448,7 @@ func newWorkerCommand() *cli.Command {

completionsClient := openrouter.NewUnifiedClient(
logger,
guardianPolicy,
openRouter,
chat.NewChatMessageCaptureStrategy(logger, db, assetStorage),
chat.NewDefaultUsageTrackingStrategy(db, logger, openRouter, billingTracker, &background.FallbackModelUsageTracker{TemporalEnv: temporalEnv}),
Expand All @@ -471,7 +458,7 @@ func newWorkerCommand() *cli.Command {
)

ragService := rag.NewToolsetVectorStore(logger, tracerProvider, db, completionsClient)
mcpRegistryClient, err := newMCPRegistryClient(logger, tracerProvider, mcpRegistryClientOptions{
mcpRegistryClient, err := newMCPRegistryClient(logger, tracerProvider, guardianPolicy, mcpRegistryClientOptions{
pulseTenantID: c.String("pulse-registry-tenant"),
pulseAPIKey: conv.NewSecret([]byte(c.String("pulse-registry-api-key"))),
cacheImpl: cache.NewRedisCacheAdapter(redisClient),
Expand All @@ -490,7 +477,7 @@ func newWorkerCommand() *cli.Command {
return fmt.Errorf("failed to create pylon client: %w", err)
}

sessionManager := sessions.NewManager(logger, tracerProvider, db, redisClient, cache.SuffixNone, c.String("speakeasy-server-address"), c.String("speakeasy-secret-key"), pylonClient, posthogClient, billingRepo, nil)
sessionManager := sessions.NewManager(logger, tracerProvider, guardianPolicy, db, redisClient, cache.SuffixNone, c.String("speakeasy-server-address"), c.String("speakeasy-secret-key"), pylonClient, posthogClient, billingRepo, nil)

chatSessionsManager := chatsessions.NewManager(logger, redisClient, c.String("jwt-signing-key"))

Expand Down
5 changes: 4 additions & 1 deletion server/internal/access/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/speakeasy-api/gram/server/internal/cache"
"github.com/speakeasy-api/gram/server/internal/contextvalues"
"github.com/speakeasy-api/gram/server/internal/conv"
"github.com/speakeasy-api/gram/server/internal/guardian"
orgrepo "github.com/speakeasy-api/gram/server/internal/organizations/repo"
"github.com/speakeasy-api/gram/server/internal/testenv"
"github.com/speakeasy-api/gram/server/internal/urn"
Expand Down Expand Up @@ -55,6 +56,8 @@ func newTestAccessService(t *testing.T) (context.Context, *testInstance) {

logger := testenv.NewLogger(t)
tracerProvider := testenv.NewTracerProvider(t)
guardianPolicy, err := guardian.NewUnsafePolicy(tracerProvider, []string{})
require.NoError(t, err)

conn, err := infra.CloneTestDatabase(t, "testdb")
require.NoError(t, err)
Expand All @@ -64,7 +67,7 @@ func newTestAccessService(t *testing.T) (context.Context, *testInstance) {

billingClient := billing.NewStubClient(logger, tracerProvider)

sessionManager := testenv.NewTestManager(t, logger, conn, redisClient, cache.Suffix("gram-local"), billingClient)
sessionManager := testenv.NewTestManager(t, logger, tracerProvider, guardianPolicy, conn, redisClient, cache.Suffix("gram-local"), billingClient)

ctx = testenv.InitAuthContext(t, ctx, conn, sessionManager)
authCtx, ok := contextvalues.GetAuthContext(ctx)
Expand Down
Loading
Loading