| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package JMX::Jmx4Perl::Agent::Jolokia::WebXmlHandler; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | JMX::Jmx4Perl::Agent::Jolokia::WebXmlHandler - Handler for web.xml | ||
| 8 | transformation | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This module is repsonsible for various manipulations on a F<web.xml> descriptor | ||
| 13 | as found in JEE WAR archives. It uses L<XML::LibXML> for the dirty work, and | ||
| 14 | L<XML::Tidy> to clean up after the manipulation. The later module is optional, | ||
| 15 | but recommended. | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =over 4 | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use Data::Dumper; | ||
| 24 | use vars qw($HAS_LIBXML $HAS_XML_TWIG); | ||
| 25 | use strict; | ||
| 26 | |||
| 27 | # Trigger for <login-config> | ||
| 28 | my $REALM = "Jolokia"; | ||
| 29 | |||
| 30 | # Class used as proxy dispatcher | ||
| 31 | my $JSR_160_PROXY_CLASS = "org.jolokia.jsr160.Jsr160RequestDispatcher"; | ||
| 32 | |||
| 33 | BEGIN { | ||
| 34 | $HAS_LIBXML = eval "require XML::LibXML; use XML::LibXML::XPathContext; 1"; | ||
| 35 | $HAS_XML_TWIG = eval "require XML::Twig; 1"; | ||
| 36 | } | ||
| 37 | |||
| 38 | =item $handler = JMX::Jmx4Perl::Agent::Jolokia::WebXmlHandler->new(%args) | ||
| 39 | |||
| 40 | Creates a new handler. The following arguments can be used: | ||
| 41 | |||
| 42 | "logger" Logger to use | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $class = shift; | ||
| 48 | my %args = @_; | ||
| 49 | my $self = {logger => $args{logger}}; | ||
| 50 | bless $self,(ref($class) || $class); | ||
| 51 | |||
| 52 | $self->_fatal("No XML::LibXML found. Please install it to allow changes and queries on web.xml") unless $HAS_LIBXML; | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | =item $handler->add_security($webxml,{ role => $role }) | ||
| 58 | |||
| 59 | Add a security constraint to the given web.xml. This triggers on the realm | ||
| 60 | "Jolokia" on the loging-config and the URL-Pattern "/*" for the security | ||
| 61 | mapping. Any previous sections are removed and replaced. | ||
| 62 | |||
| 63 | C<$role> is the role to insert. | ||
| 64 | |||
| 65 | This method returns the updated web.xml as a string. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub add_security { | ||
| 70 | my $self = shift; | ||
| 71 | my $webxml = shift; | ||
| 72 | my $args = shift; | ||
| 73 | |||
| 74 | my $doc = XML::LibXML->load_xml(string => $webxml); | ||
| 75 | |||
| 76 | my $parent = $doc->getDocumentElement; | ||
| 77 | $self->_remove_security_elements($doc); | ||
| 78 | |||
| 79 | $self->_create_login_config($doc,$parent); | ||
| 80 | $self->_create_security_constraint($doc,$parent,$args->{role}); | ||
| 81 | $self->_create_security_role($doc,$parent,$args->{role}); | ||
| 82 | $self->_info("Added security mapping for role ","[em]",$args->{role},"[/em]"); | ||
| 83 | return $self->_cleanup_doc($doc); | ||
| 84 | } | ||
| 85 | |||
| 86 | =item $handler->remove_security($webxml) | ||
| 87 | |||
| 88 | Remove login-config with Realm "Jolokia" and security constraint to | ||
| 89 | "/*" along with the associated role definit. Return the updated web.xml | ||
| 90 | as string. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub remove_security { | ||
| 95 | my $self = shift; | ||
| 96 | my $webxml = shift; | ||
| 97 | |||
| 98 | my $doc = XML::LibXML->load_xml(string => $webxml); | ||
| 99 | |||
| 100 | $self->_remove_security_elements($doc); | ||
| 101 | $self->_info("Removed security mapping"); | ||
| 102 | |||
| 103 | return $self->_cleanup_doc($doc); | ||
| 104 | } | ||
| 105 | |||
| 106 | =item $handler->add_jsr160_proxy($webxml) | ||
| 107 | |||
| 108 | Adds a JSR-160 proxy declaration which is contained as init-param of the | ||
| 109 | servlet definition ("dispatcherClasses"). If the init-param is missing, a new | ||
| 110 | is created otherwise an existing is updated. Does nothing, if the init-param | ||
| 111 | "dispatcherClasses" already contains the JSR 160 dispacher. | ||
| 112 | |||
| 113 | Returns the updated web.xml as string. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | sub add_jsr160_proxy { | ||
| 118 | my $self = shift; | ||
| 119 | my $webxml = shift; | ||
| 120 | |||
| 121 | my $doc = XML::LibXML->load_xml(string => $webxml); | ||
| 122 | my @init_params = $self->_init_params($doc,"dispatcherClasses"); | ||
| 123 | if (!@init_params) { | ||
| 124 | $self->_add_jsr160_proxy($doc); | ||
| 125 | $self->_info("Added JSR-160 proxy"); | ||
| 126 | } elsif (@init_params == 1) { | ||
| 127 | my $param = $init_params[0]; | ||
| 128 | my ($value,$classes) = $self->_extract_dispatcher_classes($init_params[0]); | ||
| 129 | unless (grep { $_ eq $JSR_160_PROXY_CLASS } @$classes) { | ||
| 130 | $self->_update_text($value,join(",",@$classes,$JSR_160_PROXY_CLASS)); | ||
| 131 | $self->_info("Added JSR-160 proxy"); | ||
| 132 | } else { | ||
| 133 | $self->_info("JSR-160 proxy already active"); | ||
| 134 | return undef; | ||
| 135 | } | ||
| 136 | } else { | ||
| 137 | # Error | ||
| 138 | $self->_fatal("More than one init-param 'dispatcherClasses' found"); | ||
| 139 | } | ||
| 140 | |||
| 141 | return $self->_cleanup_doc($doc); | ||
| 142 | } | ||
| 143 | |||
| 144 | =item $handler->remove_jsr160_proxy($webxml) | ||
| 145 | |||
| 146 | Removes a JSR-160 proxy declaration which is contained as init-param of the | ||
| 147 | servlet definition ("dispatcherClasses"). Does nothing, if the init-param | ||
| 148 | "dispatcherClasses" already doese not contain the JSR 160 dispacher. | ||
| 149 | |||
| 150 | Returns the updated web.xml as string. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub remove_jsr160_proxy { | ||
| 155 | my $self = shift; | ||
| 156 | my $webxml = shift; | ||
| 157 | |||
| 158 | my $doc = XML::LibXML->load_xml(string => $webxml); | ||
| 159 | my @init_params = $self->_init_params($doc,"dispatcherClasses"); | ||
| 160 | if (!@init_params) { | ||
| 161 | $self->info("No JSR-160 proxy active"); | ||
| 162 | return undef; | ||
| 163 | } elsif (@init_params == 1) { | ||
| 164 | my ($value,$classes) = $self->_extract_dispatcher_classes($init_params[0]); | ||
| 165 | if (grep { $_ eq $JSR_160_PROXY_CLASS } @$classes) { | ||
| 166 | $self->_update_text($value,join(",",grep { $_ ne $JSR_160_PROXY_CLASS } @$classes)); | ||
| 167 | $self->_info("Removed JSR-160 proxy"); | ||
| 168 | } else { | ||
| 169 | $self->_info("No JSR-160 proxy active"); | ||
| 170 | return undef; | ||
| 171 | } | ||
| 172 | } else { | ||
| 173 | $self->_fatal("More than one init-param 'dispatcherClasses' found"); | ||
| 174 | } | ||
| 175 | |||
| 176 | return $self->_cleanup_doc($doc); | ||
| 177 | } | ||
| 178 | |||
| 179 | =item $handler->find($webxml,$xquery) | ||
| 180 | |||
| 181 | Find a single element with a given XQuery query. Croaks if more than one | ||
| 182 | element is found. Returns either C<undef> (nothing found) or the matched | ||
| 183 | node's text content. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub find { | ||
| 188 | my $self = shift; | ||
| 189 | my $webxml = shift; | ||
| 190 | my $query = shift; | ||
| 191 | |||
| 192 | my $doc = XML::LibXML->load_xml(string => $webxml); | ||
| 193 | |||
| 194 | my @nodes = $self->_find_nodes($doc,$query); | ||
| 195 | $self->_fatal("More than one element found matching $query") if @nodes > 1; | ||
| 196 | return @nodes == 0 ? undef : $nodes[0]->textContent; | ||
| 197 | } | ||
| 198 | |||
| 199 | =item $handler->has_authentication($webxml) | ||
| 200 | |||
| 201 | Checks, whether authentication is switched on. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub has_authentication { | ||
| 206 | my $self = shift; | ||
| 207 | my $webxml = shift; | ||
| 208 | |||
| 209 | $self->find | ||
| 210 | ($webxml, | ||
| 211 | "//j2ee:security-constraint[j2ee:web-resource-collection/j2ee:url-pattern='/*']/j2ee:auth-constraint/j2ee:role-name"); | ||
| 212 | } | ||
| 213 | |||
| 214 | =item $handler->has_jsr160_proxy($webxml) | ||
| 215 | |||
| 216 | Checks, whether a JSR-160 proxy is configured. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | sub has_jsr160_proxy { | ||
| 221 | my $self = shift; | ||
| 222 | my $webxml = shift; | ||
| 223 | |||
| 224 | my $doc = XML::LibXML->load_xml(string => $webxml); | ||
| 225 | |||
| 226 | my @init_params = $self->_init_params($doc,"dispatcherClasses"); | ||
| 227 | if (@init_params > 1) { | ||
| 228 | $self->_fatal("More than one dispatcherClasses init-param found"); | ||
| 229 | } elsif (@init_params == 1) { | ||
| 230 | my $param = $init_params[0]; | ||
| 231 | my ($value,$classes) = $self->_extract_dispatcher_classes($init_params[0]); | ||
| 232 | return grep { $_ eq $JSR_160_PROXY_CLASS } @$classes; | ||
| 233 | } else { | ||
| 234 | return 0; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | # =============================================================================== | ||
| 239 | |||
| 240 | sub _remove_security_elements { | ||
| 241 | my $self = shift; | ||
| 242 | my $doc = shift; | ||
| 243 | my $role = shift; | ||
| 244 | |||
| 245 | $self->_remove_login_config($doc); | ||
| 246 | my $role = $self->_remove_security_constraint($doc); | ||
| 247 | $self->_remove_security_role($doc,$role); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub _create_login_config { | ||
| 251 | my $self = shift; | ||
| 252 | my $doc = shift; | ||
| 253 | my $parent = shift; | ||
| 254 | my $l = _e($doc,$parent,"login-config"); | ||
| 255 | _e($doc,$l,"auth-method","BASIC"); | ||
| 256 | _e($doc,$l,"realm-name",$REALM); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub _create_security_constraint { | ||
| 260 | my $self = shift; | ||
| 261 | my $doc = shift; | ||
| 262 | my $parent = shift; | ||
| 263 | my $role = shift; | ||
| 264 | |||
| 265 | my $s = _e($doc,$parent,"security-constraint"); | ||
| 266 | my $w = _e($doc,$s,"web-resource-collection"); | ||
| 267 | _e($doc,$w,"web-resource-name","Jolokia-Agent Access"); | ||
| 268 | _e($doc,$w,"url-pattern","/*"); | ||
| 269 | my $a = _e($doc,$s,"auth-constraint"); | ||
| 270 | _e($doc,$a,"role-name",$role); | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _create_security_role { | ||
| 274 | my $self = shift; | ||
| 275 | my $doc = shift; | ||
| 276 | my $parent = shift; | ||
| 277 | my $role = shift; | ||
| 278 | |||
| 279 | my $s = _e($doc,$parent,"security-role"); | ||
| 280 | _e($doc,$s,"role-name",$role); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub _remove_security_constraint { | ||
| 284 | my $self = shift; | ||
| 285 | my $doc = shift; | ||
| 286 | |||
| 287 | my @s = $doc->getElementsByTagName("security-constraint"); | ||
| 288 | for my $s (@s) { | ||
| 289 | my @r = $s->getElementsByTagName("role-name"); | ||
| 290 | my $role; | ||
| 291 | for my $r (@r) { | ||
| 292 | $role = $r->textContent; | ||
| 293 | } | ||
| 294 | my @u = $s->getElementsByTagName("url-pattern"); | ||
| 295 | for my $u (@u) { | ||
| 296 | if ($u->textContent eq "/*") { | ||
| 297 | $s->parentNode->removeChild($s); | ||
| 298 | return $role; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | sub _remove_login_config { | ||
| 305 | my $self = shift; | ||
| 306 | my $doc = shift; | ||
| 307 | |||
| 308 | my @l = $doc->getElementsByTagName("realm-name"); | ||
| 309 | for my $l (@l) { | ||
| 310 | if ($l->textContent eq $REALM) { | ||
| 311 | my $toRemove = $l->parentNode; | ||
| 312 | $toRemove->parentNode->removeChild($toRemove); | ||
| 313 | return; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | sub _remove_security_role { | ||
| 319 | my $self = shift; | ||
| 320 | my $doc = shift; | ||
| 321 | my $role = shift; | ||
| 322 | |||
| 323 | my @s = $doc->getElementsByTagName("security-role"); | ||
| 324 | for my $s (@s) { | ||
| 325 | my @r = $s->getElementsByTagName("role-name"); | ||
| 326 | for my $r (@r) { | ||
| 327 | if ($r->textContent eq $role) { | ||
| 328 | $s->parentNode->removeChild($s); | ||
| 329 | return; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | sub _init_params { | ||
| 336 | my $self = shift; | ||
| 337 | my $doc = shift; | ||
| 338 | my $param_name = shift; | ||
| 339 | |||
| 340 | return $self->_find_nodes | ||
| 341 | ($doc, | ||
| 342 | "/j2ee:web-app/j2ee:servlet[j2ee:servlet-name='jolokia-agent']/j2ee:init-param[j2ee:param-name='$param_name']"); | ||
| 343 | } | ||
| 344 | |||
| 345 | sub _extract_dispatcher_classes { | ||
| 346 | my $self = shift; | ||
| 347 | my $param = shift; | ||
| 348 | |||
| 349 | my @values = $self->_find_nodes($param,"j2ee:param-value"); | ||
| 350 | $self->_fatal("No or more than one param-value found") if (!@values || @values > 1); | ||
| 351 | my $value = $values[0]; | ||
| 352 | my $content = $value->textContent(); | ||
| 353 | my @classes = split /\s*,\s*/,$content; | ||
| 354 | return ($value,\@classes); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub _update_text { | ||
| 358 | my $self = shift; | ||
| 359 | my $el = shift; | ||
| 360 | my $value = shift; | ||
| 361 | |||
| 362 | my $parent = $el->parentNode; | ||
| 363 | $parent->removeChild($el); | ||
| 364 | $parent->appendTextChild($el->nodeName,$value); | ||
| 365 | } | ||
| 366 | |||
| 367 | sub _add_jsr160_proxy { | ||
| 368 | my $self = shift; | ||
| 369 | my $doc = shift; | ||
| 370 | my @init_params = $self->_find_nodes | ||
| 371 | ($doc, | ||
| 372 | "/j2ee:web-app/j2ee:servlet[j2ee:servlet-name='jolokia-agent']/j2ee:init-param"); | ||
| 373 | my $first = $init_params[0] || $self->_fatal("No init-params found"); | ||
| 374 | my $new_init = $doc->createElement("init-param"); | ||
| 375 | _e($doc,$new_init,"param-name","dispatcherClasses"); | ||
| 376 | _e($doc,$new_init,"param-value",$JSR_160_PROXY_CLASS); | ||
| 377 | $first->parentNode->insertBefore($new_init,$first); | ||
| 378 | } | ||
| 379 | |||
| 380 | sub _find_nodes { | ||
| 381 | my $self = shift; | ||
| 382 | my $doc = shift; | ||
| 383 | my $query = shift; | ||
| 384 | |||
| 385 | my $xpc = XML::LibXML::XPathContext->new; | ||
| 386 | $xpc->registerNs('j2ee', 'http://java.sun.com/xml/ns/j2ee'); | ||
| 387 | return $xpc->findnodes($query,$doc); | ||
| 388 | } | ||
| 389 | |||
| 390 | sub _e { | ||
| 391 | my $doc = shift; | ||
| 392 | my $parent = shift; | ||
| 393 | my $e = $doc->createElement(shift); | ||
| 394 | my $c = shift; | ||
| 395 | if ($c) { | ||
| 396 | $e->appendChild($doc->createTextNode($c)); | ||
| 397 | } | ||
| 398 | $parent->appendChild($e); | ||
| 399 | return $e; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub _cleanup_doc { | ||
| 403 | my $self = shift; | ||
| 404 | my $doc = shift; | ||
| 405 | if ($HAS_XML_TWIG) { | ||
| 406 | my $ret = XML::Twig->nparse_pp($doc->toString)->toString(1); | ||
| 407 | #print $ret; | ||
| 408 | return $ret; | ||
| 409 | } else { | ||
| 410 | return $doc->toString(1); | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | sub _fatal { | ||
| 415 | my $self = shift; | ||
| 416 | $self->{logger}->error(@_); | ||
| 417 | die "\n"; | ||
| 418 | } | ||
| 419 | |||
| 420 | sub _info { | ||
| 421 | my $self = shift; | ||
| 422 | $self->{logger}->info(@_); | ||
| 423 | } | ||
| 424 | |||
| 425 | =back | ||
| 426 | |||
| 427 | =head1 LICENSE | ||
| 428 | |||
| 429 | This file is part of jmx4perl. | ||
| 430 | Jmx4perl is free software: you can redistribute it and/or modify | ||
| 431 | it under the terms of the GNU General Public License as published by | ||
| 432 | The Free Software Foundation, either version 2 of the License, or | ||
| 433 | (at your option) any later version. | ||
| 434 | |||
| 435 | jmx4perl is distributed in the hope that it will be useful, | ||
| 436 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 437 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 438 | GNU General Public License for more details. | ||
| 439 | |||
| 440 | You should have received a copy of the GNU General Public License | ||
| 441 | along with jmx4perl. If not, see <http://www.gnu.org/licenses/>. | ||
| 442 | |||
| 443 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 444 | further details. | ||
| 445 | |||
| 446 | =head1 AUTHOR | ||
| 447 | |||
| 448 | roland@cpan.org | ||
| 449 | |||
| 450 | =cut | ||
| 451 | |||
| 452 | 1; |