Skip to content

Commit

Permalink
Separate resource type and resource for IAM policies
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Sep 18, 2023
1 parent 8349f4e commit ad2a50d
Show file tree
Hide file tree
Showing 30 changed files with 283 additions and 186 deletions.
23 changes: 15 additions & 8 deletions app/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,19 +696,22 @@ func (a *api) start(ctx context.Context) error {
{
Name: "$anon",
Domain: "$none",
Resource: "fs:/**",
Types: []string{"fs"},
Resource: "/**",
Actions: []string{"GET", "HEAD", "OPTIONS"},
},
{
Name: "$anon",
Domain: "$none",
Resource: "api:/api",
Types: []string{"api"},
Resource: "/api",
Actions: []string{"GET", "HEAD", "OPTIONS"},
},
{
Name: "$anon",
Domain: "$none",
Resource: "api:/api/v3/widget/process/**",
Types: []string{"api"},
Resource: "/api/v3/widget/process/**",
Actions: []string{"GET", "HEAD", "OPTIONS"},
},
}
Expand All @@ -728,7 +731,8 @@ func (a *api) start(ctx context.Context) error {
policies = append(policies, iamaccess.Policy{
Name: cfg.Storage.Memory.Auth.Username,
Domain: "$none",
Resource: "fs:/memfs/**",
Types: []string{"fs"},
Resource: "/memfs/**",
Actions: []string{"ANY"},
})
}
Expand All @@ -753,7 +757,8 @@ func (a *api) start(ctx context.Context) error {
policies = append(policies, iamaccess.Policy{
Name: s.Auth.Username,
Domain: "$none",
Resource: "fs:" + s.Mountpoint + "/**",
Types: []string{"fs"},
Resource: s.Mountpoint + "/**",
Actions: []string{"ANY"},
})
}
Expand All @@ -763,7 +768,8 @@ func (a *api) start(ctx context.Context) error {
policies = append(policies, iamaccess.Policy{
Name: "$anon",
Domain: "$none",
Resource: "rtmp:/**",
Types: []string{"rtmp"},
Resource: "/**",
Actions: []string{"ANY"},
})
}
Expand All @@ -772,7 +778,8 @@ func (a *api) start(ctx context.Context) error {
policies = append(policies, iamaccess.Policy{
Name: "$anon",
Domain: "$none",
Resource: "srt:**",
Types: []string{"srt"},
Resource: "**",
Actions: []string{"ANY"},
})
}
Expand All @@ -789,7 +796,7 @@ func (a *api) start(ctx context.Context) error {
}

for _, policy := range policies {
manager.AddPolicy(policy.Name, policy.Domain, policy.Resource, policy.Actions)
manager.AddPolicy(policy.Name, policy.Domain, policy.Types, policy.Resource, policy.Actions)
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions cluster/docs/ClusterAPI_docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions cluster/docs/ClusterAPI_swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,12 @@
},
"resource": {
"type": "string"
},
"types": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions cluster/docs/ClusterAPI_swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ definitions:
type: string
resource:
type: string
types:
items:
type: string
type: array
type: object
app.Config:
properties:
Expand Down
16 changes: 8 additions & 8 deletions cluster/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (m *manager) apply(op store.Operation) {
}
}

func (m *manager) Enforce(name, domain, resource, action string) bool {
return m.iam.Enforce(name, domain, resource, action)
func (m *manager) Enforce(name, domain, rtype, resource, action string) bool {
return m.iam.Enforce(name, domain, rtype, resource, action)
}

func (m *manager) HasDomain(domain string) bool {
Expand All @@ -72,20 +72,20 @@ func (m *manager) ListDomains() []string {
return m.iam.ListDomains()
}

func (m *manager) HasPolicy(name, domain, resource string, actions []string) bool {
return m.iam.HasPolicy(name, domain, resource, actions)
func (m *manager) HasPolicy(name, domain string, types []string, resource string, actions []string) bool {
return m.iam.HasPolicy(name, domain, types, resource, actions)
}

func (m *manager) AddPolicy(name, domain, resource string, actions []string) error {
func (m *manager) AddPolicy(name, domain string, types []string, resource string, actions []string) error {
return ErrClusterMode
}

func (m *manager) RemovePolicy(name, domain, resource string, actions []string) error {
func (m *manager) RemovePolicy(name, domain string, types []string, resource string, actions []string) error {
return ErrClusterMode
}

func (m *manager) ListPolicies(name, domain, resource string, actions []string) []access.Policy {
return m.iam.ListPolicies(name, domain, resource, actions)
func (m *manager) ListPolicies(name, domain string, types []string, resource string, actions []string) []access.Policy {
return m.iam.ListPolicies(name, domain, types, resource, actions)
}

func (m *manager) ReloadPolicies() error {
Expand Down
6 changes: 6 additions & 0 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5681,6 +5681,12 @@
},
"resource": {
"type": "string"
},
"types": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ definitions:
type: string
resource:
type: string
types:
items:
type: string
type: array
type: object
api.IAMUser:
properties:
Expand Down
3 changes: 3 additions & 0 deletions http/api/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (u *IAMUser) Marshal(user identity.User, policies []access.Policy) {
for _, p := range policies {
u.Policies = append(u.Policies, IAMPolicy{
Domain: p.Domain,
Types: p.Types,
Resource: p.Resource,
Actions: p.Actions,
})
Expand Down Expand Up @@ -84,6 +85,7 @@ func (u *IAMUser) Unmarshal() (identity.User, []access.Policy) {
iampolicies = append(iampolicies, access.Policy{
Name: u.Name,
Domain: p.Domain,
Types: p.Types,
Resource: p.Resource,
Actions: p.Actions,
})
Expand Down Expand Up @@ -122,6 +124,7 @@ type IAMAuth0Tenant struct {
type IAMPolicy struct {
Name string `json:"name,omitempty"`
Domain string `json:"domain"`
Types []string `json:"types"`
Resource string `json:"resource"`
Actions []string `json:"actions"`
}
2 changes: 1 addition & 1 deletion http/graph/resolver/playout.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions http/graph/resolver/process.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions http/handler/api/cluster_iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func (h *ClusterHandler) AddIdentity(c echo.Context) error {

iamuser, iampolicies := user.Unmarshal()

if !h.iam.Enforce(ctxuser, domain, "iam:"+iamuser.Name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", iamuser.Name, "write") {
return api.Err(http.StatusForbidden, "", "Not allowed to create user '%s'", iamuser.Name)
}

for _, p := range iampolicies {
if !h.iam.Enforce(ctxuser, p.Domain, "iam:"+iamuser.Name, "write") {
if !h.iam.Enforce(ctxuser, p.Domain, "iam", iamuser.Name, "write") {
return api.Err(http.StatusForbidden, "", "Not allowed to write policy: %v", p)
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func (h *ClusterHandler) UpdateIdentity(c echo.Context) error {
domain := util.DefaultQuery(c, "domain", "")
name := util.PathParam(c, "name")

if !h.iam.Enforce(ctxuser, domain, "iam:"+name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", name, "write") {
return api.Err(http.StatusForbidden, "", "not allowed to modify this user")
}

Expand All @@ -102,7 +102,7 @@ func (h *ClusterHandler) UpdateIdentity(c echo.Context) error {
}
}

iampolicies := h.iam.ListPolicies(name, "", "", nil)
iampolicies := h.iam.ListPolicies(name, "", nil, "", nil)

user := api.IAMUser{}
user.Marshal(iamuser, iampolicies)
Expand All @@ -113,12 +113,12 @@ func (h *ClusterHandler) UpdateIdentity(c echo.Context) error {

iamuser, iampolicies = user.Unmarshal()

if !h.iam.Enforce(ctxuser, domain, "iam:"+iamuser.Name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", iamuser.Name, "write") {
return api.Err(http.StatusForbidden, "", "not allowed to create user '%s'", iamuser.Name)
}

for _, p := range iampolicies {
if !h.iam.Enforce(ctxuser, p.Domain, "iam:"+iamuser.Name, "write") {
if !h.iam.Enforce(ctxuser, p.Domain, "iam", iamuser.Name, "write") {
return api.Err(http.StatusForbidden, "", "not allowed to write policy: %v", p)
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func (h *ClusterHandler) UpdateIdentityPolicies(c echo.Context) error {
domain := util.DefaultQuery(c, "domain", "")
name := util.PathParam(c, "name")

if !h.iam.Enforce(ctxuser, domain, "iam:"+name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", name, "write") {
return api.Err(http.StatusForbidden, "", "not allowed to modify this user")
}

Expand Down Expand Up @@ -199,7 +199,7 @@ func (h *ClusterHandler) UpdateIdentityPolicies(c echo.Context) error {
accessPolicies := []access.Policy{}

for _, p := range policies {
if !h.iam.Enforce(ctxuser, p.Domain, "iam:"+iamuser.Name, "write") {
if !h.iam.Enforce(ctxuser, p.Domain, "iam", iamuser.Name, "write") {
return api.Err(http.StatusForbidden, "", "not allowed to write policy: %v", p)
}

Expand Down Expand Up @@ -265,17 +265,17 @@ func (h *ClusterHandler) ListIdentities(c echo.Context) error {
users := make([]api.IAMUser, len(identities)+1)

for i, iamuser := range identities {
if !h.iam.Enforce(ctxuser, domain, "iam:"+iamuser.Name, "read") {
if !h.iam.Enforce(ctxuser, domain, "iam", iamuser.Name, "read") {
continue
}

if !h.iam.Enforce(ctxuser, domain, "iam:"+iamuser.Name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", iamuser.Name, "write") {
iamuser = identity.User{
Name: iamuser.Name,
}
}

policies := h.iam.ListPolicies(iamuser.Name, "", "", nil)
policies := h.iam.ListPolicies(iamuser.Name, "", nil, "", nil)

users[i].Marshal(iamuser, policies)
}
Expand All @@ -284,7 +284,7 @@ func (h *ClusterHandler) ListIdentities(c echo.Context) error {
Name: "$anon",
}

policies := h.iam.ListPolicies("$anon", "", "", nil)
policies := h.iam.ListPolicies("$anon", "", nil, "", nil)

users[len(users)-1].Marshal(anon, policies)

Expand All @@ -307,7 +307,7 @@ func (h *ClusterHandler) ListIdentity(c echo.Context) error {
domain := util.DefaultQuery(c, "domain", "")
name := util.PathParam(c, "name")

if !h.iam.Enforce(ctxuser, domain, "iam:"+name, "read") {
if !h.iam.Enforce(ctxuser, domain, "iam", name, "read") {
return api.Err(http.StatusForbidden, "", "Not allowed to access this user")
}

Expand All @@ -321,7 +321,7 @@ func (h *ClusterHandler) ListIdentity(c echo.Context) error {
}

if ctxuser != iamuser.Name {
if !h.iam.Enforce(ctxuser, domain, "iam:"+name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", name, "write") {
iamuser = identity.User{
Name: iamuser.Name,
}
Expand All @@ -333,7 +333,7 @@ func (h *ClusterHandler) ListIdentity(c echo.Context) error {
}
}

iampolicies := h.iam.ListPolicies(name, "", "", nil)
iampolicies := h.iam.ListPolicies(name, "", nil, "", nil)

user := api.IAMUser{}
user.Marshal(iamuser, iampolicies)
Expand All @@ -351,7 +351,7 @@ func (h *ClusterHandler) ListIdentity(c echo.Context) error {
// @Security ApiKeyAuth
// @Router /api/v3/cluster/iam/policies [get]
func (h *ClusterHandler) ListPolicies(c echo.Context) error {
iampolicies := h.iam.ListPolicies("", "", "", nil)
iampolicies := h.iam.ListPolicies("", "", nil, "", nil)

policies := []api.IAMPolicy{}

Expand Down Expand Up @@ -385,7 +385,7 @@ func (h *ClusterHandler) RemoveIdentity(c echo.Context) error {
domain := util.DefaultQuery(c, "domain", "$none")
name := util.PathParam(c, "name")

if !h.iam.Enforce(ctxuser, domain, "iam:"+name, "write") {
if !h.iam.Enforce(ctxuser, domain, "iam", name, "write") {
return api.Err(http.StatusForbidden, "", "Not allowed to delete this user")
}

Expand Down
2 changes: 1 addition & 1 deletion http/handler/api/cluster_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (h *ClusterHandler) ListNodeProcesses(c echo.Context) error {
processes := []clientapi.Process{}

for _, p := range procs {
if !h.iam.Enforce(ctxuser, domain, "process:"+p.Config.ID, "read") {
if !h.iam.Enforce(ctxuser, domain, "process", p.Config.ID, "read") {
continue
}

Expand Down
Loading

0 comments on commit ad2a50d

Please sign in to comment.