{"generator":"Jekyll","link":[{"@attributes":{"href":"https:\/\/antonio-ramadas.github.io\/feed.xml","rel":"self","type":"application\/atom+xml"}},{"@attributes":{"href":"https:\/\/antonio-ramadas.github.io\/","rel":"alternate","type":"text\/html"}}],"updated":"2021-01-29T22:57:23+00:00","id":"https:\/\/antonio-ramadas.github.io\/feed.xml","title":"Ant\u00f3nio Ramadas Website","subtitle":"My place on the web where my socials and blog posts are located. I mainly write about Software Engineering topics.","author":{"name":"Ant\u00f3nio Ramadas"},"entry":[{"title":"That one script that makes Glassdoor browsable","link":{"@attributes":{"href":"https:\/\/antonio-ramadas.github.io\/blog\/2021\/01\/29\/that-one-script-that-makes-glassdoor-browsable\/","rel":"alternate","type":"text\/html","title":"That one script that makes Glassdoor browsable"}},"published":"2021-01-29T00:00:00+00:00","updated":"2021-01-29T00:00:00+00:00","id":"https:\/\/antonio-ramadas.github.io\/blog\/2021\/01\/29\/that-one-script-that-makes-glassdoor-browsable","content":"<p>Today I discovered Glassdoor puts banners and blocks scroll if you are logged in and check some interviews.<\/p>\n\n<p>Definitely not a pleasant user experience taking into account you can check the interviews on the mobile app. To some extent, you can do the same without being logged in (just make you delete the cookies before each refreshing).<\/p>\n\n<p><img src=\"\/assets\/imgs\/2021-01-29-that-one-script-that-makes-glassdoor-browsable\/glassdoor-before.png\" alt=\"Glassdoor showing banners and blocking scroll\" \/><\/p>\n\n<p>It has been some time since I last glued some stuff together on JavaScript. This was an opportunity to refresh that. After some fizzling around, this is what I got:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-javascript\" data-lang=\"javascript\"><span class=\"nb\">window<\/span><span class=\"p\">.<\/span><span class=\"nx\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">load<\/span><span class=\"dl\">'<\/span><span class=\"p\">,<\/span> <span class=\"kd\">function<\/span><span class=\"p\">()<\/span> <span class=\"p\">{<\/span>\n  <span class=\"kd\">let<\/span> <span class=\"nx\">functions<\/span> <span class=\"o\">=<\/span> <span class=\"p\">[<\/span>\n    <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nx\">getElementById<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">ContentWallHardsell<\/span><span class=\"dl\">\"<\/span><span class=\"p\">).<\/span><span class=\"nx\">remove<\/span><span class=\"p\">(),<\/span>\n    <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nx\">getElementsByTagName<\/span><span class=\"p\">(<\/span><span class=\"dl\">'<\/span><span class=\"s1\">body<\/span><span class=\"dl\">'<\/span><span class=\"p\">)[<\/span><span class=\"mi\">0<\/span><span class=\"p\">].<\/span><span class=\"nx\">style<\/span><span class=\"p\">.<\/span><span class=\"nx\">removeProperty<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">overflow<\/span><span class=\"dl\">\"<\/span><span class=\"p\">),<\/span>\n    <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nx\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">scroll<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"nx\">event<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nx\">event<\/span><span class=\"p\">.<\/span><span class=\"nx\">stopPropagation<\/span><span class=\"p\">(),<\/span> <span class=\"kc\">true<\/span><span class=\"p\">),<\/span>\n    <span class=\"p\">()<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nb\">document<\/span><span class=\"p\">.<\/span><span class=\"nx\">addEventListener<\/span><span class=\"p\">(<\/span><span class=\"dl\">\"<\/span><span class=\"s2\">mousemove<\/span><span class=\"dl\">\"<\/span><span class=\"p\">,<\/span> <span class=\"nx\">event<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nx\">event<\/span><span class=\"p\">.<\/span><span class=\"nx\">stopPropagation<\/span><span class=\"p\">(),<\/span> <span class=\"kc\">true<\/span><span class=\"p\">),<\/span>\n  <span class=\"p\">];<\/span>\n  <span class=\"nx\">functions<\/span><span class=\"p\">.<\/span><span class=\"nx\">forEach<\/span><span class=\"p\">(<\/span><span class=\"nx\">f<\/span> <span class=\"o\">=&gt;<\/span> <span class=\"nx\">f<\/span><span class=\"p\">());<\/span>\n<span class=\"p\">});<\/span><\/code><\/pre><\/figure>\n\n<p>This script removes the banners and enables scroll. So, that is a nice win:<\/p>\n\n<p><img src=\"\/assets\/imgs\/2021-01-29-that-one-script-that-makes-glassdoor-browsable\/glassdoor-after.png\" alt=\"Glassdoor - A browsable website\" \/><\/p>\n\n<p>Of course, pasting on the Developer Tools\u2019s console tab every time is cumbersome. We can automate that with <a href=\"https:\/\/mybrowseraddon.com\/custom-style-script.html\">Custom Style Script<\/a> by entering the script from above for the Glassdoor website (<code class=\"language-plaintext highlighter-rouge\">https:\/\/www.glassdoor.com\/<\/code>).<\/p>\n\n<p>Voil\u00e0! I\u2019m back.<\/p>","author":{"name":"Ant\u00f3nio Ramadas"},"summary":"Today I discovered Glassdoor puts banners and blocks scroll if you are logged in and check some interviews."},{"title":"SSH login with Touch ID","link":{"@attributes":{"href":"https:\/\/antonio-ramadas.github.io\/blog\/2020\/10\/30\/ssh-login-with-touch-id\/","rel":"alternate","type":"text\/html","title":"SSH login with Touch ID"}},"published":"2020-10-30T00:00:00+00:00","updated":"2020-10-30T00:00:00+00:00","id":"https:\/\/antonio-ramadas.github.io\/blog\/2020\/10\/30\/ssh-login-with-touch-id","content":"<p>Logging with SSH can be a cumbersome task. Sure, there are solutions to this problem, but most are based on the assumption you can add your credentials to the server. The reality is that sometimes servers may not give you access to do that.<\/p>\n\n<p>The script I\u2019m presenting works solely on client side. Nothing complex and nothing new. In fact, I simply unified two separate solutions into a single one. If the title didn\u2019t hint you off, by the end of this post you\u2019ll be able to login and have Touch ID acting as your password. Touch ID is what I\u2019ve got close to my fingertips (pun intended), but the idea is there and you can adapt to your own preference.<\/p>\n\n<p>Here is the gist, we save our SSH login password to a file which can only be accessed with <code class=\"language-plaintext highlighter-rouge\">sudo<\/code>. We also enable usage of Touch ID for <code class=\"language-plaintext highlighter-rouge\">sudo<\/code> access. The script then relies on <code class=\"language-plaintext highlighter-rouge\">expect<\/code> and reads the password file triggering Touch ID.<\/p>\n\n<p>The file that\u2019ll contain the password must only contain it. Once created, I recommend removing the permissions to all users so only <em>sudoers<\/em> can access it. You can do this with <code class=\"language-plaintext highlighter-rouge\">chmod a-rwx &lt;PATH_TO_PASSWORD_FILE&gt;<\/code>.<\/p>\n\n<p>I won\u2019t detail how you can enable Touch ID for <code class=\"language-plaintext highlighter-rouge\">sudo<\/code> access, but instead point you to the same <a href=\"https:\/\/apple.stackexchange.com\/questions\/259093\/can-touch-id-for-the-mac-touch-bar-authenticate-sudo-users-and-admin-privileges\">Stack Overflow thread that I followed<\/a>. If you use iTerm, pay attention as you may need to do an additional step.<\/p>\n\n<p>Now, we\u2019re just a script away from automating the process. Here\u2019s my tweaked version from <a href=\"https:\/\/www.journaldev.com\/1405\/expect-script-ssh-example-tutorial\">JournalDev<\/a>:<\/p>\n\n<div class=\"language-bash highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"c\">#!\/usr\/bin\/expect<\/span>\n\n<span class=\"c\"># Connects via SSH to the host passed as argument<\/span>\n\n<span class=\"nb\">set timeout <\/span>60\n<span class=\"nb\">set <\/span>server <span class=\"o\">[<\/span>lindex <span class=\"nv\">$argv<\/span> 0]\n<span class=\"nb\">set <\/span>username &lt;USERNAME&gt;\n<span class=\"nb\">set <\/span>password <span class=\"o\">[<\/span><span class=\"nb\">exec sudo cat<\/span> &lt;PATH_TO_YOUR_PASSWORD_FILE&gt;]\n\nspawn ssh <span class=\"nv\">$username<\/span>@<span class=\"nv\">$server<\/span>\n\nexpect <span class=\"o\">{<\/span> \n\t<span class=\"s2\">\"yes\/no\"<\/span>\t<span class=\"o\">{<\/span> send <span class=\"s2\">\"yes<\/span><span class=\"se\">\\r<\/span><span class=\"s2\">\"<\/span> <span class=\"p\">;<\/span> exp_continue <span class=\"o\">}<\/span>\n\t<span class=\"s2\">\"*?assword\"<\/span>\t<span class=\"o\">{<\/span> send <span class=\"s2\">\"<\/span><span class=\"nv\">$password<\/span><span class=\"se\">\\r<\/span><span class=\"s2\">\"<\/span> <span class=\"o\">}<\/span>\n<span class=\"o\">}<\/span>\n\ninteract\n<\/code><\/pre><\/div><\/div>\n\n<p>Customize this script as you want and add it to your <code class=\"language-plaintext highlighter-rouge\">$PATH<\/code>.<\/p>\n\n<p>Everything should work now!<\/p>\n\n<p>Added bonus: Touch ID works now as an alternative of entering the password for <code class=\"language-plaintext highlighter-rouge\">sudo<\/code> commands.<\/p>","author":{"name":"Ant\u00f3nio Ramadas"},"summary":"Logging with SSH can be a cumbersome task. Sure, there are solutions to this problem, but most are based on the assumption you can add your credentials to the server. The reality is that sometimes servers may not give you access to do that."},{"title":"Tracking Kafka consumer group progress","link":{"@attributes":{"href":"https:\/\/antonio-ramadas.github.io\/blog\/2020\/10\/27\/tracking-kafka-consumer-group-progress\/","rel":"alternate","type":"text\/html","title":"Tracking Kafka consumer group progress"}},"published":"2020-10-27T00:00:00+00:00","updated":"2020-10-27T00:00:00+00:00","id":"https:\/\/antonio-ramadas.github.io\/blog\/2020\/10\/27\/tracking-kafka-consumer-group-progress","content":"<p>Imagine you\u2019ve got a Kafka topic and start a new consumer group that consumes from the beginning. Now, imagine this takes time. Lots of time. How do you know how many more messages are left? Can you guess an ETA of how much longer it\u2019ll take to the most recent state?<\/p>\n\n<p>This was a task I had in hands. I did not find a tool that right out of the box did what I want, but luckily  <a href=\"https:\/\/kafka.apache.org\/downloads\">Kafka\u2019s CLI<\/a> tools enables me to come up with single command line to solve the issue.<\/p>\n\n<p>If you\u2019re in a hurry, here is the command:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\">watch <span class=\"nt\">-n<\/span> 1 <span class=\"s2\">\"bin\/kafka-consumer-groups.sh --bootstrap-server &lt;HOST&gt; --describe --group &lt;CONSUMER_GROUP&gt; | awk '{current+=<\/span><span class=\"se\">\\$<\/span><span class=\"s2\">4;total+=<\/span><span class=\"se\">\\$<\/span><span class=\"s2\">5}END{print (current\/total)*100}'\"<\/span><\/code><\/pre><\/figure>\n\n<h2 id=\"setting-up-the-environment\">Setting up the environment<\/h2>\n\n<p>If you don\u2019t plan to see this in action in your environment, or you already have one, then skip this whole section. Really.<\/p>\n\n<p>What we\u2019re going to do is extremely similar to <a href=\"https:\/\/kafka.apache.org\/quickstart\">Kafka\u2019s quickstart<\/a>.<\/p>\n\n<p>Start by downloading the tool, enter the folder and start a healthy Kafka cluster with one topic and one consumer group:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"c\"># Start Zookeeper (do this in one window and let it keep running)<\/span>\nbin\/zookeeper-server-start.sh config\/zookeeper.properties\n\n<span class=\"c\"># Start Kafka (do this in another window and let it keep running)<\/span>\nbin\/kafka-server-start.sh config\/server.properties\n\n<span class=\"c\"># Create a topic with 5 partitions (do this in yet another window)<\/span>\nbin\/kafka-topics.sh <span class=\"nt\">--create<\/span> <span class=\"nt\">--topic<\/span> quickstart-events <span class=\"nt\">--partitions<\/span> 5 <span class=\"nt\">--bootstrap-server<\/span> localhost:9092\n\n<span class=\"c\"># Create a consumer group<\/span>\nbin\/kafka-console-consumer.sh <span class=\"nt\">--topic<\/span> quickstart-events <span class=\"nt\">--group<\/span> quickstart-group <span class=\"nt\">--from-beginning<\/span> <span class=\"nt\">--bootstrap-server<\/span> localhost:9092\n\n<span class=\"c\"># This last command will keep on running. Stop it. Don't wait.<\/span><\/code><\/pre><\/figure>\n\n<p>What you\u2019ve got now is a topic named <code class=\"language-plaintext highlighter-rouge\">quickstart-events<\/code> and a consumer group with 5 partitions named <code class=\"language-plaintext highlighter-rouge\">quickstart-group<\/code>:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"nv\">$ <\/span>bin\/kafka-consumer-groups.sh <span class=\"nt\">--describe<\/span> <span class=\"nt\">--group<\/span> quickstart-group <span class=\"nt\">--bootstrap-server<\/span> localhost:9092\n\nConsumer group <span class=\"s1\">'quickstart-group'<\/span> has no active members.\n\nGROUP            TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID\nquickstart-group quickstart-events 2          0               0               0               -               -               -\nquickstart-group quickstart-events 1          0               0               0               -               -               -\nquickstart-group quickstart-events 0          0               0               0               -               -               -\nquickstart-group quickstart-events 4          0               0               0               -               -               -\nquickstart-group quickstart-events 3          0               0               0               -               -               -<\/code><\/pre><\/figure>\n\n<p>Now, we\u2019ve got to send some messages:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"c\"># Send 1.000.000 messages 5 times and discard the output<\/span>\n<span class=\"c\"># This may take a few seconds<\/span>\nrepeat 5 <span class=\"o\">{<\/span><span class=\"nb\">seq <\/span>1000000 | bin\/kafka-console-producer.sh <span class=\"nt\">--topic<\/span> quickstart-events <span class=\"nt\">--bootstrap-server<\/span> localhost:9092 <span class=\"o\">&gt;<\/span> \/dev\/null<span class=\"o\">}<\/span><\/code><\/pre><\/figure>\n\n<p>If this command fails on you, don\u2019t worry. It is due to <code class=\"language-plaintext highlighter-rouge\">repeat<\/code>. I use ZSH and it works for me, but there are <a href=\"https:\/\/stackoverflow.com\/questions\/3737740\/is-there-a-better-way-to-run-a-command-n-times-in-bash\">other<\/a> <a href=\"https:\/\/serverfault.com\/questions\/273238\/how-to-run-a-command-multiple-times-using-bash-shell\">replacements<\/a>.<\/p>\n\n<p>If you ever need more messages, just stop the consumer and add more (or do the whole dance but for another topic).<\/p>\n\n<p>Let\u2019s check again our consumer:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"nv\">$ <\/span>bin\/kafka-consumer-groups.sh <span class=\"nt\">--describe<\/span> <span class=\"nt\">--group<\/span> quickstart-group <span class=\"nt\">--bootstrap-server<\/span> localhost:9092\n\nConsumer group <span class=\"s1\">'quickstart-group'<\/span> has no active members.\n\nGROUP            TOPIC             PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID\nquickstart-group quickstart-events 2          0               1069508         1069508         -               -               -\nquickstart-group quickstart-events 1          0               1046611         1046611         -               -               -\nquickstart-group quickstart-events 0          0               967972          967972          -               -               -\nquickstart-group quickstart-events 4          0               975416          975416          -               -               -\nquickstart-group quickstart-events 3          0               940493          940493          -               -               -<\/code><\/pre><\/figure>\n\n<p>It\u2019s ok for the offset to be different. What\u2019s important here is that the consumer is lagging 5.000.000 messages in total. This number of messages enables us to actually see the consumer\u2019s group progress. There are other ways to do it, but we\u2019ll stick with this one.<\/p>\n\n<p>Don\u2019t forget, at the end, to clean everything, stop all active executions and clean the leftovers:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"nb\">rm<\/span> <span class=\"nt\">-rf<\/span> \/tmp\/kafka-logs \/tmp\/zookeeper<\/code><\/pre><\/figure>\n\n<h2 id=\"the-command-in-action\">The command in action<\/h2>\n\n<p>One marvel of the <code class=\"language-plaintext highlighter-rouge\">bin\/kafka-consumer-groups.sh --describe<\/code> command is that it outputs a formatted table space separated (head for the last code snippet in the previous section to see one example).<\/p>\n\n<p>One way to track progress is to execute the command every few seconds (e.g., <code class=\"language-plaintext highlighter-rouge\">watch -n 1<\/code>), but that does not seem much of an option unless you want to count every lag on each update. Otherwise, you\u2019ll just stare at changing numbers.<\/p>\n\n<p>It\u2019s time to make use of the nicely formatted table with <a href=\"https:\/\/en.wikipedia.org\/wiki\/AWK\">AWK<\/a>. We just got to pipe the table and sum the fourth and fifth columns (consumed and total messages, respectively) to get a percentage:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"nb\">awk<\/span> <span class=\"s1\">'{current+=\\$4;total+=\\$5}END{print (current\/total)*100}'<\/span><\/code><\/pre><\/figure>\n\n<p>And we got the command which was presented at the beginning:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\">watch <span class=\"nt\">-n<\/span> 1 <span class=\"s2\">\"bin\/kafka-consumer-groups.sh --bootstrap-server &lt;HOST&gt; --describe --group &lt;CONSUMER_GROUP&gt; | awk '{current+=<\/span><span class=\"se\">\\$<\/span><span class=\"s2\">4;total+=<\/span><span class=\"se\">\\$<\/span><span class=\"s2\">5}END{print (current\/total)*100}'\"<\/span><\/code><\/pre><\/figure>\n\n<p>If you did the previous section, then you can see it in action:<\/p>\n\n<figure class=\"highlight\"><pre><code class=\"language-shell\" data-lang=\"shell\"><span class=\"c\"># Watch the progress of the consumer group in one window<\/span>\nwatch <span class=\"nt\">-n<\/span> 1 <span class=\"s2\">\"bin\/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group quickstart-group | awk '{current+=<\/span><span class=\"se\">\\$<\/span><span class=\"s2\">4;total+=<\/span><span class=\"se\">\\$<\/span><span class=\"s2\">5}END{print (current\/total)*100}'\"<\/span>\n\n<span class=\"c\"># Start the consumer and discard the output (run in another window)<\/span>\nbin\/kafka-console-consumer.sh <span class=\"nt\">--topic<\/span> quickstart-events <span class=\"nt\">--group<\/span> quickstart-group <span class=\"nt\">--from-beginning<\/span> <span class=\"nt\">--bootstrap-server<\/span> localhost:9092 <span class=\"o\">&gt;<\/span> \/dev\/null<\/code><\/pre><\/figure>\n\n<p>The output will be a single percentage that may be nifty to use with any other commands.<\/p>\n\n<p>I hope you enjoyed this little hack. More will come.<\/p>","author":{"name":"Ant\u00f3nio Ramadas"},"summary":"Imagine you\u2019ve got a Kafka topic and start a new consumer group that consumes from the beginning. Now, imagine this takes time. Lots of time. How do you know how many more messages are left? Can you guess an ETA of how much longer it\u2019ll take to the most recent state?"}]}