vertically challenged

65
VERTICALLY CHALLENGED 1 Tuesday, October 20, 2009

Upload: aurynn-shaw

Post on 30-Oct-2014

781 views

Category:

Technology


0 download

DESCRIPTION

Using database roles to in a WSGI application development stack.

TRANSCRIPT

Page 1: Vertically Challenged

VERTICALLY CHALLENGED

1

Tuesday, October 20, 2009

Page 2: Vertically Challenged

Strong Application Permissions, using PostgreSQL

PostgreSQL Conference West, 2009

Aurynn Shaw, Command Prompt, Inc.

2

Tuesday, October 20, 2009

Page 3: Vertically Challenged

APPLICATION ARCHITECTURE

Wherein

3

Basic Architecture

Limitations

Points of Authority

Why use DB permissions?

Implementation

Tuesday, October 20, 2009

Page 4: Vertically Challenged

Basic Architecture

Standard tiered design

Web Server

Application

Database

4

Tuesday, October 20, 2009

Page 5: Vertically Challenged

Basic Architecture

Standard tiered design

Standard ingress, logic, egress cycle.

Web Server

Application

Database

5

Tuesday, October 20, 2009

Page 6: Vertically Challenged

Layered Communications

Limitations in communication

Web Server

Application

Database

6

Tuesday, October 20, 2009

Page 7: Vertically Challenged

Layered Communications

Limitations in communication

Influences on application design

Web Server

Application

Database

7

Tuesday, October 20, 2009

Page 8: Vertically Challenged

Application Authority

The Application becomes permissions arbiter

Web Server

Application

Database

8

Tuesday, October 20, 2009

Page 9: Vertically Challenged

Application Authority

The Application becomes permissions arbiter

Loss of Portability

Web Server

Application

Database

9

Tuesday, October 20, 2009

Page 10: Vertically Challenged

Application Authority

The Application becomes permissions arbiter

Loss of Portability

Increased work for other consumers

Web Server

Application

Database

10

Tuesday, October 20, 2009

Page 11: Vertically Challenged

Using DB Permissions

Why ?

11

Tuesday, October 20, 2009

Page 12: Vertically Challenged

Using DB Permissions

Why ?It’s better than what you have now

12

Tuesday, October 20, 2009

Page 13: Vertically Challenged

Using DB Permissions

Why ?It’s better than what you have now

Easy Integration

13

Tuesday, October 20, 2009

Page 14: Vertically Challenged

Using DB Permissions

Why ?It’s better than what you have now

Easy Integration

Once it’s set up, it’s easier to use

14

Tuesday, October 20, 2009

Page 15: Vertically Challenged

Basic Mechanica

Entirely DB users

Why this is hard to implement

15

Tuesday, October 20, 2009

Page 16: Vertically Challenged

Basic Mechanica

Entirely DB users

Why this is hard to implement

DB users, with an external auth store

Still hard

Why it’s Worthwhile

16

Tuesday, October 20, 2009

Page 17: Vertically Challenged

Basic Mechanica

Entirely DB users

Why this is hard to implement

DB users, with an external auth store

Still hard

Why it’s Worthwhile

The Hybrid approach

17

The Hybrid approach - use DB roles + rows in the database, instead of DB users.

Tuesday, October 20, 2009

Page 18: Vertically Challenged

DATABASE IMPLEMENTATION

Wherein

18

Basic Componentry

Roles

Stored Procedures

Tuesday, October 20, 2009

Page 19: Vertically Challenged

Componentry

Postgres ImplementationSET ROLEGRANT / NOINHERITNOLOGIN

19

Grant / noinherit - How this works together to provide VC.

Tuesday, October 20, 2009

Page 20: Vertically Challenged

Componentry

Postgres ImplementationSET ROLEGRANT / NOINHERITNOLOGIN

sudo for the Database

20

Great for PCI compliance

Tuesday, October 20, 2009

Page 21: Vertically Challenged

Setup

vc=# CREATE ROLE vc NOINHERIT;CREATE ROLEvc=# CREATE ROLE auth_level NOLOGIN;CREATE ROLEvc=# GRANT auth_level TO vc;GRANT ROLEvc=#

21

Tuesday, October 20, 2009

Page 22: Vertically Challenged

Setup

vc=> CREATE TABLE auth_only();CREATEvc=> REVOKE SELECT ON auth_only FROM PUBLIC, vc;REVOKE vc=> SET ROLE TO vc;SETvc=>

22

Tuesday, October 20, 2009

Page 23: Vertically Challenged

sudo for Databases

vc=> SELECT * FROM auth_only;ERROR: permission denied for relation auth_onlyvc=> SET ROLE auth_level;SETvc=> SELECT * FROM auth_only;--(0 rows)

23

Tuesday, October 20, 2009

Page 24: Vertically Challenged

It’s How We Role

Basic roles

24

Tuesday, October 20, 2009

Page 25: Vertically Challenged

It’s How We Role

Basic roles

Defines your permissions tree

25

Tuesday, October 20, 2009

Page 26: Vertically Challenged

It’s How We Role

Basic Roles

Defines your permissions tree

Privileged Roles

The over-arching Container roles

User, Moderator, Admin, etc.

26

Tuesday, October 20, 2009

Page 27: Vertically Challenged

It’s How We Role

Basic Roles

Defines your permissions tree

Privileged Roles

The over-arching permissions definitions

User, Moderator, Admin, etc.

Entry Point

27

Entry point has LOGIN granted.

Tuesday, October 20, 2009

Page 28: Vertically Challenged

Your users.can do *this*

users.can

28

Tuesday, October 20, 2009

Page 29: Vertically Challenged

A Quick Check

IF users.can(user_id, ‘type.read’) THEN RETURN QUERY SELECT * FROM type;ELSE exceptable.permission_denied(‘User lacks type.read permissions’);*END IF;

* Exceptable function

29

Tuesday, October 20, 2009

Page 30: Vertically Challenged

Wait.. I don’t know you.

users.can

users.validate

30

Tuesday, October 20, 2009

Page 31: Vertically Challenged

User Legitimacy

CREATE OR REPLACE FUNCTION users.validate ( in_username text, in_password text) RETURNS int

SELECT users.validate(‘vc’,‘password’);

31

Tuesday, October 20, 2009

Page 32: Vertically Challenged

The Forge of Users

users.can

users.validate

users.create

32

Tuesday, October 20, 2009

Page 33: Vertically Challenged

Creating Users

CREATE OR REPLACE FUNCTION users.create ( in_username text, in_role text, in_password text) RETURNS int

SELECT users.create(‘user’,‘auth’,md5(‘password’));

33

Tuesday, October 20, 2009

Page 34: Vertically Challenged

Basic User Get

users.can

users.validate

users.create

users.get

34

Tuesday, October 20, 2009

Page 35: Vertically Challenged

Collecting Users

CREATE OR REPLACE FUNCTION users.get ( in_user_id int) RETURNS users.user

CREATE TYPE users.user AS ( id int, username text, roles text[] -- Used for app checks);

35

Tuesday, October 20, 2009

Page 36: Vertically Challenged

STANDING ON PYLONSWherein

36

Exceptions

Permissions checks

Repoze.Who

Tuesday, October 20, 2009

Page 37: Vertically Challenged

Exceptions

Repoze.who cares about 401s and 403s

37

Tuesday, October 20, 2009

Page 38: Vertically Challenged

Exceptions

Repoze.who cares about 401s and 403s

Trap VC errors in the Application

Emit an appropriate 401 or 403

38

Tuesday, October 20, 2009

Page 39: Vertically Challenged

Exceptions

Repoze.who cares about 401s and 403s

Trap VC errors in the Application

Emit an appropriate 401 or 403

Exceptions are handled ONCE

39

Tuesday, October 20, 2009

Page 40: Vertically Challenged

Onceclass BaseController(WSGIController):

def __call__(self, environ, start_response):

"""Invoke the Controller"""

# WSGIController.__call__ dispatches to the Controller method

# the request is routed to.

try:

return WSGIController.__call__(self, environ, start_response)

except PermissionsError, e:

abort(403)

except NoSuchUserError, e:

abort(401)

40

Tuesday, October 20, 2009

Page 41: Vertically Challenged

I have @needs too!

Easily Decorate Pylons Views - @needs(‘type.read’)

41

Tuesday, October 20, 2009

Page 42: Vertically Challenged

I have @needs too!

Easily Decorate Pylons Views - @needs(‘type.read’)

Using DB permissions in the App layer

42

Tuesday, October 20, 2009

Page 43: Vertically Challenged

I have @needs too!

Easily Decorate Pylons Views - @needs(‘type.read’)

Using DB permissions in the App layer

A single DB query - Not a query per object.

43

Tuesday, October 20, 2009

Page 44: Vertically Challenged

I have @needs too!

Easily Decorate Pylons Views - @needs(‘type.read’)

Using DB permissions in the App layer

A single DB query - Not a query per object.

Why? Multiple tiers are good.

44

Tuesday, October 20, 2009

Page 45: Vertically Challenged

Pylons in Repose

The Application Cycle

45

Tuesday, October 20, 2009

Page 46: Vertically Challenged

Pylons in Repose

The Application Cycle

A Basic Repoze Configuration

46

Tuesday, October 20, 2009

Page 47: Vertically Challenged

A Basic Configuration[plugin:form]

use =

repoze.who.plugins.form:make_redirecting_plugin

login_form_url = /account/login

login_handler_path = /account/dologin

logout_handler_path = /account/logout

rememberer_name = auth_tkt

[plugin:auth_tkt]

use = repoze.who.plugins.auth_tkt:make_plugin

secret = ticket_secret

[mdproviders]

plugins = vc.repoze:MetadataProvider;browser

47

Tuesday, October 20, 2009

Page 48: Vertically Challenged

Pylons in Repose

The Application Cycle

A Basic Repoze Configuration

The Permissions Swap

48

Tuesday, October 20, 2009

Page 49: Vertically Challenged

The Permissions Swapclass MetadataProvider(object):

def add_metadata(self, environ, identity):

if userid:

try:

from vc.model import user

u = user.user(userid)

u.become(u.role) except PermissionError, e:

if “model” in identity:

del identity[“model”]

return None

identity[“model”] = u

49

Tuesday, October 20, 2009

Page 50: Vertically Challenged

And the ProcedureCREATE FUNCTION users.become (

in_user_id int, in_role text

) RETURNS VOID AS $$

BEGIN

IF users.can($1, $2) THEN

SET ROLE TO quote_literal($2);

ELSE

except.permission_denied('User cannot become specified permission level');

END IF;

END;

$$ LANGUAGE PLPGSQL;

50

Tuesday, October 20, 2009

Page 51: Vertically Challenged

Pylons in Repose

The Application Cycle

A Basic Repoze Configuration

The Permissions Swap

Anonymous Access

51

Tuesday, October 20, 2009

Page 52: Vertically Challenged

Anonymous Access

A Wrapping Identifier

Test For an Identity

52

Tuesday, October 20, 2009

Page 53: Vertically Challenged

Wrap the Identifierclass AnonymousWrapper(object):

def identify(self, environ):

i = environ['repoze.who.plugins']['auth_tkt'].identify(environ)

if i:

# Not anonymous

return i

else:

# Is anonymous

# return a really basic anonymousness.

return {"repoze.who.userid":

anonymous_user_id}

53

Tuesday, October 20, 2009

Page 54: Vertically Challenged

Anonymous Access

A Wrapping Identifier

Test For an Identity

Return a User, or Anonymous

Anonymous users are otherwise identical

54

Tuesday, October 20, 2009

Page 55: Vertically Challenged

ADVANTAGES, DISADVANTAGES,

VULNERABILITIESWherein

55

Advantages

Disadvantages

Vulnerabilities

Tuesday, October 20, 2009

Page 56: Vertically Challenged

Advantages

Same roles in your Procedures, as your Application code

Great for consistency

Can Implement Row-level Permissions

56

Tuesday, October 20, 2009

Page 57: Vertically Challenged

Row-Level Example

PERFORM id FROM table WHERE id = i_id;IF FOUND THENPERFORM owner_id FROM table WHERE owner_id = user_id AND id = i_id;IF NOT FOUND THEN exceptable.permission_denied(‘User does not own specified record.’);END IF;

END IF;

57

Tuesday, October 20, 2009

Page 58: Vertically Challenged

Disadvantages

No PG-backed row-level authorization

58

Tuesday, October 20, 2009

Page 59: Vertically Challenged

Disadvantages

No PG-backed row-level authorization

DDL permissions take work to implement

59

Tuesday, October 20, 2009

Page 60: Vertically Challenged

Disadvantages

No direct row-level authorization

DDL permissions take work to implement

No ad-hoc Users

60

Tuesday, October 20, 2009

Page 61: Vertically Challenged

Disadvantages

No direct row-level authorization

DDL permissions take work to implement

No ad-hoc Users

Custom permissions require new entry points

61

Tuesday, October 20, 2009

Page 62: Vertically Challenged

Vulnerabilities

Any possible PG escalation attack

SET ROLE could switch to an Admin user

User errors a perennial favourite

62

Tuesday, October 20, 2009

Page 63: Vertically Challenged

AND THUSQuestions?

63

Tuesday, October 20, 2009

Page 64: Vertically Challenged

GET IT

https://projects.commandprompt.com/public/verticallychallenged

64

Tuesday, October 20, 2009

Page 65: Vertically Challenged

THANK YOU!

65

Tuesday, October 20, 2009