Skip to content

Commit

Permalink
sql: ensure postgres 9.2 and mysql 5.5 compatibility (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Jun 8, 2017
1 parent c8b0a33 commit 018f499
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ In contrast to [ACL](https://en.wikipedia.org/wiki/Access_control_list) and [RBA
you get fine-grained access control with the ability to answer questions in complex environments such as multi-tenant or distributed applications
and large organizations. Ladon is inspired by [AWS IAM Policies](http://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html).

Ladon ships with storage adapters for SQL (officially supported: MySQL, PostgreSQL) and in-memory.
Ladon ships with storage adapters for SQL (officially supported: MySQL 5.5+, PostgreSQL 9.2+) and in-memory.

---

ORY builds solutions for better internet security and accessibility. We have a couple more projects you might enjoy:

* **[Hydra](https://github.com/ory/hydra)**, a security-first open source OAuth2 and OpenID Connect server for new and existing infrastructures that uses Ladon for access control.
* **[ORY Editor](https://github.com/ory/editor)**, an extensible, modern WYSI editor for the web written in React.
* **[Fosite](https://github.com/ory/fosite)**, an extensible security first OAuth 2.0 and OpenID Connect SDK for Go.
* **[Fosite](https://github.com/ory/fosite)**, an extensible security first OAuth 2.0 and OpenID Connect SDK for Go.
* **[Dockertest](https://github.com/ory/dockertest)**: Write better integration tests with dockertest!

---
Expand Down
4 changes: 2 additions & 2 deletions integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func ConnectToMySQL() *sqlx.DB {
log.Fatalf("Could not connect to docker: %s", err)
}

resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"})
resource, err := pool.Run("mysql", "5.6", []string{"MYSQL_ROOT_PASSWORD=secret"})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func ConnectToPostgres(database string) *sqlx.DB {
log.Fatalf("Could not connect to docker: %s", err)
}

resource, err := pool.Run("postgres", "9.6", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=" + database})
resource, err := pool.Run("postgres", "9.2", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=" + database})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
Expand Down
27 changes: 25 additions & 2 deletions manager/sql/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,20 @@ func (s *SQLManager) Create(policy Policy) (err error) {
}

switch s.db.DriverName() {
case "postgres", "pgx", "mysql":
case "postgres", "pgx":
if _, err = tx.Exec(s.db.Rebind("INSERT INTO ladon_policy (id, description, effect, conditions) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM ladon_policy WHERE id = ?)"), policy.GetID(), policy.GetDescription(), policy.GetEffect(), conditions, policy.GetID()); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}
case "mysql":
if _, err = tx.Exec(s.db.Rebind("INSERT IGNORE INTO ladon_policy (id, description, effect, conditions) VALUES (?, ?, ?, ?)"), policy.GetID(), policy.GetDescription(), policy.GetEffect(), conditions); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}
default:
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -225,7 +232,7 @@ func (s *SQLManager) Create(policy Policy) (err error) {
}

switch s.db.DriverName() {
case "postgres", "pgx", "mysql":
case "postgres", "pgx":
if _, err := tx.Exec(s.db.Rebind(fmt.Sprintf("INSERT INTO ladon_%s (id, template, compiled, has_regex) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM ladon_%[1]s WHERE id = ?)", v.t)), id, template, compiled.String(), strings.Index(template, string(policy.GetStartDelimiter())) > -1, id); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
Expand All @@ -240,6 +247,22 @@ func (s *SQLManager) Create(policy Policy) (err error) {
return errors.WithStack(err)
}
break

case "mysql":
if _, err := tx.Exec(s.db.Rebind(fmt.Sprintf("INSERT IGNORE INTO ladon_%s (id, template, compiled, has_regex) VALUES (?, ?, ?, ?)", v.t)), id, template, compiled.String(), strings.Index(template, string(policy.GetStartDelimiter())) > -1); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}

if _, err := tx.Exec(s.db.Rebind(fmt.Sprintf("INSERT IGNORE INTO ladon_policy_%s_rel (policy, %s) VALUES (?, ?)", v.t, v.t)), policy.GetID(), id); err != nil {
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(err)
}
break
default:
if err := tx.Rollback(); err != nil {
return errors.WithStack(err)
Expand Down

0 comments on commit 018f499

Please sign in to comment.