-
Notifications
You must be signed in to change notification settings - Fork 40
/
execute.go
265 lines (238 loc) · 7.31 KB
/
execute.go
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
com "github.com/sqlitebrowser/dbhub.io/common"
"github.com/sqlitebrowser/dbhub.io/common/config"
"github.com/sqlitebrowser/dbhub.io/common/database"
)
func executePage(w http.ResponseWriter, r *http.Request) {
var pageData struct {
DB database.SQLiteDBinfo
PageMeta PageMetaInfo
SqlHistory []database.SqlHistoryItem
}
// Get all meta information
errCode, err := collectPageMetaInfo(w, r, &pageData.PageMeta)
if err != nil {
errorPage(w, r, errCode, err.Error())
return
}
dbName, err := getDatabaseName(r)
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Check if the database exists and the user has access to view it
exists, err := database.CheckDBPermissions(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, true)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !exists {
errorPage(w, r, http.StatusNotFound, fmt.Sprintf("Database '%s/%s' doesn't exist", dbName.Owner, dbName.Database))
return
}
// * Execution can only get here if the user has access to the requested database *
// Ensure this is a live database
isLive, liveNode, err := database.CheckDBLive(dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
if !isLive {
errorPage(w, r, http.StatusBadRequest, "Executing SQL statements is only supported for Live databases")
return
}
// Retrieve the database details
err = database.DBDetails(&pageData.DB, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database, "")
if err != nil {
errorPage(w, r, http.StatusBadRequest, err.Error())
return
}
// Ask the job queue backend for the database file size
pageData.DB.Info.DBEntry.Size, err = com.LiveSize(liveNode, pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Get SQL history
pageData.SqlHistory, err = database.LiveSqlHistoryGet(pageData.PageMeta.LoggedInUser, dbName.Owner, dbName.Database)
if err != nil {
errorPage(w, r, http.StatusInternalServerError, err.Error())
return
}
// Fill out various metadata fields
pageData.PageMeta.Title = fmt.Sprintf("Execute SQL - %s / %s", dbName.Owner, dbName.Database)
pageData.PageMeta.PageSection = "db_exec"
// Render the visualisation page
t := tmpl.Lookup("executePage")
err = t.Execute(w, pageData)
if err != nil {
log.Printf("Error: %s", err)
}
}
// execClearHistory deletes all items in the user's SQL history
func execClearHistory(w http.ResponseWriter, r *http.Request) {
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
var err error
if config.Conf.Environment.Environment == "production" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
u = sess.Values["UserName"]
} else {
u = config.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
}
// Retrieve user and database info
dbOwner, dbName, _, err := com.GetODC(2, r) // 2 = Ignore "/x/execlivesql/" at the start of the URL
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
// Delete items
err = database.LiveSqlHistoryDeleteOld(loggedInUser, dbOwner, dbName, 0) // 0 means "keep 0 items"
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
}
// execLiveSQL executes a user provided SQLite statement on a database.
func execLiveSQL(w http.ResponseWriter, r *http.Request) {
// Retrieve session data (if any)
var loggedInUser string
var u interface{}
var err error
if config.Conf.Environment.Environment == "production" {
sess, err := store.Get(r, "dbhub-user")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
u = sess.Values["UserName"]
} else {
u = config.Conf.Environment.UserOverride
}
if u != nil {
loggedInUser = u.(string)
}
// Retrieve user and database info
dbOwner, dbName, _, err := com.GetODC(2, r) // 2 = Ignore "/x/execlivesql/" at the start of the URL
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
// Grab the incoming SQLite query
bodyData, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
var data ExecuteSqlRequest
err = json.Unmarshal([]byte(bodyData), &data)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
sql, err := com.CheckUnicode(data.Sql, false)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err)
return
}
// Check if the requested database exists
exists, err := database.CheckDBPermissions(loggedInUser, dbOwner, dbName, true)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if !exists {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Database '%s/%s' doesn't exist", dbOwner, dbName)
return
}
// Make sure this is a live database
isLive, liveNode, err := database.CheckDBLive(dbOwner, dbName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
return
}
if !isLive {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Executing SQL statements is only supported on Live databases")
return
}
// In case of an error in the statement, save it in the terminal history as well.
// This should not be registered too early because we do not want to save it when
// there are validation or permission errors.
var logError = func(e error) {
// Store statement in sql terminal history
err = database.LiveSqlHistoryAdd(loggedInUser, dbOwner, dbName, sql, database.Error, map[string]interface{}{"error": e.Error()})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
}
}
// Send the SQL execution request to our job queue backend
var z interface{}
rowsChanged, err := com.LiveExecute(liveNode, loggedInUser, dbOwner, dbName, sql)
if err != nil {
if !strings.HasPrefix(err.Error(), "don't use exec with") {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
logError(err)
return
}
// The user tried to run a SELECT query. Let's just run with it...
z, err = com.LiveQuery(liveNode, loggedInUser, dbOwner, dbName, sql)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err.Error())
logError(err)
return
}
// Store statement in sql terminal history
err = database.LiveSqlHistoryAdd(loggedInUser, dbOwner, dbName, sql, database.Queried, z)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
}
} else {
// The SQL statement execution succeeded, so pass along the # of rows changed
z = com.ExecuteResponseContainer{RowsChanged: rowsChanged, Status: "OK"}
// Store statement in sql terminal history
err = database.LiveSqlHistoryAdd(loggedInUser, dbOwner, dbName, sql, database.Executed, z)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
}
}
// Return the success message
jsonData, err := json.Marshal(z)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, err)
logError(err)
return
}
fmt.Fprintf(w, "%s", jsonData)
}