Quantcast
Channel: MikroTik
Viewing all articles
Browse latest Browse all 21378

Scripting • Re: Securely storing apikey/tokens for /tool/fetch... Approaches?

$
0
0
Updated the "persisted store using ppp secrets" script so it should work on both V6 and V7. Although again persisted variables are a "needed feature" – since this is still a hack, works well enough for my purposes, but no warranties here.
Hi Amm0,

I'm trying to use this script to store tokens in ROS 7.16.2 but I don't seem to be able to retrieve the password in a script:

I create two scripts as follows:
Code:
/system scriptadd dont-require-permissions=no name=SECRET owner=admin policy=\    read,write,test source="# Credit: https://forum.mikrotik.com/viewtopic.php\    \?p=916159#p916159\r\    \n### \$SECRET\r\    \n#   get <name>\r\    \n#   set <name> password=<password>\r\    \n# . remove <name\r\    \n#   print\r\    \n:global SECRET\r\    \n:set \$SECRET do={\r\    \n    :global SECRET\r\    \n\r\    \n    # helpers\r\    \n    :local fixprofile do={\r\    \n        :if ([/ppp profile find name=\"null\"]) do={:put \"nothing\"} el\    se={\r\    \n            /ppp profile add bridge-learning=no change-tcp-mss=no local-\    address=0.0.0.0 name=\"null\" only-one=yes remote-address=0.0.0.0 session-\    timeout=1s use-compression=no use-encryption=no use-mpls=no use-upnp=no\r\    \n        }\r\    \n    }\r\    \n    :local lppp [:len [/ppp secret find where name=\$2]]\r\    \n    :local checkexist do={\r\    \n        :if (lppp=0) do={\r\    \n            :error \"\\\$SECRET: cannot find \$2 in secret store\"\r\    \n        }\r\    \n    }\r\    \n\r\    \n    # \$SECRET\r\    \n    :if ([:typeof \$1]!=\"str\") do={\r\    \n        :put \"\\\$SECRET\"\r\    \n        :put \"   uses /ppp/secrets to store stuff like REST apikeys, or\    \_other sensative data\"\r\    \n        :put \"\\t\\\$SECRET print - prints stored secret passwords\"\r\    \n        :put \"\\t\\\$SECRET get <name> - gets a stored secret\"\r\    \n        :put \"\\t\\\$SECRET set <name> password=\\\"YOUR_SECRET\\\" - s\    ets a secret password\" \r\    \n        :put \"\\t\\\$SECRET remove <name> - removes a secret\" \r\    \n    }\r\    \n\r\    \n    # \$SECRET print\r\    \n    :if (\$1~\"^pr\") do={\r\    \n        /ppp secret print where comment~\"\\\\\\\$SECRET\"\r\    \n        :return [:nothing] \r\    \n    }\r\    \n\r\    \n    # \$SECRET get\r\    \n    :if (\$1~\"get\") do={\r\    \n        \$checkexist\r\    \n       :return [/ppp secret get \$2 password] \r\    \n    }\r\    \n\r\    \n    # \$SECRET set\r\    \n    :if (\$1~\"set|add\") do={\r\    \n        :if ([:typeof \$password]=\"str\") do={} else={:error \"\\\$SECR\    ET: password= required\"}\r\    \n        :if (lppp=0) do={\r\    \n            /ppp secret add name=\$2 password=\$password \r\    \n        } else={\r\    \n            /ppp secret set \$2 password=\$password\r\    \n        }\r\    \n        \$fixprofile\r\    \n        /ppp secret set \$2 comment=\"used by \\\$SECRET\"\r\    \n        /ppp secret set \$2 profile=\"null\"\r\    \n        /ppp secret set \$2 service=\"async\"\r\    \n        :return [\$SECRET get \$2]\r\    \n    } \r\    \n\r\    \n    # \$SECRET remove\r\    \n    :if (\$1~\"rm|rem|del\") do={\r\    \n        \$checkexist\r\    \n        :return [/ppp secret remove \$2]\r\    \n    }\r\    \n    :error \"\\\$SECRET: bad command\"\r\    \n}"add dont-require-permissions=no name=testscript owner=admin policy=\    read,write,test source=":global SECRET\r\    \n:local user \"avggeek\"\r\    \n:local thepass\r\    \n:set thepass \"\$[\$SECRET get rest_apikey]\"\r\    \n:log info \"Pass is: \$thepass\""
I can run the
Code:
$SECRET
command on the terminal without issues:
Code:
>  $SECRET add "rest_apikey" password="mikrotik"nothing> :put [$SECRET get rest_apikey]                          mikrotik
But I run the script
Code:
/system script run testscript
, I can only see
Code:
Pass is:
in the Log. Is there some permissions issue or something else I'm missing?

Statistics: Posted by avggeek — Thu Jan 09, 2025 11:10 am



Viewing all articles
Browse latest Browse all 21378

Trending Articles