Just had the privilege of meeting Mr Buzz Aldrin, the second man to ever set foot on the Moon! :o))
Thursday, March 24, 2016
Wednesday, September 2, 2015
How to access a django python rest service that uses a login page for authentication
Today we bumped into a little challenge at work where we had to document a REST API in the portal we're developing.
The main problem was that this REST API did not authenticate with http auth. Instead, it relied on authenticating against the portal's (html based) login page, getting the cookie and consuming the REST API from there.
Here's how we solved the problem:
step 1- check the login page's html to see what the username and password form fields are called
(in our case, they were called uName and pwd)
step2- customize the following bash script with your username, password and form fields
The main problem was that this REST API did not authenticate with http auth. Instead, it relied on authenticating against the portal's (html based) login page, getting the cookie and consuming the REST API from there.
Here's how we solved the problem:
step 1- check the login page's html to see what the username and password form fields are called
(in our case, they were called uName and pwd)
step2- customize the following bash script with your username, password and form fields
[root@userportal ~]# cat templates.sh
LOGIN_URL=http://localhost:8081/login/
YOUR_USER='username'
YOUR_PASS='password'
COOKIES=cookies.txt
CURL_BIN="curl -s -c $COOKIES -b $COOKIES -e $LOGIN_URL"
echo "Django Auth: get csrftoken ..."
$CURL_BIN $LOGIN_URL > /dev/null
DJANGO_TOKEN="csrfmiddlewaretoken=$(grep cid $COOKIES | sed 's/^.*cid\s*//')"
echo "DJANGO TOKEN is $DJANGO_TOKEN"
echo "######################################################"
echo "Performing login..."
$CURL_BIN \
-d "$DJANGO_TOKEN&uName=$YOUR_USER&pwd=$YOUR_PASS" \
-X POST $LOGIN_URL
echo "######################################################"
echo "Getting all templates..."
$CURL_BIN \
-d "$DJANGO_TOKEN&..." \
-X GET http://localhost:8081/api/templates/ | python -m json.tool
rm $COOKIES
[root@userportal ~]#
What this script basically does is:
- it connects to the login page and saves its cookies into cookies.txt
- then it reads that file and extracts cookie "cid" to get the value of the "csrfmiddlewaretoken"
- and authenticates against the login page passing your username, password and the csrfmiddlewaretoken variables
At this point you'll be authenticated and the session will be saved in cookies.txt so all there's left to do is call out your REST web service! :o)
Hope this can be of help to someone else!
Tuesday, July 14, 2015
CRIU, a project to implement checkpoint/restore functionality for Linux in userspace
Just bumped into this really cool project called CRIU.
Basically, CRIU lets you checkpoint / restore processes in Linux (from the userspace).
"Checkpoint/Restore In Userspace, or CRIU (pronounced kree-oo, IPA: /krɪʊ/, Russian: криу), is a software tool for Linux operating system. Using this tool, you can freeze a running application (or part of it) and checkpoint it to a hard drive as a collection of files. You can then use the files to restore and run the application from the point it was frozen at. The distinctive feature of the CRIU project is that it is mainly implemented in user space."
I did a small test with a perl application and it worked like a charm. Here's what I did:
Basically, CRIU lets you checkpoint / restore processes in Linux (from the userspace).
"Checkpoint/Restore In Userspace, or CRIU (pronounced kree-oo, IPA: /krɪʊ/, Russian: криу), is a software tool for Linux operating system. Using this tool, you can freeze a running application (or part of it) and checkpoint it to a hard drive as a collection of files. You can then use the files to restore and run the application from the point it was frozen at. The distinctive feature of the CRIU project is that it is mainly implemented in user space."
I did a small test with a perl application and it worked like a charm. Here's what I did:
Step 1- run a test perl script that writes to a temporary file every second
root@docker:~/docker_tests# cat test.pl
#!/usr/bin/perl
use IO::Handle;
my $filename = '/tmp/report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
for (my $i=1; $i <= 300; $i++) {
print $fh "i is $i\n";
$fh->flush();
sleep 1;
}
close $fh;
root@docker:~/docker_tests# ./test.pl &
[1] 14969
root@docker:~/docker_tests# tail /tmp/report.txt
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
root@docker:~/docker_tests#
Step 2 - checkpoint the process id (14969) to a folder called "checkpoint"
root@docker:~/docker_tests# mkdir checkpoint
root@docker:~/docker_tests# criu-1.6/criu dump --shell-job -D checkpoint -t 14969
Warn (arch/x86/crtools.c:132): Will restore 14969 with interrupted system call
root@docker:~/docker_tests#
[1]+ Killed ./test.pl
root@docker:~/docker_tests# ls -l checkpoint/
total 1416
-rw-r--r-- 1 root root 561 Jul 14 16:53 cgroup.img
-rw-r--r-- 1 root root 822 Jul 14 16:53 core-14969.img
-rw-r--r-- 1 root root 60 Jul 14 16:53 creds-14969.img
-rw-r--r-- 1 root root 56 Jul 14 16:53 fdinfo-2.img
-rw-r--r-- 1 root root 18 Jul 14 16:53 fs-14969.img
-rw-r--r-- 1 root root 32 Jul 14 16:53 ids-14969.img
-rw-r--r-- 1 root root 38 Jul 14 16:53 inventory.img
-rw-r--r-- 1 root root 1717 Jul 14 16:53 mm-14969.img
-rw-r--r-- 1 root root 241 Jul 14 16:53 pagemap-14969.img
-rw-r--r-- 1 root root 1388544 Jul 14 16:53 pages-1.img
-rw-r--r-- 1 root root 26 Jul 14 16:53 pstree.img
-rw-r--r-- 1 root root 751 Jul 14 16:53 reg-files.img
-rw-r--r-- 1 root root 794 Jul 14 16:53 sigacts-14969.img
-rw-r--r-- 1 root root 35 Jul 14 16:53 stats-dump
-rw-r--r-- 1 root root 32 Jul 14 16:53 tty.img
-rw-r--r-- 1 root root 169 Jul 14 16:53 tty-info.img
root@docker:~/docker_tests#
Step 3 - verify that the process is no longer running
root@docker:~/docker_tests# ps axuw | grep test.pl
root 14994 0.0 0.0 11748 2208 pts/3 S+ 16:54 0:00 grep --color=auto test.pl
root@docker:~/docker_tests# tail /tmp/report.txt
i is 62
i is 63
i is 64
i is 65
i is 66
i is 67
i is 68
i is 69
i is 70
i is 71
root@docker:~/docker_tests#
Step 4 - restore the process and confirm it picked up exactly where it left off :o)
root@docker:~/docker_tests# criu-1.6/criu restore -d --shell-job -D checkpoint
root@docker:~/docker_tests# ps axuw | grep test.pl
root 14969 0.0 0.0 23548 1348 pts/3 S 16:55 0:00 /usr/bin/perl ./test.pl
root 14974 0.0 0.0 11748 2280 pts/3 S+ 16:56 0:00 grep --color=auto test.pl
root@docker:~/docker_tests# tail /tmp/report.txt
i is 74
i is 75
i is 76
i is 77
i is 78
i is 79
i is 80
i is 81
i is 82
i is 83
root@docker:~/docker_tests#
Sunday, July 12, 2015
ASUS RT-AC68U internal port forwarding issue
Just wanted to share the solution to a problem I was having with my new wifi router (an ASUS RT-AC68U)...
Basically, I have a few port forwarding rules set up and, when I would connect to the external ip address from my internal network, the port forwarding did not work.
I sniffed the network and noticed that the first few packets would be forwarded correctly (between the external ip and the local ip) but, then, the router would start NATing some packets with the router's internal ip address instead of its public one.
Anyway, the fix is pretty easy, you just have to disable NAT Acceleration under NAT \ Switch Control.
Basically, I have a few port forwarding rules set up and, when I would connect to the external ip address from my internal network, the port forwarding did not work.
I sniffed the network and noticed that the first few packets would be forwarded correctly (between the external ip and the local ip) but, then, the router would start NATing some packets with the router's internal ip address instead of its public one.
Anyway, the fix is pretty easy, you just have to disable NAT Acceleration under NAT \ Switch Control.
I hope this information can help someone else :o)
UPDATE: although disabling NAT Acceleration fixed my problem, it caused some heavy performance issues (my home connection dropped from 900Mbps+ to 300Mbps+). Fortunately, I was still able to solve the problem by simply updating to the latest available firmware (3.0.0.4.378_6975-g1f406a7) - they identified this problem as "NAT Loopback problem". The device said I was already using the latest firmware but it turned out that was a lie :oP.
Thursday, October 30, 2014
Low Carb High Fat Sweet Pizza Crust
Last weekend I did the awesome low carb high fat pizza I posted about several months ago and my wife had a great idea: "what if we didn't add the powdered garlic to the crust and did a sweet pizza with the same crust?"
So today I decided to try it and here's the result:
The idea is to use these small pieces and add a syrup on top (like chocolate or strawberry), as well as a couple of pieces of fruit (e.g. strawberry, blackberries or raspberries).
The recipe is very similar to the one I use for the crust of the low carb high fat pizza.
Crust Ingredients
So today I decided to try it and here's the result:
The idea is to use these small pieces and add a syrup on top (like chocolate or strawberry), as well as a couple of pieces of fruit (e.g. strawberry, blackberries or raspberries).
The recipe is very similar to the one I use for the crust of the low carb high fat pizza.
Crust Ingredients
- 3 cups shredded cheese (a mixture of mozzarella and cheddar is best)
- 16 oz of cream cheese
- 4 large eggs
- 2 cups of almond meal flour
- 2 cups of artificial sweetener
Melt the shreeded cheese and the cream cheese in the microwave for 1 minute and then just mix everything together in a blender (or by hand) and put it in the oven at 425F for about 10 minutes (if I use my electric convect oven, I need about 10 minutes, but on the gas oven it took about 15 minutes) - just keep an eye on the oven to make sure you don't over cook it!
And that's about it! Enjoy! :o)
Monday, September 29, 2014
iotop
Ah, I just learned a new awesome Linux command: iotop :oP
Should I feel ashamed that I only found out about it today?
Should I feel ashamed that I only found out about it today?
Thursday, August 7, 2014
Low Carb Cheese Danish
My wife just found out this recipe for a low carb cheese danish.I haven't personally tried it but wanted to record it here so I can refer back to it in the future :oP
"Dough
- 1 1/4 cup 2% mozzarella shredded cheese (140 grams)
- 6 tbsp Almond Flour (42 grams)
- 3 tbsp Coconut Flour (21 grams)
- 4 tbsp sugar equivalent (I used 5 tsp Truvia)
- 1/2 tsp baking powder
- 1/2 tsp vanilla
- 4 tbsp butter (omit if you are using a higher fat cheese but I implore you to use the 2% or part skim mozzarella cheese!)
- 1 egg
Filling
- 6 oz cream cheese
- 1 tsp lemon juice
- 1/2 tsp vanilla extract
- 1/4 cup sugar equivalent
- 1 egg yolk
Frosting
- 3 tbsp powdered sweetener (3 tbsp truvia in a coffee grinder for 30 secs and then measure out 3 tbsp of the powdered.)
- 2 tbsp heavy cream
- 2 oz cream cheese
- 1/4 tsp vanilla extract
Instructions
- Preheat oven to 400 degrees.
Filling
- Soften cream cheese and combine with lemon juice, vanilla, and sweetener and egg yolk using a whisk or electric mixer. Set aside in the fridge until you are ready with the dough.
Dough
- Measure out almond flour, coconut flour, and baking powder. Combine well with a whisk.
- Melt 4 tbsp butter and add vanilla and sweetener. Stir.
- Melt 1 1/4 cup shredded mozzarella cheese in the microwave (30% power for 2 minutes with a 1200 watt microwave).
- Combine all the ingredients and add an egg.
- Stir until batter is combined and the cheese and dough are completely mixed. Use your hands or a spatula to press and fold the cheese and the batter together. Reheat for 10 seconds in the microwave. Press and fold some more until 99% of the dough is all one color. (Tip-this gets sticky and if you wet your hands a little it helps tremendously. The water from your hands will stop the stickiness on the outside of the dough making it very easy to roll out.)
- Reheat dough for 10 seconds and roll out into a square the approximate length of a rolling pin and slice into four quarters with a pizza slicer.
- Fold the corners into the center repeating with all four squares.
- Pipe filling (or spoon it on top!) into the center of each danish.
- Bake for 10-12 minutes. (Check on this at 10 minutes and make sure to remove it from the oven as soon as it has a deep golden brown color! Just slightly past 13 minutes and 2 of mine did get burnt. Definitely keep an eye on it.)
Frosting
- Soften cream cheese in the microwave and add powdered sweetener, heavy cream and vanilla. Mix well with a whisk and pour into a ziploc bag. Cut a tiny hole in the corner and pipe onto each danish.
Notes
- Serves 4
- per serving 429 Calories; 41g Fat; 9g Protein; 8g Carbohydrate; 3g Dietary Fiber; 5 net carbs each!"
Subscribe to:
Posts (Atom)

