Skip to content

Commit

Permalink
🐛 Don't drop senstive data by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenny authored and Sergio Andrés Virviescas Santana committed Jul 19, 2020
1 parent 1288efc commit 97c56aa
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions providers/mysql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewDefaultConfig() Config {
Password: "",
Database: "session",
TableName: "session",
ResetTable: false,
Charset: "utf8",
Collation: "utf8_general_ci",
Timeout: 30 * time.Second,
Expand Down
18 changes: 13 additions & 5 deletions providers/mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
_ "github.com/go-sql-driver/mysql"
)

var initQueries = []string{
"DROP TABLE IF EXISTS %s;",
`CREATE TABLE IF NOT EXISTS %s (
var (
dropQuery = "DROP TABLE IF EXISTS %s;"
initQueries = []string{
`CREATE TABLE IF NOT EXISTS %s (
id VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'Session id',
data TEXT NOT NULL COMMENT 'Session data',
last_active BIGINT SIGNED NOT NULL DEFAULT '0' COMMENT 'Last active time',
Expand All @@ -20,7 +21,8 @@ var initQueries = []string{
KEY last_active (last_active),
KEY expiration (expiration)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='session table';`,
}
}
)

// New returns a new configured mysql provider
func New(cfg Config) (*Provider, error) {
Expand Down Expand Up @@ -64,11 +66,17 @@ func New(cfg Config) (*Provider, error) {
}

func (p *Provider) init() error {
if p.config.DropTable {
_, err := p.Exec(fmt.Sprintf(dropQuery, p.config.TableName))
if err != nil {
p.Close()
return err
}
}
for _, query := range initQueries {
_, err := p.Exec(fmt.Sprintf(query, p.config.TableName))
if err != nil {
p.Close()

return err
}
}
Expand Down
3 changes: 3 additions & 0 deletions providers/mysql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Config struct {
// DB table name
TableName string

// When set to true, this will Drop any existing table with the same name
DropTable bool

// Charset used for client-server interaction ("SET NAMES <value>")
// If multiple charsets are set (separated by a comma),
// the following charset is used if setting the charset failes
Expand Down
1 change: 1 addition & 0 deletions providers/postgre/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewDefaultConfig() Config {
Password: "",
Database: "session",
TableName: "session",
ResetTable: false,
Timeout: 30 * time.Second,
MaxOpenConns: 100,
MaxIdleConns: 100,
Expand Down
21 changes: 15 additions & 6 deletions providers/postgre/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import (
_ "github.com/lib/pq"
)

var initQueries = []string{
"DROP TABLE IF EXISTS %s;",
`CREATE TABLE IF NOT EXISTS %s (
var (
dropQuery = "DROP TABLE IF EXISTS %s;"
initQueries = []string{
`CREATE TABLE IF NOT EXISTS %s (
id VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
data TEXT NOT NULL,
last_active BIGINT NOT NULL DEFAULT '0',
expiration BIGINT NOT NULL DEFAULT '0'
);`,
"CREATE INDEX last_active ON %s (last_active);",
"CREATE INDEX expiration ON %s (expiration);",
}
"CREATE INDEX last_active ON %s (last_active);",
"CREATE INDEX expiration ON %s (expiration);",
}
)

// New returns a new configured postgres provider
func New(cfg Config) (*Provider, error) {
Expand Down Expand Up @@ -63,6 +65,13 @@ func New(cfg Config) (*Provider, error) {
}

func (p *Provider) init() error {
if p.config.DropTable {
_, err := p.Exec(fmt.Sprintf(dropQuery, p.config.TableName))
if err != nil {
p.Close()
return err
}
}
for _, query := range initQueries {
_, err := p.Exec(fmt.Sprintf(query, p.config.TableName))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions providers/postgre/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Config struct {
// DB table name
TableName string

// When set to true, this will Drop any existing table with the same name
DropTable bool

// Maximum wait for connection, in seconds. Zero or
// not specified means wait indefinitely.
Timeout time.Duration
Expand Down
1 change: 1 addition & 0 deletions providers/sqlite3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func NewDefaultConfig() Config {
return Config{
DBPath: "./",
TableName: "session",
ResetTable: false,
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
Expand Down
22 changes: 15 additions & 7 deletions providers/sqlite3/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import (
_ "github.com/mattn/go-sqlite3"
)

var initQueries = []string{
"DROP TABLE IF EXISTS %s;",
`CREATE TABLE IF NOT EXISTS %s (
var (
dropQuery = "DROP TABLE IF EXISTS %s;"
initQueries = []string{
`CREATE TABLE IF NOT EXISTS %s (
id VARCHAR(64) PRIMARY KEY NOT NULL DEFAULT '',
data TEXT NOT NULL,
last_active BIGINT NOT NULL DEFAULT '0',
expiration BIGINT NOT NULL DEFAULT '0'
);`,
"CREATE INDEX last_active ON %s (last_active);",
"CREATE INDEX expiration ON %s (expiration);",
}
"CREATE INDEX last_active ON %s (last_active);",
"CREATE INDEX expiration ON %s (expiration);",
}
)

// New returns a new configured sqlite3 provider
func New(cfg Config) (*Provider, error) {
Expand Down Expand Up @@ -60,11 +62,17 @@ func New(cfg Config) (*Provider, error) {
}

func (p *Provider) init() error {
if p.config.DropTable {
_, err := p.Exec(fmt.Sprintf(dropQuery, p.config.TableName))
if err != nil {
p.Close()
return err
}
}
for _, query := range initQueries {
_, err := p.Exec(fmt.Sprintf(query, p.config.TableName))
if err != nil {
p.Close()

return err
}
}
Expand Down
3 changes: 3 additions & 0 deletions providers/sqlite3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type Config struct {
// DB table name
TableName string

// When set to true, this will Drop any existing table with the same name
DropTable bool

// The maximum number of connections in the idle connection pool.
//
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
Expand Down

0 comments on commit 97c56aa

Please sign in to comment.