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

fix: bug on missing init logger; fix goroutines #34

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
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
43 changes: 27 additions & 16 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"
"reflect"
"sync"
"time"

Expand Down Expand Up @@ -64,30 +65,40 @@ func runDumpCmd(cmd *cobra.Command, args []string) {
func dumpData(storageConnector *connector.StorageConnector, cloudConnector *connector.CloudConnector) {
dataChan := make(chan map[string]interface{})
var wg sync.WaitGroup
wg.Add(1)

go func() {
cloudConnector.DumpAll("aws", dataChan, &wg)
defer close(dataChan)
cloudConnector.DumpAll("aws", dataChan)
}()

for data := range dataChan {
processData(data, storageConnector)
}
wg.Add(1)
go func() {
defer wg.Done()
for data := range dataChan {
processData(storageConnector, data)
}
}()
wg.Wait()
}

func processData(data map[string]interface{}, storageConnector *connector.StorageConnector) {
for key, value := range data {
obj, err := json.Marshal(value)
if err != nil {
logger.Error("Error marshalling output", "err", err)
continue
}
if storageConnector != nil {
storageConnector.ImportResults(key, obj)
}
AWSResults[key] = value
func processData(storageConnector *connector.StorageConnector, data map[string]interface{}) {
if len(data) == 0 {
return
}

v := reflect.ValueOf(data)
if v.Kind() != reflect.Map || v.Len() == 0 {
logger.Error("processData: unexpected data format")
}

mapKey := v.MapKeys()[0].Interface().(string)
obj, err := json.Marshal(data[mapKey])
if err != nil {
logger.Error("processData: error marshalling output", "err", err)
}

storageConnector.ImportResults(mapKey, obj)
AWSResults[mapKey] = data[mapKey]
}

func saveResults(awsProfile, outputDir, outputFormat string) {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/imroc/req/v3 v3.43.7
github.com/itchyny/gojq v0.12.16
github.com/joho/godotenv v1.5.1
github.com/neo4j/neo4j-go-driver/v5 v5.20.0
github.com/notdodo/arner v0.0.0-20230222134658-4fe417a6cc9c
github.com/notdodo/goflat/v2 v2.1.1
Expand Down Expand Up @@ -69,7 +68,6 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/onsi/ginkgo/v2 v2.19.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down
80 changes: 1 addition & 79 deletions go.sum

Large diffs are not rendered by default.

54 changes: 28 additions & 26 deletions pkg/connector/cloud_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package connector
import (
"errors"
"strings"
"sync"

awsconfig "github.com/primait/nuvola/pkg/connector/services/aws"
"github.com/primait/nuvola/pkg/io/logging"
Expand All @@ -24,34 +23,37 @@ func SetActions() {
awsconfig.SetActions()
}

func (cc *CloudConnector) DumpAll(cloudprovider string, c chan map[string]interface{}, wg *sync.WaitGroup) {
func (cc *CloudConnector) DumpAll(cloudprovider string, c chan map[string]interface{}) {
switch strings.ToLower(cloudprovider) {
case "aws":
cc.dumpAWSData(c, wg)
default:
cc.logger.Error("Unsupported cloud provider", "cloudprovider", cloudprovider)
}
}
dumpFunctions := []struct {
name string
dump func() interface{}
}{
{"Whoami", cc.AWSConfig.DumpWhoami},
{"CredentialReport", cc.AWSConfig.DumpCredentialReport},
{"Groups", cc.AWSConfig.DumpIAMGroups},
{"Users", cc.AWSConfig.DumpIAMUsers},
{"Roles", cc.AWSConfig.DumpIAMRoles},
{"Buckets", cc.AWSConfig.DumpBuckets},
{"EC2s", cc.AWSConfig.DumpEC2Instances},
{"VPCs", cc.AWSConfig.DumpVpcs},
{"Lambdas", cc.AWSConfig.DumpLambdas},
{"RDS", cc.AWSConfig.DumpRDS},
{"DynamoDBs", cc.AWSConfig.DumpDynamoDBs},
{"RedshiftDBs", cc.AWSConfig.DumpRedshiftDBs},
}

func (cc *CloudConnector) dumpAWSData(c chan map[string]interface{}, wg *sync.WaitGroup) {
defer wg.Done()
data := map[string]interface{}{
"Whoami": cc.AWSConfig.DumpWhoami(),
"CredentialReport": cc.AWSConfig.DumpCredentialReport(),
"Groups": cc.AWSConfig.DumpIAMGroups(),
"Users": cc.AWSConfig.DumpIAMUsers(),
"Roles": cc.AWSConfig.DumpIAMRoles(),
"Buckets": cc.AWSConfig.DumpBuckets(),
"EC2s": cc.AWSConfig.DumpEC2Instances(),
"VPCs": cc.AWSConfig.DumpVpcs(),
"Lambdas": cc.AWSConfig.DumpLambdas(),
"RDS": cc.AWSConfig.DumpRDS(),
"DynamoDBs": cc.AWSConfig.DumpDynamoDBs(),
"RedshiftDBs": cc.AWSConfig.DumpRedshiftDBs(),
}

for key, value := range data {
c <- map[string]interface{}{key: value}
for _, df := range dumpFunctions {
data := df.dump()
if data != nil {
c <- map[string]interface{}{
df.name: data,
}
}
}
default:
cc.logger.Error("unsupported cloud provider", "cloudprovider", cloudprovider)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/connector/services/aws/database/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

"github.com/primait/nuvola/pkg/connector/services/aws/ec2"
"github.com/primait/nuvola/pkg/io/logging"

"github.com/aws/aws-sdk-go-v2/aws"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
Expand All @@ -14,7 +15,7 @@ import (

// aws iam list-users
func ListRDS(cfg aws.Config) (rdsRet *RDS, re *awshttp.ResponseError) {
var rdsClient = RDSClient{Config: cfg}
var rdsClient = RDSClient{Config: cfg, logger: logging.GetLogManager()}

rdsRet = &RDS{}

Expand Down
3 changes: 2 additions & 1 deletion pkg/connector/services/aws/database/redshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

"github.com/primait/nuvola/pkg/connector/services/aws/ec2"
"github.com/primait/nuvola/pkg/io/logging"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/redshift"
Expand All @@ -13,7 +14,7 @@ import (

// aws iam list-users
func ListRedshiftDBs(cfg aws.Config) (redshiftDBs []*RedshiftDB) {
var redshiftClient = RedshiftClient{Config: cfg}
var redshiftClient = RedshiftClient{Config: cfg, logger: logging.GetLogManager()}

for i := range ec2.Regions {
cfg.Region = ec2.Regions[i]
Expand Down
3 changes: 2 additions & 1 deletion pkg/connector/services/aws/ec2/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/primait/nuvola/pkg/io/logging"
)

func ListVpcs(cfg aws.Config) (vpcs *VPC) {
ec2Client := EC2Client{Config: cfg, client: ec2.NewFromConfig(cfg)}
ec2Client := EC2Client{Config: cfg, client: ec2.NewFromConfig(cfg), logger: logging.GetLogManager()}

vpcs = &VPC{}
for _, region := range Regions {
Expand Down
1 change: 0 additions & 1 deletion pkg/connector/services/aws/iam/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ func (ic *IAMClient) listAttachedPolicies(identity string, object string) (attac
ic.logger.Warn("no user/role/group defined", "object", object)
}

fmt.Println("Asdfasd")
for _, policy := range output {
policyVersions := ic.listPolicyVersions(policy.PolicyArn)
policyDocument, errj := json.Marshal(policyVersions[0].Document)
Expand Down
Loading