Skip to content

Commit

Permalink
Merge branch 'master' into execjson
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Jun 24, 2021
2 parents 21ba7c0 + ead63cf commit 9289dc0
Show file tree
Hide file tree
Showing 70 changed files with 2,918 additions and 1,921 deletions.
46 changes: 21 additions & 25 deletions clab/cert.go → cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause

package clab
package cert

import (
"bytes"
Expand All @@ -20,6 +20,7 @@ import (
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/signer/universal"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)
Expand Down Expand Up @@ -79,7 +80,7 @@ var rootCACSRTempl string = `{
}
`

var nodeCSRTempl string = `{
var NodeCSRTempl string = `{
"CN": "{{.Name}}.{{.Prefix}}.io",
"key": {
"algo": "rsa",
Expand All @@ -97,15 +98,13 @@ var nodeCSRTempl string = `{
"{{.Fqdn}}"
]
}
`

// GenerateRootCa function
func (c *CLab) GenerateRootCa(csrRootJsonTpl *template.Template, input CaRootInput) (*Certificates, error) {
func GenerateRootCa(labCARoot string, csrRootJsonTpl *template.Template, input CaRootInput) (*Certificates, error) {
log.Info("Creating root CA")
// create root CA root directory
utils.CreateDirectory(c.Dir.LabCARoot, 0755)
utils.CreateDirectory(labCARoot, 0755)
var err error
csrBuff := new(bytes.Buffer)
err = csrRootJsonTpl.Execute(csrBuff, input)
Expand All @@ -130,16 +129,13 @@ func (c *CLab) GenerateRootCa(csrRootJsonTpl *template.Template, input CaRootInp
Csr: csrPEM,
Cert: cert,
}
c.writeCertFiles(certs, path.Join(c.Dir.LabCARoot, input.NamePrefix))
writeCertFiles(certs, path.Join(labCARoot, input.NamePrefix))
return certs, nil
}

// GenerateCert generates and signs a certificate passed as input and saves the certificate and generated private key by path
// CA used to sign the cert is passed as ca and caKey file paths
func (c *CLab) GenerateCert(ca string, caKey string, csrJSONTpl *template.Template, input CertInput, targetPath string) (*Certificates, error) {
c.m.RLock()
defer c.m.RUnlock()

func GenerateCert(ca, caKey string, csrJSONTpl *template.Template, input CertInput, targetPath string) (*Certificates, error) {
utils.CreateDirectory(targetPath, 0755)
var err error
csrBuff := new(bytes.Buffer)
Expand Down Expand Up @@ -196,14 +192,14 @@ func (c *CLab) GenerateCert(ca string, caKey string, csrJSONTpl *template.Templa
Cert: cert,
}

c.writeCertFiles(certs, path.Join(targetPath, input.Name))
writeCertFiles(certs, path.Join(targetPath, input.Name))
return certs, nil
}

// RetrieveNodeCertData reads the node private key and certificate by the well known paths
// if either of those files doesn't exist, an error is returned
func (c *CLab) RetrieveNodeCertData(n *types.Node) (*Certificates, error) {
var nodeCertFilesDir = path.Join(c.Dir.LabCA, n.ShortName)
func RetrieveNodeCertData(n *types.NodeConfig, labCADir string) (*Certificates, error) {
var nodeCertFilesDir = path.Join(labCADir, n.ShortName)
var nodeCertFile = path.Join(nodeCertFilesDir, n.ShortName+".pem")
var nodeKeyFile = path.Join(nodeCertFilesDir, n.ShortName+"-key.pem")

Expand All @@ -229,19 +225,19 @@ func (c *CLab) RetrieveNodeCertData(n *types.Node) (*Certificates, error) {
return certs, nil
}

func (c *CLab) writeCertFiles(certs *Certificates, filesPrefix string) {
createFile(filesPrefix+".pem", string(certs.Cert))
createFile(filesPrefix+"-key.pem", string(certs.Key))
createFile(filesPrefix+".csr", string(certs.Csr))
func writeCertFiles(certs *Certificates, filesPrefix string) {
utils.CreateFile(filesPrefix+".pem", string(certs.Cert))
utils.CreateFile(filesPrefix+"-key.pem", string(certs.Key))
utils.CreateFile(filesPrefix+".csr", string(certs.Csr))
}

//CreateRootCA creates RootCA key/certificate if it is needed by the topology
func (c *CLab) CreateRootCA() error {
func CreateRootCA(configName, labCARoot string, ns map[string]nodes.Node) error {
rootCANeeded := false
// check if srl kinds defined in topo
// for them we need to create rootCA and certs
for _, n := range c.Nodes {
if n.Kind == "srl" {
for _, n := range ns {
if n.Config().Kind == "srl" {
rootCANeeded = true
break
}
Expand All @@ -251,8 +247,8 @@ func (c *CLab) CreateRootCA() error {
return nil
}

var rootCaCertPath = path.Join(c.Dir.LabCARoot, "root-ca.pem")
var rootCaKeyPath = path.Join(c.Dir.LabCARoot, "root-ca-key.pem")
var rootCaCertPath = path.Join(labCARoot, "root-ca.pem")
var rootCaKeyPath = path.Join(labCARoot, "root-ca-key.pem")

var rootCaCertExists = false
var rootCaKeyExists = false
Expand All @@ -277,8 +273,8 @@ func (c *CLab) CreateRootCA() error {
if err != nil {
return fmt.Errorf("failed to parse Root CA CSR Template: %v", err)
}
rootCerts, err := c.GenerateRootCa(tpl, CaRootInput{
Prefix: c.Config.Name,
rootCerts, err := GenerateRootCa(labCARoot, tpl, CaRootInput{
Prefix: configName,
NamePrefix: "root-ca",
})
if err != nil {
Expand Down
116 changes: 0 additions & 116 deletions clab/ceos.go

This file was deleted.

Loading

0 comments on commit 9289dc0

Please sign in to comment.