sql

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Migrations = map[string]Statements{
	"postgres": {
		Migrations: &migrate.MemoryMigrationSource{
			Migrations: []*migrate.Migration{
				sharedMigrations[0],
				sharedMigrations[1],
				{
					Id: "3",
					Up: []string{
						"CREATE INDEX ladon_subject_compiled_idx ON ladon_subject (compiled text_pattern_ops)",
						"CREATE INDEX ladon_permission_compiled_idx ON ladon_action (compiled text_pattern_ops)",
						"CREATE INDEX ladon_resource_compiled_idx ON ladon_resource (compiled text_pattern_ops)",
					},
					Down: []string{
						"DROP INDEX ladon_subject_compiled_idx",
						"DROP INDEX ladon_permission_compiled_idx",
						"DROP INDEX ladon_resource_compiled_idx",
					},
				},
				{
					Id: "4",
					Up: []string{
						"ALTER TABLE ladon_policy ADD COLUMN meta json",
					},
					Down: []string{
						"ALTER TABLE ladon_policy DROP COLUMN IF EXISTS meta",
					},
				},
			},
		},
		QueryInsertPolicy:             `INSERT INTO ladon_policy(id, description, effect, conditions, meta) SELECT $1::varchar, $2, $3, $4, $5 ON CONFLICT DO NOTHING`,
		QueryInsertPolicyActions:      `INSERT INTO ladon_action (id, template, compiled, has_regex) SELECT $1::varchar, $2, $3, $4 ON CONFLICT DO NOTHING`,
		QueryInsertPolicyActionsRel:   `INSERT INTO ladon_policy_action_rel (policy, action) SELECT $1::varchar, $2::varchar ON CONFLICT DO NOTHING`,
		QueryInsertPolicyResources:    `INSERT INTO ladon_resource (id, template, compiled, has_regex) SELECT $1::varchar, $2, $3, $4 ON CONFLICT DO NOTHING`,
		QueryInsertPolicyResourcesRel: `INSERT INTO ladon_policy_resource_rel (policy, resource) SELECT $1::varchar, $2::varchar ON CONFLICT DO NOTHING`,
		QueryInsertPolicySubjects:     `INSERT INTO ladon_subject (id, template, compiled, has_regex) SELECT $1::varchar, $2, $3, $4 ON CONFLICT DO NOTHING`,
		QueryInsertPolicySubjectsRel:  `INSERT INTO ladon_policy_subject_rel (policy, subject) SELECT $1::varchar, $2::varchar ON CONFLICT DO NOTHING`,
		QueryPoliciesForSubject:       createQueryPolicies("subject", "postgres"),
		QueryPoliciesForResource:      createQueryPolicies("resource", "postgres"),
	},
	"mysql": {
		Migrations: &migrate.MemoryMigrationSource{
			Migrations: []*migrate.Migration{
				sharedMigrations[0],
				sharedMigrations[1],
				{
					Id: "3",
					Up: []string{
						"CREATE FULLTEXT INDEX ladon_subject_compiled_idx ON ladon_subject (compiled)",
						"CREATE FULLTEXT INDEX ladon_action_compiled_idx ON ladon_action (compiled)",
						"CREATE FULLTEXT INDEX ladon_resource_compiled_idx ON ladon_resource (compiled)",
					},
					Down: []string{
						"DROP INDEX ladon_subject_compiled_idx",
						"DROP INDEX ladon_permission_compiled_idx",
						"DROP INDEX ladon_resource_compiled_idx",
					},
				},
				{
					Id: "4",
					Up: []string{
						"ALTER TABLE ladon_policy ADD COLUMN meta text",
					},
					Down: []string{
						"ALTER TABLE ladon_policy DROP COLUMN meta",
					},
				},
			},
		},
		QueryInsertPolicy:             `INSERT IGNORE INTO ladon_policy (id, description, effect, conditions, meta) VALUES(?,?,?,?,?)`,
		QueryInsertPolicyActions:      `INSERT IGNORE INTO ladon_action (id, template, compiled, has_regex) VALUES(?,?,?,?)`,
		QueryInsertPolicyActionsRel:   `INSERT IGNORE INTO ladon_policy_action_rel (policy, action) VALUES(?,?)`,
		QueryInsertPolicyResources:    `INSERT IGNORE INTO ladon_resource (id, template, compiled, has_regex) VALUES(?,?,?,?)`,
		QueryInsertPolicyResourcesRel: `INSERT IGNORE INTO ladon_policy_resource_rel (policy, resource) VALUES(?,?)`,
		QueryInsertPolicySubjects:     `INSERT IGNORE INTO ladon_subject (id, template, compiled, has_regex) VALUES(?,?,?,?)`,
		QueryInsertPolicySubjectsRel:  `INSERT IGNORE INTO ladon_policy_subject_rel (policy, subject) VALUES(?,?)`,
		QueryPoliciesForSubject:       createQueryPolicies("subject", "mysql"),
		QueryPoliciesForResource:      createQueryPolicies("resource", "mysql"),
	},
}

Functions

This section is empty.

Types

type SQLManager

type SQLManager struct {
	// contains filtered or unexported fields
}

SQLManager is a postgres implementation for Manager to store policies persistently.

func NewSQLManager

func NewSQLManager(db *sqlx.DB, schema []string) *SQLManager

NewSQLManager initializes a new SQLManager for given db instance.

func (*SQLManager) Create

func (s *SQLManager) Create(policy Policy) (err error)

Create inserts a new policy

func (*SQLManager) CreateSchemas

func (s *SQLManager) CreateSchemas(schema, table string) (int, error)

CreateSchemas creates ladon_policy tables

func (*SQLManager) Delete

func (s *SQLManager) Delete(id string) error

Delete removes a policy.

func (*SQLManager) FindPoliciesForResource added in v1.0.0

func (s *SQLManager) FindPoliciesForResource(resource string) (Policies, error)

func (*SQLManager) FindPoliciesForSubject added in v1.0.0

func (s *SQLManager) FindPoliciesForSubject(subject string) (Policies, error)

func (*SQLManager) FindRequestCandidates

func (s *SQLManager) FindRequestCandidates(r *Request) (Policies, error)

func (*SQLManager) Get

func (s *SQLManager) Get(id string) (Policy, error)

Get retrieves a policy.

func (*SQLManager) GetAll

func (s *SQLManager) GetAll(limit, offset int64) (Policies, error)

GetAll returns all policies

func (*SQLManager) Update added in v0.8.0

func (s *SQLManager) Update(policy Policy) error

Update updates an existing policy.

type SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7

type SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7 struct {
	DB         *sqlx.DB
	SQLManager *SQLManager
}

func (*SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) Create

func (s *SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) Create(policy Policy) (err error)

Create inserts a new policy

func (*SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) GetManager

func (*SQLManagerMigrateFromMajor0Minor6ToMajor0Minor7) Migrate

Get retrieves a policy.

type Statements added in v0.8.5

type Statements struct {
	Migrations                    *migrate.MemoryMigrationSource
	QueryInsertPolicy             string
	QueryInsertPolicyActions      string
	QueryInsertPolicyActionsRel   string
	QueryInsertPolicyResources    string
	QueryInsertPolicyResourcesRel string
	QueryInsertPolicySubjects     string
	QueryInsertPolicySubjectsRel  string
	QueryPoliciesForSubject       string
	QueryPoliciesForResource      string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL