Getting the Remote IP address in IA scripts

This one has from time to time caused me heartburn. The REMOTE_ADDR property is supposed to contain the external IP address of the system making the injection to the IA. A print of the properties variable sent to apia_http shows it:

2014-02-24 19:30:14,975 [printParms|printParms-1] DEBUG - properties: {http.request=/http/printParms_printParms?token=stuff, responseTransformers=ServiceHttpResponseTransformer, REQUEST_BODY={  
  "stuff1": "value1",  
  "stuff2": "value2"  
}, REMOTE_ADDR=/68.101.213.48:56252, Host=ec2-54-201-209-18.us-west-2.compute.amazonaws.com:8081, User-Agent=Apache-HttpClient/4.1.1 (java 1.5), Content-Length=46, http.method=POST, Accept-Encoding=gzip,deflate, Content-Type=application/json, MULE_ORIGINATING_ENDPOINT=endpoint.https.ec2.54.201.209.18.us.west.2.compute.amazonaws.com.8081.http.printParms.printParms.responseTransformers.ServiceHttpResponseTransformer, Connection=true, http.version=HTTP/1.1}  

However, accessing this value gives 'null'. This loop goes through each element of the 'properties' object and prints them out (very handy for debugging incoming HTTP POST requests):

  var keys = properties.propertyNames();  
  while (keys.hasMoreElements())  
  {  
    var key = keys.nextElement();  
    var value = properties.getProperty(key);  
    ServiceAPI.getLogger().debug( "   " + key + ": '" + value + "'" );  

Unfortunately, when printing this way, we find that REMOTE_ADDR is 'null'!:

2014-02-24 19:30:15,026 [printParms|printParms-1] DEBUG - Begin loop:  
2014-02-24 19:30:15,027 [printParms|printParms-1] DEBUG -    http.request: '/http/printParms_printParms?token=stuff'  
2014-02-24 19:30:15,027 [printParms|printParms-1] DEBUG -    responseTransformers: 'ServiceHttpResponseTransformer'  
2014-02-24 19:30:15,028 [printParms|printParms-1] DEBUG -    REQUEST_BODY: '{  
  "stuff1": "value1",  
  "stuff2": "value2"  
}'  
2014-02-24 19:30:15,028 [printParms|printParms-1] DEBUG -    REMOTE_ADDR: 'null'  
2014-02-24 19:30:15,028 [printParms|printParms-1] DEBUG -    Host: 'ec2-54-201-209-18.us-west-2.compute.amazonaws.com:8081'  
2014-02-24 19:30:15,029 [printParms|printParms-1] DEBUG -    User-Agent: 'Apache-HttpClient/4.1.1 (java 1.5)'  
2014-02-24 19:30:15,029 [printParms|printParms-1] DEBUG -    Content-Length: '46'  
2014-02-24 19:30:15,029 [printParms|printParms-1] DEBUG -    http.method: 'POST'  
2014-02-24 19:30:15,029 [printParms|printParms-1] DEBUG -    Accept-Encoding: 'gzip,deflate'  
2014-02-24 19:30:15,030 [printParms|printParms-1] DEBUG -    Content-Type: 'application/json'  
2014-02-24 19:30:15,035 [printParms|printParms-1] DEBUG -    MULE_ORIGINATING_ENDPOINT: 'endpoint.https.ec2.54.201.209.18.us.west.2.compute.amazonaws.com.8081.http.printParms.printParms.responseTransformers.ServiceHttpResponseTransformer'  
2014-02-24 19:30:15,035 [printParms|printParms-1] DEBUG -    http.version: 'HTTP/1.1'  
2014-02-24 19:30:15,035 [printParms|printParms-1] DEBUG -    Connection: 'true'  

Update! I opened a ticket to get this investigated, and it turns out the REMOTE_ADDR property is an InetSocketAddress object and likely the toString method is not implemented or the IA can't handle it. So, check out the code below to retrieve information from this object.

  ServiceAPI.getLogger().debug( "properties.get( 'REMOTE_ADDR' ).getAddress().getCanonicalHostName(): " + properties.get( 'REMOTE_ADDR' ).getAddress().getCanonicalHostName() );  
ServiceAPI.getLogger().debug( "properties.get( 'REMOTE_ADDR' ).getAddress().getHostAddress(): " + properties.get( 'REMOTE_ADDR' ).getAddress().getHostAddress() );  
ServiceAPI.getLogger().debug( "properties.get( 'REMOTE_ADDR' ).getPort(): " + properties.get( 'REMOTE_ADDR' ).getPort() );  

Which print:

DEBUG - properties.get( 'REMOTE_ADDR' ).getAddress().getCanonicalHostName(): MACBOOKPRO-E60F  
DEBUG - properties.get( 'REMOTE_ADDR' ).getAddress().getHostAddress(): 10.2.1.196  
DEBUG - properties.get( 'REMOTE_ADDR' ).getPort(): 51593  

And the javadoc can be helpful sometimes.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Old stuff:

Great... now what? Well, I suspect the leading "/" is causing the getProperty method to do funky things. So we can just extract the value we need from the string-ized version of 'properties':

 

function getREMOTE_ADDR( properties ) {  
   prop = (""+properties)  
   items = prop.split( ', ' );  
   for( i in items ) {  
     ip = items[i].match( /REMOTE_ADDR=\/((?:[0-9]{1,3}\.){3}[0-9]{1,3})/ )  
     if( ip )  
       return ip[1]  
   }  
  
  
   return "";  
}  

So, keep this in mind the next time you need to get the external address of an incoming request. It could save you a few headaches!

xMatters Reference

Blog- Originally created by Travis Depuy

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk