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
17 changes: 17 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,21 @@ 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
}

// 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
38 changes: 20 additions & 18 deletions pkg/cli/cmd/env/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ 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)
if err != nil {
return err
}
if cmd.Flags().Changed(commonflags.AzureSubscriptionIdFlag) || cmd.Flags().Changed(commonflags.AzureResourceGroupFlag) {
azureSubId, err := cli.RequireAzureSubscriptionId(cmd)
if err != nil {
return err
}

azureRgId, err := cmd.Flags().GetString(commonflags.AzureResourceGroupFlag)
if err != nil {
return err
}

azureRgId, err := cmd.Flags().GetString(commonflags.AzureResourceGroupFlag)
if err != nil {
return err
}
if azureSubId != "" && azureRgId != "" {
r.providers.Azure.Scope = to.Ptr(fmt.Sprintf(azureScopeTemplate, azureSubId, azureRgId))
}

Expand All @@ -157,16 +158,17 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error {

// TODO: Validate AWS scope components (https://github.com/project-radius/radius/issues/5155)
// stsclient can be used to validate
awsRegion, err := cmd.Flags().GetString(commonflags.AWSRegionFlag)
if err != nil {
return err
}
if cmd.Flags().Changed(commonflags.AWSRegionFlag) || cmd.Flags().Changed(commonflags.AWSAccountIdFlag) {
awsRegion, err := cmd.Flags().GetString(commonflags.AWSRegionFlag)
if err != nil {
return err
}

awsAccountId, err := cmd.Flags().GetString(commonflags.AWSAccountIdFlag)
if err != nil {
return err
}

awsAccountId, err := cmd.Flags().GetString(commonflags.AWSAccountIdFlag)
if err != nil {
return err
}
if awsRegion != "" && awsAccountId != "" {
r.providers.Aws.Scope = to.Ptr(fmt.Sprintf(awsScopeTemplate, awsAccountId, awsRegion))
}

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