| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package basic |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "strconv" |
| 23 | "time" |
| 24 |
| 25 | "github.com/onsi/ginkgo/v2" |
| 26 |
| 27 | "github.com/fatedier/frp/test/e2e/framework" |
| 28 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 29 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 30 | ) |
| 31 |
| 32 | var _ = ginkgo.Describe("[Feature: TokenSource]", func() { |
| 33 | f := framework.NewDefaultFramework() |
| 34 |
| 35 | createExecTokenScript := func(name string) string { |
| 36 | scriptPath := filepath.Join(f.TempDirectory, name) |
| 37 | scriptContent := `#!/bin/sh |
| 38 | printf '%s\n' "$1" |
| 39 | ` |
| 40 | err := os.WriteFile(scriptPath, []byte(scriptContent), 0o600) |
| 41 | framework.ExpectNoError(err) |
| 42 | err = os.Chmod(scriptPath, 0o700) |
| 43 | framework.ExpectNoError(err) |
| 44 | return scriptPath |
| 45 | } |
| 46 |
| 47 | ginkgo.Describe("File-based token loading", func() { |
| 48 | ginkgo.It("should work with file tokenSource", func() { |
| 49 | // Create a temporary token file |
| 50 | tmpDir := f.TempDirectory |
| 51 | tokenFile := filepath.Join(tmpDir, "test_token") |
| 52 | tokenContent := "test-token-123" |
| 53 |
| 54 | err := os.WriteFile(tokenFile, []byte(tokenContent), 0o600) |
| 55 | framework.ExpectNoError(err) |
| 56 |
| 57 | serverConf := consts.DefaultServerConfig |
| 58 | clientConf := consts.DefaultClientConfig |
| 59 |
| 60 | portName := port.GenName("TCP") |
| 61 |
| 62 | // Server config with tokenSource |
| 63 | serverConf += fmt.Sprintf(` |
| 64 | auth.tokenSource.type = "file" |
| 65 | auth.tokenSource.file.path = "%s" |
| 66 | `, tokenFile) |
| 67 |
| 68 | // Client config with matching token |
| 69 | clientConf += fmt.Sprintf(` |
| 70 | auth.token = "%s" |
| 71 |
| 72 | [[proxies]] |
| 73 | name = "tcp" |
| 74 | type = "tcp" |
| 75 | localPort = {{ .%s }} |
| 76 | remotePort = {{ .%s }} |
| 77 | `, tokenContent, framework.TCPEchoServerPort, portName) |
| 78 |
| 79 | f.RunProcesses(serverConf, []string{clientConf}) |
| 80 |
| 81 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 82 | }) |
| 83 |
| 84 | ginkgo.It("should work with client tokenSource", func() { |
| 85 | // Create a temporary token file |
| 86 | tmpDir := f.TempDirectory |
| 87 | tokenFile := filepath.Join(tmpDir, "client_token") |
| 88 | tokenContent := "client-token-456" |
| 89 |
| 90 | err := os.WriteFile(tokenFile, []byte(tokenContent), 0o600) |
| 91 | framework.ExpectNoError(err) |
| 92 |
| 93 | serverConf := consts.DefaultServerConfig |
| 94 | clientConf := consts.DefaultClientConfig |
| 95 |
| 96 | portName := port.GenName("TCP") |
| 97 |
| 98 | // Server config with matching token |
| 99 | serverConf += fmt.Sprintf(` |
| 100 | auth.token = "%s" |
| 101 | `, tokenContent) |
| 102 |
| 103 | // Client config with tokenSource |
| 104 | clientConf += fmt.Sprintf(` |
| 105 | auth.tokenSource.type = "file" |
| 106 | auth.tokenSource.file.path = "%s" |
| 107 |
| 108 | [[proxies]] |
| 109 | name = "tcp" |
| 110 | type = "tcp" |
| 111 | localPort = {{ .%s }} |
| 112 | remotePort = {{ .%s }} |
| 113 | `, tokenFile, framework.TCPEchoServerPort, portName) |
| 114 |
| 115 | f.RunProcesses(serverConf, []string{clientConf}) |
| 116 |
| 117 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 118 | }) |
| 119 |
| 120 | ginkgo.It("should work with both server and client tokenSource", func() { |
| 121 | // Create temporary token files |
| 122 | tmpDir := f.TempDirectory |
| 123 | serverTokenFile := filepath.Join(tmpDir, "server_token") |
| 124 | clientTokenFile := filepath.Join(tmpDir, "client_token") |
| 125 | tokenContent := "shared-token-789" |
| 126 |
| 127 | err := os.WriteFile(serverTokenFile, []byte(tokenContent), 0o600) |
| 128 | framework.ExpectNoError(err) |
| 129 |
| 130 | err = os.WriteFile(clientTokenFile, []byte(tokenContent), 0o600) |
| 131 | framework.ExpectNoError(err) |
| 132 |
| 133 | serverConf := consts.DefaultServerConfig |
| 134 | clientConf := consts.DefaultClientConfig |
| 135 |
| 136 | portName := port.GenName("TCP") |
| 137 |
| 138 | // Server config with tokenSource |
| 139 | serverConf += fmt.Sprintf(` |
| 140 | auth.tokenSource.type = "file" |
| 141 | auth.tokenSource.file.path = "%s" |
| 142 | `, serverTokenFile) |
| 143 |
| 144 | // Client config with tokenSource |
| 145 | clientConf += fmt.Sprintf(` |
| 146 | auth.tokenSource.type = "file" |
| 147 | auth.tokenSource.file.path = "%s" |
| 148 |
| 149 | [[proxies]] |
| 150 | name = "tcp" |
| 151 | type = "tcp" |
| 152 | localPort = {{ .%s }} |
| 153 | remotePort = {{ .%s }} |
| 154 | `, clientTokenFile, framework.TCPEchoServerPort, portName) |
| 155 |
| 156 | f.RunProcesses(serverConf, []string{clientConf}) |
| 157 |
| 158 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 159 | }) |
| 160 |
| 161 | ginkgo.It("should fail with mismatched tokens", func() { |
| 162 | // Create temporary token files with different content |
| 163 | tmpDir := f.TempDirectory |
| 164 | serverTokenFile := filepath.Join(tmpDir, "server_token") |
| 165 | clientTokenFile := filepath.Join(tmpDir, "client_token") |
| 166 |
| 167 | err := os.WriteFile(serverTokenFile, []byte("server-token"), 0o600) |
| 168 | framework.ExpectNoError(err) |
| 169 |
| 170 | err = os.WriteFile(clientTokenFile, []byte("client-token"), 0o600) |
| 171 | framework.ExpectNoError(err) |
| 172 |
| 173 | serverConf := consts.DefaultServerConfig |
| 174 | clientConf := consts.DefaultClientConfig |
| 175 |
| 176 | portName := port.GenName("TCP") |
| 177 |
| 178 | // Server config with tokenSource |
| 179 | serverConf += fmt.Sprintf(` |
| 180 | auth.tokenSource.type = "file" |
| 181 | auth.tokenSource.file.path = "%s" |
| 182 | `, serverTokenFile) |
| 183 |
| 184 | // Client config with different tokenSource |
| 185 | clientConf += fmt.Sprintf(` |
| 186 | auth.tokenSource.type = "file" |
| 187 | auth.tokenSource.file.path = "%s" |
| 188 |
| 189 | [[proxies]] |
| 190 | name = "tcp" |
| 191 | type = "tcp" |
| 192 | localPort = {{ .%s }} |
| 193 | remotePort = {{ .%s }} |
| 194 | `, clientTokenFile, framework.TCPEchoServerPort, portName) |
| 195 |
| 196 | f.RunProcesses(serverConf, []string{clientConf}) |
| 197 |
| 198 | // This should fail due to token mismatch - the client should not be able to connect |
| 199 | // We expect the request to fail because the proxy tunnel is not established |
| 200 | framework.NewRequestExpect(f).PortName(portName).ExpectError(true).Ensure() |
| 201 | }) |
| 202 |
| 203 | ginkgo.It("should fail with non-existent token file", func() { |
| 204 | tmpDir := f.TempDirectory |
| 205 | nonExistentFile := filepath.Join(tmpDir, "non_existent_token") |
| 206 |
| 207 | serverPort := f.AllocPort() |
| 208 | serverConf := fmt.Sprintf(` |
| 209 | bindAddr = "0.0.0.0" |
| 210 | bindPort = %d |
| 211 | auth.tokenSource.type = "file" |
| 212 | auth.tokenSource.file.path = "%s" |
| 213 | `, serverPort, nonExistentFile) |
| 214 |
| 215 | serverConfigPath := f.GenerateConfigFile(serverConf) |
| 216 |
| 217 | _, _, _ = f.RunFrps("-c", serverConfigPath) |
| 218 |
| 219 | // Server should have failed to start, so the port should not be listening. |
| 220 | conn, err := net.DialTimeout("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(serverPort)), 1*time.Second) |
| 221 | if err == nil { |
| 222 | conn.Close() |
| 223 | } |
| 224 | framework.ExpectTrue(err != nil, "server should not be listening on port %d", serverPort) |
| 225 | }) |
| 226 | }) |
| 227 |
| 228 | ginkgo.Describe("Exec-based token loading", func() { |
| 229 | ginkgo.It("should work with server tokenSource", func() { |
| 230 | execValue := "exec-server-value" |
| 231 | scriptPath := createExecTokenScript("server_token_exec.sh") |
| 232 |
| 233 | serverPort := f.AllocPort() |
| 234 | remotePort := f.AllocPort() |
| 235 |
| 236 | serverConf := fmt.Sprintf(` |
| 237 | bindAddr = "0.0.0.0" |
| 238 | bindPort = %d |
| 239 |
| 240 | auth.tokenSource.type = "exec" |
| 241 | auth.tokenSource.exec.command = %q |
| 242 | auth.tokenSource.exec.args = [%q] |
| 243 | `, serverPort, scriptPath, execValue) |
| 244 |
| 245 | clientConf := fmt.Sprintf(` |
| 246 | serverAddr = "127.0.0.1" |
| 247 | serverPort = %d |
| 248 | loginFailExit = false |
| 249 | auth.token = %q |
| 250 |
| 251 | [[proxies]] |
| 252 | name = "tcp" |
| 253 | type = "tcp" |
| 254 | localPort = %d |
| 255 | remotePort = %d |
| 256 | `, serverPort, execValue, f.PortByName(framework.TCPEchoServerPort), remotePort) |
| 257 |
| 258 | serverConfigPath := f.GenerateConfigFile(serverConf) |
| 259 | clientConfigPath := f.GenerateConfigFile(clientConf) |
| 260 |
| 261 | _, _, err := f.RunFrps("-c", serverConfigPath, "--allow-unsafe=TokenSourceExec") |
| 262 | framework.ExpectNoError(err) |
| 263 |
| 264 | _, _, err = f.RunFrpc("-c", clientConfigPath, "--allow-unsafe=TokenSourceExec") |
| 265 | framework.ExpectNoError(err) |
| 266 |
| 267 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 268 | }) |
| 269 |
| 270 | ginkgo.It("should work with client tokenSource", func() { |
| 271 | execValue := "exec-client-value" |
| 272 | scriptPath := createExecTokenScript("client_token_exec.sh") |
| 273 |
| 274 | serverPort := f.AllocPort() |
| 275 | remotePort := f.AllocPort() |
| 276 |
| 277 | serverConf := fmt.Sprintf(` |
| 278 | bindAddr = "0.0.0.0" |
| 279 | bindPort = %d |
| 280 |
| 281 | auth.token = %q |
| 282 | `, serverPort, execValue) |
| 283 |
| 284 | clientConf := fmt.Sprintf(` |
| 285 | serverAddr = "127.0.0.1" |
| 286 | serverPort = %d |
| 287 | loginFailExit = false |
| 288 |
| 289 | auth.tokenSource.type = "exec" |
| 290 | auth.tokenSource.exec.command = %q |
| 291 | auth.tokenSource.exec.args = [%q] |
| 292 |
| 293 | [[proxies]] |
| 294 | name = "tcp" |
| 295 | type = "tcp" |
| 296 | localPort = %d |
| 297 | remotePort = %d |
| 298 | `, serverPort, scriptPath, execValue, f.PortByName(framework.TCPEchoServerPort), remotePort) |
| 299 |
| 300 | serverConfigPath := f.GenerateConfigFile(serverConf) |
| 301 | clientConfigPath := f.GenerateConfigFile(clientConf) |
| 302 |
| 303 | _, _, err := f.RunFrps("-c", serverConfigPath, "--allow-unsafe=TokenSourceExec") |
| 304 | framework.ExpectNoError(err) |
| 305 |
| 306 | _, _, err = f.RunFrpc("-c", clientConfigPath, "--allow-unsafe=TokenSourceExec") |
| 307 | framework.ExpectNoError(err) |
| 308 |
| 309 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 310 | }) |
| 311 |
| 312 | ginkgo.It("should work with both server and client tokenSource", func() { |
| 313 | execValue := "exec-shared-value" |
| 314 | scriptPath := createExecTokenScript("shared_token_exec.sh") |
| 315 |
| 316 | serverPort := f.AllocPort() |
| 317 | remotePort := f.AllocPort() |
| 318 |
| 319 | serverConf := fmt.Sprintf(` |
| 320 | bindAddr = "0.0.0.0" |
| 321 | bindPort = %d |
| 322 |
| 323 | auth.tokenSource.type = "exec" |
| 324 | auth.tokenSource.exec.command = %q |
| 325 | auth.tokenSource.exec.args = [%q] |
| 326 | `, serverPort, scriptPath, execValue) |
| 327 |
| 328 | clientConf := fmt.Sprintf(` |
| 329 | serverAddr = "127.0.0.1" |
| 330 | serverPort = %d |
| 331 | loginFailExit = false |
| 332 |
| 333 | auth.tokenSource.type = "exec" |
| 334 | auth.tokenSource.exec.command = %q |
| 335 | auth.tokenSource.exec.args = [%q] |
| 336 |
| 337 | [[proxies]] |
| 338 | name = "tcp" |
| 339 | type = "tcp" |
| 340 | localPort = %d |
| 341 | remotePort = %d |
| 342 | `, serverPort, scriptPath, execValue, f.PortByName(framework.TCPEchoServerPort), remotePort) |
| 343 |
| 344 | serverConfigPath := f.GenerateConfigFile(serverConf) |
| 345 | clientConfigPath := f.GenerateConfigFile(clientConf) |
| 346 |
| 347 | _, _, err := f.RunFrps("-c", serverConfigPath, "--allow-unsafe=TokenSourceExec") |
| 348 | framework.ExpectNoError(err) |
| 349 |
| 350 | _, _, err = f.RunFrpc("-c", clientConfigPath, "--allow-unsafe=TokenSourceExec") |
| 351 | framework.ExpectNoError(err) |
| 352 |
| 353 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 354 | }) |
| 355 |
| 356 | ginkgo.It("should fail validation without allow-unsafe", func() { |
| 357 | execValue := "exec-unsafe-value" |
| 358 | scriptPath := createExecTokenScript("unsafe_token_exec.sh") |
| 359 |
| 360 | serverPort := f.AllocPort() |
| 361 | serverConf := fmt.Sprintf(` |
| 362 | bindAddr = "0.0.0.0" |
| 363 | bindPort = %d |
| 364 |
| 365 | auth.tokenSource.type = "exec" |
| 366 | auth.tokenSource.exec.command = %q |
| 367 | auth.tokenSource.exec.args = [%q] |
| 368 | `, serverPort, scriptPath, execValue) |
| 369 |
| 370 | serverConfigPath := f.GenerateConfigFile(serverConf) |
| 371 |
| 372 | _, output, err := f.RunFrps("verify", "-c", serverConfigPath) |
| 373 | framework.ExpectNoError(err) |
| 374 | framework.ExpectContainSubstring(output, "unsafe feature \"TokenSourceExec\" is not enabled") |
| 375 | }) |
| 376 | }) |
| 377 | }) |