Securing Microservice APIs
Securing Microservice APIs
Securing Microservice APIs
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Securing Microser‐
vice APIs, the cover image, and related trade dress are trademarks of O’Reilly Media,
Inc.
While the publisher and the authors have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and
the authors disclaim all responsibility for errors or omissions, including without
limitation responsibility for damages resulting from the use of or reliance on this
work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is sub‐
ject to open source licenses or the intellectual property rights of others, it is your
responsibility to ensure that your use thereof complies with such licenses and/or
rights.
This work is part of a collaboration between O’Reilly and CA Technologies. See our
statement of editorial independence.
978-1-492-02711-9
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
1. Microservice Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Microservice API Landscape 2
API Access Control for Microservices 3
Microservice Architecture Qualities 4
A. Helpful Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
iii
Preface
v
What’s Not in This Report
This report is explicity focused on HTTP-based APIs for communi‐
cation with and between microservices. Neither security approaches
for non-HTTP transport protocols nor security approaches for con‐
tainers in general are included.
vi | Preface
O’Reilly Safari
Safari (formerly Safari Books Online) is a
membership-based training and reference
platform for enterprise, government, educa‐
tors, and individuals.
How to Contact Us
Please address comments and questions concerning this book to the
publisher:
Preface | vii
Acknowledgments
The authors would like to thank Alan Marion, Tarun Khandelwal,
Irakli Nadareishvili, Mike Sample, Sascha Preibisch, and Josh
Chiang for their invaluable contributions to the report. Thanks also
to Shiu Fun Poon, Kin Lane, Ronnie Mitra, and Daniel Bryant for
their helpful feedback.
viii | Preface
CHAPTER 1
Microservice Architecture
1
Web API communication
Microservices often publish their business functions through
HTTP-based web APIs encoded using JSON or other related
media types.
Container-based deployments
Microservices often use Linux containers—frequently Docker
containers—as their unit of deployment, allowing for a smooth
transition from development to operations in a range of frame‐
works and platforms.
Collectively, these microservice characteristics and common tech‐
nologies must be factored into any solution for microservice API
access control.
1 API endpoints are often listed in service registries like Consul, Eureka, or etcd.
Identification
Messages may be triggered by end user activity or automated events
and may be distributed and transformed through several interme‐
diaries. Service consumers and intermediaries must be able to send
API request messages that include multiple identities along with
optional attributes that detail those identities, and they must be able
to accept API requests that include multiple identities and their
attributes.
Authentication
API requests may be authenticated through included credentials,
asserted claims (e.g., a token), trust relationships, or a combination
of these methods. Services must be able to either perform the
authentication themselves or delegate authentication to a trusted
component.
Accountability
It is important to audit system activity affected by API requests in
order to provide forensic details for intentional or unintended sys‐
tem breaches. Accounting for an API message can happen at any
point in the request’s or response’s path. It is valuable to capture as
much of the message’s context as possible, given the potentially wide
range of identities, attributes, and processing components involved.
Manageability/Operability
Microservice architectures typically feature a high degree of auto‐
mation for all functions. In order for an access control solution to be
viable in a microservice architecture, it must expose machine acces‐
sible interfaces for management automation.
Performance
Due to the distributed nature of microservice architectures, the pro‐
cessing latency of each component has the potential to degrade the
performance of the overall system. As such, an access control solu‐
tion within a microservice architecture should avoid adding latency
as much as possible.
7
Establishing Trust
All security is based on trust. But trust has no effective measure,
only confidence that grows with careful diligence. Our trust in a dis‐
tributed system is an accumulation of many decisions we make to
mitigate risk.
It is important to call out these decisions, because only then can we
begin to tease out the implicit trust that hides in our design. Too
many modern platforms make security opaque. This might make
them easy to use, but it masks assumptions and limitations. Good
security architecture is transparent about where it invests the trust.
Consider a simple, static website. On the surface, it should be easy to
secure. The pages are open for everyone to read, so there is no need
for authentication or user management. It only supports simple
HTTP GETs, so it would appear there is little opportunity for an
attacker to exploit.
But dig down, and we find the implicit trust. We trust our provider
to handle DDoS mitigation. We hope they have decent physical
security. We assume they harden their CMS platforms and keep up
on the latest attack vectors that might target the infrastructure below
our simple HTML pages.
At the other extreme, imagine a secure government computing
facility. Disconnected from the internet, its systems reside in Fara‐
day cages inside a fortified building without windows. Even the
power supply is private. Yet despite this attention to detail, the secu‐
rity is only as good as the vetting of the insiders using it.
The point is, trust is about compromise, and we need to be comfort‐
able residing on a spectrum of risk. There are no absolutes in com‐
puter security; there is only trust and acceptance of risk. Security
architecture is a tuning exercise, trying to optimize trust against
many competing interests.
The following sections cover the basic building blocks used to build
a secure access to microservices. These approaches all create trust
boundaries. Some architectures use these techniques in isolation,
but they may also be combined to meet the competing needs of
developers, operators, and security professionals.
Localhost Isolation
Localhost isolation is a common developer pattern. We’ve all built
and tested applications on our development machine, confident that
the firewall is protecting us from malicious network connections. It
is simple, and because of that it’s a useful model to illustrate the pros
and cons of any network segmentation scheme. It is also very rele‐
vant today because of its widespread use in container deployments,
especially in common patterns like the sidecar.
Localhost isolation simplifies applications because they can trust all
senders. It allows us to associate services with specific ports, approx‐
imating the traditional TCP and UDP security model binding well-
known ports to specific applications.
But this model does nothing to identify client applications (source
ports are ephemeral and assigned by the network stack) and
assumes that all processes on the OS are equally trustworthy. And it
tells us nothing about users associated with a client entity—for that
we need to move up the stack.
Network Segmentation
Network segmentation, using clever combinations of physical
switches, routers and firewalls, is one of the foundation elements of
computer security. By combining trusted entities into a private seg‐
ment, developers can focus on application logic, not access control.
But this free ride comes at a cost, as any failure in the segmentation
scheme puts every entity at risk.
To mitigate this risk, network segments should be kept as small as
possible. Carving up the network into zones makes it easier to iso‐
late breaches within the boundary. Crossing a boundary should
require a higher level of scrutiny, such as security token validation.
Network-Level Controls | 9
Zone membership should balance developer experience, operations
efficiency, and security exposure.
The virtual world (both classic virtualization and container-based
networking) uses a segmentation model that is largely the same as
the physical, substituting software-defined analogs for their hard‐
ware counterparts. Models like ACLs—which are familiar to every‐
one from their use in file systems—simplify network policy
definition with succinct access control rules.
The real problem with network segmentation comes with size. As
networks become more complex, the rules governing zone member‐
ship become difficult to maintain. And as the number of zones and
hosts increase, so too does the attack surface.
SSL/TLS
One way to limit the opportunities for bad actors is to ensure that all
communications in a network segment use SSL/TLS. This provides
confidentiality and integrity protection of data in flight, server
authentication for clients, and adds important—though optional—
client-side authentication for servers.
So why don’t we use SSL/TLS everywhere? Part of the reason is iner‐
tia. In the early days of the web, the cryptographic demands of SSL
were high, so most websites restricted its use to critical operations
like credit card transmissions. The impact of SSL/TLS is negligible
using modern CPUs, but there is an historical reluctance to use it
everywhere. This is a bad web practice we need to resist; all APIs
should use SSL/TLS everywhere.
1 Based on the principles of Domain-driven Design as described in Eric Evans’ book with
this same name.
Network-Level Controls | 11
frameworks such as SPIFFE to simplify management. Use an inter‐
mediary with application-level controls to restrict access into the
network segment.
Application-Level Controls
Application entities establish trust by an exchange of security
tokens. A trusted third-party issues tokens and uses cryptography
(either across the communications channel or within the token
itself) so that entities can establish trust with no prior relationship.
Token trust models are usually based on either shared secrets or the
more common practice of public-key cryptography.
Application-Level Controls | 13
API keys
API keys are an opaque token intended to identify a client app. Many
applications may use an API, so it is useful for a product manager
responsible for the API to know where the traffic is coming from.
API keys are issued to the developer of a client app by an API’s
owner or product manager.
For example, a native gaming app on a mobile phone would have its
own API key. When the app calls an API endpoint, it includes this
key so the service can recognize it. An API key does not identify a
unique, deployed instance of an app. It will be compiled into the
binary image and so is identical across every installation. Applica‐
tion key might have been a better name.
Herein lies the problem: because this is a simple, embedded creden‐
tial, API keys can be located by a determined attacker. For this rea‐
son, you should never consider an API key authoritative. It is useful
for rough usage tracking and traffic management, but always
remember it could be spoofed.
OAuth 2.0
OAuth 2.0 is the preferred framework for secure authorization in
modern application architectures. What begin as a simple way to
delegate authorization between websites is now the primary means
of API authorization. But it is easy to misinterpret OAuth as a sim‐
ple authentication and session tracking mechanism—basically an
updated, REST-like version of what web developers have done for
years. Not only is this inaccurate, but it misses the real point of this
technology. The OAuth framework addresses trust issues between
users, applications, and infrastructure we have overlooked for years.
OAuth allows users to delegate access between distributed applica‐
tions. It is not an authentication protocol, which proves a user’s
claim to an identity. It is an authorization protocol that lets a user
(the resource owner) grant an app (the client) access to an API (the
resource) on their behalf. This access is for a limited time and with
limited scope.
The important point OAuth makes is that we should never trust any
application with unrestricted authentication factors (such as a pass‐
word). These are the keys to our kingdom, and we can never be cer‐
tain the application will use these keys for our intended purpose.
OpenID Connect
OpenID Connect is an authentication layer built on top of the
OAuth framework. OAuth is concerned only with authorization,
making no attempts to define how authentication takes place.
OpenID Connect takes this on, providing flows to authenticate an
end user and provide claims back to a relying party.
Like OAuth, OpenID Connect makes the important point that the
apps we use may not be trustworthy. If you stop and think about
this, it makes perfect sense. Your phone is full of apps written by
third parties; how can you be confident that these won’t misuse your
credentials? The answer is, of course, you can’t—so we need a
method to take apps out of the authentication business.
Application-Level Controls | 15
The complication is that apps—and not just the services they invoke
—need to be confident in the identity of a user. An app can’t derive
this from the operating system, as this user context is likely different
from that of the app.
OpenID Connect achieves both these goals by doing an end-run
around the app using a trusted channel to authenticate, such as lev‐
eraging the native browser on a mobile device. The browser inter‐
acts with an identity provider in isolation from the app. This
provider is free to perform authentication using any combination of
factors—OpenID Connect leaves this open. In return, the authoriza‐
tion server issues the app an ID token, which asserts the subject’s
identity (the user) and the token’s intended audience. OpenID Con‐
nect constrains ID tokens in scope and time, which limits the poten‐
tial for misuse. The tokens also provide a convenient packaging for
common claims such as name and phone number.
This is a lot packed into a few simple endpoints—and that’s its
attraction. OpenID Connect solves a first order problem in estab‐
lishing trust in a way that is simple, portable, and powerful.
JWT
JSON Web Token (JWT) is a simple, JSON-based packaging format
for exchanging claims. The claims can be anything you can repre‐
sent in JSON; JWT adds only a formalized header and body, a sign‐
ing mechanism (JWS), optional encryption (JWE), and a simple web
encoding. The ID Token from OpenID Connect is a JWT.
Claims are important because they let us refine our access control
decisions. If a token is completely opaque, a resource server has no
opportunity to apply local policy on a transaction. But with a trans‐
parent token like JWT, a claim such as application=iosTradingApp
could provide valuable context to a microservice making access con‐
trol (or general service-delivery) decisions.
Authoritative claims are the basis of access control models like
attribute-based access control (ABAC). ABAC shifts authorization
from individual identity-centric decisions (Bob is allowed access to
the printer) to attribute-centric rule sets (All systems on the third
floor can access the third-floor printer). This is a powerful technique
to apply to microservices, which have a need to restrict access but
also promote re-use by many different (and continuously changing)
sources.
Application-Level Controls | 17
JWT Sessions and JOSE Issues
Be careful if you use JWT—it is still an emerging
technology. We recommend using it for authori‐
zation, but avoid using it to manage user ses‐
sions.2 Researchers have also identified
problems with the JavaScript Object Signing and
Encryption (JOSE) stack. Early implementations
let attackers forge tokens, and the lack of a ver‐
sioning mechanism means that the specification
cannot evolve to exclude weak encryption algo‐
rithms.3 Efforts such as POST offer an alterna‐
tive, though nonstandard, solution.4
Infrastructure
Both network-level controls and application-level controls have a
place in a well-thought-out security architecture. The approach we
Proxy/Gateway
The distinction between proxies and gateways has blurred in recent
years. Both are reverse proxies that stand between an HTTP client
and server. The traditional proxy is a lightweight network entity
offering a few predictable functions, such as content filtering or load
distribution. Gateways do the same, but operate at a higher level,
enforcing sophisticated policies by interpreting application proto‐
cols on a transaction-by-transaction basis. They are programmable
and usually responsible for authentication, authorization, threat
detection, and sophisticated traffic management.
Proxies are an important component of all microservices architec‐
tures. An instance of a microservice may be ephemeral, so finding
all the available instances at any point in time—called runtime ser‐
vice discovery—is necessary in any architecture. For a long time,
this was the domain of web proxies such as HAProxy and NGINX.
Specialized proxies for microservices are appearing, such as Envoy,
Linkerd, and Traefik. These allow for mutual TLS, and in some cases
simple token validation.
At the other end of the spectrum are API gateways. These excel at
enforcing security policies and accommodating unusual networking
challenges, but their binary images tend to be large, and the gate‐
ways are complex to deploy. However, a new generation of light‐
weight, microservice-centric gateways are appearing that can
underpin a 12-factor, microservices architecture. This is an ideal
solution, as programmable gateways help to insulate applications
from the rapid change in this space.
Network Overlays
A number of vendors have introduced network overlay solutions on
popular cloud or container-based networking platforms. These are
intended to simplify the configuration of secure microservices
networks.
Infrastructure | 19
OpenContrail and Romana offer network overlay solutions for
cloud infrastructures. Project Calico includes native support for
Kubernetes, Docker, and Mesos. Cilium introduces new technology
to the Linux kernel in order to modify networking capabilities.
PaaS
Popular microservices platforms offer a variety of access control
abstractions to reduce complexity for operators and developers. This
requires trust in the PaaS—which is a consideration you must never
take lightly. However, used in combination with elements like proxy/
gateways, PaaS offers a very powerful microservices platform for lit‐
tle invested effort.
This section explains the API access control mechanisms in avail‐
able in Kubernetes, Cloud Foundry, and AWS.
Kubernetes
Kubernetes is a platform for run time container management. It is
widely used to manage the lifecycle of microservice instances. A
Kubernetes installation consists of one or more clusters made up of
nodes that run a collection of pods consisting of one or more con‐
tainers. Pods can be further abstracted by defining services.
Kubernetes uses service accounts to identify components or groups
of components within the system. It uses basic authentication, X.509
certificates, as well as multiple token types to authenticate users and
service accounts using its control plane API. It offers a rich set of
authorization models as plugins, including RBAC, ABAC, and web‐
hooks for integrating with other infrastructure.
Data plane communication between containers, pods, and external
applications in a Kubernetes cluster uses network controls. Contain‐
ers can only talk to containers on the same node, but “service”
abstraction allows containers or pods to talk across nodes using pri‐
vate IP addresses. Network policies can act as an ACL for container-
to-container, pod-to-pod, and external-entity-to-service
communication.
Despite these capabilities, responsibility for access control of web
APIs exposed by microservices running in a Kubernetes cluster is
generally left to the microservice itself, or to a delegated intermedi‐
ary such as an API gateway.
Infrastructure | 21
Emerging Approaches
Technology never stands still. We follow fads and re-package old
ideas. Sometimes we even come up with things that are genuinely
new. It keeps the industry fresh and exciting, but it makes predicting
the future very difficult. Time and again, we see good ideas and
great implementations lose out to weaker alternatives simply for lack
of mindshare.
That said, there are some forces acting on the microservices space
that are more predictable. As microservices architectures increase in
complexity, so too will the need to abstract both infrastructure ele‐
ments and the command-and-control system that coordinates all
the underlying pieces. This is our experience with PaaS, which
assembles all the components we need to orchestrate service lifecy‐
cle into a common platform.
Security will follow this same model. Access control will be sub‐
sumed into the platform itself, expressed using high-level policies
decoupled from the runtime state. As long as the platforms are
transparent about how they map policy to implementation, this is a
good thing. It allows us to focus on the big picture of trust, threats
and mitigation, which is where our attention should be.
One example of a policy-focused approach is the Open Policy Agent
(OPA) allows users to define policies and enforce them locally using
a sidecar container or an embedded library. The design of OPA sup‐
ports a broad range of policies, and there are examples specific to
HTTP API access control.
Service Mesh
Service mesh in an emerging technology that helps to manage inter‐
connections between services. Most consist of a command-and-
control backplane in control of lightweight proxies, acting as
intermediaries that actively manage the communication between
services. The goal is to decouple security and management from
individual services and express this through generalized policies
applied to the platform as a whole.
Istio is a service mesh platform for microservices. It focuses on traf‐
fic management, security policy enforcement, and telemetry. Istio is
an open-source effort led by Google, IBM, and Lyft.
Serverless Computing
Serverless computing extends the idea of abstraction even further.
The big idea here is that a developer’s time is best spent solving busi‐
ness problems, not fighting with infrastructure. Serverless gives the
developer a simple code-container insulated from the details of
deployment and lifecycle.
Events on the platform trigger activation of a service. An HTTP call
to a resource might be an event, but it is important to think beyond
such obvious connections. A counter reaching a particular threshold
might be an event, or a field changing in a database. It is a liberating
idea for a developer, who can now focus on data and workflows,
leaving availability, scaling, security, and metering to the platform.
The AWS Lambda services is the most prominent example of server‐
less computing, though alternatives are appearing. Developers can
write Lambda functions in various languages, such as Java and C#,
and associate their functions with a rich set of triggers. These trig‐
gers can fire in response to events across a broad range of AWS
resources, from changes in a DynamoDB table to scheduled events
in Cloudwatch. AWS Lambda is beginning to see widespread adop‐
tion by organizations developing microservices.
Emerging Approaches | 23
CHAPTER 3
A General Approach to
Microservice API Security
25
ciently at runtime. This continuous need for optimization has led to
the diversity of microservice API security solutions. Yet there are
still common patterns in the heterogeneity.
Each of the solutions in Chapter 2 considers whether or not the API
request or its source are trusted as a basis for its logic. For example,
network isolation assumes all traffic is trusted, certificate-based
access control verifies the trust chain, and platform-based solutions
rely on proof of platform residency in order to authorize API
requests. Trust verification is typically more efficient at runtime than
authenticating untrusted message sources. As a result, we can use
this trusted/untrusted API request duality to optimize a general
microservice API security solution.
A Platform-Independent DHARMA
Implementation
The purpose of this book as stated at the outset is to define a cross-
platform approach to API access control in a microservice architec‐
ture. DHARMA achieves that purpose on a universal level. However,
to make the book more tangible, this section defines a specific
instantiation of DHARMA that an organization can implement in
any platform context.
Domain Hierarchy
The domain hierarchy for this instantiation of DHARMA consists of
three tiers:
Implementation Considerations
There are a number of considerations for any organization imple‐
menting this platform-independent approach to DHARMA. The
access and trust mechanism choices necessitate some foundational
practices and infrastructure in order to make this approach work in
a performant, scalable, and secure way.
Token management
Tokens are a fundamental component of the platform-independent
DHARMA instantiation at all levels. Therefore, comprehensive
token management—the ability to validate, issue, exchange, and def‐
erence tokens—is essential to the implementation. Theoretically, an
organization could use one token management server for their
entire service domain, but it is recommended that some secure
token services be distributed to minimize the number of hops and
thus the transactional latency associated with API requests. These
distributed token servers may then be federated through certificate-
based trust.
In our platform-independent DHARMA implementation, OAuth
2.0 is used as the access mechanism for the outer domain. This
means that the organization must implement an OAuth-compliant
authorization server. The OAuth grant type will depend on the type
of external client requesting API access. For the Authorization Code
and Resource Owner Password grant types, end user authentication
is required in order for the external client to obtain an access token.
Therefore, the authorization server associated with each external
API must be able to validate end user credentials, either on its own
or by accessing the appropriate identity and access management
(IAM) services that act as the authority for such credentials.
Although there are no strict rules about the tokens used within this
DHARMA implementation, here are some guidelines. It is expected
that the JWTs used inside the domains will have a short expiry time
(less than an hour). Depending on scale and sensitivity, they may be
issued for single use. It is also expected that OAuth scopes and JWT
claims will be used to carry information useful to authorization
decisions. It will be at the discretion of how these properties are
Component provisioning
Service intermediaries and service instances must be provisioned
securely. This means that deployment activities must be performed
by authenticated administrators or user agents with appropriate
authorization and that all administrative activity be audited. Of par‐
ticular importance, certificates must be provisioned to components
within the service domain in a way that minimizes exposure and
violation of trust.
Applying DHARMA
Chapter 3 includes a detailed description of how DHARMA can be
implemented using platform-independent access and trust mecha‐
nisms. Still, it is quite possible to implement DHARMA using
37
platform-specific mechanisms such as those listed in Chapter 2. It is
expected that the service registries such as Consul and etcd that are
used for service discovery and dynamic configuration could play a
role in the security landscape as well. We hope that DHARMA can
be used to articulate and clarify existing microservice API security
approaches, and that it can be used to discover and develop new
ones.
Extending DHARMA
There is much more ground to cover in controlling data plane
access for microservices beyond web APIs. With the increasing pop‐
ularity of reactive, event-based architecture in microservice imple‐
mentations, new protocols are emerging for communication,
particularly between microservices. gRPC—originally developed by
Google but now under the stewardship of the Cloud Native Com‐
puting Foundation—offers native HTTP2 support and a binary seri‐
alization format (protocol buffers, or protobuf) that is more
compact than JSON. Apache Thrift is similar to protobuf in opti‐
mizing for message size. Multiple asynchronous messaging proto‐
cols—RabbitMQ, Apache Kafka, NATS—are being used in event
distribution and streaming. Still, none of these protocols are any‐
where near the maturity of web APIs when it comes to interoperable
access control mechanisms. With its abstract beginnings, DHARMA
has the potential to be used as a generalized data plane access con‐
trol approach that includes all protocols.
In the meantime, this book should help organizations that are
implementing microservices—especially those using multiple plat‐
forms for deployment and hosting—define a secure and scalable
approach for controlling access to the microservices’ APIs.
39
• Consul service discovery
• Envoy service proxy
• gRPC
• Istio policy management
• Kubernetes concepts
• Linkerd service proxy
• OpenContrail network virtualization
• Open Policy Agent
• Project Calico network security
• Romana network virtualization
• Secure Production Identity Framework for Everyone (SPIFFE)
• SPIFFE Verifiable Identity Documents