Skip to content

Commit

Permalink
First work for #359
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuancelin committed Oct 4, 2019
1 parent b3f15f6 commit a301410
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
7 changes: 7 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ test_server () {
# rc=$?; if [ $rc != 0 ]; then exit $rc; fi
}

test_mtls () {
cd $LOCATION/scripts/mtls-test
sh ./tests.sh
rc=$?; if [ $rc != 0 ]; then exit $rc; fi
}

case "${1}" in
all)
clean
build_ui
build_manual
build_server
test_server
test_mtls
# build_cli
;;
cli)
Expand Down
3 changes: 3 additions & 0 deletions scripts/mtls-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.out
*.pem
*.jar
45 changes: 45 additions & 0 deletions scripts/mtls-tests/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"log"
"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
// Write "Hello, world!" to the response body
io.WriteString(w, "Hello, world!\n")
}

func main() {
// Set up a /hello resource handler
http.HandleFunc("/hello", helloHandler)

// Create a CA certificate pool and add cert.pem to it
caCert, err := ioutil.ReadFile("cert-backend.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

// Create the TLS Config with the CA pool and enable Client certificate validation
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
tlsConfig.BuildNameToCertificate()

// Create a Server instance to listen on port 8443 with the TLS config
server := &http.Server{
Addr: ":8444",
TLSConfig: tlsConfig,
}

// Listen to HTTPS connections with the server certificate and wait
log.Fatal(server.ListenAndServeTLS("cert-backend.pem", "cert-backend-key.pem"))
}

2 changes: 2 additions & 0 deletions scripts/mtls-tests/certs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -out cert-backend.pem -keyout cert-backend-key.pem -subj "/CN=localhost"
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -out cert-frontend.pem -keyout cert-frontend-key.pem -subj "/CN=mtls.oto.tools"
52 changes: 52 additions & 0 deletions scripts/mtls-tests/clientbackend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
)

func main() {
// Read the key pair to create certificate
cert, err := tls.LoadX509KeyPair("cert-backend.pem", "cert-backend-key.pem")
if err != nil {
log.Fatal(err)
}

// Create a CA certificate pool and add cert.pem to it
caCert, err := ioutil.ReadFile("cert-backend.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

// Create a HTTPS client and supply the created CA pool and certificate
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{cert},
},
},
}

// Request /hello via the created HTTPS client over port 8443 via GET
r, err := client.Get("https://localhost:8444/hello")
if err != nil {
log.Fatal(err)
}

// Read the response body
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}

// Print the response body to stdout
fmt.Printf("%s\n", body)
}
52 changes: 52 additions & 0 deletions scripts/mtls-tests/clientfrontend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
)

func main() {
// Read the key pair to create certificate
cert, err := tls.LoadX509KeyPair("cert-frontend.pem", "cert-frontend-key.pem")
if err != nil {
log.Fatal(err)
}

// Create a CA certificate pool and add cert.pem to it
caCert, err := ioutil.ReadFile("cert-frontend-key.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)

// Create a HTTPS client and supply the created CA pool and certificate
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{cert},
},
},
}

// Request /hello via the created HTTPS client over port 8443 via GET
r, err := client.Get("https://mtls.oto.tools:8443/")
if err != nil {
log.Fatal(err)
}

// Read the response body
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}

// Print the response body to stdout
fmt.Printf("%s\n", body)
}
13 changes: 13 additions & 0 deletions scripts/mtls-tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sh ./certs.sh
go run backend.go &
go run clientbackend.go > clientbackend.out
# TODO: assert clientbackend.out content
# TODO: get otoroshi.jar here
# TODO: java -Dapp.domain=oto.tools -jar otoroshi.jar &
# TODO: delete existing certs in otoroshi
# TODO: inject certs in otoroshi
# TODO: waits 10sec
# TODO: go run clientfrontend.go > clientfrontend.out
# TODO: assert clientfrontend.out content
killall go >> /dev/null
killall java >> /dev/null

0 comments on commit a301410

Please sign in to comment.