Skip to content

Commit 0050d42

Browse files
authored
chore: add verbose writer to show preflight progress messages (#1728)
1 parent 353f9d7 commit 0050d42

File tree

16 files changed

+109
-1
lines changed

16 files changed

+109
-1
lines changed

lwpreflight/aws/aws.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package aws
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
78
"github.com/aws/aws-sdk-go-v2/config"
89
"github.com/aws/aws-sdk-go-v2/credentials"
10+
"github.com/lacework/go-sdk/v2/lwpreflight/verbosewriter"
911
)
1012

1113
type Preflight struct {
@@ -19,6 +21,8 @@ type Preflight struct {
1921
caller Caller
2022
details Details
2123
errors map[IntegrationType][]string
24+
25+
verboseWriter verbosewriter.WriteCloser
2226
}
2327

2428
type Result struct {
@@ -90,15 +94,24 @@ func New(params Params) (*Preflight, error) {
9094
tasks: tasks,
9195
details: Details{},
9296
errors: map[IntegrationType][]string{},
97+
verboseWriter: verbosewriter.New(),
9398
}
9499

95100
return preflight, nil
96101
}
97102

103+
// Overwrite the default verbose writer
104+
func (p *Preflight) SetVerboseWriter(vw verbosewriter.WriteCloser) {
105+
p.verboseWriter = vw
106+
}
107+
98108
func (p *Preflight) Run() (*Result, error) {
109+
defer p.verboseWriter.Close()
110+
99111
for _, task := range p.tasks {
100112
err := task(p)
101113
if err != nil {
114+
p.verboseWriter.Write(fmt.Sprintf("Error running preflight task: %s", err.Error()))
102115
return nil, err
103116
}
104117
}

lwpreflight/aws/caller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ func (c *Caller) IsAssumedRole() bool {
2121
}
2222

2323
func FetchCaller(p *Preflight) error {
24+
p.verboseWriter.Write("Discovering caller information")
25+
2426
stsSvc := sts.NewFromConfig(p.awsConfig)
2527

2628
caller, err := stsSvc.GetCallerIdentity(context.Background(), nil)

lwpreflight/aws/detail.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func FetchDetails(p *Preflight) error {
7676
}
7777

7878
func fetchOrg(p *Preflight) error {
79+
p.verboseWriter.Write("Discovering organization information")
80+
7981
ctx := context.Background()
8082
orgSvc := organizations.NewFromConfig(p.awsConfig)
8183

@@ -101,6 +103,8 @@ func fetchOrg(p *Preflight) error {
101103
p.details.IsManagementAccount = *orgOutput.Organization.MasterAccountId == p.caller.AccountID
102104
p.details.OrgID = *orgOutput.Organization.Id
103105

106+
p.verboseWriter.Write("Discovering all accounts in the organization")
107+
104108
// Get account IDs in the org
105109
accountsOutput, err := orgSvc.ListAccounts(ctx, nil)
106110
if err != nil {
@@ -110,12 +114,16 @@ func fetchOrg(p *Preflight) error {
110114
p.details.OrgAccountIDs = append(p.details.OrgAccountIDs, *a.Id)
111115
}
112116

117+
p.verboseWriter.Write("Discovering root organization unit")
118+
113119
// Get root org unit ID and all org unit IDs
114120
rootsOutput, err := orgSvc.ListRoots(ctx, nil)
115121
if err != nil {
116122
return err
117123
}
118124
if len(rootsOutput.Roots) > 0 {
125+
p.verboseWriter.Write("Discovering all organization units")
126+
119127
p.details.RootOrgUnitID = *rootsOutput.Roots[0].Id
120128
orgUnitsOutput, err := orgSvc.ListOrganizationalUnitsForParent(
121129
ctx,
@@ -131,6 +139,8 @@ func fetchOrg(p *Preflight) error {
131139
}
132140
}
133141

142+
p.verboseWriter.Write("Discovering enabled services in the organization")
143+
134144
// Check enabled services
135145
servicesOutput, err := orgSvc.ListAWSServiceAccessForOrganization(ctx, nil)
136146
if err != nil {
@@ -146,6 +156,8 @@ func fetchOrg(p *Preflight) error {
146156
}
147157

148158
func fetchRegions(p *Preflight) error {
159+
p.verboseWriter.Write("Discovering enabled regions")
160+
149161
ec2Svc := ec2.NewFromConfig(p.awsConfig)
150162
output, err := ec2Svc.DescribeRegions(context.Background(), nil)
151163
if err != nil {
@@ -197,6 +209,8 @@ To determine if an existing trail is eligible CloudTrail integration:
197209
4. No need to check KMS
198210
*/
199211
func fetchEligibleTrail(p *Preflight) (*cloudtrailTypes.Trail, error) {
212+
p.verboseWriter.Write("Discovering existing eligible CloudTrail")
213+
200214
ctx := context.Background()
201215

202216
trailSvc := cloudtrail.NewFromConfig(p.awsConfig)
@@ -236,6 +250,8 @@ func fetchEligibleTrail(p *Preflight) (*cloudtrailTypes.Trail, error) {
236250
}
237251

238252
func fetchControlTowerTrail(p *Preflight) (*cloudtrailTypes.Trail, error) {
253+
p.verboseWriter.Write("Discovering existing eligible CloudTrail for Control Tower")
254+
239255
ctx := context.Background()
240256

241257
trailSvc := cloudtrail.NewFromConfig(p.awsConfig)
@@ -276,6 +292,8 @@ func fetchControlTowerTrail(p *Preflight) (*cloudtrailTypes.Trail, error) {
276292
}
277293

278294
func fetchEKSClusters(p *Preflight) error {
295+
p.verboseWriter.Write("Discovering EKS clusters")
296+
279297
var numRegions = len(p.details.Regions)
280298
var wg sync.WaitGroup
281299
var ch = make(chan EKSCluster, numRegions)
@@ -291,7 +309,7 @@ func fetchEKSClusters(p *Preflight) error {
291309
output, err := eksSvc.ListClusters(context.Background(), nil)
292310
if err != nil {
293311
logger.Log.Warnf(
294-
"Discovering EKS Cluster details: unable to check region %s\nERROR %s",
312+
"Discovering EKS Clusters: unable to check region %s. ERROR: %s",
295313
region, err.Error(),
296314
)
297315
} else {

lwpreflight/aws/permission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ func CheckPermissions(p *Preflight) error {
1111
}
1212

1313
for _, integrationType := range p.integrationTypes {
14+
p.verboseWriter.Write(fmt.Sprintf("Checking permissions for %s", integrationType))
15+
1416
requiredPermissions := RequiredPermissions[integrationType]
1517
for _, permission := range requiredPermissions {
1618
// First check plain permission strings

lwpreflight/aws/policy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aws
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/url"
78
"strings"
89

@@ -28,37 +29,44 @@ func FetchPolicies(p *Preflight) error {
2829

2930
if p.caller.IsRoot {
3031
logger.Log.Info("Skip fetching IAM policies for root user")
32+
p.verboseWriter.Write("Skip fetching IAM policies for root user")
3133
return nil
3234
}
3335

3436
iamSvc := iam.NewFromConfig(p.awsConfig)
3537
documents := []string{}
3638

3739
if p.caller.IsAssumedRole() {
40+
p.verboseWriter.Write(fmt.Sprintf("Discovering managed IAM policies for %s", p.caller.Name))
3841
docs, err := fetchManangedRolePolicies(ctx, iamSvc, p.caller.Name)
3942
if err != nil {
4043
return err
4144
}
4245
documents = append(documents, docs...)
4346

47+
p.verboseWriter.Write(fmt.Sprintf("Discovering inline IAM policies for %s", p.caller.Name))
4448
docs, err = fetchInlineRolePolicies(ctx, iamSvc, p.caller.Name)
4549
if err != nil {
4650
return err
4751
}
4852
documents = append(documents, docs...)
4953
} else {
54+
p.verboseWriter.Write(fmt.Sprintf("Discovering managed IAM policies for %s", p.caller.Name))
55+
5056
docs, err := fetchManagedUserPolicies(ctx, iamSvc, p.caller.Name)
5157
if err != nil {
5258
return err
5359
}
5460
documents = append(documents, docs...)
5561

62+
p.verboseWriter.Write(fmt.Sprintf("Discovering inline IAM policies for %s", p.caller.Name))
5663
docs, err = fetchInlineUserPolicies(ctx, iamSvc, p.caller.Name)
5764
if err != nil {
5865
return err
5966
}
6067
documents = append(documents, docs...)
6168

69+
p.verboseWriter.Write(fmt.Sprintf("Discovering IAM groups for %s", p.caller.Name))
6270
docs, err = fetchUserGroupPolicies(ctx, iamSvc, p.caller.Name)
6371
if err != nil {
6472
return err

lwpreflight/azure/azure.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
77
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
8+
"github.com/lacework/go-sdk/v2/lwpreflight/verbosewriter"
89
)
910

1011
type Preflight struct {
@@ -18,6 +19,8 @@ type Preflight struct {
1819
caller Caller
1920
details Details
2021
errors map[IntegrationType][]string
22+
23+
verboseWriter verbosewriter.WriteCloser
2124
}
2225

2326
type Result struct {
@@ -85,11 +88,17 @@ func New(params Params) (*Preflight, error) {
8588
tasks: tasks,
8689
details: Details{},
8790
errors: map[IntegrationType][]string{},
91+
verboseWriter: verbosewriter.New(),
8892
}
8993

9094
return preflight, nil
9195
}
9296

97+
// Overwrite the default verbose writer
98+
func (p *Preflight) SetVerboseWriter(vw verbosewriter.WriteCloser) {
99+
p.verboseWriter = vw
100+
}
101+
93102
func (p *Preflight) Run() (*Result, error) {
94103
for _, task := range p.tasks {
95104
err := task(p)

lwpreflight/azure/caller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Caller struct {
2323
}
2424

2525
func FetchCaller(p *Preflight) error {
26+
p.verboseWriter.Write("Discovering caller information")
27+
2628
// Get caller identity
2729
token, err := p.cred.GetToken(context.Background(), policy.TokenRequestOptions{
2830
Scopes: []string{"https://management.azure.com/.default"},

lwpreflight/azure/detail.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func FetchDetails(p *Preflight) error {
1717
return fmt.Errorf("failed to create subscriptions client: %v", err)
1818
}
1919

20+
p.verboseWriter.Write("Discovering available regions")
21+
2022
// Get available locations using the pager
2123
pager := client.NewListLocationsPager(p.subscriptionID, nil)
2224
regions := make([]string, 0)

lwpreflight/azure/permission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ func CheckPermissions(p *Preflight) error {
1010
}
1111

1212
for _, integrationType := range p.integrationTypes {
13+
p.verboseWriter.Write(fmt.Sprintf("Checking permissions for %s", integrationType))
14+
1315
requiredPermissions := RequiredPermissions[integrationType]
1416
for _, permission := range requiredPermissions {
1517
if !p.permissions[permission] {

lwpreflight/azure/policy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func FetchPolicies(p *Preflight) error {
1919
return fmt.Errorf("failed to create credential: %v", err)
2020
}
2121

22+
p.verboseWriter.Write(fmt.Sprintf("Discovering role assigments for subscription %s", p.subscriptionID))
23+
2224
// Get role assignments for the caller
2325
client, err := armauthorization.NewRoleAssignmentsClient(p.subscriptionID, cred, nil)
2426
if err != nil {

0 commit comments

Comments
 (0)