next generation devops in drupal: drupalcamp london 2014

103
Dev Ops: the next generation

Upload: barney-hanlon

Post on 08-May-2015

2.132 views

Category:

Technology


5 download

DESCRIPTION

In this talk, Barney will be discussing and demonstrating how to: - Use nginx, Varnish and Apache together in a "SPDY sandwich" to support HTTP 2.0 - Setting up SSL properly to mitigate against attack vectors - Performance improvements with mod_pagespeed and nginx - Deploying Drupal sites with Docker containers Barney is a Technical Team Leader at Inviqa, a Drupal Association member and writes for Techportal on using technologies to improve website performance. He first started using PHP professionally in 2003, and has over seventeen years experience in software development. He is an advocate of Scrum methodology and has an interest in performance optimization, researching and speaking on various techniques to improve user experience through faster load times.

TRANSCRIPT

Page 1: Next Generation DevOps in Drupal: DrupalCamp London 2014

Dev Ops:the next generation

Page 2: Next Generation DevOps in Drupal: DrupalCamp London 2014

Warning!meme and star trek heavy

Page 3: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 4: Next Generation DevOps in Drupal: DrupalCamp London 2014

Who is this TREKKIE

Barney Hanlon

Technical Team Leader for Inviqa

@shrikeh

Page 5: Next Generation DevOps in Drupal: DrupalCamp London 2014

We’re hiring!

Page 6: Next Generation DevOps in Drupal: DrupalCamp London 2014

The story so far

(Yes I know that’s Star Wars)

Dev ops

Page 7: Next Generation DevOps in Drupal: DrupalCamp London 2014

DevOps

DevelopersSysadmins

Page 8: Next Generation DevOps in Drupal: DrupalCamp London 2014

“Get the code on the server”

Page 9: Next Generation DevOps in Drupal: DrupalCamp London 2014

FTP CVS

Subversion Git

Page 10: Next Generation DevOps in Drupal: DrupalCamp London 2014

What about all this OTHER stuff?

• Setting up Virtual hosts

• SSH access

• Log rotation

• Staging servers

• Build pipelines

• SSL certificates

• Minification

• User management

• File permissions

• Firewall rules

• Patching

• Concurrency

• Failover

• Alerting

Page 11: Next Generation DevOps in Drupal: DrupalCamp London 2014

Developers need to know more than how to push to

Github.

Page 12: Next Generation DevOps in Drupal: DrupalCamp London 2014

FIVE STAR DevOps

Page 13: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 14: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 15: Next Generation DevOps in Drupal: DrupalCamp London 2014

The Five Stars of DevOps

Page 16: Next Generation DevOps in Drupal: DrupalCamp London 2014

The Five Stars of DevOps

• Monitoring

Page 17: Next Generation DevOps in Drupal: DrupalCamp London 2014

The Five Stars of DevOps

• Monitoring

• Security

Page 18: Next Generation DevOps in Drupal: DrupalCamp London 2014

The Five Stars of DevOps

• Monitoring

• Security

• Performance

Page 19: Next Generation DevOps in Drupal: DrupalCamp London 2014

The Five Stars of DevOps

• Monitoring

• Security

• Performance

• Automation

Page 20: Next Generation DevOps in Drupal: DrupalCamp London 2014

The Five Stars of DevOps

• Monitoring

• Security

• Performance

• Automation

• Scaleability

Page 21: Next Generation DevOps in Drupal: DrupalCamp London 2014

Am I doing something in my application that is done better by the infrastructure or an external

service?

Page 22: Next Generation DevOps in Drupal: DrupalCamp London 2014

Probably.

Page 23: Next Generation DevOps in Drupal: DrupalCamp London 2014

Got root?

Page 24: Next Generation DevOps in Drupal: DrupalCamp London 2014

Common reasons to “just log onto the server quickly”

• server logs in /var/log require privileges to tail

• setting permissions on directories

• Processes require restarting

Page 25: Next Generation DevOps in Drupal: DrupalCamp London 2014

Monitoring

Page 26: Next Generation DevOps in Drupal: DrupalCamp London 2014

Monitoring

• Log EVERYTHING

• Drupal default visitor logging is heavy

• Should you be writing to the database to log visits?

Page 27: Next Generation DevOps in Drupal: DrupalCamp London 2014

NO.

Page 28: Next Generation DevOps in Drupal: DrupalCamp London 2014

Monitoring

• Logging is only one part of monitoring

• Send your Web logs to a remote service

• Set error_log to syslog in php.ini

Page 29: Next Generation DevOps in Drupal: DrupalCamp London 2014

Logging Services

• SplunkStorm

• Loggly

• Logentries

• Papertrailapp

Page 30: Next Generation DevOps in Drupal: DrupalCamp London 2014

HOSTING YOUR OWN LOGGING

• Splunk

• GrayLog2

• Sensu

• Munin

• Raven

Page 31: Next Generation DevOps in Drupal: DrupalCamp London 2014

Other monitoring

• Nagios

• Pingdom

• New Relic

• Piwik/Google Analytics

Page 32: Next Generation DevOps in Drupal: DrupalCamp London 2014

Profiling

• Don’t be afraid to turn XHProf on in live occasionally

• Regularly check your browser HAR

• Check APC and other caches for smells

Page 33: Next Generation DevOps in Drupal: DrupalCamp London 2014

Security

Page 34: Next Generation DevOps in Drupal: DrupalCamp London 2014

Where is the risk?

• Application security

• Infrastructure security

• End user security

Page 35: Next Generation DevOps in Drupal: DrupalCamp London 2014

Repelling Unwelcome Guests

Page 36: Next Generation DevOps in Drupal: DrupalCamp London 2014

Tools to help

• JumpCloud

• DuoSecurity

• Ubuntu ACL

Page 37: Next Generation DevOps in Drupal: DrupalCamp London 2014

Capturing Morpheus…

Not so bad.

Page 38: Next Generation DevOps in Drupal: DrupalCamp London 2014

Hardening SSL

Page 39: Next Generation DevOps in Drupal: DrupalCamp London 2014

Don’t bother hardening SSL

Page 40: Next Generation DevOps in Drupal: DrupalCamp London 2014

SSL is Dead, Long Live TLS

• No one should be using SSL any more.

• Transport Layer Security (TLS)

• Latest version 1.2

Page 41: Next Generation DevOps in Drupal: DrupalCamp London 2014

Vulnerabilities

• BEAST Attack

• CRIME Attack

• Lucky Thirteen

Page 42: Next Generation DevOps in Drupal: DrupalCamp London 2014

HTTPS without proper ciphers gives the illusion of security while providing none

Page 43: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 44: Next Generation DevOps in Drupal: DrupalCamp London 2014

ssl_protocols SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;

SSLProtocol all SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5

Nginx

Apache 2Supporting SSLv3 is only required for IE6

Open to Lucky Thirteen attack

Default SSL implementations

Page 45: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 46: Next Generation DevOps in Drupal: DrupalCamp London 2014

“Today, only TLS 1.2 with GCM suites offer fully robust security. All other suites suffer from one problem or another (e.g, RC4, Lucky 13, BEAST), but most are difficult to exploit in practice…”

–Ivan Ristic, Qualys

Page 47: Next Generation DevOps in Drupal: DrupalCamp London 2014

“…Because GCM suites are not yet widely supported, most communication today is carried out using one of the slightly flawed cipher suites. It is not possible to do better if you're running a public web site.”

–Ivan Ristic, Qualys

Page 48: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 49: Next Generation DevOps in Drupal: DrupalCamp London 2014

Diffie-Hellman Key Exchange

• Diffie-Hellman (DH) and Elliptic Curve Diffie-Hellman (ECDH)

• Allows Perfect Forward Secrecy

• Slow :(

Page 50: Next Generation DevOps in Drupal: DrupalCamp London 2014

server { … ! add_header Strict-Transport-Security “max-age=31536000; includeSubDomains"; ssl_session_cache shared:SSL:10m; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS; ssl_ecdh_curve secp521r1; }

https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/

Page 51: Next Generation DevOps in Drupal: DrupalCamp London 2014

https://www.ssllabs.com/ssltest/

Test your strength!

Page 52: Next Generation DevOps in Drupal: DrupalCamp London 2014

–No user ever

“I don’t mind that the site is slow, at least my data is safe.”

Page 53: Next Generation DevOps in Drupal: DrupalCamp London 2014

Performance

Page 54: Next Generation DevOps in Drupal: DrupalCamp London 2014

SPDY

Page 55: Next Generation DevOps in Drupal: DrupalCamp London 2014

• Draft HTTP 2.0

• Allows multiplexing a single connection

• Requires HTTPS

• Do you need HTTP at all?

SPDY

Page 56: Next Generation DevOps in Drupal: DrupalCamp London 2014

PageSpeed

• Library for Apache and Nginx

• Automatic minification of JavaScript, CSS and HTML

• On-the-fly optimisations based on chosen filters

Page 57: Next Generation DevOps in Drupal: DrupalCamp London 2014

The “SPDY Sandwich”

Nginx Varnish Nginx PHP-FPM

Page 58: Next Generation DevOps in Drupal: DrupalCamp London 2014

The “SPDY Sandwich”

Nginx Varnish Nginx PHP-FPM

Responsible for: • SPDY / SSL Termination • Serving static assets • Gzipping • Pagespeed is user agent-aware

Page 59: Next Generation DevOps in Drupal: DrupalCamp London 2014

The “SPDY Sandwich”

Nginx Varnish Nginx PHP-FPM

Responsible for: • Caching dynamic pages • Cookie normalisation

Page 60: Next Generation DevOps in Drupal: DrupalCamp London 2014

The “SPDY Sandwich”

Nginx Varnish Nginx PHP-FPM

Responsible for: • Serving dynamic pages • Generic Pagespeed optimisations

Page 61: Next Generation DevOps in Drupal: DrupalCamp London 2014

The “SPDY Sandwich”

Nginx Varnish Nginx PHP-FPM

Responsible for: • PHP interpreter

Page 62: Next Generation DevOps in Drupal: DrupalCamp London 2014

Cookies

Page 63: Next Generation DevOps in Drupal: DrupalCamp London 2014

Cross Site Request Forgery (CSRF)

• OWASP recommendation

• Requires a token in the form and a session token

• Breaks most reverse proxies without configuration

Page 64: Next Generation DevOps in Drupal: DrupalCamp London 2014

Am I doing something in my application that is done better by the infrastructure or an external

service?

Page 65: Next Generation DevOps in Drupal: DrupalCamp London 2014

OpenResty

• Nginx bundle

• Has modules for connecting to Redis, Drizzle, memcached and many more

• Has Lua to allow pre and post processing on requests and responses

Page 66: Next Generation DevOps in Drupal: DrupalCamp London 2014

Time for a Simplified Example

Page 67: Next Generation DevOps in Drupal: DrupalCamp London 2014

<?php !namespace Inviqa\DrupalCamp\Access; !class OpenRestyTokenGenerator implements CsrfTokenGeneratorInterface { private $token; ! public function __construct($csrfToken) { $this->token = $csrfToken; } ! public function get($value = '') { return $this->token; } }

Page 68: Next Generation DevOps in Drupal: DrupalCamp London 2014

<?php namespace Inviqa\DrupalCamp\Form; !use \Drupal\Core\Form\FormBuilder as CoreFormBuilder; !class FormBuilder extends CoreFormBuilder { public function __construct( ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface$key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, TranslationInterface $translation_manager, CsrfTokenGeneratorInterface $csrf_token = NULL, HttpKernel $http_kernel = NULL ) { …

Page 69: Next Generation DevOps in Drupal: DrupalCamp London 2014

OpenResty Varnish Nginx PHP-FPM

X-CSRF-Tokenize: “[[CSRF Here]]”

Redis

FIXING CSRF WITH LUA

Page 70: Next Generation DevOps in Drupal: DrupalCamp London 2014

OpenResty Varnish Nginx PHP-FPM

Header cached in Varnish

Redis

FIXING CSRF WITH LUA

Page 71: Next Generation DevOps in Drupal: DrupalCamp London 2014

OpenResty Varnish Nginx PHP-FPM

Redis

• Parses response (regex)!• Finds token placeholder!• Replaces with real token

FIXING CSRF WITH LUA

Page 72: Next Generation DevOps in Drupal: DrupalCamp London 2014

! ! header_filter_by_lua!! ! '!! ! if ngx.var.upstream_http_x_csrf_tokenize then!! ! ! -- the backend requested a CSRF token be set!! ! ! local csrf_cookie_token = nil!! ! ! if ngx.var.cookie_csrf then!! ! ! ! -- they have a cookie, just re-use it!! ! ! ! local csrf_cookie_token = ngx.var.cookie_csrf!! ! ! end!!! ! ! local resty_random = require "resty.random"!! ! ! local str = require "resty.string"!!! ! ! if not csrf_cookie_token then!! ! ! ! -- no valid csrf cookie found, let us make one!! ! ! ! !! ! ! ! local cookie_random = resty_random.bytes(16,true)!!! ! ! ! while cookie_random == nil do!! ! ! ! ! -- attempt to generate 16 bytes of!! !! ! -- cryptographically strong (enough) random data!! !! ! cookie_random = resty_random.bytes(16,true)!! ! ! ! end!!! ! ! ! ngx.var.csrf_cookie_token = str.to_hex(cookie_random)!

Page 73: Next Generation DevOps in Drupal: DrupalCamp London 2014

OpenResty Varnish Nginx PHP-FPM

Redis

• Stores HMAC as value in Redis with random key “csrf_”

• Generates cookie with Redis key as value

FIXING CSRF WITH LUA

Page 74: Next Generation DevOps in Drupal: DrupalCamp London 2014

!! ! -- we are about to mess around with the content of the page!! ! -- so we need to clear this as it will be wrong!! ! ngx.header.Content_Length = ""! ! ! !! ! -- set the Cookie for the CSRF token!! ! ngx.header.Set_Cookie = "csrf=" .. ngx.var.csrf_cookie_token!! ! ngx.header.tokenize = ngx.var.upstream_http_x_csrf_tokenize!!! ! -- now generate one for the form token!! ! while form_random == nil do!! !! form_random = resty_random.bytes(16,true)!! ! end!!! ! ngx.var.csrf_form_token = str.to_hex(form_random)!!! ! local redis = require "redis"!! ! local client = redis.connect("127.0.0.1", 6379)! !! ! client:set("csrf_" .. ngx.var.csrf_cookie_token, ngx.var.csrf_form_token)!! ! end!

Page 75: Next Generation DevOps in Drupal: DrupalCamp London 2014

OpenResty Varnish Nginx PHP-FPM

Redis

FIXING CSRF WITH LUA

Check token on way back in

Page 76: Next Generation DevOps in Drupal: DrupalCamp London 2014

location @backend {!! ! # You can't set variables in nginx dynamically, ! # so set this up as empty first!! ! set $csrf_validate "";!! ! access_by_lua !! ! '! if ngx.req.get_method() == "POST" then! !-- set up forbidden as default! !ngx.var.csrf_validate = ngx.HTTP_FORBIDDEN! !if ngx.var.cookie_csrf then!! ! ! ! local res = ngx.location.capture("/validate-csrf")!! ! ! ! if ngx.HTTP_OK == res.status then!! ! ! ! ngx.req.read_body()!! !! ! local args = ngx.req.get_post_args()!! !! ! local posted_token = tostring(args["csrf"])!! !! ! if posted_token == res.body then!! !! ! ! ngx.var.csrf_validate = ngx.HTTP_OK!! !! ! end!! !! end!! !end! !end! !';

Page 77: Next Generation DevOps in Drupal: DrupalCamp London 2014

Full gist:https://gist.github.com/shrikeh/4722427

Page 78: Next Generation DevOps in Drupal: DrupalCamp London 2014

Automation

Page 79: Next Generation DevOps in Drupal: DrupalCamp London 2014

Tooling

• Bash scripts (!)

• Chef/Puppet (retro)

• Ansible!

Page 80: Next Generation DevOps in Drupal: DrupalCamp London 2014

Ansible

• Requires no agent!

• Pure SSH

• Modules

• YAML-based configuration

• Playbooks (and playbooks of playbooks)

Page 81: Next Generation DevOps in Drupal: DrupalCamp London 2014

Ansible PLAYBOOK!- name: ensure SSH key exists digital_ocean: > state=present command=ssh name=case - name: ensure droplet exists digital_ocean: > state=present ssh_key_ids=57705 name={{ inventory_hostname }} size_id=66 region_id=4 image_id=1505699 wait_timeout=500 private_networking=yes virtio=yes wait=yes unique_name=yes wait_timeout=500 register: launched - debug: msg="IP is {{ launched.droplet.id }}" - debug: msg="IP is {{ launched.droplet.ip_address }}"

Page 82: Next Generation DevOps in Drupal: DrupalCamp London 2014

Ansible

ansible-playbook base.yml -vvv -i "hosts/production"

Page 83: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 84: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 85: Next Generation DevOps in Drupal: DrupalCamp London 2014

Configuring Your Application

Page 86: Next Generation DevOps in Drupal: DrupalCamp London 2014

<?php # /sites/default/settings.php ... $databases['default']['default'] = array( 'driver' => 'mysql', 'database' => 'drupal', 'username' => 'testuser', 'password' => '123secure', 'host' => 'localhost', 'prefix' => '', );

Page 87: Next Generation DevOps in Drupal: DrupalCamp London 2014

–The Twelve Factor App

“A litmus test for whether an app has all config correctly factored out of the code is whether the codebase could be made open

source at any moment, without compromising any credentials”

Page 88: Next Generation DevOps in Drupal: DrupalCamp London 2014

“The twelve-factor app stores config in environment variables (often shortened to

env vars or env). Env vars are easy to change between deploys without changing

any code” –The Twelve Factor App

Page 89: Next Generation DevOps in Drupal: DrupalCamp London 2014

env[db_name] = drupal_live env[db_host] = 192.168.0.2 env[db_user] = liveuser env[db_pass] = verysecurepass env[db_prefix] = drupalcamp_

Put your variables in PHP-FPM

/etc/php/fpm/pools/live.conf

Page 90: Next Generation DevOps in Drupal: DrupalCamp London 2014

<?php # /sites/default/settings.php ... $databases['default']['default'] = array( 'driver' => 'mysql', 'database' => getenv('db_name'), 'username' => getenv('db_user'), 'password' => getenv('db_pass'), 'host' => getenv('db_host'), 'prefix' => getenv('db_prefix'), );

Page 91: Next Generation DevOps in Drupal: DrupalCamp London 2014

Provisioning

• Idempotent deployments

• Provision every environment the same way

• Resist the urge to do something manually

• Get into a workflow of automation

Page 92: Next Generation DevOps in Drupal: DrupalCamp London 2014

Docker

Page 93: Next Generation DevOps in Drupal: DrupalCamp London 2014

Docker - AN OVERVIEW

• Lightweight Linux Container

• Portable environment

• Install all your PECL dependencies into a container

• Ship it

Page 94: Next Generation DevOps in Drupal: DrupalCamp London 2014

Problems

Page 95: Next Generation DevOps in Drupal: DrupalCamp London 2014

Problems

!

• Still heavily in development, no “right way” yet

• Hard to set up syslog inside a container

• Runs as root on the box

Page 96: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 97: Next Generation DevOps in Drupal: DrupalCamp London 2014

That’s being fixed though

Page 98: Next Generation DevOps in Drupal: DrupalCamp London 2014
Page 99: Next Generation DevOps in Drupal: DrupalCamp London 2014

We’re Done!

Page 100: Next Generation DevOps in Drupal: DrupalCamp London 2014

With thanks to Paramount Pictures

and startrek.wikia.com for not suing me

Page 101: Next Generation DevOps in Drupal: DrupalCamp London 2014

Questions

Page 102: Next Generation DevOps in Drupal: DrupalCamp London 2014

Thank You!

Page 103: Next Generation DevOps in Drupal: DrupalCamp London 2014

My first official talk!• Special thanks to Lorna Mitchell, Ian

Barber and Rowan Merewood for all the coaching

• All feedback welcome!