Skip to main content Skip to complementary content

Building a Linux shell script to convert clear text to Base64 encoding string

Talend MetaServlet string needs to be encoded to Base64 when requesting Talend Administration Center MetaServlet REST API.

To post a JSON script, take metaservlet - “addServer” as an example, as shown below. This JSON sample adds a new Jobserver/Runtime server into Talend Administration Center server page.

{"actionName": "addServer","adminConsolePort": 8040,"authPass": "admin","authUser":"admin@company.com",
"commandPort": 8000,"description": "RemoteRT", "filePort": 8001,"host": "34.251.88.225",
"instance": "trun", "label": "Remote RT server auto-scaling","mgmtRegPort": 1099,
"mgmtServerPort": 44444,"monitoringPort": 8888,"runtimePassword": "tadmin",
"runtimeUsername": "tadmin","shutdownBehavior": "Stop",
"timeoutUnknownState": "120","useSSL": false}

Procedure

  1. Send this JSON script to the Talend Administration Center metaservlet API url. The REST WS call should be as follow:
    http://{TACHost}:8080/org.talend.administrator/metaServlet? {"actionName": "addServer",
    "adminConsolePort": 8040,"authPass":  "admin","authUser": "admin@company.com",
    "commandPort": 8000,"description": "RemoteRT","filePort": 8001,
    "host": "34.251.88.225","instance": "trun","label": "Remote RT server auto-scaling",
    "mgmtRegPort": 1099,"mgmtServerPort": 44444,"monitoringPort": 8888,"runtimePassword":"tadmin",
    "runtimeUsername": "tadmin","shutdownBehavior": "Stop","timeoutUnknownState": "120","useSSL": false}
  2. However, the JSON string provided to the API url need to be encoded within Base64, so the final REST WS call will be as shown below:
    http://{TACHost}:8080/org.talend.administrator/metaServlet?eyJhY3Rpb25OYW1lI
    jogImFkZFNlcnZlciIsImFkbWluQ29uc29sZVBvcnQiOiA4MDQwLCJhdXRoUGFzcyI6ICJhZG1pbiIsImF1dGhVc2VyI
    jogImFkbWluQGNvbXBhbnkuY29tIiwiY29tbWFuZFBvcnQiOiA4MDAwLCJkZXNjcmlwdGlvbiI6ICJSZW1vdGVSVCIsI
    mZpbGVQb3J0IjogODAwMSwiaG9zdCI6ICIzNC4yNTEuODguMjI1IiwiaW5zdGFuY2UiOiAidHJ1biIsImxhYmVsIjogI
    lJlbW90ZSBSVCBzZXJ2ZXIgYXV0by1zY2FsaW5nIiwibWdtdFJlZ1BvcnQiOiAxMDk5LCJtZ210U2VydmVyUG9ydCI6I
    DQ0NDQ0LCJtb25pdG9yaW5nUG9ydCI6IDg4ODgsInJ1bnRpbWVQYXNzd29yZCI6ICJ0YWRtaW4iLCJydW50aW1lVXNlcm
    5hbWUiOiAidGFkbWluIiwic2h1dGRvd25CZWhhdmlvciI6ICJTdG9
    wIiwidGltZW91dFVua25vd25TdGF0ZSI6ICIxMjAiLCJ1c2VTU0wiOiBmYWxzZX0
  3. Encode the string by using the Linux shell script example shown below. Save the script as base64url.sh file under /app repository (or at any location of your preference).
    #! /bin/bash
                            
    table=(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 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 - _ =)
                            
    #text="{\"alg\":\"RS256\",\"typ\":\"JWT\"}"
    #text="{\"iss\":\"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com\",\"scope\":\"https://www.googleapis.com/auth/prediction\",\"aud\":\"https://accounts.google.com/o/oauth2/token\",\"exp\":1328554385,\"iat\":1328550785}"
                            
    read text
                            
    # breaking binary represantation of $text in to 6-bit blocks
    for line in $(echo -n "$text" | xxd -b -g 0 | cut -d ' ' -f 2 | paste -s -d '' | fold -w 6 -s)
    do
        length=$(echo ${#line})
        let padding=6-$length
        s="0"
        p=""
                            
        # padding 6-bit to 8-bit by adding zeros to the right
        if (($padding > 0)); then
            while ((${#p} < "$padding")); do
                p="$p$s";
            done;
        fi
                            
        # convert binary to decimal
        n=$(echo "ibase=2;$line$p" | bc)
        # output the table looked up character
        echo -n ${table[n]}
    done
                        

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!