Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sql/auth): base authentication storage implementation #1095

Merged
merged 14 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions cmd/flipt/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,23 @@ func runImport(ctx context.Context, logger *zap.Logger, args []string) error {

defer in.Close()

// drop tables if specified
if dropBeforeImport {
logger.Debug("dropping tables before import")

tables := []string{"schema_migrations", "distributions", "rules", "constraints", "variants", "segments", "flags"}

for _, table := range tables {
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", table)); err != nil {
return fmt.Errorf("dropping tables: %w", err)
}
}
}

GeorgeMac marked this conversation as resolved.
Show resolved Hide resolved
migrator, err := sql.NewMigrator(*cfg, logger)
if err != nil {
return err
}

defer migrator.Close()

if err := migrator.Run(forceMigrate); err != nil {
// drop tables if specified
if dropBeforeImport {
logger.Debug("dropping tables before import")

if err := migrator.Down(); err != nil {
return fmt.Errorf("attempting to drop during import: %w", err)
}
}

if err := migrator.Up(forceMigrate); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func main() {

defer migrator.Close()

if err := migrator.Run(true); err != nil {
if err := migrator.Up(true); err != nil {
logger().Fatal("running migrator", zap.Error(err))
}
},
Expand Down Expand Up @@ -396,7 +396,7 @@ func run(ctx context.Context, logger *zap.Logger) error {

defer migrator.Close()

if err := migrator.Run(forceMigrate); err != nil {
if err := migrator.Up(forceMigrate); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX hashed_client_token_authentications_index;
DROP TABLE IF EXISTS authentications;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS authentications (
id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL,
hashed_client_token VARCHAR(255) UNIQUE NOT NULL,
method VARCHAR(255) NOT NULL,
metadata TEXT,
expires_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);

CREATE UNIQUE INDEX hashed_client_token_authentications_index ON authentications (hashed_client_token);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX hashed_client_token_authentications_index ON authentications;
DROP TABLE IF EXISTS authentications;
12 changes: 12 additions & 0 deletions config/migrations/mysql/2_create_table_authentications.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS authentications (
id VARCHAR(255) UNIQUE NOT NULL,
hashed_client_token VARCHAR(255) UNIQUE NOT NULL,
method VARCHAR(255) NOT NULL,
metadata TEXT,
expires_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (`id`)
);

CREATE UNIQUE INDEX hashed_client_token_authentications_index ON authentications (hashed_client_token);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX hashed_client_token_authentications_index;
DROP TABLE IF EXISTS authentications;
11 changes: 11 additions & 0 deletions config/migrations/postgres/4_create_table_authentications.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS authentications (
id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL,
hashed_client_token VARCHAR(255) UNIQUE NOT NULL,
method VARCHAR(255) NOT NULL,
metadata TEXT,
expires_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);

CREATE UNIQUE INDEX hashed_client_token_authentications_index ON authentications (hashed_client_token);
9 changes: 4 additions & 5 deletions config/migrations/sqlite3/1_variants_unique_per_flag.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ CREATE TABLE variants_temp
(
id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL,
flag_key VARCHAR(255) NOT NULL REFERENCES flags ON DELETE CASCADE,
key VARCHAR(255) NOT NULL,
key VARCHAR(255) NOT NULL UNIQUE ON CONFLICT REPLACE,
name VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
UNIQUE key ON CONFLICT REPLACE
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);

INSERT INTO variants_temp (id, flag_key, key, name, description, created_at, updated_at)
SELECT id, flag_key, key, name, description, created_at, updated_at
INSERT INTO variants_temp (id, flag_key, `key`, name, description, created_at, updated_at)
SELECT id, flag_key, `key`, name, description, created_at, updated_at
FROM variants;

DROP TABLE variants;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP INDEX hashed_client_token_authentications_index;
DROP TABLE IF EXISTS authentications;
11 changes: 11 additions & 0 deletions config/migrations/sqlite3/4_create_table_authentications.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS authentications (
id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL,
hashed_client_token VARCHAR(255) UNIQUE NOT NULL,
method VARCHAR(255) NOT NULL,
metadata TEXT,
expires_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);

CREATE UNIQUE INDEX hashed_client_token_authentications_index ON authentications (hashed_client_token);
76 changes: 76 additions & 0 deletions internal/storage/sql/auth/fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package auth

import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"

"go.flipt.io/flipt/rpc/flipt/auth"
"google.golang.org/protobuf/types/known/timestamppb"
)

type timestamp struct {
*timestamppb.Timestamp
}

func (t *timestamp) Scan(value interface{}) error {
GeorgeMac marked this conversation as resolved.
Show resolved Hide resolved
if v, ok := value.(time.Time); ok {
val := timestamppb.New(v)
if err := val.CheckValid(); err != nil {
return err
}

t.Timestamp = val
}

return nil
}

func (t *timestamp) Value() (driver.Value, error) {
return t.Timestamp.AsTime(), t.Timestamp.CheckValid()
}

type method auth.Method

func (m *method) Scan(v interface{}) error {
var methodStr string
switch b := v.(type) {
case []byte:
methodStr = string(b)
case string:
methodStr = b
default:
return fmt.Errorf("unexpected method type: %T", v)
}

*m = method(auth.Method_value[methodStr])

return nil
}

func (m *method) Value() (driver.Value, error) {
return auth.Method_name[int32(*m)], nil
GeorgeMac marked this conversation as resolved.
Show resolved Hide resolved
}

type jsonField[T any] struct {
t T
}

func (d *jsonField[T]) Scan(v any) error {
var bytes []byte
switch b := v.(type) {
case []byte:
bytes = b
case string:
bytes = []byte(b)
default:
return fmt.Errorf("unexpected type for data: %T", v)
}

return json.Unmarshal(bytes, &d.t)
}

func (d *jsonField[T]) Value() (driver.Value, error) {
return json.Marshal(d.t)
}
Loading