-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth_test.go
More file actions
209 lines (175 loc) · 5.83 KB
/
auth_test.go
File metadata and controls
209 lines (175 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package airport_test
import (
"testing"
)
// TestAuthentication verifies that bearer token authentication works correctly.
func TestAuthentication(t *testing.T) {
cat := authenticatedCatalog()
auth := testAuthHandler()
server := newTestServer(t, cat, auth)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
// Test 1: Request without token should fail
t.Run("NoToken", func(t *testing.T) {
// Try to attach without token
attachName := "test_no_auth"
query := "ATTACH '" + server.address + "' AS " + attachName + " (TYPE airport)"
_, err := db.Exec(query)
if err == nil {
t.Error("Expected authentication error, but query succeeded")
}
})
// Test 2: Request with invalid token should fail
t.Run("InvalidToken", func(t *testing.T) {
attachName := "test_invalid"
// Create secret with invalid token
secretQuery := "CREATE OR REPLACE SECRET invalid_secret (TYPE AIRPORT, auth_token 'invalid-token', scope 'grpc://" + server.address + "')"
_, err := db.Exec(secretQuery)
if err != nil {
t.Fatalf("Failed to create secret: %v", err)
}
query := "ATTACH '' AS " + attachName + " (TYPE airport, SECRET invalid_secret, LOCATION 'grpc://" + server.address + "')"
_, err = db.Exec(query)
if err == nil {
t.Error("Expected authentication error with invalid token, but query succeeded")
}
})
// Test 3: Request with valid token should succeed
t.Run("ValidToken", func(t *testing.T) {
attachName := connectToFlightServer(t, db, server.address, "valid-token")
// Should be able to query tables
query := "SELECT COUNT(*) FROM " + attachName + ".secure.secrets"
var count int64
if err := db.QueryRow(query).Scan(&count); err != nil {
t.Fatalf("Query with valid token failed: %v", err)
}
if count != 2 {
t.Errorf("Expected 2 rows, got %d", count)
}
})
// Test 4: Verify data is accessible with auth
t.Run("QueryWithAuth", func(t *testing.T) {
// Create new connection with admin token
attachName := "test_admin"
// Create secret with admin token
secretQuery := "CREATE OR REPLACE SECRET admin_secret (TYPE AIRPORT, auth_token 'admin-token', scope 'grpc://" + server.address + "')"
_, err := db.Exec(secretQuery)
if err != nil {
t.Fatalf("Failed to create secret: %v", err)
}
query := "ATTACH '' AS " + attachName + " (TYPE airport, SECRET admin_secret, LOCATION 'grpc://" + server.address + "')"
_, err = db.Exec(query)
if err != nil {
t.Fatalf("Failed to attach with admin token: %v", err)
}
// Query secrets table
queryData := "SELECT key, value FROM " + attachName + ".secure.secrets ORDER BY key"
rows, err := db.Query(queryData)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
secrets := make(map[string]string)
for rows.Next() {
var key, value string
if err := rows.Scan(&key, &value); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
secrets[key] = value
}
if len(secrets) != 2 {
t.Errorf("Expected 2 secrets, got %d", len(secrets))
}
if secrets["api_key"] != "secret123" {
t.Errorf("Expected api_key='secret123', got '%s'", secrets["api_key"])
}
if secrets["db_password"] != "pass456" {
t.Errorf("Expected db_password='pass456', got '%s'", secrets["db_password"])
}
})
}
// TestAuthorizationInCatalog verifies that authentication context is passed
// to catalog methods and can be used for authorization.
func TestAuthorizationInCatalog(t *testing.T) {
// This test would verify permission-based filtering
// For now, we verify that authentication metadata is correctly propagated
cat := authenticatedCatalog()
auth := testAuthHandler()
server := newTestServer(t, cat, auth)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
t.Run("AuthenticatedDiscovery", func(t *testing.T) {
// Connect with valid token
attachName := connectToFlightServer(t, db, server.address, "valid-token")
// Should be able to discover schemas
query := "SELECT schema_name FROM duckdb_schemas() WHERE database_name = ?"
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Schema discovery failed: %v", err)
}
defer rows.Close()
schemaCount := 0
for rows.Next() {
schemaCount++
}
if schemaCount == 0 {
t.Error("Expected at least one schema with authenticated access")
}
})
}
// TestTokenValidation verifies different authentication scenarios.
func TestTokenValidation(t *testing.T) {
cat := authenticatedCatalog()
auth := testAuthHandler()
server := newTestServer(t, cat, auth)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
testCases := []struct {
name string
token string
shouldWork bool
description string
}{
{
name: "ValidUserToken",
token: "valid-token",
shouldWork: true,
description: "Valid user token should work",
},
{
name: "ValidAdminToken",
token: "admin-token",
shouldWork: true,
description: "Valid admin token should work",
},
{
name: "WrongToken",
token: "wrong-token",
shouldWork: false,
description: "Wrong token should fail",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
attachName := "test_" + tc.name
// Create secret with token
secretName := "secret_" + tc.name
secretQuery := "CREATE OR REPLACE SECRET " + secretName + " (TYPE AIRPORT, auth_token '" + tc.token + "', scope 'grpc://" + server.address + "')"
_, err := db.Exec(secretQuery)
if err != nil {
t.Fatalf("Failed to create secret: %v", err)
}
query := "ATTACH '' AS " + attachName + " (TYPE airport, SECRET " + secretName + ", LOCATION 'grpc://" + server.address + "')"
_, err = db.Exec(query)
if tc.shouldWork && err != nil {
t.Errorf("%s: Expected success, but got error: %v", tc.description, err)
}
if !tc.shouldWork && err == nil {
t.Errorf("%s: Expected failure, but query succeeded", tc.description)
}
})
}
}