Sunday, May 31, 2020

PGP encryption/decryption using Apache Camel, Spring Boot

First, an update on the computing environment. The Acer C720 i3 core with 4G RAM Chromebook was updated with a 128 GB SSD hard drive and converted to Ubuntu 18.04, most recently to Lubuntu 20.04. It's working great using the xcfe desktop I was used to using with Crouton.

So, I've got a requirement to convert a bunch of files downloaded from an SFTP server encrypted with PGP. There's plenty of resources online to see how to do generate a public-private key pair. Export the public key to an *.asc file, and make a copy of the private key, also in *.asc. These will both by armored, meaning Base64 encoded.

Copy the keys to the project root of your Spring Boot application, e.g. in a /keys directory.

From the documentation at PGP - Apache Camel, add the following dependency to build.gradle,

compile group: 'org.apache.camel.springboot', name: 'camel-crypto-starter', version:camelCoreVersion 

Now, we're ready to add some code. I've uploaded the code to a GitHub gist.

Create folders for /pgp/original, /pgp/encrypted, and
/pgp/decrypted. Create a file in /pgp/original with some secret text. Run PGPEncryptor, and you should see output in the encrypted and decrypted folders with your encrypted file, and decrypted file respectively.

Well, it's Sunday and this POC needed to be complete for Monday, just in time!

Much help was obtained from this blog post and other resources found around the web. Many thanks to the authors!

Tuesday, November 10, 2015

Why Computer Programmers Should Stop Calling Themselves Engineers - The Atlantic

There are lots of analogies to be made between the construction trade and software development. Maybe a follow up piece? It's only natural to call the practitioners by engineering titles. Not everyone who codes is an engineer, just as not everyone who writes is a journalist. However, a lot of the same concepts go into each. The interesting part is how far the analogy holds, not probing at the weak points, but it would take a practitioner to appreciate it. I probobly wouldn't click on a story about blogging versus journalism. Why is a journalist writing about engineering? It seems the journalist in this case is making the same mistake he decries, or pehaps he's qualified. 



Professional licensing is almost singularly the only difference between the professions. It's called Software Engineering for a reason, with the emphasis on "soft", versus "hard", each with its own set of requirements. Not recognizing this difference only emphasizes the authors lack of subject matter expertise. Besides, there are volatile components, mainly memory that can inexplicably fail, causing a car to not start due to software failing to error check. That's a cost component, and we know how that works out in the manufacturing industry. The similarities across disciplines are more numerous and far more interesting than the differences.



Why Computer Programmers Should Stop Calling Themselves Engineers - The Atlantic:



'via Blog this'

Saturday, March 28, 2015

Spring 3 MVC and REST

For anyone working with Spring 3 MVC and REST, I spent a lot of time getting this sorted, so I thought I would post some results.

Spring 3 is a few years old now, but I'm using it at my day job, so that's what I'm using here. You can get a Spring 3 MVC project running by following any number of tutorials. I won't go into those details here.

The biggest gotcha I ran into wasn't well documented anywhere, in fact, I found two singular pieces of information which enabled me to get this code working after some considerable effort. First is using Jackson version 1.9, instead of 2.x, for Spring MVC versions prior to 3.1. Here's the POM entries.

<properties>
 <jackson.version>1.9.13</jackson.version>
</properties>

<dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-core-asl</artifactId>
 <version>${jackson.version}</version>
</dependency>

<dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-mapper-asl</artifactId>
 <version>${jackson.version}</version>
</dependency>

Secondly, with Spring MVC 3.0, you need this in your bean configuration;

 <!-- JSON support -->
 <bean id="jacksonMessageConverter"
  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
 <bean
  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
   <list>
    <ref bean="jacksonMessageConverter" />
   </list>
  </property>
 </bean>

So, let's take a request from the beginning. I used Advanced Rest Client chrome extension, but this works as a simple URL request. Your web.xml file needs handle the request,

<!-- Initialise the Spring MVC DispatcherServlet -->
<servlet>
 <servlet-name>spring-myapp</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map the Spring MVC DispatcherServlet to intercept *.sp requests -->
<servlet-mapping>
 <servlet-name>spring-myapp</servlet-name>
 <url-pattern>*.sp</url-pattern>
</servlet-mapping> 

<servlet-mapping>
 <servlet-name>spring-myapp</servlet-name>
 <url-pattern>json/*</url-pattern>
</servlet-mapping> 


Spring maps the URL to a controller,

<context:component-scan base-package="com.nml.myapp.web.controller" />
<context:annotation-config />
<mvc:annotation-driven />

and with Jackson in the classpath, the controller code returns the JSON response,


@Controller
public class JsonController {

 private static final Logger logger = Logger.getLogger(JsonController.class);

 @RequestMapping(value = "/getCategories", method = RequestMethod.GET)
 public @ResponseBody List<String> query() {

  logger.info("rest request: getCategories");
  
  List<String> list = new ArrayList<String>();
  list.add("object is list");
  list.add("second object"); 
//  model.addAttribute("list", list);
  
  return list;
 }
}

Here's the result. My first good night's sleep in a week.