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

APP-7002: Implement “DeleteOAuthApplication” CLI command #4648

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const (
authApplicationFlagOriginURIs = "origin-uris"
authApplicationFlagRedirectURIs = "redirect-uris"
authApplicationFlagLogoutURI = "logout-uri"
authApplicationFlagClientID = "client-id"

cpFlagRecursive = "recursive"
cpFlagPreserve = "preserve"
Expand Down Expand Up @@ -425,6 +426,37 @@ var app = &cli.App{
Usage: "work with organizations",
HideHelpCommand: true,
Subcommands: []*cli.Command{
{
Name: "auth-service",
Usage: "manage auth-service",
Subcommands: []*cli.Command{
{
Name: "oauth-app",
Usage: "manage the OAuth applications for an organization",
Subcommands: []*cli.Command{
{
Name: "delete",
Usage: "delete an OAuth application",
UsageText: createUsageText("delete", []string{generalFlagOrgID, authApplicationFlagClientID}, true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide an image in the description of what this looks like so that it's easy for us to check what this looks like in the end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, never mind! I didn't know what UsageText was and thought this was Usage. Looks good!

Flags: []cli.Flag{
&cli.StringFlag{
Name: generalFlagOrgID,
Required: true,
Usage: "organization ID tied to the OAuth application",
},
&cli.StringFlag{
Name: authApplicationFlagClientID,
Required: true,
Usage: "client ID of the OAuth application to delete",
},
},
Before: createCommandWithT[deleteOAuthAppArgs](DeleteOAuthAppConfirmation),
Action: createCommandWithT[deleteOAuthAppArgs](DeleteOAuthAppAction),
},
},
},
},
},
{
Name: "list",
Usage: "list organizations for the current user",
Expand Down
68 changes: 68 additions & 0 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package cli

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -2050,3 +2051,70 @@ func logEntryFieldsToString(fields []*structpb.Struct) (string, error) {
}
return message + "}", nil
}

type deleteOAuthAppArgs struct {
OrgID string
ClientID string
}

// DeleteOAuthAppConfirmation is the Before action for 'organizations auth-service oauth-app delete'.
// It asks for the user to confirm that they want to delete the oauth app.
func DeleteOAuthAppConfirmation(c *cli.Context, args deleteOAuthAppArgs) error {
if args.OrgID == "" {
return errors.New("cannot delete oauth app without an organization ID")
}

if args.ClientID == "" {
return errors.New("cannot delete oauth app without a client ID")
}

yellow := "\033[1;33m%s\033[0m"
printf(c.App.Writer, yellow, "WARNING!!\n")
printf(c.App.Writer, yellow, fmt.Sprintf("You are trying to delete an OAuth application with client ID %s. "+
"Once deleted, any existing apps that rely on this OAuth application will no longer be able to authenticate users.\n", args.ClientID))
printf(c.App.Writer, yellow, "Do you want to continue?")
printf(c.App.Writer, "Continue: y/n")
if err := c.Err(); err != nil {
return err
}

rawInput, err := bufio.NewReader(c.App.Reader).ReadString('\n')
if err != nil {
return err
}

input := strings.ToUpper(strings.TrimSpace(rawInput))
if input != "Y" {
return errors.New("aborted")
}
return nil
}

// DeleteOAuthAppAction is the corresponding action for 'oauth-app delete'.
func DeleteOAuthAppAction(c *cli.Context, args deleteOAuthAppArgs) error {
client, err := newViamClient(c)
if err != nil {
return err
}

return client.deleteOAuthAppAction(c, args.OrgID, args.ClientID)
}

func (c *viamClient) deleteOAuthAppAction(cCtx *cli.Context, orgID, clientID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

req := &apppb.DeleteOAuthAppRequest{
OrgId: orgID,
ClientId: clientID,
}

_, err := c.client.DeleteOAuthApp(c.c.Context, req)
if err != nil {
return err
}

printf(cCtx.App.Writer, "Successfully deleted OAuth application")
return nil
}
18 changes: 18 additions & 0 deletions cli/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,24 @@ func TestGetLogoAction(t *testing.T) {
test.That(t, out.messages[0], test.ShouldContainSubstring, "https://logo.com")
}

func TestDeleteOAuthAppAction(t *testing.T) {
deleteOAuthAppFunc := func(ctx context.Context, in *apppb.DeleteOAuthAppRequest, opts ...grpc.CallOption) (
*apppb.DeleteOAuthAppResponse, error,
) {
return &apppb.DeleteOAuthAppResponse{}, nil
}

asc := &inject.AppServiceClient{
DeleteOAuthAppFunc: deleteOAuthAppFunc,
}

cCtx, ac, out, errOut := setup(asc, nil, nil, nil, nil, "token")
test.That(t, ac.deleteOAuthAppAction(cCtx, "test-org", "client-id"), test.ShouldBeNil)
test.That(t, len(errOut.messages), test.ShouldEqual, 0)
test.That(t, len(out.messages), test.ShouldEqual, 1)
test.That(t, out.messages[0], test.ShouldContainSubstring, "Successfully deleted OAuth application")
}

func TestUpdateBillingServiceAction(t *testing.T) {
updateConfigFunc := func(ctx context.Context, in *apppb.UpdateBillingServiceRequest, opts ...grpc.CallOption) (
*apppb.UpdateBillingServiceResponse, error,
Expand Down
12 changes: 12 additions & 0 deletions testutils/inject/app_service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type AppServiceClient struct {
opts ...grpc.CallOption) (*apppb.OrganizationSetLogoResponse, error)
OrganizationGetLogoFunc func(ctx context.Context, in *apppb.OrganizationGetLogoRequest,
opts ...grpc.CallOption) (*apppb.OrganizationGetLogoResponse, error)
DeleteOAuthAppFunc func(ctx context.Context, in *apppb.DeleteOAuthAppRequest,
opts ...grpc.CallOption) (*apppb.DeleteOAuthAppResponse, error)
CreateLocationFunc func(ctx context.Context, in *apppb.CreateLocationRequest,
opts ...grpc.CallOption) (*apppb.CreateLocationResponse, error)
GetLocationFunc func(ctx context.Context, in *apppb.GetLocationRequest,
Expand Down Expand Up @@ -403,6 +405,16 @@ func (asc *AppServiceClient) OrganizationGetLogo(
return asc.OrganizationGetLogoFunc(ctx, in, opts...)
}

// DeleteOAuthApp calls the injected DeleteOAuthAppFunc or the real version.
func (asc *AppServiceClient) DeleteOAuthApp(
ctx context.Context, in *apppb.DeleteOAuthAppRequest, opts ...grpc.CallOption,
) (*apppb.DeleteOAuthAppResponse, error) {
if asc.DeleteOAuthAppFunc == nil {
return asc.AppServiceClient.DeleteOAuthApp(ctx, in, opts...)
}
return asc.DeleteOAuthAppFunc(ctx, in, opts...)
}

// CreateLocation calls the injected CreateLocationFunc or the real version.
func (asc *AppServiceClient) CreateLocation(
ctx context.Context, in *apppb.CreateLocationRequest, opts ...grpc.CallOption,
Expand Down
Loading