Skip to content

Commit

Permalink
enh: complete logging refactoring to charmbracelet/log (#33)
Browse files Browse the repository at this point in the history
* enh: complete logging refactoring to charmbracelet/log

* enh: complete logging refactoring to charmbracelet/log
  • Loading branch information
notdodo authored Jun 9, 2024
1 parent 00061fa commit ad4f1db
Show file tree
Hide file tree
Showing 26 changed files with 197 additions and 213 deletions.
15 changes: 8 additions & 7 deletions cmd/assess.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func processZipFile(connector *connector.StorageConnector, f *zip.File) error {

func assess(connector *connector.StorageConnector, rulesPath string) {
// perform checks based on pre-defined static rules
logger := logging.GetLogManager()
for _, rule := range files.GetFiles(rulesPath, ".ya?ml") {
c := yamler.GetConf(rule)
if !c.Enabled {
Expand All @@ -96,13 +97,13 @@ func assess(connector *connector.StorageConnector, rulesPath string) {
query, args := yamler.PrepareQuery(c)
results := connector.Query(query, args)

logging.PrintRed("Running rule: " + rule)
logging.PrintGreen("Name: " + c.Name)
logging.PrintGreen("Arguments:")
logging.PrintDarkGreen(yamler.ArgsToQueryNeo4jBrowser(args))
logging.PrintGreen("Query:")
logging.PrintDarkGreen(query)
logging.PrintGreen("Description: " + c.Description)
logger.PrintRed("Running rule: " + rule)
logger.PrintGreen("Name: " + c.Name)
logger.PrintGreen("Arguments:")
logger.PrintDarkGreen(yamler.ArgsToQueryNeo4jBrowser(args))
logger.PrintGreen("Query:")
logger.PrintDarkGreen(query)
logger.PrintGreen("Description: " + c.Description)

for _, resultMap := range results {
for key, value := range resultMap {
Expand Down
6 changes: 3 additions & 3 deletions pkg/connector/services/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func InitAWSConfiguration(profile string, awsEndpoint string) (awsc *AWSConfig)
if awsEndpoint != "" {
cfg.BaseEndpoint = aws.String(awsEndpoint)
}
awsc = &AWSConfig{Profile: profile, Config: cfg}
awsc = &AWSConfig{Profile: profile, Config: cfg, logger: logging.GetLogManager()}
SetActions()
// Get the available AWS regions dynamically
ec2.ListAndSaveRegions(cfg)
Expand Down Expand Up @@ -80,7 +80,7 @@ func (ac *AWSConfig) DumpBuckets() interface{} {
func (ac *AWSConfig) DumpEC2Instances() interface{} {
ec2s, err := ec2.ListInstances(ac.Config)
if err != nil {
logging.HandleError(err, "EC2", "")
ac.logger.Warn("Error listing EC2 instances", "err", err)
}
return ec2s
}
Expand All @@ -96,7 +96,7 @@ func (ac *AWSConfig) DumpLambdas() interface{} {
func (ac *AWSConfig) DumpRDS() interface{} {
rds, err := database.ListRDS(ac.Config)
if err != nil {
logging.HandleError(err, "RDS", "")
ac.logger.Warn("Error listing RDS", "err", err)
}
return rds
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/connector/services/aws/database/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// aws iam list-users
func ListDynamoDBs(cfg aws.Config) (dynamoDBs []*DynamoDB) {
var dynamoClient = DynamoClient{Config: cfg}
var dynamoClient = DynamoClient{Config: cfg, logger: logging.GetLogManager()}

for i := range ec2.Regions {
cfg.Region = ec2.Regions[i]
Expand All @@ -37,9 +37,11 @@ func (dc *DynamoClient) listDynamoDBTablesForRegion() (tableNames []string) {
Limit: aws.Int32(100),
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "DynamoDB", "ListTables")
dc.logger.Warn("Error on ListTables", "err", re)
}

tableNames = output.TableNames
if output != nil {
tableNames = output.TableNames
}
return
}
5 changes: 2 additions & 3 deletions pkg/connector/services/aws/database/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 Down Expand Up @@ -37,7 +36,7 @@ func (rc *RDSClient) listRDSClustersForRegion() (clusters []types.DBCluster) {
output, err := rc.client.DescribeDBClusters(context.TODO(), &rds.DescribeDBClustersInput{})
if errors.As(err, &re) {
if re.Response.StatusCode != 501 { // When using LocalStack: this is a Pro feature
logging.HandleAWSError(re, "RDS", "DescribeDBClusters")
rc.logger.Warn("Error on DescribeDBClusters", "err", re)
}
}

Expand All @@ -54,7 +53,7 @@ func (rc *RDSClient) listRDSInstancesForRegion() (instances []types.DBInstance)
output, err := rc.client.DescribeDBInstances(context.TODO(), &rds.DescribeDBInstancesInput{})
if errors.As(err, &re) {
if re.Response.StatusCode != 501 { // When using LocalStack: this is a Pro feature
logging.HandleAWSError(re, "RDS", "DescribeDBInstances")
rc.logger.Warn("Error on DescribeDBInstances", "err", re)
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/connector/services/aws/database/redshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 Down Expand Up @@ -34,9 +33,11 @@ func ListRedshiftDBs(cfg aws.Config) (redshiftDBs []*RedshiftDB) {
func (rc *RedshiftClient) listRedshiftClustersForRegion() (clusters []types.Cluster) {
output, err := rc.client.DescribeClusters(context.TODO(), &redshift.DescribeClustersInput{})
if errors.As(err, &re) {
logging.HandleAWSError(re, "Redshift", "DescribeClusters")
rc.logger.Warn("Error on DescribeClusters", "err", re)
}

clusters = output.Clusters
if output != nil {
clusters = output.Clusters
}
return
}
4 changes: 4 additions & 0 deletions pkg/connector/services/aws/database/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
rdsTypes "github.com/aws/aws-sdk-go-v2/service/rds/types"
"github.com/aws/aws-sdk-go-v2/service/redshift"
redshiftTypes "github.com/aws/aws-sdk-go-v2/service/redshift/types"
"github.com/primait/nuvola/pkg/io/logging"
)

type DynamoDB struct {
Expand All @@ -22,6 +23,7 @@ type Table struct {
type DynamoClient struct {
client *dynamodb.Client
Config aws.Config
logger logging.LogManager
}

type RDS struct {
Expand All @@ -32,6 +34,7 @@ type RDS struct {
type RDSClient struct {
client *rds.Client
Config aws.Config
logger logging.LogManager
}

type RedshiftDB struct {
Expand All @@ -41,6 +44,7 @@ type RedshiftDB struct {
type RedshiftClient struct {
client *redshift.Client
Config aws.Config
logger logging.LogManager
}

var re *awshttp.ResponseError
44 changes: 23 additions & 21 deletions pkg/connector/services/aws/ec2/ec2s.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func ListInstances(cfg aws.Config) (ec2s []*Instance, err *awshttp.ResponseError) {
ec2Client := EC2Client{Config: cfg, client: ec2.NewFromConfig(cfg)}
ec2Client := EC2Client{Config: cfg, client: ec2.NewFromConfig(cfg), logger: logging.GetLogManager()}

for _, region := range Regions {
cfg.Region = region
Expand All @@ -34,26 +34,28 @@ func (ec *EC2Client) listInstancesForRegion() (ec2s []*Instance) {
}},
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "EC2", "listInstancesForRegion")
ec.logger.Warn("Error on listing EC2s in all region", "err", re)
}

ec2s = make([]*Instance, 0, len(output.Reservations))
instances := iter.Map(output.Reservations, func(instances *types.Reservation) []*Instance {
var instancesSlice []*Instance
for _, instance := range instances.Instances {
userData := ec.getInstanceUserDataAttribute(aws.ToString(instance.InstanceId))
instancesSlice = append(instancesSlice, &Instance{
Instance: instance,
UserData: userData,
NetworkInterfaces: ec.getNetworkInterfacesWithGroups(instance.NetworkInterfaces),
InstanceState: ec.getInstanceState(aws.ToString(instance.InstanceId)),
})
}
return instancesSlice
})
if output != nil {
ec2s = make([]*Instance, 0, len(output.Reservations))
instances := iter.Map(output.Reservations, func(instances *types.Reservation) []*Instance {
var instancesSlice []*Instance
for _, instance := range instances.Instances {
userData := ec.getInstanceUserDataAttribute(aws.ToString(instance.InstanceId))
instancesSlice = append(instancesSlice, &Instance{
Instance: instance,
UserData: userData,
NetworkInterfaces: ec.getNetworkInterfacesWithGroups(instance.NetworkInterfaces),
InstanceState: ec.getInstanceState(aws.ToString(instance.InstanceId)),
})
}
return instancesSlice
})

for _, instance := range instances {
ec2s = append(ec2s, instance...)
for _, instance := range instances {
ec2s = append(ec2s, instance...)
}
}
return
}
Expand All @@ -66,7 +68,7 @@ func (ec *EC2Client) getInstanceUserDataAttribute(instanceID string) string {
Attribute: types.InstanceAttributeNameUserData,
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "EC2", "DescribeInstanceAttribute")
ec.logger.Warn("Error on describing user data attribute", "err", re)
}

if userData.UserData != nil {
Expand All @@ -93,7 +95,7 @@ func (ec *EC2Client) getSecurityGroups(groupID string) (secGroups []types.Securi
GroupIds: []string{groupID},
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "EC2", "DescribeSecurityGroups")
ec.logger.Warn("Error on describing security groups", "err", re)
}

secGroups = append(secGroups, output.SecurityGroups...)
Expand All @@ -105,7 +107,7 @@ func (ec *EC2Client) getInstanceState(instanceID string) (state types.InstanceSt
InstanceIds: []string{instanceID},
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "EC2", "DescribeSecurityGroups")
ec.logger.Warn("Error on getting EC2 state", "err", re)
}

if output != nil && len(output.InstanceStatuses) > 0 {
Expand Down
8 changes: 3 additions & 5 deletions pkg/connector/services/aws/ec2/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type EC2Client struct {
client *ec2.Client
aws.Config
logger logging.LogManager
}

// Override SDK EC2 instance type to insert SecurityGroup information
Expand Down Expand Up @@ -44,11 +45,8 @@ func ListAndSaveRegions(cfg aws.Config) {
ec2Client := ec2.NewFromConfig(cfg)

output, err := ec2Client.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{})
if errors.As(err, &re) {
logging.HandleError(err, "EC2", "ListAndSaveRegions")
}
if output == nil {
logging.HandleError(errors.New("invalid profile or credentials"), "EC2", "ListAndSaveRegions")
if errors.As(err, &re) || output == nil {
logging.GetLogManager().Warn("Error on listing regions", "err", err)
} else {
for _, region := range output.Regions {
Regions = append(Regions, aws.ToString(region.RegionName))
Expand Down
18 changes: 9 additions & 9 deletions pkg/connector/services/aws/ec2/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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) {
Expand All @@ -30,23 +29,24 @@ func (ec *EC2Client) getVpcs() (vpcs *VPC) {
MaxResults: aws.Int32(1000),
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "EC2 - VPC", "DescribeVpcs")
ec.logger.Warn("Error on DescribeVpcs", "err", re)
}

peeringOutput, err := ec.client.DescribeVpcPeeringConnections(context.TODO(), &ec2.DescribeVpcPeeringConnectionsInput{
MaxResults: aws.Int32(1000),
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "EC2 - VPC", "DescribeVpcPeeringConnections")
ec.logger.Warn("Error on DescribeVpcPeeringConnections", "err", re)
}

for i := 0; i < len(vpcsOutput.Vpcs); i++ {
vpcs.VPCs = append(vpcs.VPCs, vpcsOutput.Vpcs[i])
}
if vpcsOutput != nil {
for i := 0; i < len(vpcsOutput.Vpcs); i++ {
vpcs.VPCs = append(vpcs.VPCs, vpcsOutput.Vpcs[i])
}

for i := 0; i < len(peeringOutput.VpcPeeringConnections); i++ {
vpcs.Peerings = append(vpcs.Peerings, peeringOutput.VpcPeeringConnections[i])
for i := 0; i < len(peeringOutput.VpcPeeringConnections); i++ {
vpcs.Peerings = append(vpcs.Peerings, peeringOutput.VpcPeeringConnections[i])
}
}

return
}
10 changes: 5 additions & 5 deletions pkg/connector/services/aws/iam/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

func ListGroups(cfg aws.Config) (groups []*Group) {
iamClient = IAMClient{client: iam.NewFromConfig(cfg), Config: cfg}
iamClient = IAMClient{client: iam.NewFromConfig(cfg), Config: cfg, logger: logging.GetLogManager()}

groups = iter.Map(listGroups(), func(group *types.Group) *Group {
groups = iter.Map(iamClient.listGroups(), func(group *types.Group) *Group {
inlines := iamClient.listInlinePolicies(aws.ToString(group.GroupName), "group")
attached := iamClient.listAttachedPolicies(aws.ToString(group.GroupName), "group")

Expand All @@ -38,19 +38,19 @@ func (ic *IAMClient) listGroupsForUser(identity string) []types.Group {
UserName: &identity,
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "IAM - Groups", "ListGroupsForUser")
ic.logger.Warn("Error on ListGroupsForUser", "err", re)
}
return output.Groups
}

func listGroups() (collectedGroups []types.Group) {
func (ic *IAMClient) listGroups() (collectedGroups []types.Group) {
var marker *string
for {
output, err := iamClient.client.ListGroups(context.TODO(), &iam.ListGroupsInput{
Marker: marker,
})
if errors.As(err, &re) {
logging.HandleAWSError(re, "IAM - Groups", "ListGroups")
ic.logger.Warn("Error on ListGroups", "err", re)
}

collectedGroups = append(collectedGroups, output.Groups...)
Expand Down
Loading

0 comments on commit ad4f1db

Please sign in to comment.