Skip to content

Commit

Permalink
Implement Galadriel Server Admin CLI - create methods
Browse files Browse the repository at this point in the history
Adding create methods to the Server CLI.
  • Loading branch information
mgbcaio authored May 22, 2023
1 parent cdc6986 commit 75289b5
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 239 deletions.
35 changes: 20 additions & 15 deletions cmd/server/cli/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"

"github.com/HewlettPackard/galadriel/cmd/server/util"
Expand All @@ -10,7 +11,7 @@ import (
)

var createCmd = &cobra.Command{
Use: "create <trustdomain| relationship>",
Use: "create <trustdomain | relationship>",
Short: "Allows creation of trust domains and relationships",
}

Expand All @@ -20,23 +21,22 @@ var createTrustDomainCmd = &cobra.Command{
Short: "Creates a new trust domain",

RunE: func(cmd *cobra.Command, args []string) error {
td, err := cmd.Flags().GetString("trustDomain")
trustDomain, err := cmd.Flags().GetString("trustDomain")
if err != nil {
return fmt.Errorf("cannot get trust domain flag: %v", err)
}

trustDomain, err := spiffeid.TrustDomainFromString(td)
client, err := util.NewServerClient(defaultSocketPath)
if err != nil {
return err
}

c := util.NewServerClient(defaultSocketPath)

if err := c.CreateTrustDomain(&entity.TrustDomain{Name: trustDomain}); err != nil {
trustDomainRes, err := client.CreateTrustDomain(context.Background(), trustDomain)
if err != nil {
return err
}

fmt.Printf("Trust Domain created: %q\n", trustDomain.String())
fmt.Printf("Trust Domain created: %s\n", trustDomainRes.Name.String())

return nil
},
Expand All @@ -48,7 +48,10 @@ var createRelationshipCmd = &cobra.Command{
Args: cobra.ExactArgs(0),

RunE: func(cmd *cobra.Command, args []string) error {
c := util.NewServerClient(defaultSocketPath)
client, err := util.NewServerClient(defaultSocketPath)
if err != nil {
return err
}

tdA, err := cmd.Flags().GetString("trustDomainA")
if err != nil {
Expand All @@ -57,26 +60,28 @@ var createRelationshipCmd = &cobra.Command{

trustDomain1, err := spiffeid.TrustDomainFromString(tdA)
if err != nil {
return err
return fmt.Errorf("failed parsing trust domain: %v", err)
}

tdb, err := cmd.Flags().GetString("trustDomainB")
tdB, err := cmd.Flags().GetString("trustDomainB")
if err != nil {
return fmt.Errorf("cannot get trust domain B flag: %v", err)
}
trustDomain2, err := spiffeid.TrustDomainFromString(tdb)

trustDomain2, err := spiffeid.TrustDomainFromString(tdB)
if err != nil {
return err
return fmt.Errorf("failed parsing trust domain: %v", err)
}

if err := c.CreateRelationship(&entity.Relationship{
_, err = client.CreateRelationship(context.Background(), &entity.Relationship{
TrustDomainAName: trustDomain1,
TrustDomainBName: trustDomain2,
}); err != nil {
})
if err != nil {
return err
}

fmt.Printf("Relationship created between trust domain %q and trust domain %q\n", trustDomain1.String(), trustDomain2.String())
fmt.Printf("Relationship created between trust domains %s and %s\n", tdA, tdB)
return nil
},
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/server/cli/generate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cli

import (
"context"
"fmt"
"strings"

"github.com/HewlettPackard/galadriel/cmd/server/util"
"github.com/spf13/cobra"
"github.com/spiffe/go-spiffe/v2/spiffeid"
)

var generateCmd = &cobra.Command{
Expand All @@ -18,30 +17,28 @@ var tokenCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Short: "Generates a join token for provided trust domain",
RunE: func(cmd *cobra.Command, args []string) error {
c := util.NewServerClient(defaultSocketPath)

td, err := cmd.Flags().GetString("trustDomain")
trustDomain, err := cmd.Flags().GetString("trustdomain")
if err != nil {
return fmt.Errorf("cannot get trust domain flag: %v", err)
}

trustDomain, err := spiffeid.TrustDomainFromString(td)
client, err := util.NewServerClient(defaultSocketPath)
if err != nil {
return err
}

joinToken, err := c.GenerateJoinToken(trustDomain)
joinToken, err := client.GetJoinToken(context.Background(), trustDomain)
if err != nil {
return err
}

fmt.Printf("Token: %s", strings.ReplaceAll(joinToken, "\"", ""))
fmt.Printf("Token: %s\n", joinToken.Token)
return nil
},
}

func init() {
generateCmd.AddCommand(tokenCmd)
tokenCmd.PersistentFlags().StringP("trustDomain", "t", "", "A trust domain which the join token is bound to.")
tokenCmd.PersistentFlags().StringP("trustdomain", "t", "", "A trust domain which the join token is bound to.")
RootCmd.AddCommand(generateCmd)
}
17 changes: 17 additions & 0 deletions cmd/server/cli/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestTokenCmd(t *testing.T) {
cmd := &cobra.Command{}
cmd.Flags().String("trustDomain", "test.com", "")

err := cmd.Execute()

assert.Nil(t, err)
}
57 changes: 3 additions & 54 deletions cmd/server/cli/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cli

import (
"fmt"

"github.com/HewlettPackard/galadriel/cmd/server/util"
"github.com/spf13/cobra"
)

Expand All @@ -12,58 +9,10 @@ var listCmd = &cobra.Command{
Short: "Lists trust domains and relationships",
}

var listTrustDomainCmd = &cobra.Command{
Use: "trustdomains",
Args: cobra.ExactArgs(0),
Short: "Lists all the Trust Domains.",
RunE: func(cmd *cobra.Command, args []string) error {
c := util.NewServerClient(defaultSocketPath)
trustDomains, err := c.ListTrustDomains()
if err != nil {
return err
}

if len(trustDomains) == 0 {
fmt.Println("No trust domains found")
return nil
}

for _, m := range trustDomains {
fmt.Printf("ID: %s\n", m.ID.UUID)
fmt.Printf("Trust Domain: %s\n", m.Name)
fmt.Println()
}

return nil
},
}
// TODO: Implement Get Relationships and Trust Domains
var listTrustDomainCmd = &cobra.Command{}

var listRelationshipsCmd = &cobra.Command{
Use: "relationships",
Args: cobra.ExactArgs(0),
Short: "Lists all the relationships.",
RunE: func(cmd *cobra.Command, args []string) error {
c := util.NewServerClient(defaultSocketPath)
rels, err := c.ListRelationships()
if err != nil {
return err
}

if len(rels) == 0 {
fmt.Println("No relationships found")
return nil
}

for _, r := range rels {
fmt.Printf("ID: %s\n", r.ID.UUID)
fmt.Printf("Trust Domain A: %s\n", r.TrustDomainAName.String())
fmt.Printf("Trust Domain B: %s\n", r.TrustDomainBName.String())
fmt.Println()
}

return nil
},
}
var listRelationshipsCmd = &cobra.Command{}

func init() {
listCmd.AddCommand(listTrustDomainCmd)
Expand Down
44 changes: 44 additions & 0 deletions cmd/server/cli/run_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
package cli

import (
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestLoadConfig(t *testing.T) {
tempFile, err := os.CreateTemp("", "server.conf")
assert.NoError(t, err)
defer os.Remove(tempFile.Name())

_, err = tempFile.WriteString(`
server {
listen_address = "localhost"
listen_port = "8085"
socket_path = "/tmp/galadriel-server/api.sock/"
log_level = "DEBUG"
}
providers {
Datastore "postgres" {
connection_string = "postgresql://postgres:postgres@localhost:5432/galadriel"
}
X509CA "disk" {
key_file_path = "./conf/server/dummy_root_ca.key"
cert_file_path = "./conf/server/dummy_root_ca.crt"
}
KeyManager "memory" {}
}
`)
assert.NoError(t, err)

cmd := &cobra.Command{}
cmd.Flags().String("config", tempFile.Name(), "")

config, err := LoadConfig(cmd)

assert.NoError(t, err)
assert.NotNil(t, config)
}
Loading

0 comments on commit 75289b5

Please sign in to comment.