Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
187 changes: 92 additions & 95 deletions pkg/sync/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,99 +852,9 @@
func (s *syncer) SyncGrantExpansion(ctx context.Context) error {
l := ctxzap.Extract(ctx)
entitlementGraph := s.state.EntitlementGraph(ctx)
if !entitlementGraph.Loaded {
pageToken := s.state.PageToken(ctx)

if pageToken == "" {
l.Info("Expanding grants...")
s.handleInitialActionForStep(ctx, *s.state.Current())
}

resp, err := s.store.ListGrants(ctx, &v2.GrantsServiceListGrantsRequest{PageToken: pageToken})
if err != nil {
return err
}

// We want to take action on the next page before we push any new actions
if resp.NextPageToken != "" {
err = s.state.NextPage(ctx, resp.NextPageToken)
if err != nil {
return err
}
} else {
l.Info("Finished loading entitlement graph", zap.Int("edges", len(entitlementGraph.Edges)))
entitlementGraph.Loaded = true
}

for _, grant := range resp.List {
annos := annotations.Annotations(grant.Annotations)
expandable := &v2.GrantExpandable{}
_, err := annos.Pick(expandable)
if err != nil {
return err
}
if len(expandable.GetEntitlementIds()) == 0 {
continue
}

principalID := grant.GetPrincipal().GetId()
if principalID == nil {
return fmt.Errorf("principal id was nil")
}

// FIXME(morgabra) Log and skip some of the error paths here?
for _, srcEntitlementID := range expandable.EntitlementIds {
l.Debug(
"Expandable entitlement found",
zap.String("src_entitlement_id", srcEntitlementID),
zap.String("dst_entitlement_id", grant.GetEntitlement().GetId()),
)

srcEntitlement, err := s.store.GetEntitlement(ctx, &reader_v2.EntitlementsReaderServiceGetEntitlementRequest{
EntitlementId: srcEntitlementID,
})
if err != nil {
l.Error("error fetching source entitlement",
zap.String("src_entitlement_id", srcEntitlementID),
zap.String("dst_entitlement_id", grant.GetEntitlement().GetId()),
zap.Error(err),
)
continue
}

// The expand annotation points at entitlements by id. Those entitlements' resource should match
// the current grant's principal, so we don't allow expanding arbitrary entitlements.
sourceEntitlementResourceID := srcEntitlement.GetEntitlement().GetResource().GetId()
if sourceEntitlementResourceID == nil {
return fmt.Errorf("source entitlement resource id was nil")
}
if principalID.ResourceType != sourceEntitlementResourceID.ResourceType ||
principalID.Resource != sourceEntitlementResourceID.Resource {
l.Error(
"source entitlement resource id did not match grant principal id",
zap.String("grant_principal_id", principalID.String()),
zap.String("source_entitlement_resource_id", sourceEntitlementResourceID.String()))

return fmt.Errorf("source entitlement resource id did not match grant principal id")
}

entitlementGraph.AddEntitlement(grant.Entitlement)
entitlementGraph.AddEntitlement(srcEntitlement.GetEntitlement())
err = entitlementGraph.AddEdge(ctx,
srcEntitlement.GetEntitlement().GetId(),
grant.GetEntitlement().GetId(),
expandable.Shallow,
expandable.ResourceTypeIds,
)
if err != nil {
return fmt.Errorf("error adding edge to graph: %w", err)
}
}
}
return nil
}

fmt.Printf("%v\n", entitlementGraph)

Check failure on line 855 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

According to the lint rules (forbidigo), usage of fmt.Printf is disallowed:
Please switch to structured logging (e.g. l.Debug) to ensure consistency, traceability, and adherence to logging policy.

Suggested fix:

- fmt.Printf("%v\n", entitlementGraph)
+ l.Debug("entitlementGraph state", zap.Any("graph", entitlementGraph))

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

855-855: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 855-855:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

if entitlementGraph.Loaded {
fmt.Printf("getting cycles1\n")

Check failure on line 857 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

Please switch to structured logging instead of fmt.Printf.

Suggested fix:

- fmt.Printf("getting cycles1\n")
+ l.Debug("getting cycles1")

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

857-857: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 857-857:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

cycle := entitlementGraph.GetFirstCycle()
if cycle != nil {
l.Warn(
Expand All @@ -955,19 +865,105 @@
if dontFixCycles {
return fmt.Errorf("cycles detected in entitlement graph")
}

fmt.Printf("fixing cycles1\n")

Check failure on line 868 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

Please switch to structured logging instead of fmt.Printf.

Suggested fix:

- fmt.Printf("fixing cycles1\n")
+ l.Debug("fixing cycles1")

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

868-868: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 868-868:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

err := entitlementGraph.FixCycles()
fmt.Printf("fixed cycles1\n")

Check failure on line 870 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

Please switch to structured logging instead of fmt.Printf.

Suggested fix:

- fmt.Printf("fixed cycles1\n")
+ l.Debug("fixed cycles1")

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

870-870: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 870-870:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

if err != nil {
return err
}
}
fmt.Printf("expandGrantsForEntitlements\n")

Check failure on line 875 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

Please switch to structured logging instead of fmt.Printf.

Suggested fix:

- fmt.Printf("expandGrantsForEntitlements\n")
+ l.Debug("expandGrantsForEntitlements")

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

875-875: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 875-875:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

return s.expandGrantsForEntitlements(ctx)
}

err := s.expandGrantsForEntitlements(ctx)
pageToken := s.state.PageToken(ctx)

if pageToken == "" {
l.Info("Expanding grants...")
s.handleInitialActionForStep(ctx, *s.state.Current())
}

resp, err := s.store.ListGrants(ctx, &v2.GrantsServiceListGrantsRequest{PageToken: pageToken})
if err != nil {
return err
}

// We want to take action on the next page before we push any new actions
if resp.NextPageToken != "" {
err = s.state.NextPage(ctx, resp.NextPageToken)
if err != nil {
return err
}
} else {
l.Info("Finished loading entitlement graph", zap.Int("edges", len(entitlementGraph.Edges)))
entitlementGraph.Loaded = true
}

for _, grant := range resp.List {
annos := annotations.Annotations(grant.Annotations)
expandable := &v2.GrantExpandable{}
_, err := annos.Pick(expandable)
if err != nil {
return err
}
if len(expandable.GetEntitlementIds()) == 0 {
continue
}

principalID := grant.GetPrincipal().GetId()
if principalID == nil {
return fmt.Errorf("principal id was nil")
}

// FIXME(morgabra) Log and skip some of the error paths here?
for _, srcEntitlementID := range expandable.EntitlementIds {
l.Debug(
"Expandable entitlement found",
zap.String("src_entitlement_id", srcEntitlementID),
zap.String("dst_entitlement_id", grant.GetEntitlement().GetId()),
)

srcEntitlement, err := s.store.GetEntitlement(ctx, &reader_v2.EntitlementsReaderServiceGetEntitlementRequest{
EntitlementId: srcEntitlementID,
})
if err != nil {
l.Error("error fetching source entitlement",
zap.String("src_entitlement_id", srcEntitlementID),
zap.String("dst_entitlement_id", grant.GetEntitlement().GetId()),
zap.Error(err),
)
continue
}

// The expand annotation points at entitlements by id. Those entitlements' resource should match
// the current grant's principal, so we don't allow expanding arbitrary entitlements.
sourceEntitlementResourceID := srcEntitlement.GetEntitlement().GetResource().GetId()
if sourceEntitlementResourceID == nil {
return fmt.Errorf("source entitlement resource id was nil")
}
if principalID.ResourceType != sourceEntitlementResourceID.ResourceType ||
principalID.Resource != sourceEntitlementResourceID.Resource {
l.Error(
"source entitlement resource id did not match grant principal id",
zap.String("grant_principal_id", principalID.String()),
zap.String("source_entitlement_resource_id", sourceEntitlementResourceID.String()))

return fmt.Errorf("source entitlement resource id did not match grant principal id")
}

entitlementGraph.AddEntitlement(grant.Entitlement)
entitlementGraph.AddEntitlement(srcEntitlement.GetEntitlement())
err = entitlementGraph.AddEdge(ctx,
srcEntitlement.GetEntitlement().GetId(),
grant.GetEntitlement().GetId(),
expandable.Shallow,
expandable.ResourceTypeIds,
)
if err != nil {
return fmt.Errorf("error adding edge to graph: %w", err)
}
}
}
return nil
}

Expand Down Expand Up @@ -1430,6 +1426,7 @@
actions := len(graph.Actions)
if actions%250 == 0 || actions < 10 {
l.Info("Expanding grants", zap.Int("actions", actions))
fmt.Printf("actions: %v: %v\n", actions, graph.Actions)

Check failure on line 1429 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

Please switch to structured logging instead of fmt.Printf.

Suggested fix:

- fmt.Printf("actions: %v: %v\n", actions, graph.Actions)
+ l.Info("expanding grants: actions", zap.Int("actions_count", actions), zap.Any("actions", graph.Actions))

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

1429-1429: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 1429-1429:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

}

actionsDone, err := s.runGrantExpandActions(ctx)
Expand All @@ -1440,7 +1437,7 @@
if !actionsDone {
return nil
}

fmt.Printf("not done: %v\n", graph.Depth)

Check failure on line 1440 in pkg/sync/syncer.go

View workflow job for this annotation

GitHub Actions / go-lint

use of `fmt.Printf` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

**Avoid using fmt.Printf in production code **

Please switch to structured logging instead of fmt.Printf.

Suggested fix:

- fmt.Printf("not done: %v\n", graph.Depth)
+ l.Debug("graph not done yet", zap.Int("depth", graph.Depth))

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 golangci-lint (1.62.2)

1440-1440: use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$

(forbidigo)

🪛 GitHub Check: go-lint

[failure] 1440-1440:
use of fmt.Printf forbidden by pattern ^(fmt\.Print(|f|ln)|print|println)$ (forbidigo)

if graph.Depth > maxDepth {
l.Error(
"expandGrantsForEntitlements: exceeded max depth",
Expand Down
Loading
Loading