Merge pull request #566 from safedep/chore/misc-cleanup-20250815

chore: Misc cleanup and test improvements
This commit is contained in:
Kunal Singh 2025-08-18 09:52:04 +05:30 committed by GitHub
commit d8b83e2bc2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 159 additions and 16 deletions

View File

@ -27,6 +27,9 @@ type McpClientToolBuilderConfig struct {
SQLQueryToolEnabled bool
SQLQueryToolDBPath string
PackageRegistryToolEnabled bool
// Enable debug mode for the MCP client.
Debug bool
}
type mcpClientToolBuilder struct {
@ -107,15 +110,18 @@ func (b *mcpClientToolBuilder) buildStdioClient() (*client.Client, error) {
return nil, fmt.Errorf("failed to get running binary path: %w", err)
}
// TODO: We should not log by default. This is only for debugging purposes.
vetMcpServerLogFile := filepath.Join(os.TempDir(), "vet-mcp-server.log")
// vet-mcp server defaults to stdio transport. See cmd/server/mcp.go
vetMcpServerCommandArgs := []string{"server", "mcp", "-l", vetMcpServerLogFile}
vetMcpServerCommandArgs := []string{"server", "mcp"}
if b.config.Debug {
vetMcpServerLogFile := filepath.Join(os.TempDir(), "vet-mcp-server.log")
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "-l", vetMcpServerLogFile)
}
if b.config.SQLQueryToolEnabled {
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--sql-query-tool")
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--sql-query-tool-db-path", b.config.SQLQueryToolDBPath)
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--sql-query-tool-db-path",
b.config.SQLQueryToolDBPath)
}
if b.config.PackageRegistryToolEnabled {
@ -126,9 +132,12 @@ func (b *mcpClientToolBuilder) buildStdioClient() (*client.Client, error) {
vetMcpServerCommandArgs = append(vetMcpServerCommandArgs, "--skip-default-tools")
}
cli, err := client.NewStdioMCPClient(binaryPath, []string{
"APP_LOG_LEVEL=debug",
}, vetMcpServerCommandArgs...)
environmentVariables := []string{}
if b.config.Debug {
environmentVariables = append(environmentVariables, "APP_LOG_LEVEL=debug")
}
cli, err := client.NewStdioMCPClient(binaryPath, environmentVariables, vetMcpServerCommandArgs...)
if err != nil {
return nil, fmt.Errorf("failed to create stdio client: %w", err)
}

View File

@ -22,6 +22,9 @@ const (
communityModeEnvKey = "VET_COMMUNITY_MODE"
controlTowerTenantEnvKey = "VET_CONTROL_TOWER_TENANT_ID"
defaultSafeDepApiKeyEnvKey = "SAFEDEP_API_KEY"
defaultSafeDepTenantIdEnvKey = "SAFEDEP_TENANT_ID"
defaultApiUrl = "https://api.safedep.io/insights/v1"
defaultCommunityApiUrl = "https://api.safedep.io/insights-community/v1"
@ -234,8 +237,11 @@ func CommunityServicesApiUrl() string {
}
func TenantDomain() string {
tenantFromEnv := os.Getenv(controlTowerTenantEnvKey)
if tenantFromEnv != "" {
if tenantFromEnv, ok := os.LookupEnv(controlTowerTenantEnvKey); ok && tenantFromEnv != "" {
return tenantFromEnv
}
if tenantFromEnv, ok := os.LookupEnv(defaultSafeDepTenantIdEnvKey); ok && tenantFromEnv != "" {
return tenantFromEnv
}
@ -271,6 +277,10 @@ func ApiKey() string {
return key
}
if key, ok := os.LookupEnv(defaultSafeDepApiKeyEnvKey); ok {
return key
}
if globalConfig != nil {
return globalConfig.ApiKey
}

View File

@ -1,12 +1,20 @@
package auth
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func withGlobalConfig(t *testing.T, fn func() *Config) {
oldConfig := globalConfig
t.Cleanup(func() {
globalConfig = oldConfig
})
globalConfig = fn()
}
func TestDefaultConfig(t *testing.T) {
config := DefaultConfig()
@ -21,7 +29,7 @@ func TestCommunityServicesApiUrl(t *testing.T) {
assert.Equal(t, defaultCommunityServicesApiUrl, CommunityServicesApiUrl())
t.Run("should return the env variable if set", func(t *testing.T) {
os.Setenv(communityServicesApiUrlEnvKey, "https://test.safedep.io")
t.Setenv(communityServicesApiUrlEnvKey, "https://test.safedep.io")
assert.Equal(t, "https://test.safedep.io", CommunityServicesApiUrl())
})
}
@ -30,7 +38,7 @@ func TestControlTowerUrl(t *testing.T) {
assert.Equal(t, defaultControlPlaneApiUrl, ControlTowerUrl())
t.Run("should return the env variable if set", func(t *testing.T) {
os.Setenv(controlPlaneUrlEnvKey, "https://test.safedep.io")
t.Setenv(controlPlaneUrlEnvKey, "https://test.safedep.io")
assert.Equal(t, "https://test.safedep.io", ControlTowerUrl())
})
}
@ -39,7 +47,7 @@ func TestDataPlaneUrl(t *testing.T) {
assert.Equal(t, defaultDataPlaneApiUrl, DataPlaneUrl())
t.Run("should return the env variable if set", func(t *testing.T) {
os.Setenv(dataPlaneUrlEnvKey, "https://test.safedep.io")
t.Setenv(dataPlaneUrlEnvKey, "https://test.safedep.io")
assert.Equal(t, "https://test.safedep.io", DataPlaneUrl())
})
}
@ -48,7 +56,7 @@ func TestSyncApiUrl(t *testing.T) {
assert.Equal(t, defaultSyncApiUrl, SyncApiUrl())
t.Run("should return the env variable if set", func(t *testing.T) {
os.Setenv(syncUrlEnvKey, "https://test.safedep.io")
t.Setenv(syncUrlEnvKey, "https://test.safedep.io")
assert.Equal(t, "https://test.safedep.io", SyncApiUrl())
})
}
@ -57,7 +65,118 @@ func TestInsightsApiV2Url(t *testing.T) {
assert.Equal(t, defaultInsightsApiV2Url, InsightsApiV2Url())
t.Run("should return the env variable if set", func(t *testing.T) {
os.Setenv(apiV2UrlEnvKey, "https://test.safedep.io")
t.Setenv(apiV2UrlEnvKey, "https://test.safedep.io")
assert.Equal(t, "https://test.safedep.io", InsightsApiV2Url())
})
}
func TestTenantDomain(t *testing.T) {
t.Run("should return the env variable if set", func(t *testing.T) {
t.Setenv("VET_CONTROL_TOWER_TENANT_ID", "test-tenant")
assert.Equal(t, "test-tenant", TenantDomain())
})
t.Run("should return env value when alternate env variable is set", func(t *testing.T) {
t.Setenv("SAFEDEP_TENANT_ID", "test-tenant")
assert.Equal(t, "test-tenant", TenantDomain())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
withGlobalConfig(t, func() *Config {
return &Config{TenantDomain: "test-tenant-from-config"}
})
assert.Equal(t, "test-tenant-from-config", TenantDomain())
})
}
func TestApiKey(t *testing.T) {
t.Run("should return the env variable if set", func(t *testing.T) {
t.Setenv("VET_API_KEY", "test-api-key")
assert.Equal(t, "test-api-key", ApiKey())
})
t.Run("should return the env value when alternate env variable is set", func(t *testing.T) {
t.Setenv("VET_INSIGHTS_API_KEY", "test-api-key-alt")
assert.Equal(t, "test-api-key-alt", ApiKey())
})
t.Run("should return other alternate env variable if set", func(t *testing.T) {
t.Setenv("SAFEDEP_API_KEY", "test-api-key-safe")
assert.Equal(t, "test-api-key-safe", ApiKey())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
withGlobalConfig(t, func() *Config {
return &Config{ApiKey: "test-api-key-from-config"}
})
assert.Equal(t, "test-api-key-from-config", ApiKey())
})
}
func TestApiUrl(t *testing.T) {
t.Run("should return the env variable if set", func(t *testing.T) {
t.Setenv("VET_INSIGHTS_API_URL", "https://test-api.safedep.io")
assert.Equal(t, "https://test-api.safedep.io", ApiUrl())
})
t.Run("should return community API URL when in community mode", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "true")
assert.Equal(t, defaultCommunityApiUrl, ApiUrl())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
withGlobalConfig(t, func() *Config {
return &Config{ApiUrl: "https://test-api-from-config.safedep.io"}
})
assert.Equal(t, "https://test-api-from-config.safedep.io", ApiUrl())
})
t.Run("should return default API URL when no config is set", func(t *testing.T) {
withGlobalConfig(t, func() *Config {
return nil
})
assert.Equal(t, defaultApiUrl, ApiUrl())
})
}
func TestCommunityMode(t *testing.T) {
t.Run("should return true when env variable is set to true", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "true")
assert.True(t, CommunityMode())
})
t.Run("should return true when env variable is set to 1", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "1")
assert.True(t, CommunityMode())
})
t.Run("should return false when env variable is set to false", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "false")
assert.False(t, CommunityMode())
})
t.Run("should return false when env variable is set to invalid value", func(t *testing.T) {
t.Setenv("VET_COMMUNITY_MODE", "invalid")
assert.False(t, CommunityMode())
})
t.Run("should fallback to default from global config", func(t *testing.T) {
withGlobalConfig(t, func() *Config {
return &Config{Community: true}
})
assert.True(t, CommunityMode())
})
t.Run("should return false when no config is set", func(t *testing.T) {
withGlobalConfig(t, func() *Config {
return nil
})
assert.False(t, CommunityMode())
})
}

View File

@ -22,6 +22,11 @@ func getLogLevelFromEnv() logrus.Level {
envLogLevel = strings.ToLower(os.Getenv("VET_LOG_LEVEL"))
}
// Fallback to safedep defaults
if envLogLevel == "" {
envLogLevel = strings.ToLower(os.Getenv("APP_LOG_LEVEL"))
}
switch envLogLevel {
case "debug":
return logrus.DebugLevel