iRules is a powerful scripting  language that allows you to  control network traffic in real time that can route, redirect, modify, drop, log or do just about anything else with network traffic passing through a BIG-IP proxy. iRules enables network programmability to consolidate functions across applications and services.

iRules LX: The Next Evolution of Network Programmability

iRules LX is the next stage of evolution for network programmability that brings Node.js language support to the BIG-IP with version 12.1.  Node.js allows JavaScript developers access to over 250,000 npm packages that makes code easier to write and maintain.  Development teams can access and work on code with the new iRules LX Workspace environment and the new plug-in available for the Eclipse IDE and can be used for continuous integration builds.

Extend with Node.js Support

iRules Language eXtensions (LX) enables node.js capabilities on the BIG-IP platform.  By using iRules LX (Node.js) extensions you can program iRules LX using javascript to control network traffic in real time.

There are only three simple Tcl commands that you need to extend iRules to Node.js

  • ILX::init – create Tcl handle, bind extension
  • ILX::call – send/receive data to/from extension method
  • ILX::notify – send data one-way to method, no response

There is only one Node.js command to know:

  • ILX.addMethod – create method, for use by above

Share and Reuse Code

iRules LX makes it easy for JavaScript developers to share and reuse Node.js code, and it makes it easy to update the code.  These bits of reusable code are called packages, or sometimes modules.  This makes it possible for you to compose complex business logic in node.js without sophisticated developers who know Tcl in-depth.  There are over 250,000 npm packages that makes code easier to write and maintain.

iRules LX Workspaces

LX Workspaces provides a portable development package environment with NPM-like directory structure that acts as the source for creation and modification of plugins and node.js extensions.  Workspace LX is developer-friendly, with Git-like staging and publishing that simplifies export and import to keep your files in sync for continuous integration and development.

iRules IDE Integration / Eclipse Plugin

Available soon, F5 is delivering a free downloadable plugin to enable our customers to develop iRules & iRules LX scripts via their own Eclipse IDE. The plugin provides modern IDE editing capabilities including syntax highlighting & validation, code completion, and code formatting for both Tcl and Javascript. This plugin will enable authenticated push and pull of iRules code files to and from the BIG-IP platform. This simplifies development workflows by enabling our customers to develop iRules scripts in a familiar development environment.

iRules LX Example: Use Node.js to Make an Off Box MySQL Lookup

This iRules example shows a connection to MySQL database via Node.js. This example uses Tcl iRulesand JavaScript code to make a MySQL call.  The iRules prompts the user for a basic auth username and password, then we lookup the username in a MySQL database table and return the user's groups.  

MySQL iRule (Tcl) Example:

  1. ###################################  
  2. # mysql_irulelx  
  3. # 2015-05-17 - Aaron Hooley - First draft example showing iRulesLX querying MySQL database  
  4. ###################################  
  5. when RULE_INIT {  
  6.     # Enable logging to /var/log/ltm?  
  7.     # 0=none, 1=error, 2=verbose  
  8.     set static::mysql_debug 2  
  9. }  
  10. when HTTP_REQUEST {  
  11.   
  12.     if {$static::mysql_debug >= 2}{log local0. "New HTTP request. \[HTTP::username\]=[HTTP::username]"}  
  13.     # If the client does not supply an HTTP basic auth username, prompt for one.  
  14.     # Else send the HTTP username to node.js and respond to client with result  
  15.     if {[HTTP::username] eq ""}{  
  16.   
  17.         HTTP::respond 401 content "Username was not included in your request.\nSend an HTTP basic auth username to test" WWW-Authenticate {Basic realm="iRulesLX example server"}  
  18.         if {$static::mysql_debug >= 2}{log local0. "No basic auth username was supplied. Sending 401 to prompt client for username"}  
  19.   
  20.     } else {  
  21.   
  22.         # Do some basic validation of the HTTP basic auth username  
  23.         # First fully URI decode it using https://devcentral.f5.com/codeshare?sid=523  
  24.         set tmp_username [HTTP::username]  
  25.         set username [URI::decode $tmp_username]  
  26.   
  27.         # Repeat decoding until the decoded version equals the previous value  
  28.         while { $username ne $tmp_username } {  
  29.             set tmp_username $username  
  30.             set username [URI::decode $tmp_username]  
  31.         }  
  32.         # Check that username only contains valid characters using https://devcentral.f5.com/codeshare?sid=726  
  33.         if {$username ne [set invalid_chars [scan $username {%[-a-zA-Z0-9_]}]]}{  
  34.             HTTP::respond 401 content "\nA valid username was not included in your request.\nSend an HTTP basic auth username to test\n" WWW-Authenticate {Basic realm="iRulesLX example server"}  
  35.             if {$static::mysql_debug >= 1}{log local0. "Invalid characters in $username. First invalid character was [string range $username [string length $invalid_chars] [string length $invalid_chars]]"}  
  36.   
  37.             # Exit this iRule event  
  38.             return  
  39.         }  
  40.         # Client supplied a valid username, so initialize the iRulesLX extension  
  41.         set RPC_HANDLE [ILX::init mysql_extension]  
  42.         if {$static::mysql_debug >= 2}{log local0. "\$RPC_HANDLE: $RPC_HANDLE"}  
  43.        
  44.         # Make the call and save the iRulesLX response  
  45.         # Pass the username and debug level as parameters  
  46.         #set rpc_response [ILX::call $RPC_HANDLE myql_nodejs $username $static::mysql_debug]  
  47.         set rpc_response [ILX::call $RPC_HANDLE myql_nodejs $username]  
  48.         if {$static::mysql_debug >= 2}{log local0. "\$rpc_response: $rpc_response"}  
  49.   
  50.         # The iRulesLX rule will return -1 if the query succeeded but no matching username was found  
  51.         if {$rpc_response == -1}{  
  52.             HTTP::respond 401 content "\nYour username was not found in MySQL.\nSend an HTTP basic auth username to test\n" WWW-Authenticate {Basic realm="iRulesLX example server"}  
  53.             if {$static::mysql_debug >= 1}{log local0. "Username was not found in MySQL"}  
  54.         } elseif {$rpc_response eq ""}{  
  55.             HTTP::respond 401 content "\nDatabase connection failed.\nPlease try again\n" WWW-Authenticate {Basic realm="iRulesLX example server"}  
  56.             if {$static::mysql_debug >= 1}{log local0. "MySQL query failed"}  
  57.         } else {  
  58.             # Send an HTTP 200 response with the groups retrieved from the iRulesLX plugin  
  59.             HTTP::respond 200 content "\nGroup(s) for '$username' are '$rpc_response'\n"  
  60.             if {$static::mysql_debug >= 1}{log local0. "Looked up \$username=$username and matched group(s): $rpc_response"}  
  61.         }  
  62.     }  
  63. }  

 

 

 

MySQL iRule LX (Node.js) Example

 

  1. /* ###################################### */  
  2. /* index.js counterpart for mysql_irulelx */  
  3.   
  4. /* Log debug to /var/log/ltm? 0=none, 1=errors only, 2=verbose */  
  5. var debug = 2;  
  6. if (debug >= 2) {console.log('Running extension1 index.js');}  
  7.   
  8. /* Import the f5-nodejs module. */  
  9. var f5 = require('f5-nodejs');  
  10.   
  11. /* Create a new rpc server for listening to TCL iRule calls. */  
  12. var ilx = new f5.ILXServer();  
  13.   
  14. /* Start listening for ILX::call and ILX::notify events. */  
  15. ilx.listen();  
  16.   
  17. /* Add a method and expect a username parameter and reply with response */  
  18. ilx.addMethod('myql_nodejs'function(username, response) {  
  19.   
  20.     if (debug >= 1) {console.log('my_nodejs' + ' ' + typeof(username.params()) + ' = ' + username.params());}  
  21.   
  22.     var mysql = require('mysql');  
  23.     var connection = mysql.createConnection({  
  24.         host     : '10.0.0.110',  
  25.         user     : 'bigip',  
  26.         password : 'bigip'  
  27.     });  
  28.   
  29.     // Connect to the MySQL server  
  30.     connection.connect(function(err) {  
  31.         if (err) {  
  32.             if (debug >= 1) {console.error('Error connecting to MySQL: ' + err.stack);}  
  33.             return;  
  34.         }  
  35.         if (debug >= 2) {console.log('Connected to MySQL as ID ' + connection.threadId);}  
  36.     });  
  37.   
  38.     // Perform the query. Escape the user-input using mysql.escape: https://www.npmjs.com/package/mysql#escaping-query-values  
  39.     connection.query('SELECT * from users_db.users_table where name = ' + mysql.escape(username.params(0)), function(err, rows, fields) {  
  40.         if (err) {  
  41.             // MySQL query failed for some reason, so send a null response back to the Tcl iRule  
  42.             if (debug >= 1) {console.error('Error with query: ' + err.stack);}  
  43.             response.reply('');  
  44.             return;  
  45.         } else {  
  46.             // Check for no result from MySQL  
  47.             if (rows < 1){  
  48.                 if (debug >= 1) {console.log('No matching records from MySQL');}  
  49.   
  50.                 // Return -1 to the Tcl iRule to show no matching records from MySQL  
  51.                 response.reply('-1');  
  52.             } else {  
  53.                 if (debug >= 2) {console.log('First row from MySQL is: ', rows[0]);}  
  54.                 //Return the group field from the first row to the Tcl iRule  
  55.                 response.reply(rows.pop());  
  56.             }  
  57.         }  
  58.     });  
  59.     // Close the MySQL connection  
  60.     connection.end();  
  61. });  

 

Getting Started

To get started with iRules LX, download a BIG-IP trial and follow the upcoming tutorials available on the DevCentral community.