Skip to content

Commit

Permalink
work on iam user poilcy
Browse files Browse the repository at this point in the history
  • Loading branch information
aanamshaikh committed Feb 9, 2023
1 parent 653081c commit da97126
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 6 deletions.
12 changes: 10 additions & 2 deletions internal/aws/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func GetPoliciesOfGrp(sess session.Session, grpName string) []*iam.AttachedPolic

// If a user belong to a Group then we can't see the user's attached policy here,
// their policies are governed on the top of the group
func GetPoliciesOfUser(sess session.Session, usrName string) []*iam.AttachedPolicy {
func GetPoliciesOfUser(sess session.Session, usrName string) []IAMUSerPolicyResponse{
imaSrv := iam.New(&sess)
result, err := imaSrv.ListAttachedUserPolicies(&iam.ListAttachedUserPoliciesInput{
UserName: &usrName,
Expand All @@ -77,7 +77,15 @@ func GetPoliciesOfUser(sess session.Session, usrName string) []*iam.AttachedPoli
fmt.Println("Error in fetching Iam policies of the User: ", usrName, " err: ", err)
return nil
}
return result.AttachedPolicies
var usersPolicy []IAMUSerPolicyResponse
for _, up := range result.AttachedPolicies {
userPolicy := &IAMUSerPolicyResponse{
PolicyArn: *up.PolicyArn,
PolicyName: *up.PolicyName,
}
usersPolicy = append(usersPolicy,*userPolicy)
}
return usersPolicy
}

func GetIamRoles(sess session.Session) []*iam.Role {
Expand Down
5 changes: 5 additions & 0 deletions internal/aws/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ type IAMUSerResp struct {
ARN string
CreationTime string
}

type IAMUSerPolicyResponse struct {
PolicyArn string
PolicyName string
}
File renamed without changes.
38 changes: 38 additions & 0 deletions internal/dao/iam_user_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dao

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/aws"
"github.com/rs/zerolog/log"
)

type IAMUP struct {
Accessor
ctx context.Context
}

func (iamup *IAMUP) Init(ctx context.Context) {
iamup.ctx = ctx
}

func (iamup *IAMUP) List(ctx context.Context) ([]Object, error) {
sess, ok := ctx.Value(internal.KeySession).(*session.Session)
if !ok {
log.Err(fmt.Errorf("conversion err: Expected session.session but got %v", sess))
}
userName := fmt.Sprintf("%v", ctx.Value(internal.UserName))
usrPolicy := aws.GetPoliciesOfUser(*sess, userName)
objs := make([]Object, len(usrPolicy))
for i, obj := range usrPolicy {
objs[i] = obj
}
return objs, nil
}

func (iamup *IAMUP) Get(ctx context.Context, path string) (Object, error) {
return nil, nil
}
1 change: 1 addition & 0 deletions internal/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const (
ObjectName ContextKey = "object_name"
FolderName ContextKey = "folder_name"
KeyAliases ContextKey = "aliases"
UserName ContextKey = "user_name"
)
4 changes: 4 additions & 0 deletions internal/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ var Registry = map[string]ResourceMeta{
DAO: &dao.IAMU{},
Renderer: &render.IAMU{},
},
"User Policy": {
DAO: &dao.IAMUP{},
Renderer: &render.IamUserPloicy{},
},
}
File renamed without changes.
33 changes: 33 additions & 0 deletions internal/render/iam_user_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package render

import (
"fmt"

"github.com/derailed/tview"
"github.com/one2nc/cloud-lens/internal/aws"
)

type IamUserPloicy struct {
}

func (iup IamUserPloicy) Header() Header {
return Header{
HeaderColumn{Name: "Policy-ARN", SortIndicatorIdx: 7, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Policy-Name", SortIndicatorIdx: 7, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
}
}

func (iup IamUserPloicy) Render(o interface{}, ns string, row *Row) error {
usrPolicy, ok := o.(aws.IAMUSerPolicyResponse)
if !ok {
return fmt.Errorf("expected S3Resp, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
usrPolicy.PolicyArn,
usrPolicy.PolicyName,

}
return nil
}
16 changes: 12 additions & 4 deletions internal/view/iamu.go → internal/view/iam_user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package view

import (
"context"

"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal"
"github.com/one2nc/cloud-lens/internal/ui"
)

Expand All @@ -23,13 +26,18 @@ func (iamu IAMU) bindKeys(aa ui.KeyActions) {
ui.KeyShiftN: ui.NewKeyAction("Sort User-Name", iamu.GetTable().SortColCmd("User-Name", true), true),
ui.KeyShiftD: ui.NewKeyAction("Sort Created-Date", iamu.GetTable().SortColCmd("Created-Date", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", iamu.App().PrevCmd, true),
// tcell.KeyEnter: ui.NewKeyAction("View", iamu.enterCmd, true),
ui.KeyShiftP: ui.NewKeyAction("View", iamu.enterCmd, true),
})
}

func (iamu *IAMU) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
groupId := iamu.GetTable().GetSelectedItem()
iamu.App().Flash().Info("groupId: " + groupId)
userName := iamu.GetTable().GetSecondColumn()
if userName != "" {
up := NewIamUserPloicy("User Policy")
ctx := context.WithValue(iamu.App().GetContext(), internal.UserName, userName)
iamu.App().SetContext(ctx)
iamu.App().Flash().Info("userName: " + userName)
iamu.App().inject(up)
}
return nil
}

25 changes: 25 additions & 0 deletions internal/view/iam_user_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package view

import (
"github.com/gdamore/tcell/v2"
"github.com/one2nc/cloud-lens/internal/ui"
)

type iamUserPloicy struct {
ResourceViewer
}

func NewIamUserPloicy(resource string) ResourceViewer {
var up iamUserPloicy
up.ResourceViewer = NewBrowser(resource)
up.AddBindKeysFn(up.bindKeys)
return &up
}

func (up *iamUserPloicy) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
tcell.KeyEscape: ui.NewKeyAction("Back", up.App().PrevCmd, true),
ui.KeyShiftA: ui.NewKeyAction("Policy-ARN", up.GetTable().SortColCmd("Policy-ARN", true), true),
ui.KeyShiftN: ui.NewKeyAction("Policy-Name", up.GetTable().SortColCmd("Policy-Name", true), true),
})
}

0 comments on commit da97126

Please sign in to comment.