-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from srl-wim/cert
cert generation
- Loading branch information
Showing
12 changed files
with
293 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,5 @@ graph/* | |
lab_wan | ||
lab_wan/* | ||
srl_config/license.key | ||
|
||
lab-wan | ||
lab-wan/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"CN": "{{.Prefix}} Root CA", | ||
"key": { | ||
"algo": "rsa", | ||
"size": 2048 | ||
}, | ||
"names": [{ | ||
"C": "BE", | ||
"L": "Antwerp", | ||
"O": "Nokia", | ||
"OU": "Container lab" | ||
}], | ||
"ca": { | ||
"expiry": "262800h" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"CN": "{{.Name}}.{{.Prefix}}.io", | ||
"key": { | ||
"algo": "rsa", | ||
"size": 2048 | ||
}, | ||
"names": [{ | ||
"C": "BE", | ||
"L": "Antwerp", | ||
"O": "Nokia", | ||
"OU": "Container lab" | ||
}], | ||
"hosts": [ | ||
"{{.Name}}.{{.Prefix}}.io" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"text/template" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func parsecfsslInput(i *[]byte) (out string) { | ||
in := strings.Split(string(*i), "{") | ||
for i, s := range in { | ||
if i != 0 { | ||
out += s | ||
} | ||
} | ||
return "{" + out | ||
} | ||
|
||
func cfssljson(i *[]byte, file string) { | ||
var input = map[string]interface{}{} | ||
var err error | ||
var cert string | ||
var key string | ||
var csr string | ||
|
||
err = json.Unmarshal([]byte(parsecfsslInput(i)), &input) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to parse input: %v\n", err) | ||
os.Exit(1) | ||
} | ||
if contents, ok := input["cert"]; ok { | ||
cert = contents.(string) | ||
} else if contents, ok = input["certificate"]; ok { | ||
cert = contents.(string) | ||
} | ||
createFile(file+".pem", cert) | ||
|
||
if contents, ok := input["key"]; ok { | ||
key = contents.(string) | ||
} else if contents, ok = input["private_key"]; ok { | ||
key = contents.(string) | ||
} | ||
createFile(file+"-key.pem", key) | ||
|
||
if contents, ok := input["csr"]; ok { | ||
csr = contents.(string) | ||
} else if contents, ok = input["certificate_request"]; ok { | ||
csr = contents.(string) | ||
} | ||
createFile(file+".csr", csr) | ||
|
||
} | ||
|
||
func (c *cLab) createRootCA() (err error) { | ||
//create root CA diretcory | ||
createDirectory(c.Dir.LabCA, 0755) | ||
|
||
//create root CA root diretcory | ||
createDirectory(c.Dir.LabCARoot, 0755) | ||
|
||
var src string | ||
var dst string | ||
|
||
// copy topology to node specific directory in lab | ||
src = "ca_config/templates/csr-root-ca.json" | ||
dst = c.Dir.LabCARoot + "/" + "csr-root-ca.json" | ||
tpl, err := template.ParseFiles(src) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
type Prefix struct { | ||
Prefix string | ||
} | ||
prefix := Prefix{ | ||
Prefix: "lab" + "-" + c.Conf.Prefix, | ||
} | ||
f, err := os.Create(dst) | ||
if err != nil { | ||
log.Error("create file: ", err) | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
if err = tpl.Execute(f, prefix); err != nil { | ||
panic(err) | ||
} | ||
log.Debug(fmt.Sprintf("CopyFile GoTemplate src %s -> dat %s succeeded\n", src, dst)) | ||
|
||
var cmd *exec.Cmd | ||
cmd = exec.Command("/home/henderiw/work/bin/cfssl", "gencert", "-initca", dst) | ||
o, err := cmd.CombinedOutput() | ||
//fmt.Println(string(o)) | ||
if err != nil { | ||
log.Errorf("cmd.Run() failed with %s\n", err) | ||
} | ||
|
||
cfssljson(&o, c.Dir.LabCARoot+"/"+"root-ca") | ||
|
||
return nil | ||
} | ||
|
||
func (c *cLab) createCERT(shortdutName string) (err error) { | ||
//create dut cert diretcory | ||
createDirectory(c.Nodes[shortdutName].CertDir, 0755) | ||
|
||
var src string | ||
var dst string | ||
|
||
// copy topology to node specific directory in lab | ||
src = "ca_config/templates/csr.json" | ||
dst = c.Nodes[shortdutName].CertDir + "/" + "csr" + "-" + shortdutName + ".json" | ||
tpl, err := template.ParseFiles(src) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
type CERT struct { | ||
Name string | ||
Prefix string | ||
} | ||
cert := CERT{ | ||
Name: shortdutName, | ||
Prefix: c.Conf.Prefix, | ||
} | ||
f, err := os.Create(dst) | ||
if err != nil { | ||
log.Error("create file: ", err) | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
if err = tpl.Execute(f, cert); err != nil { | ||
panic(err) | ||
} | ||
log.Debug(fmt.Sprintf("CopyFile GoTemplate src %s -> dat %s succeeded\n", src, dst)) | ||
|
||
var cmd *exec.Cmd | ||
rootCert := c.Dir.LabCARoot + "/" + "root-ca.pem" | ||
rootKey := c.Dir.LabCARoot + "/" + "root-ca-key.pem" | ||
cmd = exec.Command("/home/henderiw/work/bin/cfssl", "gencert", "-ca", rootCert, "-ca-key", rootKey, dst) | ||
o, err := cmd.CombinedOutput() | ||
//fmt.Println(string(o)) | ||
if err != nil { | ||
log.Errorf("cmd.Run() failed with %s\n", err) | ||
} | ||
|
||
cfssljson(&o, c.Nodes[shortdutName].CertDir+"/"+shortdutName) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.