Skip to content

Commit

Permalink
Added ARN checks in the diagnostics report
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Chouhan <[email protected]>
  • Loading branch information
achouhan09 committed Oct 14, 2024
1 parent 0ae4f76 commit 1fdfb2e
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pkg/diagnostics/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"fmt"
"strings"

nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
"github.com/noobaa/noobaa-operator/v5/pkg/bundle"
"github.com/noobaa/noobaa-operator/v5/pkg/options"
"github.com/noobaa/noobaa-operator/v5/pkg/util"
"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -36,6 +38,16 @@ func RunReport(cmd *cobra.Command, args []string) {
log.Fatalf(`❌ Could not get endpoint Deployment %q in Namespace %q`,
endpointApp.Name, endpointApp.Namespace)
}

// Fetching all Backingstores
bsList := &nbv1.BackingStoreList{
TypeMeta: metav1.TypeMeta{Kind: "BackingStoreList"},
}

// Fetching all Namespacestores
nsList := &nbv1.NamespaceStoreList{
TypeMeta: metav1.TypeMeta{Kind: "NamespaceStoreList"},
}
fmt.Println("")

// retrieving the status of proxy environment variables
Expand All @@ -44,6 +56,9 @@ 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
arnValidationCheck(bsList, nsList)

// TODO: Add support for additional features
}

Expand Down Expand Up @@ -73,6 +88,57 @@ func overriddenEnvVar(coreApp *appsv1.StatefulSet, endpointApp *appsv1.Deploymen
fmt.Println("")
}

// arnValidationCheck validates the ARNs for backingstores and namespacestores
func arnValidationCheck(bsList *nbv1.BackingStoreList, nsList *nbv1.NamespaceStoreList) {
log := util.Logger()

log.Print("⏳ Validation check for ARNs...\n")
foundARNString := false

// Validate ARNs for backingstores
fmt.Print("ARN Validation Check (BACKINGSTORES):\n----------------------------------\n")
for _, bs := range bsList.Items {
// skipping if AWS STS ARN string is not present
if bs.Spec.AWSS3.AWSSTSRoleARN == nil {
continue
}

if !isValidArn(bs.Spec.AWSS3.AWSSTSRoleARN) {
fmt.Printf(" ❌ Invalid ARN in Backingstore %s: %s\n", bs.Name, *bs.Spec.AWSS3.AWSSTSRoleARN)
} else {
fmt.Printf(" ✅ Valid ARN in Backingstore %s: %s\n", bs.Name, *bs.Spec.AWSS3.AWSSTSRoleARN)
}
foundARNString = true
}

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

foundARNString = false
// Validate ARNs for namespacestores
fmt.Print("ARN Validation Check (NAMESPACESTORES):\n----------------------------------\n")
for _, ns := range nsList.Items {
// skipping if AWS STS ARN string is not present
if ns.Spec.AWSS3.AWSSTSRoleARN == nil {
continue
}

if !isValidArn(ns.Spec.AWSS3.AWSSTSRoleARN) {
fmt.Printf(" ❌ Invalid ARN in Namespacestore %s: %s\n", ns.Name, *ns.Spec.AWSS3.AWSSTSRoleARN)
} else {
fmt.Printf(" ✅ Valid ARN in Namespacestore %s: %s\n", ns.Name, *ns.Spec.AWSS3.AWSSTSRoleARN)
}
foundARNString = true
}

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

fmt.Println("")
}

// printProxyStatus prints the proxy status
func printProxyStatus(appName string, envVars []corev1.EnvVar) {
fmt.Printf("Proxy Environment Variables Check (%s):\n----------------------------------\n", appName)
Expand Down Expand Up @@ -102,3 +168,8 @@ func printOverriddenEnvVar(appName string, envVars []corev1.EnvVar) {
}
fmt.Println("")
}

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

0 comments on commit 1fdfb2e

Please sign in to comment.