Sunday 23 December 2012

How to consume WCF Service using console application and/or Windows phone 8 application over https under Dot Net Framework (Dealing with Certificate Issues)

Certificates Overview: 

Certificate, also known as public key certificate or digital certificate, is an electronic document which uses a digital signature to bind a public key with an identity. The identity could be anything. For example it could represent a user, a device, a service or even a few lines of code.
The certificate can be used to sign the identity and could be verified by others. For example a message being signed by a certificate could be verified by the receiver, so that it will be able to know whether the message is the original one or had been modified by someone else. The certificate can also be used to encrypt and decrypt. This is the reason why we can bind a certificate on a website so that the data between the browser and server would be secured, since they are been encrypted and signed by the certificate.
The certificate authority (CA) takes the responsibility to issue the certificates. In Windows we can use the Active Directory Certificate Service.
In an enterprise there might be more than one CAs and normally they will be organized hierarchically. The top level would be the Root CA, which have a certificate signed by itself. All subordinate CAs’ certificate should be requested to and signed by the root CA.


Secure WCF service consumption using console application:

Consuming your native WCF Service over HTTPS using client application can be troublesome since the connection in this case is secure. Therefore you need a certificate from server side(in this case it is the machine hosting the WCF Service) so as the server can be authenticated by the console application. In this post I will explain how to consume your WCF service by issuing a self signed certificate.
As explained above you need a certificate for WCF service hosting machine, for this open the visual studio command prompt on the machine hosting the WCF service and make use of 'makecert' command with proper parameters as given below:


makecert -sv SignRoot.pvk -cy authority -r signroot.cer -a sha1 -n "CN=machine-name" -ss root -sr localmachine

The above command creates a self-signed root certificate. In the above command, signroot.cer is the name of the certificate file which is created.In place of the 'machine-name' parameter above you need to give the name of the machine on which the wcf service is running. The above command creates a self signed root certificate for the wcf service hosting machine and this certificate is used for server authentication. The above command also adds a new root cert in "trusted root certification authorities" on your machine. You can verify it using the using the Microsoft Management Console (mmc). Now you need to export and install this root cert on the client machine also under "Trusted root certificate" category so that the console application can authenticate the server using this certificate.

Once this is done you need to create a self signed client certificate for the server machine. For this again open the visual studio command prompt on the service hosting machine and type the following command:

makecert -iv SignRoot.pvk -ic signroot.cer -cy end -pe -n CN="Server-IP" -eku 1.3.6.1.5.5.7.3.1 -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12

In the command above in place of 'Server-IP' you need to give the ip address of the machine in which the service runs. Once this command is executed you have a new client certificate in "Personal". Once this is done you need to bind your self signed client certificate with the "https" protocol. For this do the following:

 In the IIS admin, select the topmost node, the click the "server certificates" item on the right. You should see the new cert named "Server-IP" (given in above command) in the list. Then select "default web site" in the tree, and click on "Bindings" on the far right side. In the dialog that comes up, edit the "https" entry. In the "Edit Site Binding" dialog, in the "IP Address" combobox. select the ip address of the machine. In the "SSL Certificate" combobox, select your new client cert named "Server-IP". 

Once this done your console application should work fine.


Secure WCF service consumption using Windows Phone 8 application under emulator:


The steps for issuing the certificate in this case is same as for the console application but in order to authenticate the server you need to install its certificate on the emulator once it runs. For this first go to the service hosting machine and create a new folder say "Cert" under the default http website location for hosting the websites used by IIS(ie under C:\inetpub\wwwroot\).

From the Certificates plugin in mmc, export the root cert to a new p7b file in "Cert" folder (in the certificiate export wizard, select "cryptographic message syntax standard - pkcs #7"). Any file name is okay, but the extension must be p7b.

In the emulator, bring up IE, and enter the address of your new Certs website ("http://server-ip-address/certs/rootcertexport.p7b"). The emulator will install the cert. 

Since the server root cert in now install on the emulator now you can consume the wcf service over https using the application on emulator. Note here that every time you have to install the server root cert on the emulator once it is run again.

Friday 21 December 2012

Amazon Experience

It has been one month now since I was Interviewed for the SDE(Software Developer Engineer) Position in Amazon Hyderabad. The interview just like other interviews was a life time experience.


Online Coding Round (25 OCT 2012)
3 Questions, Time 1 Hour
1. Find Lowest Common Ancestor in a BST.
2. Remove duplicate nodes in a linked list.
3. A single array is given, in which half of the array is sorted and another half is also sorted, you
need to sort the entire array without using additional memory.

Telephonic: (29th Oct 2012)
1. Two arrays are given both sorted and a number k is given, you need to find two numbers from
each array such that the sum is equal to the given number.
2. Given two BST you need  to find whether two BST are same or not.
Note: here same means whether two BST are formed from the same set of numbers ie. the
structure of the two BST may differ depending upon the order in which the numbers appear. But the
two BST will only be equal if they are formed from the same set of numbers irrespective of the
structure of the BSTs.

Onsite Hyderabad: (22 Nov 2012)
First round:
1. Find the size of the largest BST present in a Binary tree.
2. Find the maximum sum path present in a Binary Tree.

Second Round(Bar Raiser, the interviewer was Awesome):
1. Given an array of integers, now suppose we form a binary search tree from that array, now
given an integer K present in that array, you need to find the encoding required to reach
that number from the root of the binary search tree so formed. The encoding is defined as :
Starting from the root of the array if you go left you append 0 to the encoding string and if
you go right you append 1 to the encoding string. You need to return the encoding string so
formed once you reach the required number  ‘K’. The constraint is that you cannot use any
additional memory nor cant you modify the original array. And you can make only one pass
of the array.
2. Given an array of integers, and a number ‘k’ , you need to find the number of unique pairs
present in the array that sum upto ‘K’ in O(n) and in one pass of the array.


Third Round(Hiring Manager):
He didn’t ask me to code in any question
1. Find the length of the longest path from one node to another node in a Binary tree.
2. Given a tree in which every node may contain any number of childs. The tree represent the
organizing hierarchy in an enterprise. Now given a node, how will you find the income of
every node present below that particular node. He asked me the data structure to organize
the node and also asked me optimize the space in case a fixed number of children for every
node is not given in advance.
3. Puzzle: given a large cube formed out of the small cubes say N*N, now you need to find only
the number of cubes present on the surface of the Large cube.
4. Scalability problem: You are given a 1000 commodity servers, each of which has 256MB of
RAM and 4GB hard disk. Now each hard disk has 1 Billion numbers. Now you need to find
the median of the the numbers present on the commodity servers.

Last Round:
1. Given a number of square shaped plates of length and breadth, now you need to find the
maximum number of plates which can be placed on the top of each other. A plate P1 can
only be placed in top of another plate P2 only if L1 < L2 and B1 < B2 where L and B represent
the length and breadth of the plate.
2. Then he just asked me informally if I have knowledge of oops concept in any Language, I said
yes in C++ and then he asked me to explain the virtual functions and their implementation in
C++.


Note: In every interview question they asked two questions at the beginning, though the questions are
not technical but they help interviewers to judge your character.
1. Tell me About Yourself.
2. Why do you want to leave your current company.
You must have strong reason for second question and for the first question you must present your skills
in well organized manner. In both of these questions they cross question you on real life example. Apart
from these, some interviewers might also ask you “Why you want to join Amazon.”


Sunday 16 December 2012

How to configure Eclipse for Python Development Environment

Python is an interpreted programming language and claims to be a very effective programming language. Python was develop from Guido van Rossum. Python source code is translated into bytecode which is then interpreted by the Python interpreter. Pyhon source code can run on Java Virtual Machine, in case you are using Jython.
In this post I will explain how to configure eclipse for Python:

Download windows installer 2.7.x from http://www.python.org. Once this is done you need to configure eclipse for the python interpreter

Python Plugin:

Install PyDev plugin using eclipse update manager using the http://pydev.org/updates link.
Once this is done you need to tell eclipse the location of your Python installation. For this open 
Windows->Preference and then select Pydev->interpreter.

Press new and select the path for your "python.exe" file.


The resulting window should be like:


Once this is done press ok and now you are ready to make your own python program :-)


Saturday 15 December 2012

How to install Apache, MySql and Php on Linux- LAMP

In order to install Apache, Mysql and PHP on Linux you need to run certain commands in the terminal and the rest of the things will be taken care automatically. You also need to have internet connection for this.
Open the terminal(under Applications > Accessories).
Before installing anything you need to make sure you are signed in as a root user, for that execute the following command in the terminal:
        sudo su
It will ask you for the password. Once this is done you can start installing the various components.

Install Apache:

In order to install Apache, execute the following command:
       sudo apt-get install apache2 

If the "sudo apt-get install " command does nothing that means your package management tools are not up to date. In order to fix this, you need to run the following two commands:
      sudo apt-get update
      sudo apt-get dist-update
once, this is done you need to make sure whether the apache server is up and running, for this open the web browser and type "http://localhost", if you see the following screen then your installation is successful
apache-install-success

Configuring Apache: 

Once installation is compete you can start, stop and restart the Apache using the following commands:
      sudo /etc/init.d/apache2 start           #starts apache
      sudo /etc/init.d/apache2 stop           #stops apache
      sudo /etc/init.d/apache2 restart        #restarts apache

To prevent apache from auto start while booting up, use:
    sudo update-rc.d -f apache2 remove
To restore the apache to   auto-start on boot up use,
     sudo update-rc.d apache2 defaults

  
Changing the default website folder for apache:

"/var/www" is the default folder which is used by apache server for hosting the website. In order to change this folder to your own folder. For this type the following command:
     gedit /etc/apache2/sites-enabled/000-default

Now make the following two changes in the file and save it. Note that if you are not able to make the changes then you are not signed in as a root user.
change DocumentRoot /var/www  to  DocumentRoot 'new-folder-path'
change <Directory /var/www/>  to  <Directory /home/user/public_html/>


Installing PHP: 

Once the Apache installation is complete we can install PHP and configure Apache to understand the PHP scripts.
In order to install PHP run the following command:
    sudo apt-get install php5 libapache2-mod-php5
Once the PHP installation is complete, we need to restart the Apache so as the settings could be loaded. Run the following command to restart Apache:
    sudo /etc/init.d/apache2 restart

Now in order to test whether PHP support is enabled or not do the following:
edit the file /var/www/test.php
and copy the following line in it:

   <?php phpinfo(); ?>  


Now open the browser and type http://localhost/test.php. The page should look like this:




Installing MySQL: 

In order to install MySQL, use the following command:
    sudo apt-get install mysql-server mysql-client libmysqlclient-dev

The above command installs three packages:
  1. mysql server
  2. mysql client
  3. mysql client library

Once this is done you can use the following command to log in into mysql and creating databases and tables:
    mysql -u root -p
It asks you for the password.
Once this is done your LAMP server is ready to be used  :-)