security for microservices with spring and oauth2

44
Security for Microservices with Spring Dave Syer, 2012 Twitter: @david_syer Email: [email protected] (Security for Microservices with Spring) http://presos.dsyer.com/decks/microservice-securi... 1 of 44 24/09/14 21:50

Upload: spring-io

Post on 14-Jun-2015

4.251 views

Category:

Software


4 download

DESCRIPTION

Speaker: Dr. Dave Syer Web / JavaScript Track OAuth2 is a lightweight security protocol that is well-suited for use with HTTP, the protocol at the heart of many modern architectures. Spring Security OAuth2 has a load of new features, not the least of which being the `@Configuration` support in version 2.0. Combine these with Spring Boot and you have a platform which can get you a secure HTTP service application in about 20 lines of code. This presentation shows how the combination of rapid development and production-ready features in the modern Spring stack are a perfect mixture for developing secure components in a system composed of microservices. We explore the new features in Spring OAuth2, guide you through the choice of which to use and when, and show how easy they are to enable quickly.

TRANSCRIPT

Page 1: Security for Microservices with Spring and OAuth2

Security for Microservices with SpringDave Syer, 2012Twitter: @david_syerEmail: [email protected]

(Security for Microservices with Spring)

http://presos.dsyer.com/decks/microservice-securi...

1 of 44 24/09/14 21:50

Page 2: Security for Microservices with Spring and OAuth2

Agenda

What is a Microservice?

How would it be secure?

If I was going to use Spring how would that look?

What's the easiest way to get something working?

http://presos.dsyer.com/decks/microservice-securi...

2 of 44 24/09/14 21:50

Page 3: Security for Microservices with Spring and OAuth2

Introduction

There is a strong trend in distributed systems withlightweight architectures

People have started to call them "microservices"

So what are people doing about security in such systems?

http://presos.dsyer.com/decks/microservice-securi...

3 of 44 24/09/14 21:50

Page 4: Security for Microservices with Spring and OAuth2

What is a Microservice?

HTTP transport (usually).

Text-based message content, usually JSON.

Small, compact messages, and quick responses.

REST-ful, or at least inspired by the REST

Some degree of statelessness

Interoperability.

http://presos.dsyer.com/decks/microservice-securi...

4 of 44 24/09/14 21:50

Page 5: Security for Microservices with Spring and OAuth2

What Are the SecurityRequirements

Stop bad guys from accessing your resources

Identity and permissions:

How is identity and permission information conveyed to aservice?

How is it decoded and interpreted?

What data are needed to make the access decision (useraccounts, roles, ACLs etc.)?

How is the data managed: who is responsible for storingand retrieving it?

How can you verify that the request hasn't been tamperedwith?

http://presos.dsyer.com/decks/microservice-securi...

5 of 44 24/09/14 21:50

Page 6: Security for Microservices with Spring and OAuth2

HTTP BasicAuthentication

Something of a lowest common denominator

Supported on practically all servers natively and out of thebox

Ubiquitous support on the client side in all languages

Good support in Spring Security

Spring Boot autoconfigures it out of the box

Example:

$ curl "https://$username:$password@myhost/resource"

http://presos.dsyer.com/decks/microservice-securi...

6 of 44 24/09/14 21:50

Page 7: Security for Microservices with Spring and OAuth2

Simple Service

@Grab('spring-boot-starter-security')@RestControllerclass Application {

@RequestMapping("/") def home() { [status: 'OK'] }

}

http://presos.dsyer.com/decks/microservice-securi...

7 of 44 24/09/14 21:50

Page 8: Security for Microservices with Spring and OAuth2

So what's wrong withthat?

Nothing, but...

Where do you get the credentials (the username andpassword)?

Fine for systems where all participants can share secretssecurely

In practice that means small systems

Only supports username/password

Only covers authentication

No distinction between users and machines

http://presos.dsyer.com/decks/microservice-securi...

8 of 44 24/09/14 21:50

Page 9: Security for Microservices with Spring and OAuth2

Network Security

Microservice endpoints not visible outside an internalnetwork

Very secure

Very flexible (e.g. using virtual networks)

Suits architecture based on "edge" server

http://presos.dsyer.com/decks/microservice-securi...

9 of 44 24/09/14 21:50

Page 10: Security for Microservices with Spring and OAuth2

So what's wrong withthat?

Nothing, but...

Annoying to debug and a little bit fiddly to maintain

Configuration is out of the control of developers in manycases for organizational reasons

There's no identity or authentication

http://presos.dsyer.com/decks/microservice-securi...

10 of 44 24/09/14 21:50

Page 11: Security for Microservices with Spring and OAuth2

Certificate BasedSecurity

Set up SSL on server so that request has to containcertificate

Spring Security (X.509) can turn that into anAuthentication

If SSL is in the service layer (i.e. not at therouter/loadbalancer) then you have a keystore anyway

Example:

$ curl -k --cert rod.pem:password https://localhost:8443/hello

http://presos.dsyer.com/decks/microservice-securi...

11 of 44 24/09/14 21:50

Page 12: Security for Microservices with Spring and OAuth2

So what's wrong withthat?

Nothing, but...

There's no user identity (only at most the machine) unlessbrowsers have certificates installed

Requires keystores and certificates in all applications andservices (significantly non-trivial if done properly, but someorganizations require it anyway)

No fine distinction between users and machines

http://presos.dsyer.com/decks/microservice-securi...

12 of 44 24/09/14 21:50

Page 13: Security for Microservices with Spring and OAuth2

Custom AuthenticationToken

Random identifier per authentication

Grant them from a central service and/or store in a centraldatabase

Can be exposed directly via developer UI

Re-hydrate authentication in service

Spring SecurityAbstractPreAuthenticatedProcessingFilterand friends

Even easier: Spring Session identifier

Github example: token is temporary password in HTTPBasic

http://presos.dsyer.com/decks/microservice-securi...

13 of 44 24/09/14 21:50

Page 14: Security for Microservices with Spring and OAuth2

So what's wrong withthat?

Nothing, but...

No separation of client app from user authentication

Coarse grained authorization (all tokens activate allresources)

It's not a "standard" (but there are ready madeimplementations)

For user authentication, need to collect user credentials inapp

http://presos.dsyer.com/decks/microservice-securi...

14 of 44 24/09/14 21:50

Page 15: Security for Microservices with Spring and OAuth2

OAuth2 Key Features

Extremely simple for clients

Access tokens carry information (beyond identity)

Resources are free to interpret token content

http://presos.dsyer.com/decks/microservice-securi...

15 of 44 24/09/14 21:50

Page 16: Security for Microservices with Spring and OAuth2

So what's wrong withthat?

Nothing, but...

No standard (yet) for request signing

http://presos.dsyer.com/decks/microservice-securi...

16 of 44 24/09/14 21:50

Page 17: Security for Microservices with Spring and OAuth2

Quick Introduction toOAuth2

A Client application, often web application, acts on behalf ofa User, but with the User's approval

Authorization Server

Resource Server

Client application

Common examples of Authorization Servers and ResourceServers on the internet:

Facebook - Graph API

Google - Google APIs

Cloud Foundry - Cloud Controller

http://presos.dsyer.com/decks/microservice-securi...

17 of 44 24/09/14 21:50

Page 18: Security for Microservices with Spring and OAuth2

OAuth2 Bearer Tokens

Centralizing account management and permissions:

OAuth 2.0 adds an extra dimension - more information forthe access decision

Standards always help in security

Lightweight - easy to curl

Requires HTTPS for secure operation, but you can testwith HTTP

http://presos.dsyer.com/decks/microservice-securi...

18 of 44 24/09/14 21:50

Page 19: Security for Microservices with Spring and OAuth2

OAuth2 and theMicroservice

Example command line Client:

$ curl -H "Authorization: Bearer $TOKEN" https://myhost/resource

https://myhost is a Resource Server

TOKEN is a Bearer Token

it came from an Authorization Server

http://presos.dsyer.com/decks/microservice-securi...

19 of 44 24/09/14 21:50

Page 20: Security for Microservices with Spring and OAuth2

Simple AuthorizationServer

@EnableAuthorizationServerclass AuthorizationServer extendsAuthorizationServerConfigurerAdapter {

@Override void configure(ClientDetailsServiceConfigurerclients) throws Exception { clients.inMemory() .withClient("my-client-with-secret")... }

}

http://presos.dsyer.com/decks/microservice-securi...

20 of 44 24/09/14 21:50

Page 21: Security for Microservices with Spring and OAuth2

Example token contents

Client id

Resource id (audience)

Issuer

User id

Role assignments

http://presos.dsyer.com/decks/microservice-securi...

21 of 44 24/09/14 21:50

Page 22: Security for Microservices with Spring and OAuth2

JWT Bearer Tokens

OAuth 2.0 tokens are opaque to clients

But they carry important information to Resource Servers

Example of implementation (from Cloud Foundry UAA,JWT = signed, base64-encoded, JSON):

{ "client_id":"cf", "exp":1346325625, "scope":["cloud_controller.read","openid","password.write"], "aud":["openid","cloud_controller","password"], "iss": "https://login.run.pivotal.io", "user_name":"[email protected]", "user_id":"52147673-9d60-4674-a6d9-225b94d7a64e", "email":"[email protected]", "jti":"f724ae9a-7c6f-41f2-9c4a-526cea84e614" }

http://presos.dsyer.com/decks/microservice-securi...

22 of 44 24/09/14 21:50

Page 23: Security for Microservices with Spring and OAuth2

JWT Authorization Server

@EnableAuthorizationServerclass AuthorizationServer extendsAuthorizationServerConfigurerAdapter {

@Bean public JwtAccessTokenConverteraccessTokenConverter() { return new JwtAccessTokenConverter(); }

@Override public voidconfigure(AuthorizationServerEndpointsConfigurerendpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .accessTokenConverter(accessTokenConverter()); }

... // client config}

http://presos.dsyer.com/decks/microservice-securi...

23 of 44 24/09/14 21:50

Page 24: Security for Microservices with Spring and OAuth2

User Approvals

An access token represents a user approval:

http://presos.dsyer.com/decks/microservice-securi...

24 of 44 24/09/14 21:50

Page 25: Security for Microservices with Spring and OAuth2

User Approvals as Token

An access token represents a user approval:

http://presos.dsyer.com/decks/microservice-securi...

25 of 44 24/09/14 21:50

Page 26: Security for Microservices with Spring and OAuth2

Formal Model for UserApprovals

It can be an advantage to store individual approvalsindependently (e.g. for explicit revokes of individual scopes):

http://presos.dsyer.com/decks/microservice-securi...

26 of 44 24/09/14 21:50

Page 27: Security for Microservices with Spring and OAuth2

Spring OAuth Support

@EnableAuthorizationServerclass AuthorizationServer extendsAuthorizationServerConfigurerAdapter {

@Override public voidconfigure(AuthorizationServerEndpointsConfigurerendpoints) throws Exception { endpoints.tokenStore(tokenStore()); }

@Bean public ApprovalStore approvalStore() throwsException { TokenApprovalStore store = newTokenApprovalStore(); store.setTokenStore(tokenStore()); return store; }

... // client config and tokenStore() defined here}

http://presos.dsyer.com/decks/microservice-securi...

27 of 44 24/09/14 21:50

Page 28: Security for Microservices with Spring and OAuth2

Simple Resource Server

@EnableResourceServerclass ResourceServer { @Bean TokenStore tokenStore() throws Exception { ... }}

or

@EnableResourceServerclass ResourceServer { @Bean ResourceServerTokenServices tokenServices() throwsException { ... }}

http://presos.dsyer.com/decks/microservice-securi...

28 of 44 24/09/14 21:50

Page 29: Security for Microservices with Spring and OAuth2

Simple Client Application

@EnableOAuth2Clientclass ClientApplication {

@Autowired // session-scoped private OAuth2ClientContext clientContext

@Bean OAuth2RestOperations restTemplate() { new OAuth2RestTemplate(resource, clientContext) }

...

}

http://presos.dsyer.com/decks/microservice-securi...

29 of 44 24/09/14 21:50

Page 30: Security for Microservices with Spring and OAuth2

Authorization Code GrantSummary

Authorization Server authenticates the User1.

Client starts the authorization flow and obtain User'sapproval

2.

Authorization Server issues an authorization code(opaque one-time token)

3.

Client exchanges the authorization code for an accesstoken.

4.

http://presos.dsyer.com/decks/microservice-securi...

30 of 44 24/09/14 21:50

Page 31: Security for Microservices with Spring and OAuth2

Role of Resource Server

Extract token from request and decode it1.

Make access control decision

Scope

Audience

User account information (id, roles etc.)

Client information (id, roles etc.)

2.

Send 403 (FORBIDDEN) if token not sufficient3.

http://presos.dsyer.com/decks/microservice-securi...

31 of 44 24/09/14 21:50

Page 32: Security for Microservices with Spring and OAuth2

Role of the AuthorizationServer

Grant tokens1.

Interface for users to confirm that they authorize the Clientto act on their behalf

2.

Authenticate users (/authorize)3.

Authenticate clients (/token)4.

#1 and #4 are covered thoroughly by the spec; #2 and #3 not(for good reasons).

http://presos.dsyer.com/decks/microservice-securi...

32 of 44 24/09/14 21:50

Page 33: Security for Microservices with Spring and OAuth2

Client Registration andScopes

For secure channels a client has to authenticate itself to obtaina token, so it has to be known to the Authorization Server.Registration provides at a mimimum:

authentication (shared secret)

registered redirect URI (optional but essential to preventattacks)

allowed scopes (clients are not permitted access to allresources)

Also useful:

a way to identify which resources can be accessed

ownership information (which user registered the client)

http://presos.dsyer.com/decks/microservice-securi...

33 of 44 24/09/14 21:50

Page 34: Security for Microservices with Spring and OAuth2

More on Scopes

Per the spec they are arbitrary strings. The AuthorizationServer and the Resource Servers agree on the content andmeanings.

Examples:

Google: https://www.googleapis.com/auth/userinfo.profile

Facebook: email, read_stream, write_stream

UAA: cloud_controller.read,cloud_controller.write, scim.read, openid

Authorization Server has to decide whether to grant a token toa given client and user based on the requested scope (if any).

http://presos.dsyer.com/decks/microservice-securi...

34 of 44 24/09/14 21:50

Page 35: Security for Microservices with Spring and OAuth2

Authentication and theAuthorization Server

Authentication (checking user credentials) is orthogonal toauthorization (granting tokens)

They don't have to be handled in the same component ofa large system

Authentication is often deferred to existing systems (SSO)

Authorization Server has to be able to authenticate theOAuth endpoints (/authorize and /token)

It does not have to collect credentials (except forgrant_type=password)

http://presos.dsyer.com/decks/microservice-securi...

35 of 44 24/09/14 21:50

Page 36: Security for Microservices with Spring and OAuth2

OAuth2 and theMicroservice

Resource Servers might be microservices

Web app clients: authorization code grant

Browser clients (single page app): authorization codegrant (better) or implicit grant

Mobile and non-browser clients: password grant (maybewith mods for multifactor etc.)

Service clients (intra-system): client credentials or relayuser token

http://presos.dsyer.com/decks/microservice-securi...

36 of 44 24/09/14 21:50

Page 37: Security for Microservices with Spring and OAuth2

Single Page Apps

With backend services CORS restrictions make reverse proxyuseful (@EnableZuulProxy). Then you can acquire tokens inthe client app and relay them to back end.

With no backend services, don't be shy, use the session(authorization code flow is vastly superior).

Spring Session helps a lot.

http://presos.dsyer.com/decks/microservice-securi...

37 of 44 24/09/14 21:50

Page 38: Security for Microservices with Spring and OAuth2

Relaying User Tokens

Front end app sends SSO token with user credentials toauthenticate back end requests, back ends just relay it toeach other as necessary.

Simple but possibly flawed: the front end only needs access touser details to authenticate, but you need to give it permissionto do other things to allow it access to the back ends.

Idea: exchange (with full authentication) the incoming token foran outgoing one with different permissions (client but notscope). Can use password grant (e.g. with the incoming tokenas a password).

http://presos.dsyer.com/decks/microservice-securi...

38 of 44 24/09/14 21:50

Page 39: Security for Microservices with Spring and OAuth2

OAuth 1.0

Another (slightly older) standard

Includes request signing

Common in early wave public APIs (e.g. Twitter)

Spring Security OAuth is full solution at framework level

http://presos.dsyer.com/decks/microservice-securi...

39 of 44 24/09/14 21:50

Page 40: Security for Microservices with Spring and OAuth2

So What's Wrong withThat?

Nothing but...

It's hard work for client app developers (crypto)

(Notionally at least) superseded by OAuth2

http://presos.dsyer.com/decks/microservice-securi...

40 of 44 24/09/14 21:50

Page 41: Security for Microservices with Spring and OAuth2

SAML Assertions

Another standard with similar features to OAuth2

XML based

Common infrastructure in enterprise

Spring Security SAML provides SP and Consumer roles(not IDP)

Request signing is standadized

http://presos.dsyer.com/decks/microservice-securi...

41 of 44 24/09/14 21:50

Page 42: Security for Microservices with Spring and OAuth2

So What's Wrong withThat?

Nothing but...

Painful to set up for servers and client

Large amounts of XML data in HTTP headers

Huge complexity for developers

N.B. exchanging SAML assertion for OAuth2 token is quotenormal

http://presos.dsyer.com/decks/microservice-securi...

42 of 44 24/09/14 21:50

Page 43: Security for Microservices with Spring and OAuth2

In Conclusion

Lightweight services demand lightweight infrastructure

Security is important, but should be unobtrusive

Spring Security makes it all easier

Special mention for Spring Session

OAuth 2.0 is a standard, and has a lot of useful features

Spring Security OAuth aims to be a complete OAuth2solution at the framework level

Cloudfoundry has an open source, OAuth2 identityservice (UAA)

http://presos.dsyer.com/decks/microservice-securi...

43 of 44 24/09/14 21:50

Page 44: Security for Microservices with Spring and OAuth2

Links

http://github.com/spring-projects/spring-security-oauth

http://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation

http://github.com/cloudfoundry/uaa

http://blog.spring.io

http://blog.cloudfoundry.org

http://presos.dsyer.com/decks/microservice-security.html

Twitter: @david_syer

Email: [email protected]

http://presos.dsyer.com/decks/microservice-securi...

44 of 44 24/09/14 21:50