|
| 1 | +package start |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + netUrl "net/url" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + types "github.com/loft-sh/api/v4/pkg/auth" |
| 14 | + "gotest.tools/v3/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestIsTLSError(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + err error |
| 21 | + expected bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "nil error", |
| 25 | + err: nil, |
| 26 | + expected: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "URL error with certificate verification error", |
| 30 | + err: &netUrl.Error{ |
| 31 | + Op: "Get", |
| 32 | + URL: "https://localhost:9898", |
| 33 | + Err: &tls.CertificateVerificationError{ |
| 34 | + UnverifiedCertificates: []*x509.Certificate{}, |
| 35 | + Err: x509.UnknownAuthorityError{}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + expected: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "URL error with unknown authority", |
| 42 | + err: &netUrl.Error{ |
| 43 | + Op: "Get", |
| 44 | + URL: "https://localhost:9898", |
| 45 | + Err: x509.UnknownAuthorityError{}, |
| 46 | + }, |
| 47 | + expected: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "URL error with non-TLS error", |
| 51 | + err: &netUrl.Error{ |
| 52 | + Op: "Get", |
| 53 | + URL: "https://localhost:9898", |
| 54 | + Err: &netUrl.Error{Op: "dial", URL: "localhost", Err: nil}, |
| 55 | + }, |
| 56 | + expected: false, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + if tt.err == nil { |
| 63 | + assert.Assert(t, !isTLSError(nil)) |
| 64 | + return |
| 65 | + } |
| 66 | + assert.Equal(t, isTLSError(tt.err), tt.expected) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestPasswordLogin_SecureSuccess(t *testing.T) { |
| 72 | + expectedKey := "test-access-key-123" |
| 73 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 | + if r.URL.Path == "/auth/password/login" { |
| 75 | + resp := types.AccessKey{AccessKey: expectedKey} |
| 76 | + w.Header().Set("Content-Type", "application/json") |
| 77 | + _ = json.NewEncoder(w).Encode(resp) |
| 78 | + return |
| 79 | + } |
| 80 | + w.WriteHeader(http.StatusNotFound) |
| 81 | + })) |
| 82 | + defer server.Close() |
| 83 | + |
| 84 | + // passwordLogin with insecure=true should succeed against self-signed cert. |
| 85 | + l := &LoftStarter{} |
| 86 | + key, insecure, err := l.passwordLogin(server.URL, []byte(`{}`), true) |
| 87 | + assert.NilError(t, err) |
| 88 | + assert.Equal(t, key, expectedKey) |
| 89 | + assert.Assert(t, insecure) |
| 90 | +} |
| 91 | + |
| 92 | +func TestPasswordLogin_InsecureFalseFailsAgainstSelfSigned(t *testing.T) { |
| 93 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 94 | + resp := types.AccessKey{AccessKey: "key"} |
| 95 | + _ = json.NewEncoder(w).Encode(resp) |
| 96 | + })) |
| 97 | + defer server.Close() |
| 98 | + |
| 99 | + // passwordLogin with insecure=false should fail against self-signed cert. |
| 100 | + l := &LoftStarter{} |
| 101 | + _, _, err := l.passwordLogin(server.URL, []byte(`{}`), false) |
| 102 | + assert.Assert(t, err != nil, "expected TLS error") |
| 103 | + assert.Assert(t, isTLSError(err), "error should be a TLS error, got: %v", err) |
| 104 | +} |
| 105 | + |
| 106 | +func TestPasswordLogin_FallbackBehavior(t *testing.T) { |
| 107 | + expectedKey := "fallback-key" |
| 108 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 109 | + if r.URL.Path == "/auth/password/login" { |
| 110 | + resp := types.AccessKey{AccessKey: expectedKey} |
| 111 | + w.Header().Set("Content-Type", "application/json") |
| 112 | + _ = json.NewEncoder(w).Encode(resp) |
| 113 | + return |
| 114 | + } |
| 115 | + w.WriteHeader(http.StatusNotFound) |
| 116 | + })) |
| 117 | + defer server.Close() |
| 118 | + |
| 119 | + l := &LoftStarter{} |
| 120 | + |
| 121 | + // Simulate the try-secure-then-fallback pattern from loginViaCLI. |
| 122 | + key, insecure, err := l.passwordLogin(server.URL, []byte(`{}`), false) |
| 123 | + if err != nil && isTLSError(err) { |
| 124 | + // Fall back to insecure — this is the expected path for self-signed certs. |
| 125 | + key, insecure, err = l.passwordLogin(server.URL, []byte(`{}`), true) |
| 126 | + } |
| 127 | + assert.NilError(t, err) |
| 128 | + assert.Equal(t, key, expectedKey) |
| 129 | + assert.Assert(t, insecure, "should have fallen back to insecure") |
| 130 | +} |
| 131 | + |
| 132 | +func TestPasswordLogin_EmptyAccessKey(t *testing.T) { |
| 133 | + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 134 | + resp := types.AccessKey{AccessKey: ""} |
| 135 | + _ = json.NewEncoder(w).Encode(resp) |
| 136 | + })) |
| 137 | + defer server.Close() |
| 138 | + |
| 139 | + l := &LoftStarter{} |
| 140 | + _, _, err := l.passwordLogin(server.URL, []byte(`{}`), true) |
| 141 | + assert.Assert(t, err != nil, "expected error for empty access key") |
| 142 | + assert.Assert(t, strings.Contains(err.Error(), "couldn't retrieve access key")) |
| 143 | +} |
0 commit comments