Skip to content

Commit

Permalink
Removed the redundant code, change arn validation check logic and upd…
Browse files Browse the repository at this point in the history
…ated some comments

Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Oct 22, 2024
1 parent 7eaf663 commit ffee490
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions pkg/diagnostics/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func RunReport(cmd *cobra.Command, args []string) {
TypeMeta: metav1.TypeMeta{Kind: "BackingStoreList"},
}
if !util.KubeList(bsList, &client.ListOptions{Namespace: options.Namespace}) {
log.Fatalf(`❌ Could not get backingstores in Namespace %q`, options.Namespace)
log.Fatalf(`❌ No backingstores were found in the %q namespace`, options.Namespace)
}

// Fetching all Namespacestores
nsList := &nbv1.NamespaceStoreList{
TypeMeta: metav1.TypeMeta{Kind: "NamespaceStoreList"},
}
if !util.KubeList(nsList, &client.ListOptions{Namespace: options.Namespace}) {
log.Fatalf(`❌ Could not get namespacestores in Namespace %q`, options.Namespace)
log.Fatalf(`❌ No namespacestores were found in the %q namespace`, options.Namespace)
}
fmt.Println("")

Expand All @@ -63,7 +63,7 @@ func RunReport(cmd *cobra.Command, args []string) {
// retrieving the overridden env variables using `CONFIG_JS_` prefix
overriddenEnvVar(coreApp, endpointApp)

// validating ARNs for backingstore and namespacestore
// validating ARNs for backingstores and namespacestores
arnValidationCheck(bsList, nsList)

// TODO: Add support for additional features
Expand Down Expand Up @@ -99,53 +99,25 @@ func overriddenEnvVar(coreApp *appsv1.StatefulSet, endpointApp *appsv1.Deploymen
func arnValidationCheck(bsList *nbv1.BackingStoreList, nsList *nbv1.NamespaceStoreList) {
log := util.Logger()

log.Print("⏳ Performing validation check for ARNs...\n")
foundARNString := false
log.Print("⏳ Validating store ARNs...\n")

// Validate ARNs for backingstores
fmt.Print("ARN Validation Check (BACKINGSTORES):\n----------------------------------\n")
bsArnList := make(map[string]string)
for _, bs := range bsList.Items {
if bs.Spec.AWSS3 != nil {
if bs.Spec.AWSS3.AWSSTSRoleARN != nil {
arn := *bs.Spec.AWSS3.AWSSTSRoleARN
if isValidArn(&arn) {
fmt.Printf(" ✅ Backingstore \"%s\":\n\t ARN: %s\n\t Status: ✅ Valid\n", bs.Name, arn)
} else {
fmt.Printf(" ⚠️ Backingstore \"%s\":\n\t ARN: %s\n\t Status: ⚠️ Invalid (Not an S3 bucket ARN)\n", bs.Name, arn)
}
fmt.Println("")
foundARNString = true
}
bsArnList[bs.Name] = *bs.Spec.AWSS3.AWSSTSRoleARN
}
}
printARNStatus("BACKINGSTORE", bsArnList)

if !foundARNString {
fmt.Print(" ❌ No aws sts arn string found.\n")
}
fmt.Println("")

foundARNString = false
// Validate ARNs for namespacestores
fmt.Print("ARN Validation Check (NAMESPACESTORES):\n----------------------------------\n")
nsArnList := make(map[string]string)
for _, ns := range nsList.Items {
if ns.Spec.AWSS3 != nil {
if ns.Spec.AWSS3.AWSSTSRoleARN != nil {
arn := *ns.Spec.AWSS3.AWSSTSRoleARN
if isValidArn(&arn) {
fmt.Printf(" ✅ Namespacestore \"%s\":\n\t ARN: %s\n\t Status: ✅ Valid\n", ns.Name, arn)
} else {
fmt.Printf(" ⚠️ Namespacestore \"%s\":\n\t ARN: %s\n\t Status: ⚠️ Invalid (Not an S3 bucket ARN)\n", ns.Name, arn)
}
fmt.Println("")
foundARNString = true
}
nsArnList[ns.Name] = *ns.Spec.AWSS3.AWSSTSRoleARN
}
}

if !foundARNString {
fmt.Print(" ❌ No aws sts arn string found.\n")
}
fmt.Println("")
printARNStatus("NAMESPACESTORE", nsArnList)

fmt.Println("")
}
Expand Down Expand Up @@ -182,5 +154,25 @@ func printOverriddenEnvVar(appName string, envVars []corev1.EnvVar) {

// isValidArn is a function to validate the ARN format for an s3 buckets
func isValidArn(arn *string) bool {
return strings.HasPrefix(*arn, "arn:aws:s3:::") && len(*arn) > len("arn:aws:s3:::")
return strings.HasPrefix(*arn, "arn:aws:s3::") && len(*arn) > len("arn:aws:s3::")
}

// printARNStatus is a function to print ARN validation status
func printARNStatus(listType string, arnList map[string]string) {
foundARNString := false
fmt.Printf("%s ARNs:\n----------------------------------\n", listType)
for name, arn := range arnList {
if isValidArn(&arn) {
fmt.Printf(" ✅ %s \"%s\":\n\t ARN: %s\n\t Status: ✅ Valid\n", listType, name, arn)
} else {
fmt.Printf(" ⚠️ %s \"%s\":\n\t ARN: %s\n\t Status: ⚠️ Invalid (Not an S3 bucket ARN)\n", listType, name, arn)
}
fmt.Println("")
foundARNString = true
}

if !foundARNString {
fmt.Print(" ❌ No AWS STS ARN string found.\n")
}
fmt.Println("")
}

0 comments on commit ffee490

Please sign in to comment.