/
main.go
94 lines (75 loc) · 2.12 KB
/
main.go
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
/*
* Copyright (C) 2023 Kendall Tauser
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package main
import (
"crypto/ed25519"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
"github.com/fire833/morfic/pkg/config"
"github.com/fire833/morfic/pkg/node"
)
func main() {
testConfig() // set up the configuration struct.
cert, err := ephemeralCert()
if err != nil {
fmt.Printf("Unable to create node server cert: %v\n", err)
os.Exit(1)
}
if e := node.BeginNodeServer(cert); e != nil {
fmt.Printf("Unable to start node grpc server: %v\n", e)
os.Exit(1)
}
}
func testConfig() {
config.CPRF = new(config.ControlPlaneRuntimeConfig)
config.CPRF.CRISocket = "/var/run/morfic/morfic_node.sock"
}
func ephemeralCert() (*tls.Certificate, error) {
pub, priv, e := ed25519.GenerateKey(rand.Reader)
if e != nil {
return nil, e
}
templ := &x509.Certificate{
SerialNumber: big.NewInt(1),
SignatureAlgorithm: x509.PureEd25519,
IsCA: false, // This is the CA.
NotBefore: time.Now(),
NotAfter: time.Now().Local().AddDate(0, 0, 1),
PublicKey: pub,
PublicKeyAlgorithm: x509.Ed25519,
}
cert, e1 := x509.CreateCertificate(rand.Reader, templ, templ, pub, priv)
if e1 != nil {
return nil, e1
}
data := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
})
finalcert, e2 := tls.X509KeyPair(data, priv)
if e2 != nil {
return nil, e2
}
return &finalcert, nil
}