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
[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:

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.

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.

Saturday, July 11, 2015

Five Reasons Why The Greeks Were Right (ou, "a versao da esquerda sobre como a culpa e dos outros")

Five Reasons Why The Greeks Were Right (ou, "a versao da esquerda sobre como a culpa e dos outros")Que artigo mais comuna...

1-"Austerity is not a solution for any economic problem: What creditors and EU bankers and policy makers want is a continuation of the austerity programs that have led to 25% unemployment. Greece said no, and quite right – what economic problem is solved by throwing people out of work? If you were heavily in debt and your bank called you in to discuss your options, can you imagine them recommending that you quit your job?"
- Ah, agora ja percebi. A austeridade de que tanto falam significa despedir pessoas. Fodasse, assim, realmente, nao posso defender a austeridade :o). Eu pensava que austeridade significava "qualidade ou caracteristica do que e austero = RIGOR, SEVERIDADE" ou, por outras palavras, fazer o sector publico gastar so o que pode. La esta, o problema e meu, que estava a usar a definicao do dicionario e nao a definicao da esquerda :o).

2-"From 2001 (the year of Greek entry into the Euro system) through 2007 (the beginning of the world financial crisis), Greece devoted an average of 20.6% of GDP to social programs, while Germany and France were at 26.7% and 28.7%. That’s a significant difference, and in a direction that does not support the lazy-Greek hypothesis."

- ah, pois...o governo Alemao e Frances gastaram mais "programas sociais", LOGO, os gajos nao sao preguicosos. Hmm, e onde esta o custo "escondido" (para o idiota que escreveu o artigo), de ter legislacao que aumenta o salario minimo desproporcionalmente ao valor do que os Gregos produzem, as reformas a partir dos 45 anos, os bonus por "aparecer para trabalhar", etc?

3-"Greek labor productivity has risen faster than German: In fact, not only does Greece spend comparatively less on social programs, but their workers’ productivity rose faster than that of Germans.[...] What the wage repression in Germany meant was that the rising Greek standard of living – one of the goals of joining the euro in the first place – caused a modest Greek trade deficit to grow steadily up through the crisis as German labor costs were suppressed."
- ou seja, a culpa da desgraca Grega e que na Alemanha os SACANAS "suprimiram o custo do trabalho" e os Gregos foram recompensados pelo seu trabalho arduo e productividade, criando esta situacao. La esta, o problema e a Alemanha :o).

4-"The Greek crisis is function of their trade deficit, not government social spending: In order to grasp the true nature of the crisis, it is extremely important to understand that what has happened is a function of Greek trade deficits, not Greek government budget deficits. If Greece buys more goods and services from Germany than Germans buy from Greece, they must finance this by selling financial assets and/or borrowing. This creates external debt. Conversely, if they sell more to the Germans than the Germans buy, then Germany must sell financial assets to or borrow from Greece."
- mas e CLARO, a culpa e da Alemanha, novamente, que exporta mais do que a Grecia. :o)

5-"The Greek crisis is a function of how the Eurozone is organized: The problem facing Europe is a systemic one. Those who try to understand it as being a reflection of individual national characteristics will completely miss the point and any policies that emerge from such an analysis will range from impotent to disastrous (with current recommendations leaning toward the latter). Simply put, the core issue is that the system punishes success."
- Ou seja, os Gregos estao na merda por causa da UE, que penaliza o seu ENORME sucesso ;o(...

Thursday, June 25, 2015

Um pequeno exercicio mental...

Proponho aos meus amigos Socialistas (e uso o termo "amigo" de forma realmente vaga :oP) o seguinte exercicio mental...

Dados estes pontos, com quantos e que voces concordam? :o)

1- Exigimos que o Estado tenha a obrigacao de providenciar a oportunidade para uma vida digna aos cidadaos.

2- Todos os cidadaos devem ter os mesmos direitos e obrigacoes.

3- A primeira obrigacao de cada cidadao e de trabalhar espiritual e fisicamente. A actividade dos individuos nao deve contrair o interesse da universalidade, e os seus resultados devem servir para o beneficio de todos.

4- Abolicao de qualquer rendimento que nao seja merecido. Quebrar a escravidao dos juros da divida.

5- Em consideracao do monstruoso sacrificio em propriedade e sangue que cada guerra obriga ao povo, o enriquecimento pessoal atraves de guerra tem de ser designado como um crime contra o povo. De acordo com isto, exigimos a total confiscacao de todos os lucros de guerra.

6- Exigimos a nacionalizacao de todas as industrias associadas.

7- Exigimos uma divisao dos lucros de todas as industrias pesadas.

8- Exigimos uma expansao a larga escala dos programas de apoio a idosos.

9- Exigimos a criacao de uma classe media saudavel e a sua conservacao, imediata conversao dos grandes armazens em comunas e o seu aluguer a baixo custo a pequenas empresas, as quais as arrendaram ao Estado ou Municipalidade.

10- Exigimos uma reforma agragaria de acordo com as nossas necessidades e uma provisao de lei que permita a livre expropriacao da terra para fins de utilidade publica, abolicao de impostos sobre propriedade e prevencao contra qualquer expeculacao sobre propriedades.

11- Exigimos uma luta sem consideracao contra aqueles cuja a sua actividade e injuriosa para o bem estar geral. Criminosos, usurarios e agiotas devem ser punidos com morte.

12- O Estado e responsavel por uma reconstrucao fundamental do nosso programa de educacao, garantindo a todo o cidadao capaz e industrioso, a possibilidade de obter um grau superior e, consequentemente, chegar a posicoes de lideranca.
Os planos de instrucao a todos os niveis educacionais devem ser feitos de acordo com areas de valor pratico. O conceito de Estado deve ser ensinado pela Escola assim que se atinja uma idade de entendimento. Exigimos tambem que a educacao de criancas extremamente brilhantes, independentemente se os pais sao pobres ou da sua posicao ou profissao, seja garantida pelo Estado.

Pois deverao gostar de saber que estes sao 12 pontos dos 25 do programa Nazi.

Interessante como um grupo "fascista", e "de direita" (embora o seu nome seja "Partido Nacional Socialista"), tem tanto em comum com o Socialismo e Comunismo, nao e? :oP

Tuesday, May 5, 2015

Definicao de Capitalismo (Inglesa vs Portuguesa)



Definicao Inglesa de Capitalismo:
"Um sistema economico no qual o investimento e possessao dos meios de producao, distribuicao e troca de riqueza e feita e mantida, primariamente, por individuos privados ou corporacoes, em contraste com riqueza gerida e possuida por cooperativas ou pelo Estado.

Definicao Portuguesa de Capitalismo:
"Influencia ou predominio economico ou politico do capital.
Conjunto de Capitalistas"

Sou so eu que noto aqui uma certa influencia de Esquerda na conotacao negativa da definicao do Capitalismo...? :o)

Tuesday, March 3, 2015

5 Reasons Why Socialist Sweden Stinks

Just came across this really interesting article titled "5 Reasons Why Socialist Sweden Stinks".
Its a very interesting read so...here's the entire article (in case they some day take it down):

Socialists love to point to Sweden as a model of success for a centrally planned economy. They hate talking about Venezuela or North Korea, or the former Soviet Union. Still, Sweden stinks too. And here’s why.

#1. Something about those Swedes…

Once a Scandinavian socialist bragged to Milton Friedman about their low level of poverty in the country, saying “In Scandinavia, we have no poverty. Friedman offered a witty rejoinder, saying: “That’s interesting-because in America, among Scandinavians, we have no poverty either.”
That wasn’t perfectly accurate, of course. At the time, in Sweden they had a poverty rate of about 6.7%. And in America, the poverty rate amongst Swedish Americans? It was 6.7%. Apparently Swedish people do fairly well wherever they are, not just in socialist Sweden. So there doesn’t seem to be anything special about being in Sweden, in terms of poverty. Maybe there’s just something about their culture that makes them so hardworking?

#2. What’s good for the goose isn’t good for the gander.

Sweden’s centrally planned economy and social order might work well in a homogenous country with a state-sponsored, taxpayer funded churches, but that wouldn’t fly in heterogenous countries like the U.S. Even President Obama understood the problem of trying to apply socialism to the United States when he was asked a question about why Sweden was able to recover from the 2008 financial crisis so quickly. Someone asked Obama how Sweden managed their banking crisis better than the United States directly, and the president surprisingly responded:
“They took over the banks, nationalized them, got rid of the bad assets, resold the banks and, a couple years later, they were going again. So you’d think looking at it, Sweden looks like a good model. Here’s the problem; Sweden had like five banks. [LAUGHS] We’ve got thousands of banks. You know, the scale of the U.S. economy and the capital markets are so vast and the problems in terms of managing and overseeing anything of that scale, I think, would — our assessment was that it wouldn’t make sense. And we also have different traditions in this country.
Obviously, Sweden has a different set of cultures in terms of how the government relates to markets and America’s different. And we want to retain a strong sense of that private capital fulfilling the core — core investment needs of this country.”
Quite a strong statement from Obama, and certainly it’s one you might hear at any Tea Party rally across the country.

#3. Sure they have socialized medicine, but they’re the “sickest” people in the world.

Swedish Journalist Ulf Nilson reports that Sweden has the “sickest work force in the world.” And (perhaps not) surprisingly, Monday is the sickest day of the week! Everybody hates Mondays! Some people are even calling it the “Swedish disease,” because on Mondays, out of about five million in the workforce, there will be approximately 1 million of those people calling out sick to work. Strangely, the sickliest group of individuals are young men, worse than any other group in the country, which is strange considering how they are usually the healthiest group overall.
Nilson thinks he knows why:
“… Most of the young men, thousands of them every day, lie when they call in sick. The same goes for thousands of young women. And older people too. They call in sick, without being sick-and why? Because it has become a habit. And because-very important this!- given the idiotic tax system, you lose very little by not working.”
If reports are correct, then Swedish people are the sickest people in the world! Approximately 20% of working-age Swedes receive some form of an unemployment benefit, mostly related to illness or disabilities.

#4. Guess why we have IKEA?

IKEA is the poor college student’s dream. Affordable, attractive furniture at a very reasonable price, and no staffers on the floor. But did you know how that company developed its business model? It did it because of the high labor costs. Who can afford Swedish workers at these prices?
“Some in Washington jokingly refer to IKEA as the Swedish Embassy. And there is no doubt it is the most successful Swedish retail outlet throughout the world. IKEA is well-known for its lack of staff both on the floor and in the back office. Shoppers experience IKEA as a do-it-yourself store. The shopping style stems from the high cost of Swedish employees. Ingvar Kemprad, founder of IKEA, constructed an employment model that minimized state-imposed labor costs.”
Minimum wage standards in Sweden are set by collective bargaining across different sectors. About 90% of all workers have collective bargaining agreements imposed on them, so they don’t really get that much choice in trying to negotiate with their employers. That’s all taken care of for them.
Former trade-unionist Jan Edling is harshly critical of the government’s reporting on the unemployment statistics in the nation, arguing that actual unemployment in the country is about 20%. Edling argues that the government is suppressing the real numbers. The government also finds ways to keep the numbers down by taking unemployed workers and putting them into what they call “labor market political activities.” Sounds like make work jobs to me.

#5. The gravy train always ends one day.

While socialist countries such as Sweden and Norway are doing fairly well overall, owing to the fact that they are using their oil revenues to fund big government, but that won’t always be the case.
Danske Bank, one of the largest financial enterprises in the region have warned the countries that the falling oil prices could have a severe impact on their economies in the near future. “The Nordic countries have been looking strong in recent years, with economic and financial crisis in much of Europe… However, after years of robust growth, the shine seems to be wearing a bit off,” said a team of their economists.
Also, Sweden’s economy isn’t expanding very quickly, likely due to a housing crisis that is stifling growth. Growing levels of household debt and an overly complicated marketplace which bars investment is one of their biggest problems. The Organization for Economic Cooperation and Development recently limited their growth projections from 3.1 percent to 2.8 percent. Still, Sweden’s own finance minister, Magdalena Andersson, believes it will be lower, at 2.4 percent this year. The Swedish government is planning to do what socialists always do when the chips are down… raise taxes.
Andersson claims that “the barns are empty,” and “bleeding.”
“In the short term, we see that Swedish growth has still not really taken off. The slow recovery abroad is holding back growth in the Swedish economy,” she added.
“It’s not looking better than in the autumn, quite the opposite. The money has run out,” Andersson said.