Redefine variable

Objectives

When we said that a variable is immutable when an integrator defined it in structured data, we said that it had to be done explicitly. Let’s see how that works.

Prerequisites

  • We assume that Rougail’s library is installed on your computer.

  • It is possible to retrieve the current state of the various Rougail files manipulated in this tutorial step by checking out the corresponding tag of the rougail-tutorials git repository. Each tag corresponds to a stage of progress in the tutorial. Of course, you can also decide to copy/paste or download the tutorial files contents while following the tutorial steps.

If you want to follow this tutorial with the help of the corresponding rougail-tutorials git repository, this workshop page corresponds to the tags v1.1_240 to v1.1_241

git clone https://forge.cloud.silique.fr/stove/rougail-tutorials.git
git switch --detach v1.1_240

Username is mandatory if a password is set

We now have two new variables following the same pattern as the previous three one:

  • A variable with the new type unix_username username, this variable is accessible in type HTTP, HTTPS/SSL, SOCKS4 or SOCKS5

  • A variable with the new type secret password, this variable is accessible in type HTTP, HTTPS/SSL, SOCKS4 or SOCKS5

Nothing really new from now. But we haven’t said everything! In fact, we have to deal with a new situation. We have three coherence configuration choices:

  • either we fill in the username/password pair

  • we fill only the username

  • or leave it blank

Defining only a password makes no sense.

There are several ways to solve this case. Let’s assume that if the operator sets a password, they must also enter a username. The password can be null.

In Rougail format, we would say that the password is nullable and that the username has a variable calculation to determine if it is nullable. If the password is empty, the username is nullable; otherwise, it is not.

Here’s how to do it:

The foxyproxy/00-foxyproxy.yml
%YAML 1.2
---
version: 1.1

proxies:
  description: Proxy configuration
  _type: sequence

  title:
    description: Title or Description
    mandatory: false

  type:
    description: Proxy Type
    choices:
      - HTTP
      - HTTPS/SSL
      - SOCKS4
      - SOCKS5
      - PAC URL
      - WPAD
      - System (use system settings)
      - Direct (no proxy)
    default: Direct (no proxy)

  color:
    description: Color
    regexp: ^#(?:[0-9a-f]{3}){1,2}$
    default:
      jinja: >-
        #{%- for i in range(6) -%}{{- '0123456789abcdef' | random -}}{%- endfor -%}
      description: random color value
    auto_save: true

  address:
    description: IP address, DNS name, server name
    type: domainname
    params:
      allow_ip: true
      allow_without_dot: true
    default:
      description: copy HTTP address if proxy is not "Manual"
      variable: firefox.manual.http_proxy.address
      propertyerror: false
    disabled:
      jinja: |-
        {{ _.type not in ['HTTP', 'HTTPS/SSL', 'SOCKS4', 'SOCKS5'] }}
      return_type: boolean
      description: |-
        if type not in:
        - HTTP
        - HTTPS/SSL
        - SOCKS4
        - SOCKS5

  port:
    description: Port
    type: port
    default:
      description: copy HTTP port if proxy is not "Manual"
      variable: firefox.manual.http_proxy.port
      propertyerror: false
    disabled:
      jinja: |-
        {{ _.type not in ['HTTP', 'HTTPS/SSL', 'SOCKS4', 'SOCKS5'] }}
      return_type: boolean
      description: |-
        if type not in:
        - HTTP
        - HTTPS/SSL
        - SOCKS4
        - SOCKS5

  username:
    description: Username
    type: unix_user
    mandatory:
      variable: _.password
      when_not: null
    disabled:
      jinja: |-
        {{ _.type not in ['HTTP', 'HTTPS/SSL', 'SOCKS4', 'SOCKS5'] }}
      return_type: boolean
      description: |-
        if type not in:
        - HTTP
        - HTTPS/SSL
        - SOCKS4
        - SOCKS5

  password:
    description: Password
    type: secret
    mandatory: false
    disabled:
      jinja: |-
        {{ _.type not in ['HTTP', 'HTTPS/SSL', 'SOCKS4', 'SOCKS5'] }}
      return_type: boolean
      description: |-
        if type not in:
        - HTTP
        - HTTPS/SSL
        - SOCKS4
        - SOCKS5

  url:
    description: URL
    type: web_address
    default:
      description: copy HTTP address if proxy is "Auto"
      variable: firefox.auto
      propertyerror: false
    disabled:
      jinja: |-
        {{ _.type not in ['PAC URL', 'WPAD'] }}
      return_type: boolean
      description: |-
        if type is not in:
        - PAC URL
        - WPAD
...

Now let’s test with the following user data file:

The config/02/config.yml user data
 1---
 2foxyproxy:
 3  proxies:
 4    - title: My company
 5      type: HTTP
 6      color: '#66cc66'
 7      address: proxy.company.net
 8      port: 8080
 9      username: one_user
10      password: P4ssW0rD
11    - title: An other company
12      type: HTTP
13      address: proxy.company.net
14      port: 8080

If we launch the Rougail CLI:

rougail -m firefox/ -s Firefox -xn FoxyProxy -xd 0 foxyproxy/ --types types/proxy --modes_level basic standard advanced -u yaml -yf config/02/config.yml

We have this output:

╭────────────── Caption ───────────────╮
│ Variable Default value               │
│          Modified value              │
│          (⏳ Original default value) │
╰──────────────────────────────────────╯
Variables:
┣━━ 📂 firefox (Firefox)
┣━━ 📓 proxy_mode (Configure Proxy Access to the Internet): No proxy
┗━━ 📂 dns_over_https (DNS over HTTPS)
    ┗━━ 📓 enable_dns_over_https (Enable DNS over HTTPS): false
┗━━ 📂 foxyproxy (FoxyProxy)
    ┗━━ 📂 proxies (Proxy configuration)
        ┣━━ 📂 title (Title or Description)
        ┣━━ 📓 title (Title or Description): My company ◀ loaded from the 
        YAML file "config/02/config.yml"
        ┣━━ 📓 type (Proxy Type): HTTP ◀ loaded from the YAML file 
        "config/02/config.yml" (⏳ Direct (no proxy))
        ┣━━ 📓 color (Color): #66cc66 ◀ loaded from the YAML file 
        "config/02/config.yml"
        ┣━━ 📓 address (IP address, DNS name, server name): 
        proxy.company.net ◀ loaded from the YAML file 
        "config/02/config.yml"
        ┣━━ 📓 port (Port): 8080 ◀ loaded from the YAML file 
        "config/02/config.yml"
        ┣━━ 📓 username (Username): one_user ◀ loaded from the YAML file 
        "config/02/config.yml"
        ┗━━ 📓 password (Password): ********** ◀ loaded from the YAML file 
            "config/02/config.yml"
        ┗━━ 📂 title (Title or Description)
            ┣━━ 📓 title (Title or Description): An other company ◀ loaded from 
            the YAML file "config/02/config.yml"
            ┣━━ 📓 type (Proxy Type): HTTP ◀ loaded from the YAML file 
            "config/02/config.yml" (⏳ Direct (no proxy))
            ┣━━ 📓 color (Color): #100796
            ┣━━ 📓 address (IP address, DNS name, server name): 
            proxy.company.net ◀ loaded from the YAML file 
            "config/02/config.yml"
            ┣━━ 📓 port (Port): 8080 ◀ loaded from the YAML file 
            "config/02/config.yml"
            ┣━━ 📓 username (Username): null
            ┗━━ 📓 password (Password): null

Note

The value of a variable of type secret is replaced by asterisks (*) when it is displayed.

Redefine it to password is hidden if an username is not set

For those who follow the tutorial with the help of the git repository

Now you need to checkout the v1.1_241 version:

git switch --detach v1.1_241

Another solution is to say to ourselves: the username is never mandatory (just like the password) but the password is hidden if the username is empty.

We could modify the structure file as we have been doing since the beginning of this tutorial. Perhaps it’s time to illustrate how to redefine a variable.

Redefine means that the variable is unify (combine) from multiple variables declarations.

So we will create a new file with, inside, the same variable name.

First of all, we will to redefine the mandatory attribute of the username variable.

Secondly we will added a new calculation in the Hidden variable parameters.

Here is the final result of this file:

The foxyproxy/10-redefine.yml
%YAML 1.2
---
version: 1.1

proxies:

  username:
    redefine: true
    mandatory: false

  password:
    redefine: true
    hidden:
      variable: _.username
      when: null
      description: if username is empty
...

Key points

Redefine a variable involves changing some of its parameters with new ones.