Monday, August 24, 2009

GIT Tutorial[ Cont. ]

Following up from where I left in the last post. I will now introduce you to some concepts where GIT embeds itself into the internet and how it maintains security of project hosted on GIT.

Public Key Cryptography

We all have passwords, but the major limitation of passwords is that it's secret! Yeah, seems very odd ? Also it is a form of symmetric encryption, that is whatever you use as key( passwords ) to lock your data is used to unlock it.

Consider this simple Alice[ Bandi ] and Bob[ banda ] example. Now if Alice wants to send some data to Bob which will be encrypted over the network, they need to decide a way to encrypt it. Suppose they decide to use a simple Shift Cipher to do it. Shift Cipher Works as follows:

Plaintext: abc
Key : 2 ( say )
Ciphertext: (a+2)(b+2)(c+2) == cde

The method of Encryption is open and so is the method of Decryption. The only thing that holds any security is the KEY( 2 in the above case ). But if Alice and Bob are to use the same key for encryption and decryption, then there must be some way to communicate it before they actually do any conversation.! And hence the security of Key falls in hands of method of communication. Suppose we encrypt the method of Key Distribution we're into a loop....!!!

Public Key Cryptography comes as a rather surprise. In this scheme a person has a pair of Public and Private keys( generated by a suitable software ). The above problem of Alice<-->Bob interaction is handled in the following way.

Preconditions: Alice's and Bob's Public Keys are open to everyone, but the private keys are kept secret. A message encrypted by the Private Key can be Decrypted by the Public key and a message encrypted by a Public Key can be decrypted by the private key only.

Message Sending: Alice sends a message "abc"( say ) and encrypts that with the Public Key of Bob. Hence when Bob receives it, only he can Decrypt it with his private key.

If you notice we have completely avoided the problem of Key Distribution.

Public-Private Keys and GIT

Whenever you make a commit to the GIT repository there should be a way in which GIT can authenticate whether you're the authorised person to do so. It contains the Public Keys of all those who are allowed to make changes to the GIT Repository. When you send a message to Repository saying: "hey git, add this file", such a query is Encrypted with your Private Key, GIT knows your username and hence it sees if it has a Public Key corresponding to your username. If it exists, it'll try to Decrypt the query using the Public Key. If successful, that is the query makes sense, it'll authorise you. An incorrect Public Key will give garbage results which GIT will discard.

Making your Public Keys

Making Public Keys is simple and we'll be using a simple software called ssh to do so. All further steps are done on an Ubuntu.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sanket/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Give appropriate information, remember to give a passphrase it'll save you in case you miss your private key. The key'll be generated in /home/.ssh/
The names of the files will be: id_rsa( private key ) and id_rsa.pub( public key ).

From now on this key is your identity, and yeah it's your universal identity.!

Handing me the keys

As I am the repository owner, you'll have to hand me over the Public Keys. You may do so by mailing me a copy at my email address or Posting on the list itself, Public Keys are meant to be public so don't hesitate in telling it to the whole world ;).

Please mail the file id_rsa.pub ONLY. And also remember to rename it to a username you would like to have.

Recommendation: Suppose my gmail account is: snktagarwal. I would send a copy of id_rsa.pub as snktagarwal.pub. You are also expected to follow the protocol.

Using the public-private key pair in GIT

Now I'll show you how to use your Public Private key pair with GIT. It is assumed that you've already done the following above steps:

  • Made a Public-Private( RSA ) key pair using ssh-keygen. SSH produces RSA Key Pairs.
  • Given me the Public Key with the name .pub
  • I have added you to the trusted users group!
First let's have a view of the project repository online: http://203.110.246.113/viewgit/

In the free_monkey repository you may see many Branches below in Heads section. These are the Branches created by various "Trusted" users. Wait for it, you'll have your own branch very soon :P.

1. Configuring ssh to port 4545

SSH on port 22 is blocked outside halls, so you won't be able to do normal SSH traffic. For bypassing this security you need to be able to talk with my server on Port 4545, my server is configured so as to read on port 4545. Make the following changes:

$ sudo gedit /etc/ssh/ssh_config

Change the # Port 22 to Port 4545.

Before:

# IdentityFile ~/.ssh/id_dsa
# Port 22
# Protocol 2,1

After:

# IdentityFile ~/.ssh/id_dsa
Port 4545
# Protocol 2,1

2. Create a username for yourself in GIT

Execute these commands on your machine to tell others what your name stands for:

$ git config --global user.name "Your Name Comes Here"
$ git config --global user.email mail@domain.com

3. Check out a copy of the repository

It's as simple as it may get:

$ git clone git@203.110.246.113:free_monkey.git

Let me explain this command to you:
  • git@ means that the machine that you are contacting has a user called git which will provide the git services. It could be git or repos or anything depending on the server administrator. Hence in our case I have named it to git on my server.
  • 203.110.246.113 is IP of the server.
  • free_monkey is the name of the repository which has default extension of .git.

If you're successful you'll have somthing like this:

Initialized empty Git repository in /home/user1/git/free_monkey/.git/
Enter passphrase for key '/home/user1/.ssh/id_rsa':
remote: Counting objects: 111, done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 111 (delta 17), reused 0 (delta 0)
Receiving objects: 100% (111/111), 48.99 KiB, done.
Resolving deltas: 100% (17/17), done.

4. Make your own branch and have fun with the repository!

Here are the brief steps to make your own branch, refer to this if you want a review of commands.

$ git branch
$ git checkout
$ vim README
...make some changes...
$ git add README
$ git commit -a -m " branch created and updated"
$ git push origin


The last command needs some explanation:
  • Push: make a commit on the remote server
  • origin branchname: This tells git to push on branchname, which is your branch!

This wiki might be awefully incomplete, but given the time limitations, I am bounded. Please ask any doubts in this post or on the KGP list if needed.!


Sunday, August 16, 2009

GIT Tutorial

Hey guys and gals,

This is my version of tutorial on Git.

I won't go into much details of what git is all about, though you can always go here to have a look, git tutorial, which is actually an excellent boot up guide to Git and it's advantages. To be very precise Git was developed by Linus Torvalds to manage the Linux Kernel Tree and it turned out that Git is presently the best Version Control system on paper and in implementation!

Public and Private Repositories

Repositories are the place where you actually keep your source code. So that a person who wishes to have a look at your code in a programmer friendly way! Also it is used to do collaborative coding, while maintaining an Organizational Hierarchy, it is equivalent to a management system built into the haphazard world of Open Source :).

Public repositories are the repositories which everyone can have a look at. Generally Repositories are public so that anyone can have look, open source needs to be open ;). But the ability to write to a repository still lies in the hands of "the" few from the Circle of Trust.

Private repositories are different in the sense that you can't have a look at the code it u aren't a part of the project, private repositories are rare... though they exist.

Starting a Basic Project in Git

There are a plethora of excellent tutorials that tell you how to use git, and I won't try to reinvent the wheel. Though here's something important to note. As Git is meant for collaborative work, you check out code from some remote host( repos ) and then make local changes, and then push it back to the remote repository. First thing you need to learn is to use Git in the local system. I would suggest you to go through the following texts.

0. Use a Linux Box if you may. Windows users should look at this: windows link. But try to use a linux box because it'll be easier to get help :).

1. Official Git Tutorial - A great place to start, follow the exact steps uptill the point you can pursue. Try to read upto the section: Managing Branches, and yeah work each command on your machine.

2. Those familiar with svn should look at this

3. Once you've dealt a fair deal with using git locally it's time to dive into some Remote servers.

Web View of Git Repository

If you are familiar with version control you might be aware of Web Views for common Version Control services. Familiar are Github, Google Code. You can visit these site and look at an arbitrary project and have a feel of the Repositories. I have maintained my own version of Git Server and the Web Views can be looked up from here. We'll be using this for the rest of the tutorial so make sure you are able to get it work!

http://maillist-cse.iitkgp.ernet.in/viewgit/
or
http://203.110.246.113/viewgit/

Note:- To use the maillist-cse.iitgkp.ernet.in Link please use 144.16.192.247 as your proxy and while using the second link you don't need to change your proxies :).

So you can see a free_monkey repository. It's just a test repository containing some of my interesting source code ;).
Things to watch out for( make urself familiar, now is the time ):

1. When you enter the repository you can see, Shortlogs, Tags and Heads. Shortlogs are the commits that coders do on a repository. Something like you make a local change and push it to the remote server.

2. There are two heads, master and experimental. These are the branches that exist for the code. A single project( code ) can have multiple, non interferring, branches.

3. Some hyperlinks above show a Tree link, which is the listing of code.... the stuff inside ;).

Heads

Heads are the branches of the code. Suppose you wish to work with me on this project, so instead of working on the master copy( which is a compulsary branch ), I would ask you to start your own branch and start commiting there. In our case we have the master and experimental.

You can switch the Code tree for any branch from these links.

Checking out Code

As this repository is public, you might wish to check code to have your own copy to work with. These are the steps you need to do.

First of all, git runs on a ssh service, and as port 22 is blocked in Institue. I am running this server on 4545 port. In a standard ubuntu box you will have to make the following changes:

$ sudo vim /etc/ssh/ssh_config

Search for a line which says "Port". Uncomment it and write: Port 4545. So the section now looks like:

# IdentityFile ~/.ssh/identity
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
Port 4545
# Protocol 2,1
# Cipher 3des

Cool! Let's move further. You now need to clone the public repository into your computer. Do the following steps.

$ mkdir git # Make a folder for git repositories
$ cd git
$ git clone git://203.110.246.113/free_monkey/
Initialized empty Git repository in /home/user1/git/free_monkey/.git/
remote: Counting objects: 34, done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 34 (delta 7), reused 0 (delta 0)
Receiving objects: 100% (34/34), 42.23 KiB, done.
Resolving deltas: 100% (7/7), done.

Voila! We have got our own copy of Git source. These are the interesting things that you might wish to do:

$ cd free_monkey
$ git branch -a
$ git checkout experimental
Note: moving to 'origin/experimental' which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
git checkout -b
HEAD is now at 878804d... Graph for activity logger

If you are done till here, watch ourt for the next iteration of git tutorial. Any doubts, please reply here :)!

Saturday, August 15, 2009

Pidgin Activity logger!

Hey guys,

I am really short of time to catch up with my lunch, and hence excuse me for my gibberish English :P. Recently my dear friend came up with this hilarious idea to log the activity of online users on our Gtalk accounts. Hmm, not a bad idea heh!

So I sat down( many though I was doin madness ) and this is the strategy that I blogged down:

1. Use DBus connectivity to access information from Pidgin client. DBus is a server based model by which you can connect to applications running on your Localhost transparently.

2. Use python as the binding to DBus.

3. When first starting the program, get a count of current online users.

4. Trap signals for Signed In and Signed Out, and whenever one happens, just increment/decrement the count.

5. Log all changes to a file.

6. Make a Graph from the Log file using Matlab.

You would like to see the final output first right ? Heh:


It was a nice experience to jot down the hueristics of your Gtalk activity because we use it way too often. The graph starts at 00:00:00 Midnight for 24 hrs so that makes it one full day. And you can visibly see that the Maximum peak is at about 8-10 Pm and the lowest at around 5-6 AM.

Want your own graph, reply to this post.... and btw I might be releasing a Pidgin Plugin named Activity-Logger pretty soon ;)

For the interested if you can manufacture a plugin earlier than I may, have a look at the code urself: git hub code