Saturday, 11 November 2017

SSL Implementation on Operating System CentOS - 6.5

What is SSL?

SSL (Secure Sockets Layer) is the standard security protocol for establishing an encrypted link between server and client platform i.e between a web server and client browser. SSL ensures that all data transmitted between web server and browser remains encrypted and secure.

Steps to implement SSL on Linux server

For implementing SSL on linux, we first have to installed LAMP (optional if already installed) and then need to create SSL certificate as mentioned below:

  1. Installation of LAMP (Linux/Apache/MySQL/PHP) - no need to install linux as already installed centOS.
  1. Install Apache - open terminal and type command for different purposes:


    1. For installing apache
      • yum install httpd
    2. For starting apache after installation
      • /sbin/service httpd start
      • /etc/init.d/httpd start
    3. For restarting apache
      • /sbin/service httpd restart
      • /etc/init.d/httpd restart
    4. For stopping apache
      • /sbin/service httpd stop
      • /etc/init.d/httpd stop
    5. For checking current status of apache
      • /sbin/service httpd status
      • /etc/init.d/httpd status
  1. Install MySQL- open terminal and type command for different purposes:


    1. For installing MySQL
      • yum install mysql-server
    2. For starting MySQL after installation
      • /sbin/service mysqld start
      • /etc/init.d/mysqld start
    3. For restarting MySQL
      • /sbin/service mysqld restart
      • /etc/init.d/mysqld restart
    4. For stopping MySQL
      • /sbin/service mysqld stop
      • /etc/init.d/mysqld stop
    5. For checking current status of apache
      • /sbin/service mysqld status
      • /etc/init.d/mysqld status
  1. Install PHP- open terminal and type command for installing PHP:

      • yum install php php-mysql


  1. Create SSL Certificate on Apache on CentOS
  1. Install SSL - In order to set up the self signed certificate, we first have to be sure that Apache and Mod_SSL are installed on our server. You can install both with one command:

yum install mod_ssl
  1. Create New Directory - we need to create a new directory where we will store the server key and certificate

mkdir /etc/httpd/ssl
  1. Create a Self-Signed Certificate - When we request a new certificate, we can specify how long the certificate should remain valid by changing the 365 to the number of days we prefer. As it stands this certificate will expire after one year.

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/httpd/ssl/apache.key -out /etc/httpd/ssl/apache.crt 
Below is the details of each keyword used in this command:
    • openssl: This is the basic command line tool provided by OpenSSL to create and manage certificates, keys, signing requests, etc.
    • req: This specifies a sub-command for X.509 certificate signing request (CSR) management. X.509 is a public key infrastructure standard that SSL adheres to for its key and certificate management. Since we are wanting to create a new X.509 certificate, this is what we want.
    • -x509: This option specifies that we want to make a self-signed certificate file instead of generating a certificate request.
    • -nodes: This option tells OpenSSL that we do not wish to secure our key file with a passphrase. Having a password protected key file would get in the way of Apache starting automatically as we would have to enter the password every time the service restarts.
    • -days 365: This specifies that the certificate we are creating will be valid for one year.
    • -newkey rsa:2048: This option will create the certificate request and a new private key at the same time. This is necessary since we didn't create a private key in advance. The rsa:2048 tells OpenSSL to generate an RSA key that is 2048 bits long.
    • -keyout: This parameter names the output file for the private key file that is being created.
    • -out: This option names the output file for the certificate that we are generating.
When you hit "ENTER", you will be asked a number of questions. The most important item that is requested is the line that reads "Common Name (e.g. server FQDN or YOUR name)". You should enter the domain name you want to associate with the certificate, or the server's public IP address if you do not have a domain name.
  1. Set-up the Certificate- To setup hosting server to display the new certificate:


    • Open up the SSL config file using command:
vi /etc/httpd/conf.d/ssl.conf
    • Find the section that begins with <VirtualHost _default_:443> and make some quick changes.
Uncomment the DocumentRoot and ServerName line and 
replace example.com with your DNS approved domain name  
or server IP address  
(it should be the same as the common name on the certificate)
    • Find the following three lines, and make sure that they match the extensions below:
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/apache.crt  
SSLCertificateKeyFile /etc/httpd/ssl/apache.key 
Your virtual host is now all set up! Save and Exit out of the file.
  1. Restart apache - Restarting the Apache server will reload it with all of your changes in place.

    • In your browser, type https://youraddress to view the new certificate.




Saturday, 4 April 2015

Create API Doc Using Node.Js

Node.js 

Node.js is a platform built on Chrome's Javascript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.



Steps to create API Doc:

For creating API Doc using Node.js, we have to perform following steps:
  1. Installing Node.js 
  2. Installing Node packaged modules via npm 
  3. Creating input files required to generate API Doc using Node.js 
  4. Run commands to create API Docs
 
Installation of Node.js

Node.js has a Windows and Mac installer, as well as binaries for Linux users. Here I am describing installation process of Node.js for Windows but general process is same regardless of operating system or version you are using. 
To start over with node.js, first you have to download suitable Node.js installer and install it. For downloading installer, visit https://nodejs.org/download/. 


After downloading appropriate installer, install it on your computer by clicking on installer file to open Node.js Setup Wizard.


Just go through the Setup Wizard until Node has finished installing. 





Finally, we are done with node.js installation on our computer. After this installation, you have successfully installed node.js and npm (npm installed by default with node.js) .

Installation of Node packaged modules via npm

What is npm?

npm is a NodeJS package manager. As its name would imply, you can use it to install node programs. Also, if you use it in development, it makes it easier to specify and link dependencies. 

Node provides various packaged modules that you can install via npm. In this section, we will talk about apidoc module. 

We will install apidoc module and apidoc grunt-module in order to create documents. Here are npm commands to install above listed modules:

npm command to install apidoc module:

> npm install apidoc -g


npm command to install grunt-apidoc module:

> npm install grunt-apidoc --save-dev




Creating input files required to generate API Doc using Node.js


For creating document using node.js apidoc, we have generate following files:

  1. apidoc.json
  2. _apidoc.js
  3. input file and it can be generated using any of following programming languages:
    1. C#, Go, Dart, Java, JavaScript, PHP (all DocStyle capable languages)
    2. CoffeeScript
    3. Perl
    4. Python and more

For more details, please read https://www.npmjs.com/package/apidoc


apidoc.json  - configuration file

The optional apidoc.json in your projects root dir includes common information about your project like title, short description, version and configuration options like header / footer settings or template specific options. 
{
"name": "write name of document",
"version": "0.2.0",
"description": "write your description about document",
"title": "write title of document",
"url" : "url to access document file",
}


_apidoc.js  - history file to maintain version

Nice feature is the keeping of previous defined documentation blocks. That makes it possible to compare a methods version with its predecessor. Frontend Developer can thus simply see what have changed and modify their code.
Before you change your documentation block in apidoc.json, copy the old block to to this file, that's all.
/*
 * This file contain old version documentation blocks.
 */

/**
 * @api {get} /user/:id Get User information
 * @apiVersion 0.1.0
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "Kalpana",
 *       "lastname": "Dixit"
 *     }
 *
 * @apiError UserNotFound The <code>id</code> of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
 */ 


Input File (here i am creating using PHP)
/*
 * Versioning Example
 *
 * This is an versioning example for apiDoc.
 * Versioning keeps previous defined documentation blocks. That makes it possible to compare a methods version with its predecessor.
 *
 * Important is to set the version with "@apiVersion".
 *
 * Documentation blocks without @api (like this block) will be ignored.
 */

/**
 * @api {get} /user/:id Get User information and Date of Registration.
 * @apiVersion 0.2.0
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Number} id Users unique ID.
 *
 * @apiSuccess {String} firstname  Firstname of the User.
 * @apiSuccess {String} lastname   Lastname of the User.
 * @apiSuccess {Date}   registered Date of Registration.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "Kalpana",
 *       "lastname": "Dixit"
 *     }
 *
 * @apiError UserNotFound The <code>id</code> of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
 */

For more details, please visit http://apidocjs.com/ 


Run commands to create API Docs 

After creating all input files, last step is to execute apidoc command for generating apiDoc document. 

Command for generating document: 

> apidoc -i myapp/ -o apidoc/ -t mytemplate/ 

Creates an apiDoc of all files within dir myapp/, uses template (if any) from dir mytemplate/ and put all output to dir apidoc/. 
Here -t template is optional if you don't have any template files. 

Without any parameter, apiDoc generate a documentation from all .cs .dart .erl .go .java .js .php .py .rb .ts files in current dir (incl. subdirs) and writes the output to ./apidoc/.



Congratulations! We have successfully generated document using node.js .

Check on your mentioned path for output, you will have index.html file inside apidoc (or folder name which you have specified for output) folder , open index.html file in browser and have a look at your api doc.