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 this more dynamically instead of a fixed value.

Thanks in advance

 

  • 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

     

5 Replies

  • I´m not sure if I get your question right.

    You are able to switch between persistence profiles in case they are of the same type as the default persistance profile of your virtual server.

    You may use a Cookie Hash persistance profile in case your server sets a JSESSIONID cookie in its responses. This will be learned and if the client provides the cookie with the next request it will persist.

    What is your exact use case?

    • F5_LB_Eng's avatar
      F5_LB_Eng
      Icon for Cirrostratus rankCirrostratus

      You may use a Cookie Hash persistance profile in case your server sets a JSESSIONID cookie in its responses. This will be learned and if the client provides the cookie with the next request it will persist.

      --- this is one i am looking  but cookied is abc_SESSIONID_<id>_<client>=HASH  , the cookie name itself is dynamic Which is the problem

    • StephanManthey's avatar
      StephanManthey
      Icon for MVP rankMVP

      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

       


  • abc_SESSIONID_<id>_<client>=HASH  , the cookie name itself is dynamic Which is the problem