| // |
| 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 validation |
| 16 |
| 17 | import ( |
| 18 | "testing" |
| 19 |
| 20 | "github.com/stretchr/testify/require" |
| 21 |
| 22 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 23 | "github.com/fatedier/frp/pkg/policy/security" |
| 24 | ) |
| 25 |
| 26 | const ( |
| 27 | tokenSourceConflictErr = "cannot specify both auth.token and auth.tokenSource" |
| 28 | tokenSourceExecErr = "unsafe feature \"TokenSourceExec\" is not enabled. To enable it, ensure it is allowed in the configuration or command line flags" |
| 29 | invalidFileSourceErr = "invalid auth.tokenSource: file configuration is required when type is 'file'" |
| 30 | unsupportedSourceErr = "invalid auth.tokenSource: unsupported value source type: env (only 'file' and 'exec' are supported)" |
| 31 | ) |
| 32 |
| 33 | func TestValidateAuthTokenSource(t *testing.T) { |
| 34 | for _, tc := range authTokenSourceTestCases() { |
| 35 | t.Run(tc.name, func(t *testing.T) { |
| 36 | validator := newAuthTokenSourceValidator(tc.unsafeAllowed) |
| 37 | err := validator.validateAuthTokenSource(tc.token, tc.tokenSource()) |
| 38 | requireValidationErrors(t, err, tc.wantErrs) |
| 39 | }) |
| 40 | } |
| 41 | } |
| 42 |
| 43 | func TestValidateClientAuthTokenSource(t *testing.T) { |
| 44 | for _, tc := range authTokenSourceTestCases() { |
| 45 | t.Run(tc.name, func(t *testing.T) { |
| 46 | auth := v1.AuthClientConfig{ |
| 47 | Method: v1.AuthMethodToken, |
| 48 | Token: tc.token, |
| 49 | TokenSource: tc.tokenSource(), |
| 50 | } |
| 51 | validator := newAuthTokenSourceValidator(tc.unsafeAllowed) |
| 52 | _, err := validator.ValidateClientCommonConfig(validClientConfigWithAuth(auth)) |
| 53 | requireValidationErrors(t, err, tc.wantErrs) |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 |
| 58 | func TestValidateServerAuthTokenSource(t *testing.T) { |
| 59 | for _, tc := range authTokenSourceTestCases() { |
| 60 | t.Run(tc.name, func(t *testing.T) { |
| 61 | auth := v1.AuthServerConfig{ |
| 62 | Method: v1.AuthMethodToken, |
| 63 | Token: tc.token, |
| 64 | TokenSource: tc.tokenSource(), |
| 65 | } |
| 66 | validator := newAuthTokenSourceValidator(tc.unsafeAllowed) |
| 67 | _, err := validator.ValidateServerConfig(validServerConfigWithAuth(auth)) |
| 68 | requireValidationErrors(t, err, tc.wantErrs) |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 |
| 73 | type authTokenSourceTestCase struct { |
| 74 | name string |
| 75 | token string |
| 76 | tokenSource func() *v1.ValueSource |
| 77 | unsafeAllowed bool |
| 78 | wantErrs []string |
| 79 | } |
| 80 |
| 81 | func authTokenSourceTestCases() []authTokenSourceTestCase { |
| 82 | return []authTokenSourceTestCase{ |
| 83 | { |
| 84 | name: "empty token config", |
| 85 | tokenSource: nilTokenSource, |
| 86 | }, |
| 87 | { |
| 88 | name: "valid file tokenSource", |
| 89 | tokenSource: validFileTokenSource, |
| 90 | }, |
| 91 | { |
| 92 | name: "literal token without tokenSource", |
| 93 | token: "token", |
| 94 | tokenSource: nilTokenSource, |
| 95 | }, |
| 96 | { |
| 97 | name: "literal token conflicts with file tokenSource", |
| 98 | token: "token", |
| 99 | tokenSource: validFileTokenSource, |
| 100 | wantErrs: []string{tokenSourceConflictErr}, |
| 101 | }, |
| 102 | { |
| 103 | name: "exec tokenSource requires unsafe feature", |
| 104 | tokenSource: validExecTokenSource, |
| 105 | wantErrs: []string{tokenSourceExecErr}, |
| 106 | }, |
| 107 | { |
| 108 | name: "exec tokenSource with unsafe feature allowed", |
| 109 | tokenSource: validExecTokenSource, |
| 110 | unsafeAllowed: true, |
| 111 | }, |
| 112 | { |
| 113 | name: "literal token conflicts with exec tokenSource and unsafe feature disabled", |
| 114 | token: "token", |
| 115 | tokenSource: validExecTokenSource, |
| 116 | wantErrs: []string{ |
| 117 | tokenSourceConflictErr, |
| 118 | tokenSourceExecErr, |
| 119 | }, |
| 120 | }, |
| 121 | { |
| 122 | name: "literal token conflicts with exec tokenSource and unsafe feature allowed", |
| 123 | token: "token", |
| 124 | tokenSource: validExecTokenSource, |
| 125 | unsafeAllowed: true, |
| 126 | wantErrs: []string{tokenSourceConflictErr}, |
| 127 | }, |
| 128 | { |
| 129 | name: "invalid file tokenSource is wrapped", |
| 130 | tokenSource: invalidFileTokenSource, |
| 131 | wantErrs: []string{invalidFileSourceErr}, |
| 132 | }, |
| 133 | { |
| 134 | name: "unsupported tokenSource type is wrapped", |
| 135 | tokenSource: unsupportedTokenSource, |
| 136 | wantErrs: []string{unsupportedSourceErr}, |
| 137 | }, |
| 138 | } |
| 139 | } |
| 140 |
| 141 | func newAuthTokenSourceValidator(unsafeAllowed bool) *ConfigValidator { |
| 142 | if !unsafeAllowed { |
| 143 | return NewConfigValidator(nil) |
| 144 | } |
| 145 | return NewConfigValidator(security.NewUnsafeFeatures([]string{security.TokenSourceExec})) |
| 146 | } |
| 147 |
| 148 | func requireValidationErrors(t *testing.T, err error, wantErrs []string) { |
| 149 | t.Helper() |
| 150 | if len(wantErrs) == 0 { |
| 151 | require.NoError(t, err) |
| 152 | return |
| 153 | } |
| 154 | require.Error(t, err) |
| 155 | // Client/server validators may wrap joined errors in another join layer; compare leaf errors. |
| 156 | gotErrs := unwrapValidationErrors(err) |
| 157 | require.Len(t, gotErrs, len(wantErrs)) |
| 158 | for i, wantErr := range wantErrs { |
| 159 | require.EqualError(t, gotErrs[i], wantErr) |
| 160 | } |
| 161 | } |
| 162 |
| 163 | func unwrapValidationErrors(err error) []error { |
| 164 | type joinedError interface { |
| 165 | Unwrap() []error |
| 166 | } |
| 167 | joined, ok := err.(joinedError) |
| 168 | if !ok { |
| 169 | return []error{err} |
| 170 | } |
| 171 |
| 172 | var errs []error |
| 173 | for _, err := range joined.Unwrap() { |
| 174 | errs = append(errs, unwrapValidationErrors(err)...) |
| 175 | } |
| 176 | return errs |
| 177 | } |
| 178 |
| 179 | // nilTokenSource keeps the shared table shape uniform for cases without a tokenSource. |
| 180 | func nilTokenSource() *v1.ValueSource { |
| 181 | return nil |
| 182 | } |
| 183 |
| 184 | func validFileTokenSource() *v1.ValueSource { |
| 185 | return &v1.ValueSource{ |
| 186 | Type: "file", |
| 187 | File: &v1.FileSource{Path: "token.txt"}, |
| 188 | } |
| 189 | } |
| 190 |
| 191 | func validExecTokenSource() *v1.ValueSource { |
| 192 | return &v1.ValueSource{ |
| 193 | Type: "exec", |
| 194 | Exec: &v1.ExecSource{Command: "print-token"}, |
| 195 | } |
| 196 | } |
| 197 |
| 198 | func invalidFileTokenSource() *v1.ValueSource { |
| 199 | return &v1.ValueSource{ |
| 200 | Type: "file", |
| 201 | } |
| 202 | } |
| 203 |
| 204 | func unsupportedTokenSource() *v1.ValueSource { |
| 205 | return &v1.ValueSource{Type: "env"} |
| 206 | } |
| 207 |
| 208 | func validClientConfigWithAuth(auth v1.AuthClientConfig) *v1.ClientCommonConfig { |
| 209 | return &v1.ClientCommonConfig{ |
| 210 | Auth: auth, |
| 211 | Log: v1.LogConfig{ |
| 212 | Level: "info", |
| 213 | }, |
| 214 | Transport: v1.ClientTransportConfig{ |
| 215 | Protocol: "tcp", |
| 216 | WireProtocol: "v1", |
| 217 | }, |
| 218 | } |
| 219 | } |
| 220 |
| 221 | func validServerConfigWithAuth(auth v1.AuthServerConfig) *v1.ServerConfig { |
| 222 | return &v1.ServerConfig{ |
| 223 | Auth: auth, |
| 224 | Log: v1.LogConfig{ |
| 225 | Level: "info", |
| 226 | }, |
| 227 | } |
| 228 | } |