Lightweight server monitoring with historical data, docker stats, and alerts.
fix(agent): accept key file path in -k flag (#2764, #2765)
yi111b4195bddeb40
diff --git a/internal/cmd/agent/agent.go b/internal/cmd/agent/agent.goindex 2ba82b670..a4b1cb2f6 100644--- a/internal/cmd/agent/agent.go+++ b/internal/cmd/agent/agent.go@@ -24,6 +24,26 @@ func (noKeyProvidedError) Error() string {return "no key provided: must set -key flag, KEY env var, or KEY_FILE env var. Use 'beszel-agent help' for usage"}+// parseKeyOrFile parses inline SSH public key material, falling back to+// reading it as a file when inline parsing fails.+func parseKeyOrFile(value string) ([]ssh.PublicKey, error) {+ keys, parseErr := agent.ParseKeys(value)+ if parseErr == nil {+ return keys, nil+ }++ data, readErr := os.ReadFile(value)+ if readErr != nil {+ return nil, fmt.Errorf("invalid inline key or unreadable key file: %w", errors.Join(parseErr, readErr))+ }++ keys, err := agent.ParseKeys(string(data))+ if err != nil {+ return nil, fmt.Errorf("invalid key file %q: %w", value, err)+ }+ return keys, nil+}+// cli optionstype cmdOptions struct {key string // key is the public key(s) for SSH authentication.@@ -55,7 +75,7 @@ func (opts *cmdOptions) parse() bool {}// pflag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true- pflag.StringVarP(&opts.key, "key", "k", "", "Public key(s) for SSH authentication")+ pflag.StringVarP(&opts.key, "key", "k", "", "Public key(s) for SSH authentication: inline key material or path to a key file")pflag.StringVarP(&opts.listen, "listen", "l", "", "Address or port to listen on")pflag.StringVarP(&opts.hubURL, "url", "u", "", "URL of the Beszel hub")pflag.StringVarP(&opts.token, "token", "t", "", "Token to use for authentication")@@ -128,7 +148,7 @@ func (opts *cmdOptions) parse() bool {func (opts *cmdOptions) loadPublicKeys() ([]ssh.PublicKey, error) {// Try command line flag firstif opts.key != "" {- return agent.ParseKeys(opts.key)+ return parseKeyOrFile(opts.key)}// Try environment variablediff --git a/internal/cmd/agent/agent_test.go b/internal/cmd/agent/agent_test.goindex 438f6a1a0..b4b267578 100644--- a/internal/cmd/agent/agent_test.go+++ b/internal/cmd/agent/agent_test.go@@ -150,6 +150,16 @@ func TestLoadPublicKeys(t *testing.T) {},wantErr: true,},+ {+ name: "load key from file path passed to flag",+ opts: cmdOptions{+ // rewritten to the real temporary path below+ key: "testkey.pub",+ },+ setupFiles: map[string][]byte{+ "testkey.pub": pubKey,+ },+ },}for _, tt := range tests {@@ -164,6 +174,9 @@ func TestLoadPublicKeys(t *testing.T) {if tt.envVars != nil {tt.envVars["KEY_FILE"] = path}+ if tt.opts.key == name {+ tt.opts.key = path+ }}}@@ -188,6 +201,54 @@ func TestLoadPublicKeys(t *testing.T) {}}+func TestParseKeyOrFile(t *testing.T) {+ var publicKeys []ssh.PublicKey+ var keyMaterial []byte+ for range 2 {+ pub, _, err := ed25519.GenerateKey(nil)+ require.NoError(t, err)+ key, err := ssh.NewPublicKey(pub)+ require.NoError(t, err)+ publicKeys = append(publicKeys, key)+ keyMaterial = append(keyMaterial, ssh.MarshalAuthorizedKey(key)...)+ }++ t.Run("file path with spaces and multiple keys", func(t *testing.T) {+ path := filepath.Join(t.TempDir(), "hub keys.pub")+ require.NoError(t, os.WriteFile(path, keyMaterial, 0600))++ keys, err := parseKeyOrFile(path)+ require.NoError(t, err)+ require.Len(t, keys, len(publicKeys))+ for i, key := range keys {+ assert.Equal(t, publicKeys[i].Marshal(), key.Marshal())+ }+ })++ t.Run("invalid file contents", func(t *testing.T) {+ path := filepath.Join(t.TempDir(), "invalid.pub")+ require.NoError(t, os.WriteFile(path, []byte("invalid-key-data"), 0600))++ _, err := parseKeyOrFile(path)+ require.Error(t, err)+ assert.Contains(t, err.Error(), "invalid key file")+ assert.Contains(t, err.Error(), path)+ assert.Contains(t, err.Error(), "failed to parse key: invalid-key-data")+ })++ t.Run("missing file preserves read error", func(t *testing.T) {+ path := filepath.Join(t.TempDir(), "missing.pub")++ _, err := parseKeyOrFile(path)+ require.Error(t, err)+ assert.ErrorIs(t, err, os.ErrNotExist)+ assert.Contains(t, err.Error(), "failed to parse key")+ var pathErr *os.PathError+ require.ErrorAs(t, err, &pathErr)+ assert.Equal(t, path, pathErr.Path)+ })+}+func TestIsBenignStartupError(t *testing.T) {tests := []struct {name string