Skip to content

Commit

Permalink
Merge pull request #12 from SlyngDK/request-ip
Browse files Browse the repository at this point in the history
Support enroll with groups and requesting nebula ip
  • Loading branch information
SlyngDK authored Nov 22, 2021
2 parents 9bdd7e7 + 3018a35 commit d10f3ba
Show file tree
Hide file tree
Showing 26 changed files with 887 additions and 342 deletions.
40 changes: 37 additions & 3 deletions cmd/agent/enrollment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"os"
"strings"
"time"

"github.com/slackhq/nebula/cert"
Expand All @@ -31,8 +32,16 @@ var enrollCmd = &cobra.Command{
if err != nil {
l.WithError(err).Fatalln("failed to get token")
}
groups, err := cmd.Flags().GetString("groups")
if err != nil {
l.WithError(err).Fatalln("failed to get groups")
}
ip, err := cmd.Flags().GetString("ip")
if err != nil {
l.WithError(err).Fatalln("failed to get ip")
}

if err = enroll(agent, token); err != nil {
if err = enroll(agent, token, groups, ip); err != nil {
l.WithError(err).Fatalln("failed to enroll to server")
}
},
Expand Down Expand Up @@ -86,7 +95,16 @@ var enrollWaitCmd = &cobra.Command{
if status == 0 {
token, _ := cmd.Flags().GetString("token")
if token != "" {
if err := enroll(agent, token); err != nil {
groups, err := cmd.Flags().GetString("groups")
if err != nil {
l.WithError(err).Fatalln("failed to get groups")
}
ip, err := cmd.Flags().GetString("ip")
if err != nil {
l.WithError(err).Fatalln("failed to get ip")
}

if err := enroll(agent, token, groups, ip); err != nil {
l.WithError(err).Fatalln("failed to enroll to server")
os.Exit(2)
}
Expand Down Expand Up @@ -136,14 +154,19 @@ func getStatus(agent *agentClient) int8 {

func init() {
enrollCmd.Flags().StringP("token", "t", "", "Enrollment token")
enrollCmd.Flags().StringP("groups", "g", "", "Comma separated list of groups")
enrollCmd.Flags().StringP("ip", "i", "", "Requesting for this specific nebula ip")
enrollCmd.MarkFlagRequired("token")
enrollWaitCmd.Flags().StringP("token", "t", "", "Enrollment token")
enrollWaitCmd.Flags().StringP("groups", "g", "", "Comma separated list of groups")
enrollWaitCmd.Flags().StringP("ip", "i", "", "Requesting for this specific nebula ip")
enrollWaitCmd.MarkFlagRequired("token")

enrollCmd.AddCommand(enrollStatusCmd)
enrollCmd.AddCommand(enrollWaitCmd)
}

func enroll(c *agentClient, enrollmentToken string) error {
func enroll(c *agentClient, enrollmentToken, groups, ip string) error {
if enrollmentToken == "" {
return fmt.Errorf("requires enrollmentToken")
}
Expand All @@ -158,6 +181,17 @@ func enroll(c *agentClient, enrollmentToken string) error {
CsrPEM: string(csr),
}

if groups != "" {
g := strings.Split(groups, ",")
if len(g) > 0 {
enrollRequest.Groups = g
}
}

if ip != "" {
enrollRequest.RequestedIP = ip
}

hostname, err := os.Hostname()
if err == nil {
enrollRequest.Name = hostname
Expand Down
147 changes: 79 additions & 68 deletions protocol/agent-service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protocol/agent-service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ message EnrollRequest {
string csrPEM = 2;
repeated string groups = 3;
string name = 4;
string requestedIP = 5;
}

message EnrollResponse {
Expand Down
9 changes: 8 additions & 1 deletion server/agent-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"crypto/x509"
"fmt"
"net"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -58,6 +59,12 @@ func (a *agentService) Enroll(ctx context.Context, request *protocol.EnrollReque
if request.CsrPEM == "" {
return nil, status.Error(codes.InvalidArgument, "CsrPEM is required")
}
if request.RequestedIP != "" {
ip := net.ParseIP(request.RequestedIP)
if ip == nil {
return nil, status.Error(codes.InvalidArgument, "RequestedIP is not valid")
}
}

_, _, err = cert.UnmarshalX25519PublicKey([]byte(request.CsrPEM))
if err != nil {
Expand All @@ -68,7 +75,7 @@ func (a *agentService) Enroll(ctx context.Context, request *protocol.EnrollReque
addr := p.Addr.String()
ip := addr[0:strings.LastIndex(addr, ":")]

_, err = a.store.CreateEnrollmentRequest(fingerprint, request.Token, request.CsrPEM, ip, request.Name)
_, err = a.store.CreateEnrollmentRequest(fingerprint, request.Token, request.CsrPEM, ip, request.Name, request.RequestedIP, request.Groups)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("%s", err))
}
Expand Down
Loading

0 comments on commit d10f3ba

Please sign in to comment.