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

Added ARN checks in the diagnostics report #1462

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
89 changes: 89 additions & 0 deletions pkg/diagnostics/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws/arn"
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"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand All @@ -36,6 +40,22 @@ 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"},
}
if !util.KubeList(bsList, &client.ListOptions{Namespace: 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(`❌ No namespacestores were found in the %q namespace`, options.Namespace)
}
fmt.Println("")

// retrieving the status of proxy environment variables
Expand All @@ -44,6 +64,9 @@ func RunReport(cmd *cobra.Command, args []string) {
// retrieving the overridden env variables using `CONFIG_JS_` prefix
overriddenEnvVar(coreApp, endpointApp)

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

// TODO: Add support for additional features
}

Expand Down Expand Up @@ -73,6 +96,33 @@ 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("⏳ Validating store ARNs...\n")

// Validate ARNs for backingstores
bsArnList := make(map[string]string)
for _, bs := range bsList.Items {
if bs.Spec.AWSS3 != nil && bs.Spec.AWSS3.AWSSTSRoleARN != nil {
bsArnList[bs.Name] = *bs.Spec.AWSS3.AWSSTSRoleARN
}
}
printARNStatus("BACKINGSTORE", bsArnList)

// Validate ARNs for namespacestores
nsArnList := make(map[string]string)
for _, ns := range nsList.Items {
if ns.Spec.AWSS3 != nil && ns.Spec.AWSS3.AWSSTSRoleARN != nil {
nsArnList[ns.Name] = *ns.Spec.AWSS3.AWSSTSRoleARN
}
}
printARNStatus("NAMESPACESTORE", nsArnList)

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 +152,42 @@ func printOverriddenEnvVar(appName string, envVars []corev1.EnvVar) {
}
fmt.Println("")
}

// isValidSTSArn is a function to validate the STS ARN format
func isValidSTSArn(arnStr *string) bool {
if arnStr == nil {
return false
}

parsedArn, err := arn.Parse(*arnStr)
if err != nil {
return false
}

if parsedArn.Service == "sts" {
return true
}
return false
}

// 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 {
fmt.Printf("\t%s \"%s\":\n\t ARN: %s\n\t", listType, name, arn)
// currently validating only for AWS STS ARN, can be changed accordingly for other formats and validation
if isValidSTSArn(&arn) {
fmt.Printf(" Status: ✅ Valid STS ARN\n")
} else {
fmt.Printf(" Status: ⚠️ Invalid (Not an STS ARN)\n")
}
foundARNString = true
fmt.Println("")
}

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