Skip to content

Commit

Permalink
make nebula-cert build+function
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDoanRivian committed Jul 10, 2024
1 parent 6e72a45 commit 2b03d65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
31 changes: 9 additions & 22 deletions cmd/nebula-cert/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"io"
"math"
"net"
"net/netip"
"os"
"strings"
"time"
Expand Down Expand Up @@ -106,37 +106,24 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
}
}

var ips []*net.IPNet
var ips netip.Prefix
if *cf.ips != "" {
for _, rs := range strings.Split(*cf.ips, ",") {
rs := strings.Trim(rs, " ")
if rs != "" {
ip, ipNet, err := net.ParseCIDR(rs)
if err != nil {
return newHelpErrorf("invalid ip definition: %s", err)
}
if ip.To4() == nil {
return newHelpErrorf("invalid ip definition: can only be ipv4, have %s", rs)
}

ipNet.IP = ip
ips = append(ips, ipNet)
}
rs := strings.Trim(*cf.ips, " ")
ips, err = netip.ParsePrefix(rs)
if err != nil {
return newHelpErrorf("invalid ip definition: %s", err)
}
}

var subnets []*net.IPNet
var subnets []netip.Prefix
if *cf.subnets != "" {
for _, rs := range strings.Split(*cf.subnets, ",") {
rs := strings.Trim(rs, " ")
if rs != "" {
_, s, err := net.ParseCIDR(rs)
s, err := netip.ParsePrefix(rs)
if err != nil {
return newHelpErrorf("invalid subnet definition: %s", err)
}
if s.IP.To4() == nil {
return newHelpErrorf("invalid subnet definition: can only be ipv4, have %s", rs)
}
subnets = append(subnets, s)
}
}
Expand Down Expand Up @@ -195,7 +182,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
Details: cert.NebulaCertificateDetails{
Name: *cf.name,
Groups: groups,
Ips: ips,
Ip: ips,
Subnets: subnets,
NotBefore: time.Now(),
NotAfter: time.Now().Add(*cf.duration),
Expand Down
17 changes: 5 additions & 12 deletions cmd/nebula-cert/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"flag"
"fmt"
"io"
"net"
"net/netip"
"os"
"strings"
"time"
Expand Down Expand Up @@ -139,14 +139,10 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
*sf.duration = time.Until(caCert.Details.NotAfter) - time.Second*1
}

ip, ipNet, err := net.ParseCIDR(*sf.ip)
ipNet, err := netip.ParsePrefix(*sf.ip)
if err != nil {
return newHelpErrorf("invalid ip definition: %s", err)
}
if ip.To4() == nil {
return newHelpErrorf("invalid ip definition: can only be ipv4, have %s", *sf.ip)
}
ipNet.IP = ip

groups := []string{}
if *sf.groups != "" {
Expand All @@ -158,18 +154,15 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
}
}

subnets := []*net.IPNet{}
var subnets []netip.Prefix
if *sf.subnets != "" {
for _, rs := range strings.Split(*sf.subnets, ",") {
rs := strings.Trim(rs, " ")
if rs != "" {
_, s, err := net.ParseCIDR(rs)
s, err := netip.ParsePrefix(rs)
if err != nil {
return newHelpErrorf("invalid subnet definition: %s", err)
}
if s.IP.To4() == nil {
return newHelpErrorf("invalid subnet definition: can only be ipv4, have %s", rs)
}
subnets = append(subnets, s)
}
}
Expand All @@ -196,7 +189,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
nc := cert.NebulaCertificate{
Details: cert.NebulaCertificateDetails{
Name: *sf.name,
Ips: []*net.IPNet{ipNet},
Ip: ipNet,
Groups: groups,
Subnets: subnets,
NotBefore: time.Now(),
Expand Down

0 comments on commit 2b03d65

Please sign in to comment.