Forum Discussion

F5_LB_Eng's avatar
F5_LB_Eng
Icon for Cirrostratus rankCirrostratus
Jul 14, 2022
Solved

dynamically instead of a fixed value in persistance

Hi , Any one has Irule for dynamically attribute  instead of a fixed value in persistance. Does the builtin cookie persistence allows something like this: Cookie_name: SESSIONID* So can we set th...
  • StephanManthey's avatar
    StephanManthey
    Jul 14, 2022

    You can go with universal persistence (have universal persistence configured for your virtual server) and use the following iRule:

     

    when RULE_INIT {
        set static::dynamic_persist_timeout 600
    }
    when HTTP_REQUEST {
        set http_cookie_list [HTTP::cookie names]
        if { [lsearch -glob $http_cookie_list "*_SESSIONID_*"] >= 0 } {
            set http_cookie_name [lindex $http_cookie_list [lsearch -glob $http_cookie_list "*_SESSIONID_*"]]
            set http_cookie_value [HTTP::cookie value [lindex $http_cookie_list [lsearch -glob $http_cookie_list "*_SESSIONID_*"]]]
            log local0. "cookiematch=${http_cookie_name},cookievalue=${http_cookie_value}"
            persist uie "${http_cookie_name}:${http_cookie_value}"
        }
    }
    when HTTP_RESPONSE {
        set http_cookie_list [HTTP::cookie names]
        if { [lsearch -glob $http_cookie_list "*_SESSIONID_*"] >= 0 } {
            set http_cookie_name [lindex $http_cookie_list [lsearch -glob $http_cookie_list "*_SESSIONID_*"]]
            set http_cookie_value [HTTP::cookie value [lindex $http_cookie_list [lsearch -glob $http_cookie_list "*_SESSIONID_*"]]]
            log local0. "cookiematch=${http_cookie_name},cookievalue=${http_cookie_value}"
            persist add uie "${http_cookie_name}:${http_cookie_value}" ${static::dynamic_persist_timeout}
        }
    }
    

     

    I tested this one in my environment using another BIG-IPs virtual server as a server emulation:

     

    when HTTP_REQUEST {
        set http_cookie_list [HTTP::cookie names]
        if { [lsearch -glob $http_cookie_list "*_SESSIONID_*"] >= 0 } {
            set http_cookie_name [lindex $http_cookie_list [lsearch -glob $http_cookie_list "*_SESSIONID_*"]]
            set http_cookie_value [HTTP::cookie value [lindex $http_cookie_list [lsearch -glob $http_cookie_list "*_SESSIONID_*"]]]
            log local0. "cookiematch=${http_cookie_name},cookievalue=${http_cookie_value}"
        } else {
            if { [info exists http_cookie_name] } {
                unset http_cookie_name
            }
            if { [info exists http_cookie_value] } {
                unset http_cookie_value
            }
            for { set i 0 } { $i < 3 } { incr i } {
                append http_cookie_name [lindex {A B C D E F G H I J K L M N O P Q R S T U V W Z Y Z} [expr int(rand()*26)]]
            }
            append http_cookie_name "_SESSIONID_"
            for { set i 0 } { $i < 6 } { incr i } {
                append http_cookie_name [lindex {0 1 2 3 4 5 6 7 8 9} [expr int(rand()*10)]]
            }
            append http_cookie_name "_"
            for { set i 0 } { $i < 8 } { incr i } {
                append http_cookie_name [lindex {A B C D E F G H I J K L M N O P Q R S T U V W Z Y Z 0 1 2 3 4 5 6 7 8 9} [expr int(rand()*36)]]
            }
            for { set i 0 } { $i < 16 } { incr i } {
                append http_cookie_value [lindex {a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9} [expr int(rand()*36)]]
            }
            log local0. "cookiegenerator=${http_cookie_name},valuegenerator=${http_cookie_value}"
        }
        HTTP::respond 200 content "responder\r\n" noserver Connection Close Set-Cookie "${http_cookie_name}=${http_cookie_value}"
        return
    }

    You can monitor your persistence table on CLI of your device under test:

    watch tmsh show ltm persist persist-records mode universal all-properties