Tuesday, January 28, 2014

How to delete a remote git repository from github web page

In order to delete a git repository from github web page, first log on to github site and select the git repository, once inside the git repository you want to delete, select the settings button on top left of the page, then scroll to the bottom of the page called "Danger Zone", it has button to allow you to make it public or private, transfer the repository to others, or delete the repository.

By the way, if it fails to clone a git remote repository to local or push it to remote, you will need to set the SSH keys from settings page in github web site. You need first generate the private and public key and then load the public key from ~/ssh/id_rsa.pub to github web site's SSH key settings.

Wednesday, January 22, 2014

Understanding ios ARC and CFRefType

1. Why ARC does not handle CFRefType?
CFRefType basically is a "void *" type, it point to a core foundation object that handles CFRelease and CFRetain, but not retain and release as objective-c object does. So CFRefType is not managed by ARC. That is why when casting between Objective-c type and CFRefType, you need to tell ARC who owns the object.

__bridge: no ownership transfer
NSMutableString * string1 = [[NSMutableString allocinitWithFormat:@"First : %@"@"adb"];
 CFStringRef cfString = nil;
 cfString = (__bridge CFStringRef )string1;

 string1 = nil; //at this moment, cfString is also set to nil


__bridge_retained or CFBridgingRetain: transfer ownershjp of  Objective-C object to Core Foundation pointer, and need to call CFRelease to collect the object
 NSMutableString * string1 = nil;
 CFStringRef cfString = nil;
 string1 = [[NSMutableString allocinitWithFormat:@"First Name: %@"@"adb"];
 cfString = (__bridge_retained CFStringRef )string1;

 string1 = nil//at this moment, cfString is still hold the original string, as it has been retained

__bridge_transfer or CFBridgingRelease: transfer ownership of core foundation object to ARC, ARC will release the object when it is no longer referenced
NSMutableString * string1 = nil;
 CFStringRef cfString = CFSTR("Hello, world.");;
 string1 = (__bridge_transfer NSMutableString*)cfString;
 CFRelease(cfString); //at this moment string1 still hold the valid string data

2. Ownership transfer for Objective C property and variable for ARC
When assigning values to objective c property or variable, it implies the ownership transferring, the receiver either become the owner of the assigned object or not depending on the variable qualifier:
__strong: the receiver becomes the new owner. (default)
__weak: the receiver is not the owner, and the object may become nil anytime
__unsafe_unretained: the receive is not the owner, and not set to nil if the object is deleted.
__autoreleasing: the receiver is retained and autoreleased.  is usually used for declaring by reference  parameter, it should always used within an autorelease. Most of time, the arced object is released when the variable is set to nil or the variable is out of definition scope. __autoleasing will allow to collect an object when the thread ends or the autorelease pool close, so it uses thread instead of variable scope to control the object's life time.

3. CFRefType ownership transfer
If a method contains copy or create and return a CFRefType object, then it transfers the ownership to caller, and caller needs to CFRelease it.

4. Objective C ownership transfer
If the object transfer the ownership to caller and needs ARC to release the object, then the method needs to start with alloc, copy, mutableCopy or new. Otherwise, ARC will not collect it when it is no longer referred.

5. id and void*
id represents an Objective C object, which responds to retain and release method, while void * is just an memory blob.  A lot of objective C API accepts id as parameter, but not void*, so void* cannot be cast as id.



Wednesday, January 8, 2014

npm package folder on mac

The npm package may be installed on the following folders on mac
1.  ./node_modules
If the package is installed locally, the folder is in the current folder's ./node_modules of the current package. You should install the package locally, if the package will be loaded into your js app with require() method.

2.    /usr/local/lib/node_modules folder
If the package is installed globally, i.e, installed with -g flag, then it is installed in
 /usr/local/lib/node_modules folder

3. ~/.npm 
There will be another copy in your home directory's ~/.npm folder, it is a cache that npm uses to avoid re-downloading the same package multiple times. There's no harm in removing it. You can empty it with the command:
npm cache clean


To uninstall a local npm package, use
npm rm

to uninstall a global npm package, use
npm rm -g

To check an installed npm package version, use
npm view packagename

Thursday, January 2, 2014

Terminal command shortcut for Windows, Mac and Visual Studio Code

For Windows

start .
open Windows File Explorer at the current folder of command line console. 

code .
open Visual Studio Code at the current folder of command line console.

 
For MAC

Command+T
create a new tab

Command+D
split terminal window

Command+shife+D
unsplit terminal window

Command+option +D
hide or show dock bar

open .bash_history
open terminal history in editor

history
display all terminal history

alias name='command text'
Create a command alias for current terminal session. The alias must be the first word when run from the terminal. Set the alias to profile if the alias is used for every terminal window.

Note, when running .sh file from mac terminal, first you need to set it executable file attribute with
chmod +x myshell.sh
In addition, bash command does not search the current directory for the file to run, so you need to specify the full path to run it as below (assume the shell file is in the current directory)
./myshell.sh


For Visual Studio Code OnWindows

Shortcut for visual studio code on Windows
to open a file in new VS code window, first select the file, then Ctrl + K, and O

Comment a block of code in visual studio code on Windows
select the block of code, then Ctrl + K + C to comment,
Ctrl + K + U to uncomment.


For Chrome Debugger on Windows

Ctrl + O to open the javascript source file based on file name.

Tuesday, December 31, 2013

Use p4merge for git diff and merge on mac (update based on p4merge for mac 2018)

(update based on mac P4Merge 2018)

Assuming p4merge is already installed on mac in the default /Application folder.

1. create a sh script file called mymerge.sh with below content in /usr/local/bin folder
#!/bin/sh 
/Application/p4merge.app/Contents/Resources/launchp4merge $*


2. create a sh script file: /usr/local/bin/extMerge with the following content
#!/bin/sh mymerge.sh $*

3.create another script file: /usr/local/bin/extDiff with the following content
#!/bin/sh
[ $# -eq 7 ] && /usr/local/bin/extMerge "$2" "$5"
4. set the execute permission for the above files
$ sudo chmod +x /usr/local/bin/mymerge.sh
$ sudo chmod +x /usr/local/bin/extMerge $ sudo chmod +x /usr/local/bin/extDiff
5. edit .gitConfig file under the user home directory as below
[merge] tool = extMerge [mergetool "extMerge"] cmd = extMerge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\" trustExitCode = false prompt = false [diff] external = extDiff tool = extDiff [difftool "extDiff"] cmd = mymerge.sh \"$LOCAL\" \"$REMOTE\" trustExitCode = false prompt = false

6. run
git diff
or
git difftool
or
git mergetool
on an updated repository to see the result.

Link:
https://community.perforce.com/s/article/2848
http://git-scm.com/book/en/Customizing-Git-Git-Configuration

Monday, December 30, 2013

Publish local git repository on mac for backup

Unlike other source management system, commit in git only saves the change in your local box before pushing the change into remote repository. If your local box stops working, you will lose all you committed work in git.

To avoid happening, before you are ready to push the local git repository's change into remote git repository, you can publish your local git repository using git daemon, and then backup it in another git repository on another box.

Steps to publish a local git repository on mac:
cd YourGitFolder
git daemon --base-path=. --export-all --enable=receive-pack --reuseaddr --informative-errors --verbose

From a different box, run the below clone commands to get the repository
git clone git://FirstBoxIPAddress/

Note client can’t push into the change into the server's active git branch. Before pushing, user on the server should change to another branch.

By the way, the following command can be used to monitor the commit history for all branch
gitk --all
to avoid block the terminal app, append & at the end
gitk --all &




Monday, December 16, 2013

Configure client certificate mapping on iis for mutual authentication

IIS Client certificate settings:
There are two places for client certificate settings in iis manager. It is very important to understanding the difference of these two settings.

The first place is in SSL settings under each web application's setting. There is a client certificate radio button. If it is selected as required, it means, when client connects to server using ssl, server will challenge the client's certificate, and the client certification must be signed by a root CA trusted by server (existing in server's Trusted root CA store). For example, a web application can allow anonymous authentication, but require client to use ssl with "Require" client certificate. If so, as long as client certificate is signed by a trusted CA, the client can finish the request without any one-to-one or many-to-one setting described in the second place.

The second place is web site scope setting of Configuration Editor under "Default web site".  The setting is for authenticating a client using client certificate. Note under web application's authentication setting, you can only set Anonymous, Basic, Digest, Form... authentication, there is not a client certificate setting for you to enable. So in order to enable client certificate authentication for your web app, you should disable all authentication items for your app under its authentication settings. And then using configuration editor under the default web site to enable it by configuring either one-to-one mapping or one-to-many mapping. Otherwise, as you already disabled all authentication method, even if the client certificate is trusted by server, the client cannot be authenticated by server and causes the request to fail. Note if you has enabled other kind authentication under authentication settings, the client certificate mapping is really not necessary.

Generate client certificate
(Steps are from: http://msdn.microsoft.com/en-us/library/ff650751.aspx)
1.Generated root certificate for creating client cert.
Open a Visual Studio command prompt and browse to the location where you want to save the certificate files. Run the following command to create the root CA:
makecert -n "CN=RootCaClientTest" -r -sv RootCaClientTest.pvk RootCaClientTest.cer

2. Create a Certificate Revocation List File from the Root Certificate with following command:
makecert -crl -n "CN=RootCaClientTest" -r -sv RootCaClientTest.pvk RootCaClientTest.crl

3. Install Your Client Root Certificate Authority on the Client and Server Machines with following steps:
In the command console, type MMC and then click OK.
In the Microsoft Management Console, on the File menu, click Add/Remove Snap-in.
In the Add Remove Snap-in dialog box, click Add.
In the Add Standalone Snap-in dialog box, select Certificates and then click Add.In the Certificates snap-in dialog box, select the Computer account radio button (because the certificate needs to be made available to all users), and then click Next.
In the Select Computer dialog box, leave the default Local computer: (the computer this console is running on) selected and then click Finish.
In the Add Standalone Snap-in dialog box, click Close.
In the Add/Remove Snap-in dialog box, click OK.
In the left pane, expand the Certificates (Local Computer) node, and then expand the Trusted Root Certification Authorities folder.
Under Trusted Root Certification Authorities, right-click the Certificates subfolder, click All Tasks, and then click Import.
On the Certificate Import Wizard welcome screen, click Next.
On the File to Import screen, click Browse.
Browse to the location of the signed root CA RootCaClientTest.cer file copied in Step 1, select the file, and then click Open.
On the File to Import screen, click Next
On the Certificate Store screen, accept the default choice and then click Next.
On the Completing the Certificate Import Wizard screen, click Finish.

4. Install the Certificate Revocation List File (CLR) on the Server and Client Machines, which is checked during the certificate validation process.

In the command line, type MMC, add Certificates snap-in, and then click Add.
In the Certificates snap-in dialog box, select the Computer account radio button (because the certificate needs to be made available to all users), and then click Next.
In the Select Computer dialog box, leave the default Local computer: (the computer this console is running on) selected and then click Finish.
In the left pane, expand the Certificates (Local Computer) node, and then expand the Trusted Root Certification Authorities folder.
Under Trusted Root Certification Authorities, right-click the Certificates subfolder, select All Tasks, and then click Import.
On the Certificate Import Wizard welcome screen, click Next.
On the File to Import screen, click Browse.
On the Files of Type screen, select Certificate Revocation List.
Browse to the location of the signed root CA RootCaClientTest.crl file copied in Step 1, select the file, and then click Open.
On the File to Import screen, click Next.
On the Certificate Store screen, accept the default choice and then click Next.
On the Completing the Certificate Import Wizard screen, click Finish.

5. Create and Install Your Temporary Client Certificate

Open a Visual Studio command prompt and browse to the location where the root CA certificate and private key file you created are stored.
Run the following command for creating a certificate signed by the root CA certificate:
makecert -sk MyKeyName -iv RootCaClientTest.pvk -n "CN=tempClientcert" -ic RootCaClientTest.cer -sr currentuser -ss my -sky signature -pe 

In this command:
-sk specifies the key container name for the certificate. This needs to be unique for each certificate you create.
-iv specifies the private key file from which the temporary certificate will be created. You need to specify the root certificate private key file name that was created in the previous step and make sure that it is available in the current directory. This will be used for signing the certificate and for key generation.
-n specifies the key subject name for the temporary certificate. The convention is to prefix the subject name with "CN = " for "Common Name".
-ic specifies the file containing the root CA certificate file generated in the previous step.
-sr specifies the store location where the certificate will be installed. The default location is currentuser. For certificate authentication, this is the default location that Microsoft Internet Explorer uses for when browsing Web sites that require a client certificate.
-ss specifies the store name for the certificate. My is the personal store location of the certificate.
-sky specifies the key type, which could be either signature or exchange. Using signature makes the certificate capable of signing and enables certificate authentication.
-pe specifies that the private key is generated in the certificate and installed with it in the certificate store. When you double-click the certificate on the General tab, you should see the message “You have a private key that corresponds to this certificate” displayed at the bottom. This is a requirement for certificate authentication. If the certificate does not have the corresponding private key, it cannot be used for certificate authentication.

6. The steps to generate iis server certificate for ssl connection  is not included here, please refer http://jonathanblog2000.blogspot.ca/2013/12/how-to-deploy-aspnet-project-to-iis-by.html.

Configure IIS for client certificate authentication (one-to-one mapping)
(http://www.iis.net/learn/manage/configuring-security/configuring-one-to-one-client-certificate-mappings)
1. Getting the Certificate Blob
Export the client cert file TempClientCert.cer from MMC certificate snap-in with Base64 encoding.  Right click on your client .cer file, and open it in notepad.
Remove -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----
Format the certificate blob to be a single line.
Save this file as clientCertBlob.txt

2. Configure IIS for client certificate One to One Mapping
Start  IIS Manager,
Select the web site (Default Web Site, this can not be done on web application) that is being configured and open Configuration Editor icon
Type "system.webServer/security/authentication/iisClientCertificateMappingAuthentication" in the Section drop down box.
Select the enabled field and change the value to true
Select the oneToOneCertificateMappingsEnabled property grid entry and change the value to true
Select the oneToOneMappings property grid entry and click Edit Items... in the Actions Task Pane
Click Add in the Collection Editor task list
Copy the single string certificate blob from above and paste it into the certificate field
Set the userName and password that clients will be authenticated as.
Set the enabled field to true
Close Collection Editor
Click Apply in the Actions Task Pane

3: Enabling Client Certificate Authentication For A Web Site Using SSL
Once a mapping has been created and the feature has been enabled, a site must be configured to use client certificates.
From  IIS Manager UI, select the SSL web application you want to use client certificates
Select the SSL settings module
Under Client certificates: select the Require or Accept radio button
Click Apply in the Actions Task Pane
Disable all authentication method for the web application.


4: Verifying It All Works (using firefox)

Export client certificate with private key to a file.
Import client certificate (with private key) into firefox browser by opening option->advanced->Certificate tab. Select view certificate, and import the certificate into "Your Certificate" tab. Once it is done, it will show the certificate under RootCaClientTest node.
Use https connection to visit the iis web application. You will be prompted to select a client certificate.


Configure IIS for client certificate authentication (many-to-one mapping)
(https://blogs.iis.net/webtopics/archive/2010/04/27/configuring-many-to-one-client-certificate-mappings-for-iis-7-7-5.aspx)
If you are within an enterprise environment, and each developer already has his own corporate certificate, it is easier to setup many-to-one client certificate for iis mutual authentication.

Similar to one-to-one mapping, select the configuration editor under the default web site, and set enabled to true
Set manyToOneCertificateMappingsEnabled to True
Select manyToOneMappings and click on the extreme end at the Ellipsis button to launch the new window for configuring mappings.
Under this new window go ahead and Add a new item. You can modify the properties from within the window
Click on the Ellipsis button for rules and this will give you an option to add multiple patterns for matching based on certificate properties. For example, you can set certificateField to "Issuer" and certificateSubField to "CN", and matchCriteria to "SSO_CA", it will map the client certificate issued by SSO_CA to the specified user account.
Set the userName and password that clients will be authenticated as.
Apply the change.
Disable all authentication methods under web application's authentication settings.
Request the server from browser and you should be prompted for client certificate.