Subversion in IntelliJ

If you use Subversion command line client from IntelliJ, and you have multiple versions of Subversion on your machine (e.g. Cygwin and TortoiseSVN):

Set the command line client in IntelliJ Settings to just "svn" and not the full path to e.g. C:\cygwin\bin\svn.exe

Remote JMX connection through Firewall and jump host

1. The application to monitor must first be started with jmxremote enabled with the following command line parameters.

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=6786 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false

2. Setup an ssh pipe forwarding local port to ssh port on remote host via jump box.

$ # ssh –L local_port:remote_host_address:22 jumphost_address
$ ssh –L 20000:10.229.10.144:22 artemis.corp.tnsi.com

3. Create a dynamic ssh tunnel which acts as a SOCKS proxy.

$ ssh -p 20000 -D 6786 localhost 

4.
a. Configure Java VisualVM to use a SOCKS Proxy
i. select: Tools->Options->Network->Manual proxy settings
ii. Enter localhost and the port created in the dynamic port forward in step 3.
b. Add a remote host being the destination host running the java application
i. Right click Remote, select “Add Remote Host”
ii. Enter the address of the destination host
c. Next, right click destination remote host icon and select “Add JMX Connection”
i. Enter the address of destination host together with jmx registry port number



5. Connecting JConsole to the remote java application also requires directing it to the SOCKS proxy.

C:\Program Files\Java\jdk1.7.0_11\bin>jconsole.exe -J-DsocksProxyHost=proxy_host_address -J-DsocksProxyPort=proxy_port service:jmx:rmi:///jndi/rmi://localhost:jmxregistry_port/jmxrmi

e.g. 
C:\Program Files\Java\jdk1.7.0_11\bin>jconsole.exe -J-DsocksProxyHost=localhost -J-DsocksProxyPort=6786 service:jmx:rmi:///jndi/rmi://localhost:6786/jmxrmi


Thanks to David O'Connor for figuring this out.

HTTP on the command line

OpenSSL s_client


Once connected using c_client -connect, simply type HTTP on the command line to GET/PUT/POST data.

For example:

$ openssl s_client -connect host:port -cert client.pem -key clientkey.pem -CAfile cacerts.pem
Enter pass phrase for clientkey.pem: ********

[...]

PUT /api/rest/version/9/TPRGMPITEST/3DSecureId/12345678 HTTP/1.1
Host: [...]
User-Agent: [...]
Accept: application/json
Content-Type: application/json
Content-Length: 367
Connection: close

{"3DSecure":{ ... }}





This works for JSON data. If you need to POST a html form, set the Content-Type header to "application/x-www-form-urlencoded", e.g. something like:

POST /bin/login HTTP/1.1
Host: 127.0.0.1:8000
Accept: image/gif, image/jpeg, */*
Referer: http://127.0.0.1:8000/login.html
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Content-Length: 37
Connection: Keep-Alive
Cache-Control: no-cache
   
User=Peter+Lee&pw=123456&action=login



Other Options


For examples of using telnet to send raw HTTP commands:
http://wiki.apache.org/couchdb/CouchIn15Minutes
http://www.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html

Config file modification in VI

I often need to change config files in VI, unfortunately many times in an obvious violation of DRY: we copy/paste a section of the config file, then replace a string inside the pasted section with something else. While the copy-and-paste-then-modify practice is not ideal in general, here is a way to make it a little easier and safer. This uses a sed command from within vi.


vi file.conf
...
(esc)20yy 
(esc)p
:.,.+20s/old_string/new_string/g
...

AJP Test Tool

Here is a link to a brief Perl script which is useful to quickly check whether a Tomcat server is listening on its AJP port.
http://www.joedog.org/2012/06/ajp-functional-test

Another option may be this tool, but I haven't tested this and there is no documentation available.
https://github.com/espenhw/ajp-client

As an aside, the best description of the AJP protocol I've seen so far can be found here:
http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html

Testing an SSL service with openssl

This example
  • opens an SSL connection to a host
  • sends a request
  • waits for the response

echo "request body" | openssl s_client -connect <host>:<port> -quiet


For example:

ttreitlinger@IRE1ttreitliL1 /usr/bin
$ echo "-rfgetVersion -dv19901 -tr98 -x" | openssl s_client -connect 192.168.17.128:4433 -quiet
depth=1 C = US, ST = Virgina, L = Reston, O = Transaction Network Services Inc., OU = Development, CN = TNS Internal Test CA
verify error:num=19:self signed certificate in certificate chain
verify return:0
ACK
-rfgetVersion -tr98 -rc30  -ms"CU:02.01.73 AU:02.00.65 TU:02.01.76 "  -x

ttreitlinger@IRE1ttreitliL1 /usr/bin
$

OpenSSL Commands

A collection of useful OpenSSL commands

Assumes the following files:

privkey.pem - a private key
cert.pem - a X.509 certificate
certreq.csr - a certificate signing request

# Generate a new RSA 2048 bit key
openssl genrsa -des3 -out privkey.pem 2048

# Generate a new certificate request
openssl req -new -key privkey.pem -out certreq.csr

# ...using values from a config file
openssl req -new -key privkey.pem -config cert.config -out certreq.csr

# Check a Certificate Signing Request (CSR)
openssl req -text -noout -verify -in certreq.csr

# Check a certificate
openssl x509 -in cert.pem -text -noout


The cert.config file looks as follows:

[ req ]
 default_bits           = 2048
 default_keyfile        = privkey.pem
 distinguished_name     = req_distinguished_name
 attributes             = req_attributes
 prompt                 = no

 [ req_distinguished_name ]
 C                      = US
 ST                     = Tennessee
 L                      = Chattanooga
 O                      = My Organisations Name
 OU                     = Department Name
 CN                     = My Name
 emailAddress           = info@company.com

 [ req_attributes ]
 keyUsage               = critical,clientAuth
 extendedKeyUsage       = clientAuth

CVS tagging

CVS commands related to tagging

# Tag the head
cvs rtag TAP_1_0_0_5 dev/tap

# tag at a given date/time
cvs rtag -D "2012-02-02 12:09" TAP_1_0_0_4 dev/tap


Warning: if the date for a change was taken from ChangeLog (output from cvs2cl.pl), then the timestamps reported may be different from the server time. If you are not careful, the tag could be applied to the wrong (previous) version of a file.

CVS Change Log

Here is the simplest way I have found to create log of changes from a CVS repository:

$ cd my_repo
$ cvs2cl.pl
[... lots of sysout...]
$ 

This creates a file named "ChangeLog" in the working directory. Not surprisingly, the file contains a list of changes sorted by date and userid, along with the files affected and the CVS log messages.

The perl script that does the work can be downloaded from http://www.red-bean.com/cvs2cl/cvs2cl.pl.

Testing the log4j SyslogAppender with socat

Log4j includes a SyslogAppender which can be used to forward log messages directly to a Linux/Unix syslog daemon. This requires syslog to be configured for remote logging, by adding the -r switch to SYSLOGD_OPTIONS in /etc/sysconfig/syslog. Once configured this way, Syslog listens on UDP port 514 for log messages.

When you are developing on Windows, a local syslog daemon is not available. To provide an endpoint that simply prints messages sent from the log4j SyslogAppender to stdout, you can use socat. Start socat in UDP receive mode on port 514 like this:

$ socat stdio udp-recv:514
$    # or
$ socat - udp-recv:514

To send UDP datagrams to this emulator for testing purposes, socat can be used as well:

$ socat stdio udp:127.0.0.1:514
$    # or
$ socat - udp:127.0.0.1:514

This waits for messages on stdin. Under cygwin, sends the message, or sends the message with a terminating CRLF.

One of the differences between real syslog and this socat syslog simulator is, the real syslog interprets the number in brackets at the beginning of each message as facility.level.

Syslog facilities are defined in rfc3164 as follows:


Numerical             Facility
          Code

           0             kernel messages
           1             user-level messages
           2             mail system
           3             system daemons
           4             security/authorization messages (note 1)
           5             messages generated internally by syslogd
           6             line printer subsystem
           7             network news subsystem
           8             UUCP subsystem
           9             clock daemon (note 2)
          10             security/authorization messages (note 1)
          11             FTP daemon
          12             NTP subsystem
          13             log audit (note 1)
          14             log alert (note 1)
          15             clock daemon (note 2)
          16             local use 0  (local0)
          17             local use 1  (local1)
          18             local use 2  (local2)
          19             local use 3  (local3)
          20             local use 4  (local4)
          21             local use 5  (local5)
          22             local use 6  (local6)
          23             local use 7  (local7)

           Table 1.  syslog Message Facilities



The syslog levels are: (also in rfc3164, or see man syslog)


Numerical         Severity
          Code

           0       Emergency: system is unusable
           1       Alert: action must be taken immediately
           2       Critical: critical conditions
           3       Error: error conditions
           4       Warning: warning conditions
           5       Notice: normal but significant condition
           6       Informational: informational messages
           7       Debug: debug-level messages

           Table 2. syslog Message Severities



To convert facility.level to the integer required by syslog (quote from rfc3164):


   The Priority value is calculated by first multiplying the Facility
   number by 8 and then adding the numerical value of the Severity. For
   example, a kernel message (Facility=0) with a Severity of Emergency
   (Severity=0) would have a Priority value of 0.  Also, a "local use 4"
   message (Facility=20) with a Severity of Notice (Severity=5) would
   have a Priority value of 165.  In the PRI part of a syslog message,
   these values would be placed between the angle brackets as <0> and
   <165> respectively.




How does log4j translate from Java log level to syslog level?

This is set statically in class org.apache.log4.Level as follows:

final static public Level FATAL = new Level(FATAL_INT, "FATAL", 0);
final static public Level ERROR = new Level(ERROR_INT, "ERROR", 3);
final static public Level WARN  = new Level(WARN_INT, "WARN",  4);
final static public Level INFO  = new Level(INFO_INT, "INFO",  6);
final static public Level DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
public static final Level TRACE = new Level(TRACE_INT, "TRACE", 7);

see http://www.docjar.com/html/api/org/apache/log4j/Level.java.html