the lost art of defensive programming

27
@magma_digital The Lost Art of Defensive Programming https://pixton.com/ic:d2yrnf2h

Upload: jeremy-coates

Post on 14-Jan-2017

1.478 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: The Lost Art of Defensive Programming

@magma_digital

The Lost Art ofDefensive Programming

https://pixton.com/ic:d2yrnf2h

Page 2: The Lost Art of Defensive Programming

@magma_digital

Who am I?

๏ Jeremy Coates, CEO, Magma Digital Ltd

๏ Founder of PHPNW User Group & Conference

๏ Lancashire Digital CIC Founding Director

๏ International Conference Speaker

๏ Coach of Coaches - WeCa.mp (code camp)

๏ @phpcodemonkey

๏ linkedin.com/in/jeremycoates

Page 3: The Lost Art of Defensive Programming

@magma_digital

https://upload.wikimedia.org/wikipedia/commons/8/8a/BBC_Micro_left.jpeg

BBC Micro (Model B!)8-bit, 32Kb, 2Mhz

Page 4: The Lost Art of Defensive Programming

@magma_digital

https://www.facebook.com/photo.php?fbid=10208320036025458&set=a.2962106258635.156004.1440932589&type=3&theater

Page 5: The Lost Art of Defensive Programming

@magma_digital

๏ Purpose — Prompt thought, Discussion, Reasoned argument (in the bar!)

๏ Themes — Philosophy;Professionalism; Practical examples

๏ Convey a sense / approach —not a rote-learnable technique(s)

Intro

Page 6: The Lost Art of Defensive Programming

@magma_digital

๏ Murphy’s Law: “Anything that can possibly go wrong, does.”

๏ Finagle’s Law: “Anything that can go wrong, will - at the worst possible moment.”

๏ Sod’s Law: “If something can go wrong, it will”(in British culture: “at the worst possible time”)

• “Hope for the best, expect the worst.”

Applicable ‘Laws’

Page 7: The Lost Art of Defensive Programming

@magma_digital

๏ Plan for the worst — related to Fail Fast - with klaxons!๏ Not anti-TDD, complimentary๏ Security focussed — code, privacy,

encryption, servers๏ Discipline — planning, consistency, shared

standards (team), comments!๏ Cross over point between Dev and Ops

Philosophy

Page 8: The Lost Art of Defensive Programming

@magma_digital

๏ All engineers love new shiny! and other myths:

• “New systems need new tech!”

• “Old is slow”, “It doesn’t scale”

• “It’s just not cool any more”, “It breaks when”

๏ Longevity, scale, licensing, compliance, risk, support

Philosophy: Technology Choices

Page 9: The Lost Art of Defensive Programming

@magma_digital

๏ In a war with hackers, bots, human mistakes

๏ Tactics — establish a defensible perimeter๏ Attempt to prevent

• Defacement, Malware, Linkinjection, privilege escalation

Battlefield: Internet

Page 10: The Lost Art of Defensive Programming

@magma_digital

๏ Filter Input, Escape Output — Filtering is not about preventing security vulnerabilities, it's about not populating your database with garbage. If you're expecting a date, make sure it at least looks like a date prior to storing it. @ircmaxell

๏ CSRF / XSS / CORS / SQL Injection๏ Password hashing / Nonce hashes๏ SSL — current generation — TLS 1.2+

Security basics

Page 11: The Lost Art of Defensive Programming

@magma_digital

๏ Deal with default states๏ Ambiguity in return types๏ Don’t spill errors to users

- try/catch; log

Graceful Failures“When you assume,you make an ass out of u and me”

Oscar Wilde on Assumption

Page 12: The Lost Art of Defensive Programming

@magma_digital

๏ Mixed coding styles / naming / PSR - x๏ One technique to rule them all๏ Peer review๏ Documentation๏ Architecture

Team Standards

Page 13: The Lost Art of Defensive Programming

@magma_digital

The unit tests all passWe’ve got good code coverage!

Page 14: The Lost Art of Defensive Programming

@magma_digital

๏ QA Tools —PHPMD, Code Sniffer,PHP Metrics

๏ Profiling —XDebug, blackfire.io

Quality Assurance

Page 15: The Lost Art of Defensive Programming

@magma_digital

๏ Latency varies — simulate• OS X Xcode Hardware

IO Tools: Network Link Conditioner

๏ Caches — temporary storage• Plan for failure, code

shouldn’t rely on it being there!

Infrastructure:Remove key components

Network Link Conditioner

Page 16: The Lost Art of Defensive Programming

@magma_digital

Page 17: The Lost Art of Defensive Programming

@magma_digital

๏ Low bandwidth — simulate• Hotspot to your phone and

turn off 4G!๏ File systems fail — abstract —

flysystem?๏ Server reboot — do services

restart?๏ Failover — kill the master or

slave

Infrastructure:Remove key components

Page 18: The Lost Art of Defensive Programming

@magma_digital

๏ Block third party services:

• Test socket timeouts, API error handling

Infrastructure:Remove key components

Page 19: The Lost Art of Defensive Programming

@magma_digital

๏ Narrowing down to errors

• Actually read the error message!

• Not just Googling parts of the message๏ Develop a strategy

• Be scientific, eliminate sources one at a time

Practical approaches:Debugging

Page 20: The Lost Art of Defensive Programming

@magma_digital

๏ Noisy logs - reduce / eliminate unexpected output — work with error_reporting(E_ALL) — in dev

๏ Graphite / StatsD — measure everything else about your software,method calls, keyactions, any events,deployments

Practical approaches: Logs

Page 21: The Lost Art of Defensive Programming

@magma_digital

๏ Monitoring / Alerts

• NewRelic

• Logstash / Logster / Loggly

• Chat servers / SMS etc.

Practical approaches: Visibility

Page 22: The Lost Art of Defensive Programming

@magma_digital

Page 23: The Lost Art of Defensive Programming

@magma_digital

๏ Database — indexes, field types, query optimisation

๏ Test for planned scale — ab, siege, jMeter, LoadRunner

Practical approaches: Performance

Page 24: The Lost Art of Defensive Programming

@magma_digital

๏ Automation is the key

• Rsync; Phing; Ansible; DeployHQ; Capistrano

• Symlink switching; full Atomic deploys

๏ Continous Integration — Jenkins, Bamboo etc.?

Practical approaches: Deployment

Page 25: The Lost Art of Defensive Programming

@magma_digital

๏ Upgrading libraries — justbefore deploy!

๏ Front-end — same strategyright?

• composer.lock, package.json,bower.json, Gruntfile.js

Practical approaches: Supporting Code

Page 26: The Lost Art of Defensive Programming

@magma_digital

๏ Defensive programming

• more than just code, lots of moving parts

• easier to learn with feedback from peers

• requires discipline and experience

• risk management, there’s a war on!

Summary

Page 27: The Lost Art of Defensive Programming

@magma_digital

๏ Jeremy Coates, CEO, Magma Digital Ltd

๏ Founder of PHPNW User Group & Conference

๏ @phpcodemonkey

๏ linkedin.com/in/jeremycoates

Defensive Programming:Lost Art? https://joind.in/talk/a6b65

http://bit.ly/LostArtDefensiveProgramming