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

Add UUID validation to rad env update #5758

Merged
merged 9 commits into from
Jun 21, 2023
Merged
19 changes: 19 additions & 0 deletions pkg/cli/clivalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"path"
"strings"

"github.com/google/uuid"
"github.com/project-radius/radius/pkg/cli/clients"
"github.com/project-radius/radius/pkg/cli/clierrors"
"github.com/project-radius/radius/pkg/cli/cmd/commonflags"
"github.com/project-radius/radius/pkg/cli/config"
"github.com/project-radius/radius/pkg/cli/workspaces"
"github.com/project-radius/radius/pkg/ucp/resources"
Expand Down Expand Up @@ -259,6 +261,23 @@ func RequireAzureResource(cmd *cobra.Command, args []string) (azureResource Azur
}, nil
}

// RequireAzureSubscriptionId is used by commands that require specifying an Azure subscriptionId using a flag
func RequireAzureSubscriptionId(cmd *cobra.Command) (string, error) {
subscriptionId, err := cmd.Flags().GetString(commonflags.AzureSubscriptionIdFlag)
if err != nil {
return "", err
}

if subscriptionId != "" {

Choose a reason for hiding this comment

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

If subscriptionId is empty, then we need to return error, don't we? I think uuid.Parse can returns error since subscriptionId is empty. So we do not need this if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah originally I implemented it that way but it caused an issue when the flag was not set: https://github.com/project-radius/radius/actions/runs/5315965610/jobs/9625032925

I have an idea on how to better handle this though, one sec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed an update so validation of the subscriptionId format only occurs if the flags are explicitly set, so it doesn't validate when they're omitted

// Validate that subscriptionId is a valid GUID
if _, err := uuid.Parse(subscriptionId); err != nil {
return "", fmt.Errorf("'%s' is not a valid subscription ID", subscriptionId)
}
}

return subscriptionId, err
}

func RequireOutput(cmd *cobra.Command) (string, error) {
return cmd.Flags().GetString("output")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/cmd/env/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error {
}

// TODO: Validate Azure scope components (https://github.com/project-radius/radius/issues/5155)
azureSubId, err := cmd.Flags().GetString(commonflags.AzureSubscriptionIdFlag)
azureSubId, err := cli.RequireAzureSubscriptionId(cmd)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/cli/cmd/env/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "Update Env Command with invalid Azure subscriptionId arg",
Input: []string{"default", "--azure-subscription-id", "subscriptionName", "--azure-resource-group", "testResourceGroup"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
{
Name: "Update Env Command with single provider set",
Input: []string{"default", "--azure-subscription-id", "testSubId", "--azure-resource-group", "testResourceGroup"},
Input: []string{"default", "--azure-subscription-id", "00000000-0000-0000-0000-000000000000", "--azure-resource-group", "testResourceGroup"},
ExpectedValid: true,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Expand All @@ -75,7 +84,7 @@ func Test_Validate(t *testing.T) {
},
{
Name: "Update Env Command with both providers set",
Input: []string{"default", "--azure-subscription-id", "testSubId", "--azure-resource-group", "testResourceGroup",
Input: []string{"default", "--azure-subscription-id", "00000000-0000-0000-0000-000000000000", "--azure-resource-group", "testResourceGroup",
"--aws-region", "us-west-2", "--aws-account-id", "testAWSAccount",
},
ExpectedValid: true,
Expand Down