Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update, Delete and List operations for Trust Domain CLI #221

Merged
merged 13 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions cmd/common/cli/flags.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cli

const (
SocketPathFlagName = "socketPath"
ConfigFlagName = "config"
TrustDomainFlagName = "trustDomain"
TrustDomainAFlagName = "trustDomainA"
TrustDomainBFlagName = "trustDomainB"
ConsentStatusFlagName = "status"
TTLFlagName = "ttl"
RelationshipIDFlagName = "relationshipID"
JoinTokenFlagName = "joinToken"
SocketPathFlagName = "socketPath"
ConfigFlagName = "config"
TrustDomainFlagName = "trustDomain"
TrustDomainAFlagName = "trustDomainA"
TrustDomainBFlagName = "trustDomainB"
TrustDomainDescriptionFlagName = "trustDomainDescription"
ConsentStatusFlagName = "status"
TTLFlagName = "ttl"
RelationshipIDFlagName = "relationshipID"
JoinTokenFlagName = "joinToken"
)
111 changes: 106 additions & 5 deletions cmd/server/cli/trustdomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/spf13/cobra"
)

var (
errMarkFlagAsRequired = "Error marking %q flag as required: %v\n"
)

const trustDomainCommonText = `
A trust domain represents a distinct trust boundary or realm within a distributed system.
By creating a new trust domain, you can establish a namespace for workload identities
Expand Down Expand Up @@ -49,10 +53,6 @@ The 'create' command allows you to create a new trust domain in the Galadriel Se
return fmt.Errorf("cannot get trust domain flag: %v", err)
}

if trustDomain == "" {
return fmt.Errorf("trust domain name is required")
}

client, err := util.NewGaladrielUDSClient(socketPath, nil)
if err != nil {
return err
Expand All @@ -79,6 +79,34 @@ var listTrustDomainCmd = &cobra.Command{
Long: `The 'list' command allows you to retrieve a list of registered trust domains.`,

RunE: func(cmd *cobra.Command, args []string) error {
socketPath, err := cmd.Flags().GetString(cli.SocketPathFlagName)
if err != nil {
return fmt.Errorf("cannot get socket path flag: %v", err)
}

client, err := util.NewGaladrielUDSClient(socketPath, nil)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

trustDomains, err := client.ListTrustDomains(ctx)
if err != nil {
return err
}

if len(trustDomains) == 0 {
fmt.Printf("No trust domains registered.")
}

fmt.Println()
Victorblsilveira marked this conversation as resolved.
Show resolved Hide resolved
for _, td := range trustDomains {
fmt.Printf("%s\n", td.ConsoleString())
}
fmt.Println()
Victorblsilveira marked this conversation as resolved.
Show resolved Hide resolved

return nil
},
}
Expand All @@ -94,6 +122,31 @@ with it are removed or deleted. This ensures the integrity of the system and pre
potential disruptions in secure communication between trust domains.`,

RunE: func(cmd *cobra.Command, args []string) error {
socketPath, err := cmd.Flags().GetString(cli.SocketPathFlagName)
if err != nil {
return fmt.Errorf("cannot get socket path flag: %v", err)
}

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

client, err := util.NewGaladrielUDSClient(socketPath, nil)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = client.DeleteTrustDomainByName(ctx, trustDomainName)
if err != nil {
return err
}

fmt.Printf("Trust Domain %q deleted\n", trustDomainName)

return nil
},
}
Expand All @@ -106,6 +159,36 @@ var updateTrustDomainCmd = &cobra.Command{
in the Galadriel Server.`,

RunE: func(cmd *cobra.Command, args []string) error {
socketPath, err := cmd.Flags().GetString(cli.SocketPathFlagName)
if err != nil {
return fmt.Errorf("cannot get socket path flag: %v", err)
}

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

description, err := cmd.Flags().GetString(cli.TrustDomainDescriptionFlagName)
if err != nil {
return fmt.Errorf("cannot get description flag: %v", err)
}

client, err := util.NewGaladrielUDSClient(socketPath, nil)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_, err = client.UpdateTrustDomainByName(ctx, trustDomainName, description)
if err != nil {
return err
}

fmt.Printf("Trust Domain %q updated \n", trustDomainName)

return nil
},
}
Expand All @@ -120,6 +203,24 @@ func init() {
createTrustDomainCmd.Flags().StringP(cli.TrustDomainFlagName, "t", "", "The trust domain name.")
err := createTrustDomainCmd.MarkFlagRequired(cli.TrustDomainFlagName)
if err != nil {
fmt.Printf("Error marking trustDomain flag as required: %v\n", err)
fmt.Printf(errMarkFlagAsRequired, cli.TrustDomainFlagName, err)
}

deleteTrustDomainCmd.Flags().StringP(cli.TrustDomainFlagName, "t", "", "The trust domain name.")
err = deleteTrustDomainCmd.MarkFlagRequired(cli.TrustDomainFlagName)
if err != nil {
fmt.Printf(errMarkFlagAsRequired, cli.TrustDomainFlagName, err)
}

updateTrustDomainCmd.Flags().StringP(cli.TrustDomainFlagName, "t", "", "The trust domain to be updated.")
err = updateTrustDomainCmd.MarkFlagRequired(cli.TrustDomainFlagName)
if err != nil {
fmt.Printf(errMarkFlagAsRequired, cli.TrustDomainFlagName, err)
}

updateTrustDomainCmd.Flags().StringP(cli.TrustDomainDescriptionFlagName, "d", "", "The trust domain description.")
err = updateTrustDomainCmd.MarkFlagRequired(cli.TrustDomainDescriptionFlagName)
if err != nil {
fmt.Printf(errMarkFlagAsRequired, cli.TrustDomainDescriptionFlagName, err)
}
}
78 changes: 62 additions & 16 deletions cmd/server/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const (
type GaladrielAPIClient interface {
CreateTrustDomain(context.Context, api.TrustDomainName) (*entity.TrustDomain, error)
GetTrustDomainByName(context.Context, api.TrustDomainName) (*entity.TrustDomain, error)
UpdateTrustDomainByName(context.Context, api.TrustDomainName) (*entity.TrustDomain, error)
ListTrustDomains(context.Context) ([]*entity.TrustDomain, error)
DeleteTrustDomainByName(context.Context, api.TrustDomainName) error
UpdateTrustDomainByName(context.Context, api.TrustDomainName, string) (*entity.TrustDomain, error)
CreateRelationship(context.Context, *entity.Relationship) (*entity.Relationship, error)
GetRelationshipByID(context.Context, uuid.UUID) (*entity.Relationship, error)
GetRelationships(context.Context, api.ConsentStatus, api.TrustDomainName) (*entity.Relationship, error)
Expand Down Expand Up @@ -55,8 +57,8 @@ func NewGaladrielUDSClient(socketPath string, httpClient *http.Client) (Galadrie
return &galadrielAdminClient{client: adminClient}, nil
}

func (c *galadrielAdminClient) GetTrustDomainByName(ctx context.Context, trustDomainName api.TrustDomainName) (*entity.TrustDomain, error) {
res, err := c.client.GetTrustDomainByName(ctx, trustDomainName)
func (g *galadrielAdminClient) GetTrustDomainByName(ctx context.Context, trustDomainName api.TrustDomainName) (*entity.TrustDomain, error) {
res, err := g.client.GetTrustDomainByName(ctx, trustDomainName)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand All @@ -75,9 +77,53 @@ func (c *galadrielAdminClient) GetTrustDomainByName(ctx context.Context, trustDo
return trustDomain, nil
}

func (c *galadrielAdminClient) UpdateTrustDomainByName(ctx context.Context, trustDomainName api.TrustDomainName) (*entity.TrustDomain, error) {
payload := api.TrustDomain{Name: trustDomainName}
res, err := c.client.PutTrustDomainByName(ctx, trustDomainName, payload)
func (g *galadrielAdminClient) ListTrustDomains(ctx context.Context) ([]*entity.TrustDomain, error) {
res, err := g.client.ListTrustDomains(ctx)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
defer res.Body.Close()

body, err := httputil.ReadResponse(res)
if err != nil {
return nil, err
}

var trustDomains []*api.TrustDomain
if err := json.Unmarshal(body, &trustDomains); err != nil {
return nil, fmt.Errorf(errUnmarshalRelationships, err)
}

tds := make([]*entity.TrustDomain, 0, len(trustDomains))
for i, td := range trustDomains {
trustDomain, err := td.ToEntity()
if err != nil {
return nil, fmt.Errorf("failed to convert trust domain %d: %v", i, err)
}
tds = append(tds, trustDomain)
}

return tds, nil
}

func (g *galadrielAdminClient) DeleteTrustDomainByName(ctx context.Context, trustDomainName api.TrustDomainName) error {
res, err := g.client.DeleteTrustDomainByName(ctx, trustDomainName)
if err != nil {
return fmt.Errorf(errorRequestFailed, err)
}
defer res.Body.Close()

_, err = httputil.ReadResponse(res)
if err != nil {
return err
}

return nil
}

func (g *galadrielAdminClient) UpdateTrustDomainByName(ctx context.Context, trustDomainName api.TrustDomainName, description string) (*entity.TrustDomain, error) {
payload := api.TrustDomain{Name: trustDomainName, Description: &description}
res, err := g.client.PutTrustDomainByName(ctx, trustDomainName, payload)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand All @@ -96,10 +142,10 @@ func (c *galadrielAdminClient) UpdateTrustDomainByName(ctx context.Context, trus
return trustDomain, nil
}

func (c *galadrielAdminClient) CreateTrustDomain(ctx context.Context, trustDomainName api.TrustDomainName) (*entity.TrustDomain, error) {
func (g *galadrielAdminClient) CreateTrustDomain(ctx context.Context, trustDomainName api.TrustDomainName) (*entity.TrustDomain, error) {
payload := admin.PutTrustDomainJSONRequestBody{Name: trustDomainName}

res, err := c.client.PutTrustDomain(ctx, payload)
res, err := g.client.PutTrustDomain(ctx, payload)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand All @@ -118,9 +164,9 @@ func (c *galadrielAdminClient) CreateTrustDomain(ctx context.Context, trustDomai
return trustDomain, nil
}

func (c *galadrielAdminClient) CreateRelationship(ctx context.Context, rel *entity.Relationship) (*entity.Relationship, error) {
func (g *galadrielAdminClient) CreateRelationship(ctx context.Context, rel *entity.Relationship) (*entity.Relationship, error) {
payload := admin.PutRelationshipJSONRequestBody{TrustDomainAName: rel.TrustDomainAName.String(), TrustDomainBName: rel.TrustDomainBName.String()}
res, err := c.client.PutRelationship(ctx, payload)
res, err := g.client.PutRelationship(ctx, payload)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand All @@ -139,10 +185,10 @@ func (c *galadrielAdminClient) CreateRelationship(ctx context.Context, rel *enti
return relationship, nil
}

func (c *galadrielAdminClient) GetRelationships(ctx context.Context, consentStatus api.ConsentStatus, trustDomainName api.TrustDomainName) (*entity.Relationship, error) {
func (g *galadrielAdminClient) GetRelationships(ctx context.Context, consentStatus api.ConsentStatus, trustDomainName api.TrustDomainName) (*entity.Relationship, error) {
payload := &admin.GetRelationshipsParams{ConsentStatus: &consentStatus, TrustDomainName: &trustDomainName}

res, err := c.client.GetRelationships(ctx, payload)
res, err := g.client.GetRelationships(ctx, payload)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand All @@ -161,8 +207,8 @@ func (c *galadrielAdminClient) GetRelationships(ctx context.Context, consentStat
return relationship, nil
}

func (c *galadrielAdminClient) GetRelationshipByID(ctx context.Context, relID uuid.UUID) (*entity.Relationship, error) {
res, err := c.client.GetRelationshipByID(ctx, relID)
func (g *galadrielAdminClient) GetRelationshipByID(ctx context.Context, relID uuid.UUID) (*entity.Relationship, error) {
res, err := g.client.GetRelationshipByID(ctx, relID)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand All @@ -181,9 +227,9 @@ func (c *galadrielAdminClient) GetRelationshipByID(ctx context.Context, relID uu
return relationship, nil
}

func (c *galadrielAdminClient) GetJoinToken(ctx context.Context, trustDomainName api.TrustDomainName, ttl int32) (*entity.JoinToken, error) {
func (g *galadrielAdminClient) GetJoinToken(ctx context.Context, trustDomainName api.TrustDomainName, ttl int32) (*entity.JoinToken, error) {
params := &admin.GetJoinTokenParams{Ttl: ttl}
res, err := c.client.GetJoinToken(ctx, trustDomainName, params)
res, err := g.client.GetJoinToken(ctx, trustDomainName, params)
if err != nil {
return nil, fmt.Errorf(errorRequestFailed, err)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/common/api/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ func MapRelationships(relationships ...*entity.Relationship) []*Relationship {

return cRelationships
}

// MapTrustDomains transforms a slice of TrustDomain entities to a slice of API TrustDomain representation.
func MapTrustDomains(trustDomains ...*entity.TrustDomain) []*TrustDomain {
cTrustDomains := make([]*TrustDomain, len(trustDomains))

for i, td := range trustDomains {
cTrustDomains[i] = TrustDomainFromEntity(td)
}

return cTrustDomains
}
Loading