echo "Hello, World!"#!/bin/bash
set -ux
NATS_URL= $(env| grep NATSJS_CLUSTER_URL | grep -oe '[^=]*$');
workdir=$PWD
cd /nsc
vault kv get -format=json "storagecentral/app/nats/sys"| jq -r .data.credentials|base64 -d > /tmp/natsconfig.zip
chmod 777 /tmp/natsconfig.zip
unzip -o /tmp/natsconfig.zip -x .git/*
cp -r  * /nsc
   account= $(cat /sandbox/authz.yaml|yq '.account.name')
   cd /nsc
for ns in $(yq '.account.roles[]' /sandbox/authz.yaml); do
    pub_permissions= $(echo "$ns" | yq e '.pub' -)
    sub_permissions=$(echo "$ns" | yq e '.sub' -)
    role=$(echo "$ns" | yq e '.name' -)
    key=$(nsc describe account $account -J|jq --arg role $role '.nats.signing_keys[] | select(.role == $role) | .'|jq -r .key)
    if [ ! -n "$key" ] then
	    echo "$role is not present"
        nsc edit account -n $account --sk generate
        absolutepath=$(find ./keys -type f -exec stat -c '%X %n' {} \; | sort -nr | awk 'NR==1 {print $2}')
        keyPath=$(basename $absolutepath)
    	echo "Role:$role created with signing_key:s $keyPath"
        nsc edit signing-key --account $account --role $role --sk $keyPath --allow-pub $pub_permissions --allow-sub $sub_permissions
    else
        nsc edit signing-key --account $account --role $role --sk $key --allow-pub $pub_permissions --allow-sub $sub_permissions
    fi
done 
for usr in $(yq '.account.users[]' /sandbox/authz.yaml); do
   user_name= $(echo "$usr" | yq e '.name' -)
   role_name= $(echo "$role" | yq e '.role' -)
   user_desc= $(nsc describe  user $user_name  --data-dir . -J)
   prefix="Error: user"
    if [[ "$user_desc" =~ ^"$prefix" ]]; then
      nsc add user $user_name -a $account -K $role_name
    else
        key=$(nsc describe account $account -J|jq --arg role $role '.nats.signing_keys[] | select(.role == $role_name) | .'|jq -r .key)
        issuer=$($user_desc|jq -r .iss)
        if (key <> issuer) then
            nsc delete user $user_name
            nsc add user $user_name -K $role_name
                       echo "User added: $user_name with role $role_name"
        else
           echo "User already exists $user_name"
        fi
    fi                  
done 
nsc push -u $(env| grep NATSJS_CLUSTER_URL | grep -oe '[^=]*$') -A
zip -r natsconfig.zip .
archive_bytes=$(cat natsconfig.zip | base64)
vault kv put "storagecentral/app/nats/sys" "credentials=$archive_bytes"
rm natsconfig.zip
echo "Directory zipped and stored in Vault successfully!"

sleep 300
 

Online Bash Shell

Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.

About Bash

Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.

Features

  • Open source GNU project
  • Read and execute the commands from a Shell Script
  • Can be invoked by either single-character command line options (-a, -b, -c, -i, -l, -r, etc. ) or by multi-character command line options also like --help, --debugger,--login, etc.
  • Consists of Key bindings
  • Available in restricted mode for the environment security
  • Contains one-dimensional arrays to manipulate the lists of data.

Syntax help

Variables

name="Foo"
echo $name
echo "$name"
echo "${name}"

Conditional Statements

If

if [ conditional-expression ]  
then  
statements  
fi  

If-else

if [ conditional-expression ]  
then  
   statements  
else  
  statements
fi  

Else-If

if [ conditional-expression ]  
then  
   statements  
elif [ conditional-expression ]  
then  
 statements  
else  
  statements
fi