-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_provider.go
69 lines (62 loc) · 1.73 KB
/
mysql_provider.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
package openapi
import (
"database/sql"
"errors"
"fmt"
)
/**
# the sql to create the table app
CREATE TABLE `app` (
`app_key` varchar(32) NOT NULL,
`app_secret` varchar(128) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`app_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/
const (
queryTemplate = "SELECT `%s` FROM `%s` WHERE `%s` = ? LIMIT 1"
insertTemplate = "INSERT INTO `%s`(`%s`, `%s`)VALUE(?, ?)"
)
// default provided sql
type SqlSecretKeeper struct {
Db *sql.DB // the client to access database
TableName string // the table where the secret stores
KeyCol string // the column name of the key
SecretCol string // the column name of the secret
}
// get secret from a sql data source
func (s SqlSecretKeeper) GetSecret(key string) (string, error) {
if s.Db == nil {
return EmptyString, errors.New("db client should not be nil")
}
row := s.Db.QueryRow(s.constructQuery(), key)
var secret string
err := row.Scan(&secret)
if err != nil {
return EmptyString, err
}
return secret, nil
}
// construct query for getting secret
func (s SqlSecretKeeper) constructQuery() string {
return fmt.Sprintf(queryTemplate, s.SecretCol, s.TableName, s.KeyCol)
}
func (s SqlSecretKeeper) GeneratePair() *KvPair {
p := KvPair{
Key: string(randomStr(keyLen, kindAll)),
Value: string(randomStr(secretLen, kindAll)),
}
// do the insert work
insertSql := fmt.Sprintf(insertTemplate, s.TableName, s.KeyCol, s.SecretCol)
r, err := s.Db.Exec(insertSql, p.Key, p.Value)
if err != nil || r == nil {
return nil
}
a, err := r.RowsAffected()
// check result
if err != nil || a < 1 {
return nil
}
return &p
}