From 8dfc8c189e38fee9835c1faaee4bb568f29bf2c2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Sep 2025 12:02:58 +0000 Subject: [PATCH 1/2] Initial plan From c4f2279d32b1a7109caa05d1c7fe4e3db0413b70 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Sep 2025 12:11:42 +0000 Subject: [PATCH 2/2] Fix CHISEL_KEY environment variable ignored when --keyfile not set Co-authored-by: jpillora <633843+jpillora@users.noreply.github.com> --- main.go | 3 +- test/e2e/env_key_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/e2e/env_key_test.go diff --git a/main.go b/main.go index 01f9ca3b..7af1f45e 100644 --- a/main.go +++ b/main.go @@ -237,7 +237,8 @@ func server(args []string) { } if config.KeyFile == "" { config.KeyFile = settings.Env("KEY_FILE") - } else if config.KeySeed == "" { + } + if config.KeySeed == "" { config.KeySeed = settings.Env("KEY") } if config.Auth == "" { diff --git a/test/e2e/env_key_test.go b/test/e2e/env_key_test.go new file mode 100644 index 00000000..c193f748 --- /dev/null +++ b/test/e2e/env_key_test.go @@ -0,0 +1,65 @@ +package e2e_test + +import ( + "os" + "testing" + + chclient "github.com/jpillora/chisel/client" + chserver "github.com/jpillora/chisel/server" +) + +func TestChiselKeyEnvironmentVariable(t *testing.T) { + // Set the CHISEL_KEY environment variable + os.Setenv("CHISEL_KEY", "test-key-value") + defer os.Unsetenv("CHISEL_KEY") + + tmpPort := availablePort() + + // Create server with empty config - should pick up CHISEL_KEY env var + serverConfig := &chserver.Config{} + + // Setup server and client + teardown := simpleSetup(t, + serverConfig, + &chclient.Config{ + Remotes: []string{tmpPort + ":$FILEPORT"}, + }) + defer teardown() + + // Test that the connection works - if the key is properly set, + // the server should start successfully and connections should work + result, err := post("http://localhost:"+tmpPort, "env-key-test") + if err != nil { + t.Fatal(err) + } + if result != "env-key-test!" { + t.Fatalf("expected exclamation mark added, got: %s", result) + } +} + +func TestChiselKeyEnvironmentVariableConsistency(t *testing.T) { + // This test verifies that the same CHISEL_KEY value produces + // consistent behavior (same fingerprint) by manually setting KeySeed + keyValue := "consistency-test-key" + + // Create two server instances with the same KeySeed (simulating what main.go does) + server1, err := chserver.NewServer(&chserver.Config{ + KeySeed: keyValue, + }) + if err != nil { + t.Fatalf("Failed to create first server: %v", err) + } + + server2, err := chserver.NewServer(&chserver.Config{ + KeySeed: keyValue, + }) + if err != nil { + t.Fatalf("Failed to create second server: %v", err) + } + + // Both servers should have the same fingerprint since they use the same key + if server1.GetFingerprint() != server2.GetFingerprint() { + t.Fatalf("Expected same fingerprint for same key, got %s and %s", + server1.GetFingerprint(), server2.GetFingerprint()) + } +} \ No newline at end of file