summaryrefslogtreecommitdiff
path: root/main.go
blob: 2f61ec8bd5145242fc999e82754c7220dc042722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main

import (
	"flag"
	"log"
	"net/http"
	"os"
	"strconv"
)

// TODO get this from the config yaml?
const keySize = 2048
const expiryDays = 28

type certHandler struct {
	cainfo caInfo
}

func (ch *certHandler) certResponder(w http.ResponseWriter, r *http.Request) {
	ch.cainfo.CertWriter(w)
}

func doCaFilesSanityCheck(caCrt string, caKey string) {
	if _, err := os.Stat(caCrt); os.IsNotExist(err) {
		log.Fatal("cannot find caCrt file")
	}
	if _, err := os.Stat(caKey); os.IsNotExist(err) {
		log.Fatal("cannot find caKey file")
	}
}

func doTlsFilesSanityCheck(tlsCrt string, tlsKey string) {
	if _, err := os.Stat(tlsCrt); os.IsNotExist(err) {
		log.Fatal("cannot find tlsCrt file")
	}
	if _, err := os.Stat(tlsKey); os.IsNotExist(err) {
		log.Fatal("cannot find tlsKey file")
	}
}

func httpFileHandler(route string, path string) {
	http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, path)
	})
}

type Opts struct {
	caCrt  string
	caKey  string
	port   int
	notls  bool
	tlsCrt string
	tlsKey string
}

func initializeFlags(opts *Opts) {
	flag.StringVar(&opts.caCrt, "caCrt", "", "path to the CA public key")
	flag.StringVar(&opts.caKey, "caKey", "", "path to the CA private key")
	flag.IntVar(&opts.port, "port", 8000, "port where the server will listen")
	flag.BoolVar(&opts.notls, "notls", false, "disable TLS on the service")
	flag.StringVar(&opts.tlsCrt, "tls_crt", "", "path to the cert file for TLS")
	flag.StringVar(&opts.tlsKey, "tls_key", "", "path to the key file for TLS")
	flag.Parse()

	auth := os.Getenv("AUTH")
	log.Println("AUTH-->", auth)

}

func checkConfigurationOptions(opts *Opts) {

	if opts.caCrt == "" {
		log.Fatal("missing caCrt parameter")
	}
	if opts.caKey == "" {
		log.Fatal("missing caKey parameter")
	}

	if opts.notls == false {
		if opts.tlsCrt == "" {
			log.Fatal("missing tls_crt parameter. maybe use -notls?")
		}
		if opts.tlsKey == "" {
			log.Fatal("missing tls_key parameter. maybe use -notls?")
		}
	}

	doCaFilesSanityCheck(opts.caCrt, opts.caKey)
	if opts.notls == false {
		doTlsFilesSanityCheck(opts.tlsCrt, opts.tlsKey)
	}
}

func main() {
	opts := new(Opts)
	initializeFlags(opts)
	checkConfigurationOptions(opts)

	ci := newCaInfo(opts.caCrt, opts.caKey)
	ch := certHandler{ci}

	// add routes here
	http.HandleFunc("/3/cert", ch.certResponder)
	httpFileHandler("/3/configs.json", "./public/3/configs.json")
	httpFileHandler("/3/service.json", "./public/3/service.json")
	httpFileHandler("/3/config/eip-service.json", "./public/3/eip-service.json")
	httpFileHandler("/provider.json", "./public/provider.json")
	httpFileHandler("/ca.crt", "./public/ca.crt")
	httpFileHandler("/3/ca.crt", "./public/ca.crt")

	pstr := ":" + strconv.Itoa(opts.port)

	if opts.notls == true {
		log.Fatal(http.ListenAndServe(pstr, nil))
	} else {
		log.Fatal(http.ListenAndServeTLS(pstr, opts.tlsCrt, opts.tlsKey, nil))

	}
}