| // |
| 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 | "time" |
| 20 |
| 21 | "github.com/onsi/ginkgo/v2" |
| 22 |
| 23 | "github.com/fatedier/frp/test/e2e/framework" |
| 24 | "github.com/fatedier/frp/test/e2e/framework/consts" |
| 25 | "github.com/fatedier/frp/test/e2e/mock/server/oidcserver" |
| 26 | "github.com/fatedier/frp/test/e2e/pkg/port" |
| 27 | ) |
| 28 |
| 29 | var _ = ginkgo.Describe("[Feature: OIDC]", func() { |
| 30 | f := framework.NewDefaultFramework() |
| 31 |
| 32 | ginkgo.It("should work with OIDC authentication", func() { |
| 33 | oidcSrv := oidcserver.New(oidcserver.WithBindPort(f.AllocPort())) |
| 34 | f.RunServer("", oidcSrv) |
| 35 |
| 36 | portName := port.GenName("TCP") |
| 37 |
| 38 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 39 | auth.method = "oidc" |
| 40 | auth.oidc.issuer = "%s" |
| 41 | auth.oidc.audience = "frps" |
| 42 | `, oidcSrv.Issuer()) |
| 43 |
| 44 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 45 | auth.method = "oidc" |
| 46 | auth.oidc.clientID = "test-client" |
| 47 | auth.oidc.clientSecret = "test-secret" |
| 48 | auth.oidc.tokenEndpointURL = "%s" |
| 49 |
| 50 | [[proxies]] |
| 51 | name = "tcp" |
| 52 | type = "tcp" |
| 53 | localPort = {{ .%s }} |
| 54 | remotePort = {{ .%s }} |
| 55 | `, oidcSrv.TokenEndpoint(), framework.TCPEchoServerPort, portName) |
| 56 |
| 57 | f.RunProcesses(serverConf, []string{clientConf}) |
| 58 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 59 | }) |
| 60 |
| 61 | ginkgo.It("should authenticate heartbeats with OIDC", func() { |
| 62 | oidcSrv := oidcserver.New(oidcserver.WithBindPort(f.AllocPort())) |
| 63 | f.RunServer("", oidcSrv) |
| 64 |
| 65 | serverPort := f.AllocPort() |
| 66 | remotePort := f.AllocPort() |
| 67 |
| 68 | serverConf := fmt.Sprintf(` |
| 69 | bindAddr = "0.0.0.0" |
| 70 | bindPort = %d |
| 71 | log.level = "trace" |
| 72 | auth.method = "oidc" |
| 73 | auth.additionalScopes = ["HeartBeats"] |
| 74 | auth.oidc.issuer = "%s" |
| 75 | auth.oidc.audience = "frps" |
| 76 | `, serverPort, oidcSrv.Issuer()) |
| 77 |
| 78 | clientConf := fmt.Sprintf(` |
| 79 | serverAddr = "127.0.0.1" |
| 80 | serverPort = %d |
| 81 | loginFailExit = false |
| 82 | log.level = "trace" |
| 83 | auth.method = "oidc" |
| 84 | auth.additionalScopes = ["HeartBeats"] |
| 85 | auth.oidc.clientID = "test-client" |
| 86 | auth.oidc.clientSecret = "test-secret" |
| 87 | auth.oidc.tokenEndpointURL = "%s" |
| 88 | transport.heartbeatInterval = 1 |
| 89 |
| 90 | [[proxies]] |
| 91 | name = "tcp" |
| 92 | type = "tcp" |
| 93 | localPort = %d |
| 94 | remotePort = %d |
| 95 | `, serverPort, oidcSrv.TokenEndpoint(), f.PortByName(framework.TCPEchoServerPort), remotePort) |
| 96 |
| 97 | serverConfigPath := f.GenerateConfigFile(serverConf) |
| 98 | clientConfigPath := f.GenerateConfigFile(clientConf) |
| 99 |
| 100 | _, _, err := f.RunFrps("-c", serverConfigPath) |
| 101 | framework.ExpectNoError(err) |
| 102 | clientProcess, _, err := f.RunFrpc("-c", clientConfigPath) |
| 103 | framework.ExpectNoError(err) |
| 104 |
| 105 | // Wait for several authenticated heartbeat cycles instead of a fixed sleep. |
| 106 | err = clientProcess.WaitForOutput("send heartbeat to server", 3, 10*time.Second) |
| 107 | framework.ExpectNoError(err) |
| 108 |
| 109 | // Proxy should still work: heartbeat auth has not failed. |
| 110 | framework.NewRequestExpect(f).Port(remotePort).Ensure() |
| 111 | }) |
| 112 |
| 113 | ginkgo.It("should work when token has no expires_in", func() { |
| 114 | oidcSrv := oidcserver.New( |
| 115 | oidcserver.WithBindPort(f.AllocPort()), |
| 116 | oidcserver.WithExpiresIn(0), |
| 117 | ) |
| 118 | f.RunServer("", oidcSrv) |
| 119 |
| 120 | portName := port.GenName("TCP") |
| 121 |
| 122 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 123 | auth.method = "oidc" |
| 124 | auth.oidc.issuer = "%s" |
| 125 | auth.oidc.audience = "frps" |
| 126 | `, oidcSrv.Issuer()) |
| 127 |
| 128 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 129 | auth.method = "oidc" |
| 130 | auth.additionalScopes = ["HeartBeats"] |
| 131 | auth.oidc.clientID = "test-client" |
| 132 | auth.oidc.clientSecret = "test-secret" |
| 133 | auth.oidc.tokenEndpointURL = "%s" |
| 134 | transport.heartbeatInterval = 1 |
| 135 |
| 136 | [[proxies]] |
| 137 | name = "tcp" |
| 138 | type = "tcp" |
| 139 | localPort = {{ .%s }} |
| 140 | remotePort = {{ .%s }} |
| 141 | `, oidcSrv.TokenEndpoint(), framework.TCPEchoServerPort, portName) |
| 142 |
| 143 | _, clientProcesses := f.RunProcesses(serverConf, []string{clientConf}) |
| 144 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 145 |
| 146 | countAfterLogin := oidcSrv.TokenRequestCount() |
| 147 |
| 148 | // Wait for several heartbeat cycles instead of a fixed sleep. |
| 149 | // Each heartbeat fetches a fresh token in non-caching mode. |
| 150 | err := clientProcesses[0].WaitForOutput("send heartbeat to server", 3, 10*time.Second) |
| 151 | framework.ExpectNoError(err) |
| 152 |
| 153 | framework.NewRequestExpect(f).PortName(portName).Ensure() |
| 154 |
| 155 | // Each heartbeat should have fetched a new token (non-caching mode). |
| 156 | countAfterHeartbeats := oidcSrv.TokenRequestCount() |
| 157 | framework.ExpectTrue( |
| 158 | countAfterHeartbeats > countAfterLogin, |
| 159 | "expected additional token requests for heartbeats, got %d before and %d after", |
| 160 | countAfterLogin, countAfterHeartbeats, |
| 161 | ) |
| 162 | }) |
| 163 |
| 164 | ginkgo.It("should reject invalid OIDC credentials", func() { |
| 165 | oidcSrv := oidcserver.New(oidcserver.WithBindPort(f.AllocPort())) |
| 166 | f.RunServer("", oidcSrv) |
| 167 |
| 168 | portName := port.GenName("TCP") |
| 169 |
| 170 | serverConf := consts.DefaultServerConfig + fmt.Sprintf(` |
| 171 | auth.method = "oidc" |
| 172 | auth.oidc.issuer = "%s" |
| 173 | auth.oidc.audience = "frps" |
| 174 | `, oidcSrv.Issuer()) |
| 175 |
| 176 | clientConf := consts.DefaultClientConfig + fmt.Sprintf(` |
| 177 | auth.method = "oidc" |
| 178 | auth.oidc.clientID = "test-client" |
| 179 | auth.oidc.clientSecret = "wrong-secret" |
| 180 | auth.oidc.tokenEndpointURL = "%s" |
| 181 |
| 182 | [[proxies]] |
| 183 | name = "tcp" |
| 184 | type = "tcp" |
| 185 | localPort = {{ .%s }} |
| 186 | remotePort = {{ .%s }} |
| 187 | `, oidcSrv.TokenEndpoint(), framework.TCPEchoServerPort, portName) |
| 188 |
| 189 | f.RunProcesses(serverConf, []string{clientConf}) |
| 190 | framework.NewRequestExpect(f).PortName(portName).ExpectError(true).Ensure() |
| 191 | }) |
| 192 | }) |