Security Enforcement of Microservices
How can you best secure your microservices architecture? Here are a few patterns to consider depending on the needs of your organization.
Join the DZone community and get the full member experience.Join For Free
securing an application is a common task that many developers will embrace during the architecture design and coding phase of a project. although the securing of a web service or restful endpoint could be achieved easily when the modules of a project are deployed within the same java container, when they rely on the same security spec (jaas, ws-security, ws-trust, jax-rs) or security framework (apache shiro, spring security, …) the design could become really complex when we have to build a distributed application.
as the current trend is to split a monolithic application into a collection of microservices exposing the service as a restful endpoint, or web service interconnected to each other, our security officer will be very embarrassed to guide and provide recommendations for the actors of such a project.
the difficulty is not related to the microservice architecture itself but because:
- the number of services to be managed within such a project is multiplied by a factor of 10x, even 100x;
- each service, which is part of a logical/business domain, could be managed according to different security governance rules (basic auth, digest, and encryption).
- the information transported between the services belong to a different confidentiality level (public, private, …).
for all these reasons, the security and the governance will become even more critical for the success of a microservice architecture. from a technical point of view, we will be faced with the following concerns: as the modules will be deployed within different types of java container (apache tomcat, jboss wildfly, apache karaf, jboss swarm, vert.x, …), that each container addresses a specific need (non-blocking, high throughput of connections, http/2 protocol, …), and that they don’t necessarily support/follow the javaee spec or jaxrs/jaxws standards, different security patterns or frameworks will be required to resolve our security challenge.
to help architects and developers adopt the security approach that best fits the company requirements, practices, microservices architecture, and their skills, we will investigate within this blog some patterns that you could easily implement within your project in order to secure your microservices:
- web container
- api gateway
we will now present, detail, and mitigate the approaches in order to measure the consequences of the adoption of the different patterns.
the use case that we will discuss is very simple and consists of securing a restful service which has been created using the java integration framework apache camel. as we can see within the following picture, the rest service or endpoint is exposed by a local http web container created using eclipse jetty. a client which is our http agent will issue http requests using an http method (get, post, delete, …) and a url to access the web resources of the service (“/rest/myservice/”).
the goal of the interceptor is to include within the flow of the code a class responsible for collecting/handling the information passed through the http request like the authorization header, the http uri, url, and the credentials provided by the http agent. the principle is presented within the next picture
within the flow describing how a request or an exchange is processed, an interceptor is added before or after the call to the processor responsible for handling the logic declared within the application. this interceptor wraps your code and will act as a security guardian. this pattern is supported by the apache camel framework using a policy .
the interceptor can use the existing security frameworks/technologies to handle the authentication/authorization process like jaas, apache shiro, spring security, apache ws4j, …) but you can also create your own interceptor and plug it within the flow as we will demonstrate here.
an apache camel interceptor will look at the simpleauthenticationpolicy class where the wrap method will be called when the framework will consume an exchange that corresponds to an http request received by the jetty server acting as a consumer.
the simpleauthenticationprocessor class contains the logic needed to access the content of the exchange when the processor will be called and of course, it will call the class/method responsible for authenticating / authorizing the incoming request applysecuritypolicy .
the last thing to be done is to register the interceptor within the apache camel route definition in order to secure your microservice.
as you can see this approach is very flexible as you can plug whatever technology you prefer to use to secure the access to the rest service exposed as an endpoint by the jetty server.
we can also enrich the architecture and use the features offered by the jetty/netty http server in order to
- enable the ssl/tls security layer to encrypt the data exchanged between the http agent and the server.
- setup mutual tls.
- restrict access to the web resources using a securityconstraint associated with a user’s role.
- authenticate the user using some jaas plugin (hashlogin with property file, ldap, jdbc)
as you can see in the next diagram, the security architecture proposed is more robust as managed by two distinct levels if you combine the interceptor pattern with the http security model :
- the web http container will be responsible for authenticating the user and restricting the access to the web resources.
- the connection between the http agent and the server is more secured as the data will be encrypted and the confidentiality of the data exchanged will be guaranteed.
- the interceptor will become responsible for authorizing the access to the service.
time to mitigate the approach
it is time now to review the approach from a security officer point of view and to mitigate the approach. he/she will be very pleased about the solution as:
- it offers a great level of flexibility to support the java standards (jaas, ws-security, …) or standards enacted by the company.
- the applications developed will be independent of any third party tool or vendor to support the security.
but they will certainly complain that the approach is really intrusive, as it will require developing some home security code to support the interceptor pattern. the management of the collection of the microservices will be very difficult as we will suffer from a lack of governance & centralized capability as proposed by an api management platform to define in one place the security rules applied to the different services, the services secured, …
|no product lock||intrusive|
|great flexibility||low management capability|
|spec managed||lack of governance|
the gateway pattern as depicted in the next diagram delegates the responsibility of managing the security aspect to an external player, which is an interceptor acting as a proxy. this actor, which is an api management actor, can be part of the same network segment where the microservices are deployed or can be deployed behind a firewall to reenforce the security of the services hosted by the java containers.
the apiman project has been developed to support this pattern and has the advantage that it can be deployed on premises. the role of the api management platform will be to:
- define centrally the different services/endpoints to be secured (rest, web service).
- select using the available plugins the policies to be applied (basic auth, oauth2, …).
- map the web resources, the web actions (get, post, put, delete) with the services.
- deploy within one to many gateways (http web server, vert.x) the policies that the server will use to intercept the http requests.
the management of the services and the governance is simplified for the security officer using the apiman server.
this web console will allow him to:
- manage the services according to an organization that abstracts the related services.
- select the services to be secured and assign to an api.
- group using a plan the policies to be applied to the different services.
- select the plugins to be used; basic authentication, authorization, black-listed ip, white ip list, throttle, keycloak - oauth2.
- manage the different versions of the apis published.
- integrate the gateway where the policies are applied on top of the requests received.
- collect the metrics/statistics of the usage done.
the adoption of an api management platform will certainly offer more possibilities to govern the microservices deployed in different java containers and potentially non-java containers as offered by the node.js technology.
some additional benefits about this technology concern also :
- the disappearance of the coupling between the services and the frameworks required to authenticate/authorize the access to the microservices.
- the centralization in one place of the apis and consumers of the services.
- the collection/aggregation of the metrics/statistics about the usage of the services which is required in some institutions where security audits are performed regularly.
there are nevertheless some drawbacks as summarized within the next table; as a new technology must be investigated and adopted within the company, the performance could be impacted as a proxy http server will interrupt the normal flow between the http agent and the service, the project will become dependent of the api product used and new processes will be defined within the company to support the api platform like also the actors in charge to maintain the security governance and the infrastructure.
|centralized governance policy configuration||performance|
|loose coupling||new architecture brick|
|tracking of apis and consumers of those apis||plugins available|
|gathering statistics/metrics||product locked|
|simplify security audit|
while the usage of the interceptor completed with the web container security model will be adopted by many companies when the number of the microservices to secure is not important or when few security standards should be supported, it is evident that the gateway pattern will become the de facto standard within large institutions where the security audit and the governance aspects are non-negotiable.
another benefit of the api management platform when it is used on top of a virtualized environment as proposed by the openshift/kubernetes cloud platform is the fact that the microservices deployed and running as pods will be automatically discovered!
Published at DZone with permission of Charles Moulliard. See the original article here.
Opinions expressed by DZone contributors are their own.