Skip to content

Commit

Permalink
add iam role, role policies related UI and its functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrar-Ahmed7 committed Feb 10, 2023
1 parent a26d3de commit 1c6eaf5
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 5 deletions.
6 changes: 3 additions & 3 deletions internal/config/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func (a *Aliases) loadDefaultAliases() {
a.declare("sg", "SG")
a.declare("iam:u", "IAM:U")
a.declare("ebs", "EBS")
a.declare("iam:u","IAM:U")
a.declare("iam:g","IAM:G")

a.declare("iam:u", "IAM:U")
a.declare("iam:g", "IAM:G")
a.declare("iam:r", "IAM:R")

a.declare("help", "h", "?")
a.declare("quit", "q", "q!", "Q")
Expand Down
37 changes: 37 additions & 0 deletions internal/dao/iam_role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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 IamRole struct {
Accessor
ctx context.Context
}

func (iamu *IamRole) Init(ctx context.Context) {
iamu.ctx = ctx
}

func (iamu *IamRole) 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))
}
usr := aws.GetIamRoles(*sess)
objs := make([]Object, len(usr))
for i, obj := range usr {
objs[i] = obj
}
return objs, nil
}

func (iamu *IamRole) Get(ctx context.Context, path string) (Object, error) {
return nil, nil
}
38 changes: 38 additions & 0 deletions internal/dao/iam_role_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 IamRolePloicy struct {
Accessor
ctx context.Context
}

func (irp *IamRolePloicy) Init(ctx context.Context) {
irp.ctx = ctx
}

func (irp *IamRolePloicy) 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))
}
rn := fmt.Sprintf("%v", ctx.Value(internal.RoleName))
rp := aws.GetPoliciesOfRoles(*sess, rn)
objs := make([]Object, len(rp))
for i, obj := range rp {
objs[i] = obj
}
return objs, nil
}

func (iamup *IamRolePloicy) 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 @@ -16,4 +16,5 @@ const (
KeyAliases ContextKey = "aliases"
UserName ContextKey = "user_name"
GroupName ContextKey = "group_name"
RoleName ContextKey = "role_name"
)
8 changes: 8 additions & 0 deletions internal/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var Registry = map[string]ResourceMeta{
DAO: &dao.IAMUG{},
Renderer: &render.IAMUG{},
},
"iam:r": {
DAO: &dao.IamRole{},
Renderer: &render.IamRole{},
},
"User Policy": {
DAO: &dao.IAMUP{},
Renderer: &render.IamUserPloicy{},
Expand All @@ -42,4 +46,8 @@ var Registry = map[string]ResourceMeta{
DAO: &dao.IAMUGP{},
Renderer: &render.IamUserGroupPloicy{},
},
"Role Policy": {
DAO: &dao.IamRolePloicy{},
Renderer: &render.IamRolePloicy{},
},
}
36 changes: 36 additions & 0 deletions internal/render/iam_role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package render

import (
"fmt"

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

type IamRole struct {
}

func (ir IamRole) Header() Header {
return Header{
HeaderColumn{Name: "User-Id", SortIndicatorIdx: 5, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "User-Name", SortIndicatorIdx: 5, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "ARN", SortIndicatorIdx: -1, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Created-Date", SortIndicatorIdx: 8, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: true},
}
}

func (ir IamRole) Render(o interface{}, ns string, row *Row) error {
irResp, ok := o.(aws.IamRoleResp)
if !ok {
return fmt.Errorf("expected iam role didn't receive, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
irResp.RoleId,
irResp.RoleName,
irResp.ARN,
irResp.CreationTime,
}
return nil
}
32 changes: 32 additions & 0 deletions internal/render/iam_role_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package render

import (
"fmt"

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

type IamRolePloicy struct {
}

func (irp IamRolePloicy) 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 (irp IamRolePloicy) Render(o interface{}, ns string, row *Row) error {
usrPolicy, ok := o.(aws.IamRolePolicyResponse)
if !ok {
return fmt.Errorf("expected usrPolicy, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
usrPolicy.PolicyArn,
usrPolicy.PolicyName,
}
return nil
}
4 changes: 2 additions & 2 deletions internal/render/iam_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func (iamu IAMU) Header() Header {
func (iamu IAMU) Render(o interface{}, ns string, row *Row) error {
iamuResp, ok := o.(aws.IAMUSerResp)
if !ok {
return fmt.Errorf("Expected iamuResp, but got %T", o)
return fmt.Errorf("didn't get expected iam user, but got %T", o)
}

row.ID = ns
row.Fields = Fields{
iamuResp.UserId,
Expand Down
43 changes: 43 additions & 0 deletions internal/view/iam_role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package view

import (
"context"

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

type IamRole struct {
ResourceViewer
}

// NewSG returns a new viewer.
func NewIamRole(resource string) ResourceViewer {
var iamu IamRole
iamu.ResourceViewer = NewBrowser(resource)
iamu.AddBindKeysFn(iamu.bindKeys)
return &iamu
}

func (ir IamRole) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyShiftI: ui.NewKeyAction("Sort Role-Id ", ir.GetTable().SortColCmd("Role-Id", true), true),
ui.KeyShiftN: ui.NewKeyAction("Sort Role-Name", ir.GetTable().SortColCmd("Role-Name", true), true),
ui.KeyShiftD: ui.NewKeyAction("Sort Created-Date", ir.GetTable().SortColCmd("Created-Date", true), true),
tcell.KeyEscape: ui.NewKeyAction("Back", ir.App().PrevCmd, true),
ui.KeyShiftP: ui.NewKeyAction("View", ir.enterCmd, true),
})
}

func (ir *IamRole) enterCmd(evt *tcell.EventKey) *tcell.EventKey {
roleName := ir.GetTable().GetSecondColumn()
if roleName != "" {
irp := NewIamRolePloicy("Role Policy")
ctx := context.WithValue(ir.App().GetContext(), internal.RoleName, roleName)
ir.App().SetContext(ctx)
ir.App().Flash().Info("Role Name: " + roleName)
ir.App().inject(irp)
}
return nil
}
25 changes: 25 additions & 0 deletions internal/view/iam_role_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 IamRolePloicy struct {
ResourceViewer
}

func NewIamRolePloicy(resource string) ResourceViewer {
var irp IamRolePloicy
irp.ResourceViewer = NewBrowser(resource)
irp.AddBindKeysFn(irp.bindKeys)
return &irp
}

func (up *IamRolePloicy) 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),
})
}
3 changes: 3 additions & 0 deletions internal/view/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ func coreViewers(vv MetaViewers) {
vv["iam:g"] = MetaViewer{
viewerFn: NewIAMUG,
}
vv["iam:r"] = MetaViewer{
viewerFn: NewIamRole,
}
}

0 comments on commit 1c6eaf5

Please sign in to comment.