Using/setting up user authentication for sqlite3 in golang

huangapple go评论115阅读模式
英文:

Using/setting up user authentication for sqlite3 in golang

问题

我必须在学校的任务中将我的数据库设置为密码保护。例如,如果有人试图访问我的数据库,它将要求输入密码。
我正在尝试使用go-sqlite3包,并已经阅读了官方指南。
第一步是使用go build --tags <FEATURE>
它给我一个错误信息:build .: cannot find module for path .
我不知道为什么,首先我们要构建什么。我还尝试搜索了一些实际的例子,但没有找到任何内容。

你能解释一下如何使用Go语言的go-sqlite3包来设置数据库的用户认证吗?
包的链接

英文:

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.<br>
I am trying to use go-sqlite3 package and I have tried reading the official guide.<br>
First step is to use go build --tags &lt;FEATURE&gt;.<br>
It gaves me an error build .: cannot find module for path .<br>
I dont know why and what are we building in the first place. I tried searching for practical examples also and didnt found any.<br><br>
Can you explain to me how I can setup user authentication for my database using the golangs go-sqlite3 package?<br>
Link to the package

答案1

得分: 3

你需要将该指令中的&lt;FEATURE&gt;替换为你想要启用的扩展名,可以从下表中选择(似乎README中有一个错误,示例中去掉了sqlite_前缀;构建标签确实是sqlite_userauth)。

因此,要启用用户身份验证,可以使用go build -tags "sqlite_userauth"

在依赖go-sqlite3模块的项目中,只需确保使用-tags sqlite_userauth进行构建。

以下是一个最简示例,展示了如何在项目中使用此功能:

  1. mkdir sqlite3auth
  2. cd sqlite3auth
  3. go mod init sqlite3auth
  4. touch main.go

main.go:

  1. package main
  2. import (
  3. "database/sql"
  4. "log"
  5. "github.com/mattn/go-sqlite3"
  6. )
  7. func main() {
  8. // 这不是必需的;只是为了查看是否启用了认证扩展
  9. sql.Register("sqlite3_log", &sqlite3.SQLiteDriver{
  10. ConnectHook: func(conn *sqlite3.SQLiteConn) error {
  11. log.Printf("Auth enabled: %v\n", conn.AuthEnabled())
  12. return nil
  13. },
  14. })
  15. // 这是常规的数据库操作(除了我们使用的sqlite3_log驱动程序)
  16. db, err := sql.Open("sqlite3_log", "file:test.db?_auth&_auth_user=admin&_auth_pass=admin")
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. defer db.Close()
  21. _, err = db.Exec(`select 1`)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }
  1. go mod tidy
  2. go: finding module for package github.com/mattn/go-sqlite3
  3. go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.10
  4. # 首先使用认证扩展进行构建(-o NAME只是为二进制文件命名)
  5. go build -tags sqlite_userauth -o auth .
  6. # 然后再次构建,不使用认证扩展
  7. go build -o noauth .
  8. ./auth
  9. 2022/01/27 21:47:46 Auth enabled: true
  10. ./noauth
  11. 2022/01/27 21:47:46 Auth enabled: false
英文:

You need to replace &lt;FEATURE&gt; 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 &quot;sqlite_userauth&quot;.

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:

  1. mkdir sqlite3auth
  2. cd sqlite3auth
  3. go mod init sqlite3auth
  4. touch main.go

main.go:

  1. package main
  2. import (
  3. &quot;database/sql&quot;
  4. &quot;log&quot;
  5. &quot;github.com/mattn/go-sqlite3&quot;
  6. )
  7. func main() {
  8. // This is not necessary; just to see if auth extension is enabled
  9. sql.Register(&quot;sqlite3_log&quot;, &amp;sqlite3.SQLiteDriver{
  10. ConnectHook: func(conn *sqlite3.SQLiteConn) error {
  11. log.Printf(&quot;Auth enabled: %v\n&quot;, conn.AuthEnabled())
  12. return nil
  13. },
  14. })
  15. // This is usual DB stuff (except with our sqlite3_log driver)
  16. db, err := sql.Open(&quot;sqlite3_log&quot;, &quot;file:test.db?_auth&amp;_auth_user=admin&amp;_auth_pass=admin&quot;)
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. defer db.Close()
  21. _, err = db.Exec(`select 1`)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }
  1. go mod tidy
  2. go: finding module for package github.com/mattn/go-sqlite3
  3. go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.10
  4. # First build with auth extension (-o NAME is just to give binary a name)
  5. go build -tags sqlite_userauth -o auth .
  6. # then build without it
  7. go build -o noauth .
  8. ./auth
  9. 2022/01/27 21:47:46 Auth enabled: true
  10. ./noauth
  11. 2022/01/27 21:47:46 Auth enabled: false

huangapple
  • 本文由 发表于 2022年1月27日 18:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/70877203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定