/
main.go
157 lines (148 loc) · 4.02 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// 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 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// 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, see <http://www.gnu.org/licenses/>.
package main
import (
"crypto/x509/pkix"
"encoding/asn1"
"fmt"
"net"
"os"
"sort"
"github.com/Sirupsen/logrus"
version "github.com/goodrain/rainbond/cmd"
"github.com/urfave/cli"
)
//Config Config
type Config struct {
CrtName, KeyName string
Address []string
IsCa bool
CAName, CAKeyName string
Domains []string
}
func main() {
App := cli.NewApp()
App.Version = version.GetVersion()
App.Commands = []cli.Command{
cli.Command{
Name: "create",
Flags: []cli.Flag{
cli.StringFlag{
Name: "crt-name",
Value: "",
Usage: "creat crt file name",
},
cli.StringFlag{
Name: "crt-key-name",
Value: "",
Usage: "creat crt key file name",
},
cli.StringSliceFlag{
Name: "address",
Value: &cli.StringSlice{"127.0.0.1"},
Usage: "address list",
},
cli.StringSliceFlag{
Name: "domains",
Value: &cli.StringSlice{""},
Usage: "domain list",
},
cli.StringFlag{
Name: "ca-name",
Value: "./ca.pem",
Usage: "creat or read ca file name",
},
cli.StringFlag{
Name: "ca-key-name",
Value: "./ca.key.pem",
Usage: "creat or read ca key file name",
},
cli.BoolFlag{
Name: "is-ca",
Hidden: false,
Usage: "is create ca",
},
},
Action: create,
},
}
sort.Sort(cli.FlagsByName(App.Flags))
sort.Sort(cli.CommandsByName(App.Commands))
App.Run(os.Args)
}
func parseConfig(ctx *cli.Context) Config {
var c Config
c.Address = ctx.StringSlice("address")
c.CAKeyName = ctx.String("ca-key-name")
c.CAName = ctx.String("ca-name")
c.CrtName = ctx.String("crt-name")
c.KeyName = ctx.String("crt-key-name")
c.Domains = ctx.StringSlice("domains")
c.IsCa = ctx.Bool("is-ca")
return c
}
func create(ctx *cli.Context) error {
c := parseConfig(ctx)
info := c.CreateCertInformation()
if c.IsCa {
err := CreateCRT(nil, nil, info)
if err != nil {
logrus.Fatal("Create crt error,Error info:", err)
}
} else {
info.Names = []pkix.AttributeTypeAndValue{
pkix.AttributeTypeAndValue{
Type: asn1.ObjectIdentifier{2, 1, 3},
Value: "MAC_ADDR",
},
}
crt, pri, err := Parse(c.CAName, c.CAKeyName)
if err != nil {
logrus.Fatal("Parse crt error,Error info:", err)
}
err = CreateCRT(crt, pri, info)
if err != nil {
logrus.Fatal("Create crt error,Error info:", err)
}
}
fmt.Println("create success")
return nil
}
//CreateCertInformation CreateCertInformation
func (c *Config) CreateCertInformation() CertInformation {
baseinfo := CertInformation{
Country: []string{"CN"},
Organization: []string{"Goodrain"},
IsCA: c.IsCa,
OrganizationalUnit: []string{"goodrain rainbond"},
EmailAddress: []string{"zengqg@goodrain.com"},
Locality: []string{"BeiJing"},
Province: []string{"BeiJing"},
CommonName: "rainbond",
CrtName: c.CrtName,
KeyName: c.KeyName,
Domains: c.Domains,
}
if c.IsCa {
baseinfo.CrtName = c.CAName
baseinfo.KeyName = c.CAKeyName
}
var address []net.IP
for _, a := range c.Address {
address = append(address, net.ParseIP(a))
}
baseinfo.IPAddresses = address
return baseinfo
}