OSX show used ports or listening applications with their PID

On OSX you can display applications listening on a given port using the lsof the commands described below will show listening applications as well as the PID for each application

lsof -nP -i4TCP | grep LISTEN

lsof -nP -i4TCP:[PORT] | grep LISTEN

Where [PORT] is the given port you want to check for


lsof -nP -i4TCP:80 | grep LISTEN

Your output should look something similar to this :


idea      28856 ufasoli   15u  IPv4 0xdc1e11a68e7f918b      0t0  TCP 127.0.0.1:17434 (LISTEN)
idea      28856 ufasoli   52u  IPv4 0xdc1e11a690bab54b      0t0  TCP 127.0.0.1:6942 (LISTEN)
idea      28856 ufasoli  748u  IPv4 0xdc1e11a690b9dccb      0t0  TCP 127.0.0.1:63344 (LISTEN)
idea      28856 ufasoli 1076u  IPv4 0xdc1e11a68e7fa54b      0t0  TCP 127.0.0.1:54243 (LISTEN)
idea      28856 ufasoli 1082u  IPv4 0xdc1e11a67124254b      0t0  TCP 127.0.0.1:54266 (LISTEN)
rapportd  28871 ufasoli    5u  IPv4 0xdc1e11a665f15b6b      0t0  TCP *:51387 (LISTEN)
node      33709 ufasoli   28u  IPv4 0xdc1e11a66304354b      0t0  TCP 127.0.0.1:4200 (LISTEN)
java      34245 ufasoli  168u  IPv6 0xdc1e11a66dd5f11b      0t0  TCP *:52439 (LISTEN)
java      34245 ufasoli  182u  IPv6 0xdc1e11a6803af4db      0t0  TCP *:33389 (LISTEN)
java      34245 ufasoli  183u  IPv6 0xdc1e11a6803afafb      0t0  TCP *:8080 (LISTEN)

Soap WebService NTLM Authentication with Apache CXF and Spring

Recently I had the "chance" to consume an old school WebService in good old SOAP on top of that it was a C# WebService using the dataset construct which is in itself a world of pain... but we will not talk about that here

A more easy thing to tackle is the NTLM authentication, below is how I implemented it using Spring and Spring boot :

1. Define a set of custom properties in the application.yaml file
 ws:
    base-url: "http://host/ws.asmx?wsdl"
    username: "myusername"
    password: "mypassword"
    authentication: "NTLM"
2. Map the properties in application.yaml to a Java class

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;


@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "app")
@Data
public class YAMLConfig {

    private Ws ws;    

    @Data
    public static class Ws {
        private String baseUrl;
        private String username;
        private String password;
        private String authentication;


    }   

}


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;


import javax.xml.bind.JAXBContext;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
//Injection and other properties omitted for brevity
public void consume(){

        JaxWsProxyFactoryBean factory1 = new JaxWsProxyFactoryBean();
        factory1.setServiceClass(EntityInformationSoap.class);
        factory1.setAddress(yamlConfig.getWs().getBaseUrl());
        factory1.setUsername(yamlConfig.getWs().getUsername());
        factory1.setPassword(yamlConfig.getWs().getPassword());

        EntityInformationSoap service = (EntityInformationSoap) factory1.create();
        Client client = ClientProxy.getClient(service);

        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        conduit.getAuthorization().setAuthorizationType(yamlConfig.getWs().getAuthentication());
        conduit.getAuthorization().setUserName(yamlConfig.getWs().getUsername());
        conduit.getAuthorization().setPassword(yamlConfig.getWs().getPassword());

        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(36000);
        httpClientPolicy.setAllowChunking(false);
        conduit.setClient(httpClientPolicy);
}

OSX find and display saved wifi password

https://www.mactip.net/how-to-find-a-saved-wi-fi-password-on-the-mac/

If you are like me once you entered a Wi-fi password, literally 2 seconds after it's forgotten, so what happens when your colleague asks you for the password? Well it turns out it's quite easy to accomplish on OSX (as long as you have an administrator account, below you will find the required steps

1. Open the keychain app (you can find it in the utilities folder or find it using spotlight)
2. Select the "System" icon on the sidebar
3. Double click on the Wi-fi network that you are interested in and then click on the "Show password" checkbox
4. You will now be prompted to enter an admin username/password
5. Once you entered a valid username/password you will be able to see the password for the Wi-fi network

Top 5 small java libraries for projects (functional, date, etc.)

For some time now most of my Java projects revolve around the Spring Boot Stack which contains pretty much everything you can need, however there's a list of small libraries and utilities that I love to use in my projects

Below is my top 5 small libraries I use in my projects

  • TotallyLazy : enhances the functional standard library with some syntaxic sugar
  • Lombok : because who wants to write setters/getters manually
  • JodaTime Yes, yes I know I should use now the new java API but I still like Joda best!
  • jOOλ : More functional sweetness
  • Apache Commons : Multi-purpose library

The above are my way-to-go small libraries for my projects, of course there are some specific libraries for a given case Jackson JSON or Caffeine Cache

Docker copy images between hosts

Copying docker images (import/export) between hosts

Sometimes it's useful to export a local docker image to another host without going through a repository

docker save -o [path for generated tar file] [image name]

For example :

docker save -o mycool-container.tar ufasoli/mycool-container:1.0

You will then need to copy the saved .tar image to the target host by however mean suits you (FTP, USB, SCP)

Once the image copied to the target host import the image to the local docker

docker load -i [path to image tar file]

Like so :

docker load -i mycool-container.tar

Depending on the size of the image this can be a long operation, but once it's finished you should be able to see your image with the usual docker images command

docker compose on multiple environments

docker-compose is a pretty cool tool that allows you to bootstrap and run multiple docker containers with 1 configuration file

Below is an example docker-compose file for staring a 3 node confluent kafka cluster

---
version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    network_mode: host
    environment:
      ZOOKEEPER_CLIENT_PORT: 32181
      ZOOKEEPER_TICK_TIME: 2000
    extra_hosts:
      - "moby:127.0.0.1"
    restart: always

  kafka-1:
    image: confluentinc/cp-kafka:latest
    network_mode: host
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: localhost:32181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://myserver.com:29092
    restart: always
    extra_hosts:
      - "moby:127.0.0.1"

  kafka-2:
      image: confluentinc/cp-kafka:latest
      network_mode: host
      depends_on:
        - zookeeper
      environment:
        KAFKA_BROKER_ID: 2
        KAFKA_ZOOKEEPER_CONNECT: localhost:32181
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://myserver.com:29093
      restart: always
      extra_hosts:
        - "moby:127.0.0.1"

  kafka-3:
        image: confluentinc/cp-kafka:latest
        network_mode: host
        depends_on:
          - zookeeper
        environment:
          KAFKA_BROKER_ID: 3
          KAFKA_ZOOKEEPER_CONNECT: localhost:32181
          KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://myserver.com:29094
        restart: always
        extra_hosts:
          - "moby:127.0.0.1"

  kafka-rest:
    image: confluentinc/cp-kafka-rest:latest
    network_mode: host
    depends_on:
      - kafka-1
      - kafka-2
      - kafka-3
    restart: always
    environment:
      KAFKA_REST_ZOOKEEPER_CONNECT: localhost:32181
      KAFKA_REST_LISTENERS: http://0.0.0.0:8082
      KAFKA_REST_HOST_NAME: kafka-rest
    extra_hosts:
      - "moby:127.0.0.1"

---
version: '2'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    network_mode: host
    environment:
      ZOOKEEPER_CLIENT_PORT: 32181
      ZOOKEEPER_TICK_TIME: 2000
    extra_hosts:
      - "moby:127.0.0.1"
    restart: always

  kafka-1:
    image: confluentinc/cp-kafka:latest
    network_mode: host
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: localhost:32181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://test.myserver.com:29092
    restart: always
    extra_hosts:
      - "moby:127.0.0.1"

  kafka-2:
      image: confluentinc/cp-kafka:latest
      network_mode: host
      depends_on:
        - zookeeper
      environment:
        KAFKA_BROKER_ID: 2
        KAFKA_ZOOKEEPER_CONNECT: localhost:32181
        KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://test.myserver.com:29093
      restart: always
      extra_hosts:
        - "moby:127.0.0.1"

  kafka-3:
        image: confluentinc/cp-kafka:latest
        network_mode: host
        depends_on:
          - zookeeper
        environment:
          KAFKA_BROKER_ID: 3
          KAFKA_ZOOKEEPER_CONNECT: localhost:32181
          KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://test.myserver.com:29094
        restart: always
        extra_hosts:
          - "moby:127.0.0.1"

  kafka-rest:
    image: confluentinc/cp-kafka-rest:latest
    network_mode: host
    depends_on:
      - kafka-1
      - kafka-2
      - kafka-3
    restart: always
    environment:
      KAFKA_REST_ZOOKEEPER_CONNECT: localhost:32181
      KAFKA_REST_LISTENERS: http://0.0.0.0:8082
      KAFKA_REST_HOST_NAME: kafka-rest
    extra_hosts:
      - "moby:127.0.0.1"

You can now run docker-compose by with a second -f and the redefined properties in the test file will ovewrite those in the base file

docker-compose -f docker-compose.yml -f docker-compose.test.yml up -d

More information regarding docker compose advanced configuration can be found here

Angular6 hiding component selector

It's sometimes useful to have angular's component selector not to be rendered in the page, as sometimes this can cause problems with CSS rendering (since we will have an intermediate tag in between 2 tags)

For example :


@Component({
    selector: 'my-component'
})
export class MyComponent{

@Input()
title:String;


}
...

Now you will use your component for example like so :


<html>
.....

<div class='container'>
<my-component [title]="'Test'" ></my-component>

</div>
.....

</html>

With this approach in the rendered HTML you will have the angular component tab generated inside the div but what if this causes problems with some CSS code ?

Well luckily enough there's a simple way to change this and that is changer your selector from a tag to an attribute


@Component({
    selector: '[my-component]'
})
export class MyComponent{

@Input()
title:String;


}
...

By changing the value of the selector property from my-component to [my-component] you should be able to "apply" your component directly to a standard HTML tag, so this will prevent the <my-component> tag to be rendered in the final HTML


<html>
.....
<div class='container' [title]="'Test'" my-component >


</div>
.....

</html>

OSX show used ports or listening applications with their PID

On OSX you can display applications listening on a given port using the lsof the commands described below will show listening application...