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

Introduces surrogate_id to SQLManager #21

Merged
merged 1 commit into from
Nov 12, 2017
Merged
Changes from all 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
16 changes: 9 additions & 7 deletions rule/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

type sqlRule struct {
ID string `db:"id"`
ID string `db:"surrogate_id"`
InternalID int64 `db:"id"`
MatchesMethods string `db:"matches_methods"`
MatchesURL string `db:"matches_url"`
RequiredScopes string `db:"required_scopes"`
Expand Down Expand Up @@ -76,9 +77,10 @@ var migrations = &migrate.MemoryMigrationSource{
{
Id: "1",
Up: []string{`CREATE TABLE IF NOT EXISTS oathkeeper_rule (
id varchar(64) NOT NULL PRIMARY KEY,
id SERIAL PRIMARY KEY,
surrogate_id varchar(190) NOT NULL UNIQUE,
matches_methods varchar(64) NOT NULL,
matches_url text NOT NULL,
matches_url text NOT NULL,
required_scopes text NOT NULL,
required_action text NOT NULL,
required_resource text NOT NULL,
Expand All @@ -95,7 +97,7 @@ var migrations = &migrate.MemoryMigrationSource{
}

var sqlParams = []string{
"id",
"surrogate_id",
"matches_methods",
"matches_url",
"required_scopes",
Expand Down Expand Up @@ -146,7 +148,7 @@ func (s *SQLManager) ListRules() ([]Rule, error) {

func (s *SQLManager) GetRule(id string) (*Rule, error) {
var d sqlRule
if err := s.db.Get(&d, s.db.Rebind("SELECT * FROM oathkeeper_rule WHERE id=?"), id); err == sql.ErrNoRows {
if err := s.db.Get(&d, s.db.Rebind("SELECT * FROM oathkeeper_rule WHERE surrogate_id=?"), id); err == sql.ErrNoRows {
return nil, errors.WithStack(helper.ErrResourceNotFound)
} else if err != nil {
return nil, errors.WithStack(err)
Expand Down Expand Up @@ -175,14 +177,14 @@ func (s *SQLManager) UpdateRule(rule *Rule) error {
for _, param := range sqlParams {
query = append(query, fmt.Sprintf("%s=:%s", param, param))
}
if _, err := s.db.NamedExec(fmt.Sprintf(`UPDATE oathkeeper_rule SET %s WHERE id=:id`, strings.Join(query, ", ")), sr); err != nil {
if _, err := s.db.NamedExec(fmt.Sprintf(`UPDATE oathkeeper_rule SET %s WHERE surrogate_id=:surrogate_id`, strings.Join(query, ", ")), sr); err != nil {
return errors.WithStack(err)
}
return nil
}

func (s *SQLManager) DeleteRule(id string) error {
if _, err := s.db.Exec(s.db.Rebind(`DELETE FROM oathkeeper_rule WHERE id=?`), id); err != nil {
if _, err := s.db.Exec(s.db.Rebind(`DELETE FROM oathkeeper_rule WHERE surrogate_id=?`), id); err != nil {
return errors.WithStack(err)
}
return nil
Expand Down