I have to make my database password protected as a task in my school. For example if anyone tries to access my database it will ask the password.
I am trying to use go-sqlite3 package and I have tried reading the official guide.
First step is to use go build --tags <FEATURE>
.
It gaves me an error build .: cannot find module for path .
I dont know why and what are we building in the first place. I tried searching for practical examples also and didnt found any.
Can you explain to me how I can setup user authentication for my database using the golangs go-sqlite3 package?
Link to the package
Asked
Active
Viewed 1,251 times
0

Kristofer Kangro
- 89
- 1
- 9
1 Answers
3
You need to replace <FEATURE>
in that instruction by extension name(s) you want to enable from table below (Seems there's an error in README and it has sqlite_
prefix stripped in example; build tag is indeed sqlite_userauth
).
So, to enable user authentication that will be go build -tags "sqlite_userauth"
.
In your project with go-sqlite3
module dependency just make sure that you build with -tags sqlite_userauth
.
Here is minimal example showing how you would work with this in your project:
mkdir sqlite3auth
cd sqlite3auth
go mod init sqlite3auth
touch main.go
main.go:
package main
import (
"database/sql"
"log"
"github.com/mattn/go-sqlite3"
)
func main() {
// This is not necessary; just to see if auth extension is enabled
sql.Register("sqlite3_log", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
log.Printf("Auth enabled: %v\n", conn.AuthEnabled())
return nil
},
})
// This is usual DB stuff (except with our sqlite3_log driver)
db, err := sql.Open("sqlite3_log", "file:test.db?_auth&_auth_user=admin&_auth_pass=admin")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`select 1`)
if err != nil {
log.Fatal(err)
}
}
go mod tidy
go: finding module for package github.com/mattn/go-sqlite3
go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.10
# First build with auth extension (-o NAME is just to give binary a name)
go build -tags sqlite_userauth -o auth .
# then build without it
go build -o noauth .
./auth
2022/01/27 21:47:46 Auth enabled: true
./noauth
2022/01/27 21:47:46 Auth enabled: false

blami
- 6,588
- 2
- 23
- 31
-
Why do I have to clone the repository? I am like modifying the repository and then installing thr modified repository to my project with go get? Cheers for helping :) – Kristofer Kangro Jan 27 '22 at 12:14
-
@KristoferKangro sorry it was hard to understand what you are trying to do; I modified my answer to show how you'd typically use go-sqlite3 in modules enabled project with user authentication extension enabled. – blami Jan 27 '22 at 12:54
-
Cheers for nice answer! I am gonna look at it now and try to understand. I will get back if I have more issues – Kristofer Kangro Jan 27 '22 at 13:03
-
When I try to build with (as I did before posting) `go build -tags sqlite_userauth -o auth .` I get still the same error `build .: cannot find module for path . .` I am curious.. Based on what are we building? – Kristofer Kangro Jan 27 '22 at 13:12
-
@KristoferKangro the example above is fully working (unless you have very old version of `go`). That error looks like `main` package of your project is not in same directory where you run `go build`. In that case you need to provide path to `main` package instead of `.` e.g. `go build cmd/foo` or so. – blami Jan 27 '22 at 13:18
-
Never said yours is wrong. I am just trying to do it in my actual project. Okay, so go build uses main.go file to compile? Anyway it did not give me an error message so thats good! I will keep you updated – Kristofer Kangro Jan 27 '22 at 13:22
-
@KristoferKangro sorry didn't meant to accuse you of saying that. Just wanted to say that if you follow step-by-step you should get working build. `go build` takes path to a package (a directory of one or more `.go` files which have same `package
` header). To build binary this name has to be `main` and exactly one of them has to have `func main()`. It is all explained here https://go.dev/doc/tutorial/getting-started – blami Jan 27 '22 at 13:27 -
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241458/discussion-between-kristofer-kangro-and-blami). – Kristofer Kangro Jan 27 '22 at 13:51