loadPublicKeys loads the public keys from the command line flag, environment variable, or key file.
()
| 111 | |
| 112 | // loadPublicKeys loads the public keys from the command line flag, environment variable, or key file. |
| 113 | func (opts *cmdOptions) loadPublicKeys() ([]ssh.PublicKey, error) { |
| 114 | // Try command line flag first |
| 115 | if opts.key != "" { |
| 116 | return agent.ParseKeys(opts.key) |
| 117 | } |
| 118 | |
| 119 | // Try environment variable |
| 120 | if key, ok := utils.GetEnv("KEY"); ok && key != "" { |
| 121 | return agent.ParseKeys(key) |
| 122 | } |
| 123 | |
| 124 | // Try key file |
| 125 | keyFile, ok := utils.GetEnv("KEY_FILE") |
| 126 | if !ok { |
| 127 | return nil, fmt.Errorf("no key provided: must set -key flag, KEY env var, or KEY_FILE env var. Use 'beszel-agent help' for usage") |
| 128 | } |
| 129 | |
| 130 | pubKey, err := os.ReadFile(keyFile) |
| 131 | if err != nil { |
| 132 | return nil, fmt.Errorf("failed to read key file: %w", err) |
| 133 | } |
| 134 | return agent.ParseKeys(string(pubKey)) |
| 135 | } |
| 136 | |
| 137 | func (opts *cmdOptions) getAddress() string { |
| 138 | return agent.GetAddress(opts.listen) |