| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
| 2 |
<!-- |
| 3 |
~ Copyright 2009-2013 Roland Huss |
| 4 |
~ |
| 5 |
~ Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
~ you may not use this file except in compliance with the License. |
| 7 |
~ You may obtain a copy of the License at |
| 8 |
~ |
| 9 |
~ http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
~ |
| 11 |
~ Unless required by applicable law or agreed to in writing, software |
| 12 |
~ distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
~ See the License for the specific language governing permissions and |
| 15 |
~ limitations under the License. |
| 16 |
--> |
| 17 |
|
| 18 |
<chapter id="security"> |
| 19 |
<title>Security</title> |
| 20 |
|
| 21 |
<para> |
| 22 |
Security in JSR-160 remoting is an all-or-nothing option. Either |
| 23 |
all or none of your MBeans are accessible (except when your application server |
| 24 |
uses a SecurityManager, but that is not often the case). Jolokia, on |
| 25 |
the other hand, allows for fine grained security defined in an |
| 26 |
XML security policy file. It allows for access restrictions on |
| 27 |
MBean names (or patterns), attributes, operations, source IP |
| 28 |
address (or a subnet) and type of Jolokia operation. |
| 29 |
</para> |
| 30 |
|
| 31 |
<section id="security-policy"> |
| 32 |
<title>Policy based security</title> |
| 33 |
<para> |
| 34 |
Access to MBean and to the Jolokia agents in general can be |
| 35 |
restricted with an XML policy file. This policy can be configured |
| 36 |
for various parameters and is divided into several sections. |
| 37 |
</para> |
| 38 |
<section> |
| 39 |
<title>IP based restrictions</title> |
| 40 |
<para> |
| 41 |
Overall access can be granted based on the IP address of an |
| 42 |
HTTP client. These restrictions are |
| 43 |
specified within a <literal><remote></literal> section, |
| 44 |
which contains one or more <literal><host></literal> |
| 45 |
elements. The source can be given either as an IP address, |
| 46 |
a host name, or a netmask given in |
| 47 |
<ulink url="http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing">CIDR format</ulink> |
| 48 |
(e.g. "10.0.0.0/16" for all clients coming from the 10.0 |
| 49 |
network). The following allows access from localhost and all |
| 50 |
clients whose IP addresses start with "10.0". For all other |
| 51 |
IP addresses access is denied. |
| 52 |
</para> |
| 53 |
<programlisting language="xml"><![CDATA[ |
| 54 |
<remote> |
| 55 |
<host>localhost</host> |
| 56 |
<host>10.0.0.0/16</host> |
| 57 |
</remote> |
| 58 |
]]></programlisting> |
| 59 |
</section> |
| 60 |
<section> |
| 61 |
<title>Commands</title> |
| 62 |
<para> |
| 63 |
This section specifies the Jolokia commands for which access |
| 64 |
is generally granted. For each command in the list, access can |
| 65 |
be further restricted within the |
| 66 |
<literal><deny></literal> part and each command |
| 67 |
missing in the list, which is forbidden globally, can be |
| 68 |
selectively enabled for certain MBeans in the |
| 69 |
<literal><allow></literal> section. If the |
| 70 |
<literal><commands></literal> section is missing |
| 71 |
completely, access to all commands is allowed. |
| 72 |
</para> |
| 73 |
<para> |
| 74 |
All Jolokia commands described in <xref |
| 75 |
linkend="protocol"/> can be used in this section: |
| 76 |
</para> |
| 77 |
<variablelist> |
| 78 |
<varlistentry> |
| 79 |
<term>read</term> |
| 80 |
<listitem> |
| 81 |
Reading of MBean attributes |
| 82 |
</listitem> |
| 83 |
</varlistentry> |
| 84 |
<varlistentry> |
| 85 |
<term>write</term> |
| 86 |
<listitem> |
| 87 |
Setting of MBean attributes |
| 88 |
</listitem> |
| 89 |
</varlistentry> |
| 90 |
<varlistentry> |
| 91 |
<term>exec</term> |
| 92 |
<listitem> |
| 93 |
Execution of JMX operations |
| 94 |
</listitem> |
| 95 |
</varlistentry> |
| 96 |
<varlistentry> |
| 97 |
<term>list</term> |
| 98 |
<listitem> |
| 99 |
List the available MBeans along with their supported |
| 100 |
attributes and operations. |
| 101 |
</listitem> |
| 102 |
</varlistentry> |
| 103 |
<varlistentry> |
| 104 |
<term>search</term> |
| 105 |
<listitem> |
| 106 |
Searching for MBeans |
| 107 |
</listitem> |
| 108 |
</varlistentry> |
| 109 |
<varlistentry> |
| 110 |
<term>version</term> |
| 111 |
<listitem> |
| 112 |
Getting version and server information |
| 113 |
</listitem> |
| 114 |
</varlistentry> |
| 115 |
</variablelist> |
| 116 |
<para> |
| 117 |
In the following example, access is granted to the |
| 118 |
<literal>read</literal>, <literal>list</literal>, |
| 119 |
<literal>search</literal> and <literal>version</literal> command, but |
| 120 |
not to <literal>write</literal> and <literal>exec</literal> |
| 121 |
operations. |
| 122 |
</para> |
| 123 |
<programlisting language="xml"><![CDATA[ |
| 124 |
<commands> |
| 125 |
<command>read</command> |
| 126 |
<command>list</command> |
| 127 |
<command>version</command> |
| 128 |
<command>search</command> |
| 129 |
</commands> |
| 130 |
]]></programlisting> |
| 131 |
|
| 132 |
</section> |
| 133 |
<section> |
| 134 |
<title>Allow and deny access to certain MBeans</title> |
| 135 |
<para> |
| 136 |
Within an <literal><allow></literal> section, access |
| 137 |
to MBeans can be granted regardless of the |
| 138 |
operations specified in the |
| 139 |
<literal><commands></literal> section. The reverse is |
| 140 |
true for the <literal><deny></literal> section: It |
| 141 |
rejects access to the MBeans specified here. Both sections |
| 142 |
contain one or more <literal><mbean></literal> |
| 143 |
elements which have a format like: |
| 144 |
</para> |
| 145 |
<programlisting><![CDATA[ |
| 146 |
<mbean> |
| 147 |
<name>java.lang:type=Memory</name> |
| 148 |
<attribute>*Memory*</attribute> |
| 149 |
<attribute mode="read">Verbose</attribute> |
| 150 |
<operation>gc</operation> |
| 151 |
</mbean> |
| 152 |
]]></programlisting> |
| 153 |
<para> |
| 154 |
Within the <literal><name></literal> section the name |
| 155 |
of the MBean is specified. This can bei either a complete |
| 156 |
ObjectName or a MBean pattern containing wildcards. The |
| 157 |
value given here must conform to the JMX specification for a |
| 158 |
valid <classname>ObjectName</classname>. On this MBean (or |
| 159 |
<emphasis>MBeans</emphasis> if <literal>name</literal> is a pattern), |
| 160 |
attributes are specified within one or more |
| 161 |
<literal><attribute></literal> elements and operations |
| 162 |
within one or more <literal><operation></literal> |
| 163 |
elements. The content can also be a pattern, which uses a wildcard |
| 164 |
<literal>*</literal>. e.g. <literal><attribute>*</attribute></literal> |
| 165 |
specifies all attributes on the given MBean. If for an |
| 166 |
<literal><attribute></literal> element the XML |
| 167 |
attribute <literal>mode="read"</literal> is given, then this |
| 168 |
attribute can be accessed only read-only. |
| 169 |
</para> |
| 170 |
</section> |
| 171 |
<section> |
| 172 |
<title>HTTP method restrictions</title> |
| 173 |
<para> |
| 174 |
Finally, access can be restricted based on the HTTP method |
| 175 |
with which an Jolokia request was received with the |
| 176 |
<literal><http></literal> element. Method allowed |
| 177 |
(<literal>post</literal> or <literal>get</literal>) are |
| 178 |
specified with an <literal><method></literal> inner |
| 179 |
element. The following example restricts the access to POST |
| 180 |
requests only: |
| 181 |
</para> |
| 182 |
<programlisting><![CDATA[ |
| 183 |
<http> |
| 184 |
<method>post</method> |
| 185 |
</http> |
| 186 |
]]></programlisting> |
| 187 |
<para> |
| 188 |
It the <literal><http></literal> section is missing |
| 189 |
completely, any HTTP method can be used. |
| 190 |
</para> |
| 191 |
</section> |
| 192 |
<section> |
| 193 |
<title>Cross-Origin Resource Sharing (CORS) restrictions</title> |
| 194 |
<para> |
| 195 |
Jolokia (since version 1.0.3) supports the W3C specification |
| 196 |
for <ulink url="http://www.w3.org/TR/cors/">Cross-Origin |
| 197 |
Resource Sharing</ulink> (also known as "CORS") which allows |
| 198 |
browser to access resources which are located on a different |
| 199 |
server than the calling script is loaded from. This |
| 200 |
specification provides a controlled way to come around the |
| 201 |
<emphasis>same origin policy</emphasis>. Most <ulink |
| 202 |
url="http://caniuse.com/#search=CORS">contemporary |
| 203 |
browsers</ulink> support CORS. |
| 204 |
</para> |
| 205 |
|
| 206 |
<para> |
| 207 |
By default Jolokia allows cross origin access from any |
| 208 |
host. This can be limited to certain hosts by using |
| 209 |
<literal><allow-origin></literal> sections within a |
| 210 |
<literal><cors></literal> sections. This tags can |
| 211 |
contain the origin URL provided by browsers with the |
| 212 |
<literal>Origin:</literal> header literally or a |
| 213 |
wildcard specification with <literal>*</literal>. |
| 214 |
</para> |
| 215 |
<programlisting language="xml"><![CDATA[ |
| 216 |
<cors> |
| 217 |
<!-- Allow cross origin access from www.jolokia.org ... --> |
| 218 |
<allow-origin>http://www.jolokia.org</allow-origin> |
| 219 |
|
| 220 |
<!-- ... and all servers from jmx4perl.org with any protocol -> |
| 221 |
<allow-origin>*://*.jmx4perl.org</allow-origin> |
| 222 |
|
| 223 |
<!-- Check for the proper origin on the server side, too --> |
| 224 |
<strict-checking/> |
| 225 |
</cors> |
| 226 |
]]></programlisting> |
| 227 |
<para> |
| 228 |
If the option <literal><strict-checking/></literal> is given in this section, too, then the given patterns |
| 229 |
are not only used for CORS checking but also every request is checked on the server side whether the |
| 230 |
<literal>Origin:</literal> or <literal>Referer:</literal> header matches one of the given patterns. |
| 231 |
If neither <literal>Origin:</literal> nor <literal>Referer:</literal> is given and strict checking is enabled, then the access is denied. This useful for protecting against Cross-Site Request Forgery. |
| 232 |
</para> |
| 233 |
<para> |
| 234 |
Please note that <literal><strict-checking/></literal> might not be good enough because of potential |
| 235 |
browser bugs which could allow to forge the origin header. Examples of these issues are |
| 236 |
<ulink url="https://blog.bentkowski.info/2018/06/setting-arbitrary-request-headers-in.html">arbitrary header injection</ulink> |
| 237 |
or <ulink url="https://www.brokenbrowser.com/referer-spoofing-defeating-xss-filter/">referer and origin |
| 238 |
spoofing</ulink>. User facing application which uses Jolokia has backend should consider to implement |
| 239 |
additional measures like using the |
| 240 |
<ulink url="https://tools.ietf.org/html/draft-west-first-party-cookies-07">same-site</ulink> flag |
| 241 |
on the session cookie. |
| 242 |
</para> |
| 243 |
</section> |
| 244 |
<section> |
| 245 |
<title>Example for a security policy</title> |
| 246 |
<para> |
| 247 |
The following complete example applies various access |
| 248 |
restrictions: |
| 249 |
</para> |
| 250 |
<itemizedlist> |
| 251 |
<listitem> |
| 252 |
Access is only allowed for clients coming from localhost |
| 253 |
</listitem> |
| 254 |
<listitem> |
| 255 |
Only HTTP Post requests are allowed |
| 256 |
</listitem> |
| 257 |
<listitem> |
| 258 |
By default, only <literal>read</literal> and |
| 259 |
<literal>list</literal> requests are allowed. |
| 260 |
</listitem> |
| 261 |
<listitem> |
| 262 |
A single <literal>exec</literal> request is allowed for |
| 263 |
triggering garbage collection. |
| 264 |
</listitem> |
| 265 |
<listitem> |
| 266 |
Read access to the C3P0 connection pool is restricted to |
| 267 |
forbid fetching the pool's properties, which in fact |
| 268 |
contains the DB password as clear text. |
| 269 |
</listitem> |
| 270 |
</itemizedlist> |
| 271 |
<programlisting><![CDATA[ |
| 272 |
<?xml version="1.0" encoding="utf-8"?> |
| 273 |
|
| 274 |
<restrict> |
| 275 |
|
| 276 |
<remote> |
| 277 |
<host>127.0.0.1</host> |
| 278 |
</remote> |
| 279 |
|
| 280 |
<http> |
| 281 |
<method>post</method> |
| 282 |
</http> |
| 283 |
|
| 284 |
<commands> |
| 285 |
<command>read</command> |
| 286 |
<command>list</command> |
| 287 |
</commands> |
| 288 |
|
| 289 |
<allow> |
| 290 |
<mbean> |
| 291 |
<name>java.lang:type=Memory</name> |
| 292 |
<operation>gc</operation> |
| 293 |
</mbean> |
| 294 |
</allow> |
| 295 |
|
| 296 |
<deny> |
| 297 |
<mbean> |
| 298 |
<name>com.mchange.v2.c3p0:type=PooledDataSource,*</name> |
| 299 |
<attribute>properties</attribute> |
| 300 |
</mbean> |
| 301 |
</deny> |
| 302 |
|
| 303 |
</restrict> |
| 304 |
]]></programlisting> |
| 305 |
</section> |
| 306 |
|
| 307 |
<section id="security-policy-location"> |
| 308 |
<title>Policy Location</title> |
| 309 |
<sidebar> |
| 310 |
<para> |
| 311 |
A great tool which helps in repackaging an agent for |
| 312 |
inclusion of a <literal>jolokia-access.xml</literal> |
| 313 |
policy file |
| 314 |
is the command line tool <ulink |
| 315 |
url="http://search.cpan.org/~roland/jmx4perl/scripts/jolokia">jolokia</ulink>, |
| 316 |
which comes with the <ulink |
| 317 |
url="http://www.jmx4perl.org">jmx4perl</ulink> distribution. See |
| 318 |
<xref linkend="tools"/> for an introduction. |
| 319 |
</para> |
| 320 |
</sidebar> |
| 321 |
<para> |
| 322 |
But how do the agents lookup the policy file ? By default, |
| 323 |
the agents will lookup for a policy file top-level in the |
| 324 |
classpath under the name |
| 325 |
<filename>jolokia-access.xml</filename>. Hence for the war |
| 326 |
agent, the policy file must be packaged within the war at |
| 327 |
<filename>WEB-INF/classes/jolokia-access.xml</filename>, for |
| 328 |
all other agents at |
| 329 |
<filename>/jolokia-access.xml</filename>. The location can |
| 330 |
be overwritten with the configuration parameter |
| 331 |
<literal>policyLocation</literal>, which has to be set |
| 332 |
differently depending on the agent type. Please refer to |
| 333 |
<xref linkend="agents"/> for more details. The value of this |
| 334 |
init parameter can be any URL which can loaded by the JVM. A |
| 335 |
special case is an URL with the scheme |
| 336 |
<literal>classpath:</literal> which results in a lookup of |
| 337 |
the policy file within the classpath. As stated above, the |
| 338 |
default value of this parameter is |
| 339 |
<literal>classpath:/jolokia-access.xml</literal>. If a |
| 340 |
non-classpath URL is provided with this parameter, and the |
| 341 |
target policy file could not be found then access is |
| 342 |
completely denied. If a classpath lookup fails then access |
| 343 |
is globally granted and a warning is given on standard |
| 344 |
output. |
| 345 |
</para> |
| 346 |
<para> |
| 347 |
The parameter specified with <literal>policyLocation</literal> can contain placeholders: |
| 348 |
</para> |
| 349 |
<itemizedlist> |
| 350 |
<listitem> |
| 351 |
<para><literal>$ip</literal>: IP - Address</para> |
| 352 |
</listitem> |
| 353 |
<listitem> |
| 354 |
<para><literal>$host</literal> : Host - Address</para> |
| 355 |
</listitem> |
| 356 |
<listitem> |
| 357 |
<para><literal>${prop:foo}</literal> : System property <emphasis>foo</emphasis></para> |
| 358 |
</listitem> |
| 359 |
<listitem> |
| 360 |
<para><literal>${env:FOO}</literal> : Environment variable <emphasis>FOO</emphasis></para> |
| 361 |
</listitem> |
| 362 |
</itemizedlist> |
| 363 |
</section> |
| 364 |
</section> |
| 365 |
|
| 366 |
<section id="security-restrictor"> |
| 367 |
<title>Jolokia Restrictors</title> |
| 368 |
<para> |
| 369 |
In order to provide fine grained security, Jolokia using the |
| 370 |
abstract concept of an <emphasis>Restrictor</emphasis>. It is |
| 371 |
represented by the Java interface |
| 372 |
<classname>org.jolokia.restrictor.Restrictor</classname> and |
| 373 |
comes with several implementations. The most prominent one is |
| 374 |
the <classname>PolicyRestrictor</classname> which is described |
| 375 |
in <xref linkend="security-policy"/>. This is also the |
| 376 |
restrictor which is active by default. For special needs, it is |
| 377 |
possible to provide a custom implementation of this |
| 378 |
interface for the WAR and OSGi agents. It is recommended to |
| 379 |
subclass either |
| 380 |
<classname>org.jolokia.restrictor.AllowAllRestrictor</classname> |
| 381 |
or |
| 382 |
<classname>org.jolokia.restrictor.DenyAllRestrictor</classname>. |
| 383 |
</para> |
| 384 |
<para> |
| 385 |
For the WAR agent (<xref linkend="agents-war"/>), a subclass |
| 386 |
of <classname>org.jolokia.http.AgentServlet</classname> should |
| 387 |
be created which overrides the |
| 388 |
<methodname>createRestrictor()</methodname> |
| 389 |
</para> |
| 390 |
<programlisting language="java"><![CDATA[ |
| 391 |
public class RestrictedAgentServlet extends AgentServlet { |
| 392 |
|
| 393 |
@Override |
| 394 |
protected Restrictor createRestrictor(String policyLocation) { |
| 395 |
return new MyOwnRestrictor(); |
| 396 |
} |
| 397 |
}]]></programlisting> |
| 398 |
<para> |
| 399 |
<literal>policyLocation</literal> is a URL pointing to the |
| 400 |
policy file, which is either the default value |
| 401 |
<filename>classpath:/jolokia-access.xml</filename> or the |
| 402 |
value specified with the init parameter |
| 403 |
<literal>policyLocation</literal>. This servlet can then be |
| 404 |
easily configured in a custom <filename>web.xml</filename> |
| 405 |
the same way as the Jolokia agent. |
| 406 |
</para> |
| 407 |
<para> |
| 408 |
For programmatic usage there is an even simpler way: |
| 409 |
<classname>AgentServlet</classname> provides an constructor |
| 410 |
which takes an restrictor as argument, so no sublcassing is |
| 411 |
required in this case. |
| 412 |
</para> |
| 413 |
<para> |
| 414 |
For an OSGi agent (<xref linkend="agents-osgi"/>), |
| 415 |
<classname>org.jolokia.osgi.servlet.JolokiaServlet</classname> |
| 416 |
is the proper extension point. It can be subclassed the same |
| 417 |
way as shown above and allows a restrictor implementation as |
| 418 |
constructor parameter, too. In contrast to |
| 419 |
<classname>AgentServlet</classname> this class is also OSGi |
| 420 |
exported and can be referenced from other |
| 421 |
bundles. Additionally, the OSGi agent can also pick up a |
| 422 |
restrictor as an OSGi service. See <xref |
| 423 |
linkend="agents-osgi"/> for details. |
| 424 |
</para> |
| 425 |
|
| 426 |
</section> |
| 427 |
|
| 428 |
</chapter> |