Sunday, May 15, 2016

Two And More SSH Keys in OSX

Short note about maintaining several "public - private" ssh key pairs in OSX.

Let's assume that You already have one "public - private" key pair locally which You're using for accessing something (git, jenkins etc.) and You don't want to use same keys for another resource then just follow next steps.

1. Move to Your .ssh directory

 $ cd ~/.ssh  

2. Generate key pair with next command

 $ ssh-keygen -t rsa -f ~/.ssh/secondKey -C "email@email.com"  

if You won't add secondKey (or whatever You want it to be named) this command will override Your main key, also You can add a key phrase or leave it blank.

3. Add an entry to Your config file inside .ssh folder (if You don't have that just create one with - $ touch config)

 Host firsthost.com  
  User git  
  Hostname firsthost.com  
  PreferredAuthentications publickey  
  IdentityFile ~/.ssh/id_rsa  
 Host secondhost.com  
  User git  
  Hostname secondhost.com  
  PreferredAuthentications publickey  
  IdentitiesOnly yes  
  IdentityFile ~/.ssh/secondKey  

That's simply it and if You need more keys - just repeat those steps above.

Wednesday, May 11, 2016

Enum and Property file cooperation in Java

Short reminder how to populate Enum in Java with values from property file.

1. Create an Enum as described in example below:

 public enum Users {  
   USER_1, USER_2;  
   private static final String PATH = "/selenium/environment/users.properties";  
   private static Properties properties;  
   private String value;  
   private void init() {  
     if (properties == null) {  
       properties = new Properties();  
       try {  
         properties.load(Users.class.getResourceAsStream(PATH));  
       }  
       catch (Exception e) {  
         e.printStackTrace();  
       }  
     }  
     value = (String) properties.get(this.toString());  
   }  
   public String getValue() {  
     if (value == null) {  
       init();  
     }  
     return value;  
   }  
 }  

Of course it's not an ideal solution and You may come up with better one but this works for me as expected and that's all I need.

2. Create a properties file (short reminder - this file should be placed into resources folder in Your project)

 USER_1=secretlogin1  
 USER_2=secretlogin2  

in this case we have just two entries but You can expand that.

3. Call that from code

 journey.login(Users.USER_1);  

Well it's pretty much it and hopefully this will save me from "googling" next time I'll need such code.