Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security policy rules #5157

Merged
merged 20 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions admin/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (s *Service) OpenMetricsProject(ctx context.Context) (*metrics.Client, bool
auth.ReadObjects,
},
},
Attributes: map[string]any{"admin": true},
})
if err != nil {
return nil, false, fmt.Errorf("could not issue jwt: %w", err)
Expand Down
16 changes: 0 additions & 16 deletions admin/server/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,6 @@ func (s *Server) GetDeploymentCredentials(ctx context.Context, req *adminv1.GetD
return nil, status.Error(codes.InvalidArgument, "invalid 'for' type")
}
}
// if no attributes found, we add standard non-admin user attrs to ensure security policies are applied correctly
if len(attr) == 0 {
attr = map[string]any{
"email": "",
"domain": "",
"admin": false,
}
}

ttlDuration := runtimeAccessTokenEmbedTTL
if req.TtlSeconds > 0 {
Expand Down Expand Up @@ -284,14 +276,6 @@ func (s *Server) GetIFrame(ctx context.Context, req *adminv1.GetIFrameRequest) (
return nil, status.Error(codes.InvalidArgument, "invalid 'for' type")
}
}
// if no attributes found, we add standard non-admin user attrs to ensure security policies are applied correctly
if len(attr) == 0 {
attr = map[string]any{
"email": "",
"domain": "",
"admin": false,
}
}

ttlDuration := runtimeAccessTokenEmbedTTL
if req.TtlSeconds > 0 {
Expand Down
47 changes: 37 additions & 10 deletions admin/server/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/rilldata/rill/admin/server/auth"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/pkg/duckdbsql"
"github.com/rilldata/rill/runtime/pkg/email"
"github.com/rilldata/rill/runtime/pkg/observability"
Expand Down Expand Up @@ -146,12 +147,14 @@ func (s *Server) GetProject(ctx context.Context, req *adminv1.GetProjectRequest)
}

var attr map[string]any
var security *runtimev1.MetricsViewSpec_SecurityV2
var rules []*runtimev1.SecurityRule
if claims.OwnerType() == auth.OwnerTypeUser {
attr, err = s.jwtAttributesForUser(ctx, claims.OwnerID(), proj.OrganizationID, permissions)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
} else if claims.OwnerType() == auth.OwnerTypeService {
attr = map[string]any{"admin": true}
} else if claims.OwnerType() == auth.OwnerTypeMagicAuthToken {
mdl, ok := claims.AuthTokenModel().(*database.MagicAuthToken)
if !ok {
Expand All @@ -160,21 +163,45 @@ func (s *Server) GetProject(ctx context.Context, req *adminv1.GetProjectRequest)

attr = mdl.Attributes

security = &runtimev1.MetricsViewSpec_SecurityV2{
Access: fmt.Sprintf("'{{ .self.name }}'=%s", duckdbsql.EscapeStringValue(mdl.MetricsView)),
}
// Deny access to all resources except themes and mdl.MetricsView
rules = append(rules, &runtimev1.SecurityRule{
Rule: &runtimev1.SecurityRule_Access{
Access: &runtimev1.SecurityRuleAccess{
Condition: fmt.Sprintf(
"NOT ('{{.self.kind}}'='%s' OR '{{.self.kind}}'='%s' AND '{{ .self.name }}'=%s)",
runtime.ResourceKindTheme,
runtime.ResourceKindMetricsView,
duckdbsql.EscapeStringValue(mdl.MetricsView),
),
Allow: false,
},
},
})

if mdl.MetricsViewFilterJSON != "" {
expr := &runtimev1.Expression{}
err := protojson.Unmarshal([]byte(mdl.MetricsViewFilterJSON), expr)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not unmarshal metrics view filter: %s", err.Error())
}
security.QueryFilter = expr

rules = append(rules, &runtimev1.SecurityRule{
Rule: &runtimev1.SecurityRule_RowFilter{
RowFilter: &runtimev1.SecurityRuleRowFilter{
Expression: expr,
},
},
})
}

if len(mdl.MetricsViewFields) > 0 {
security.Include = append(security.Include, &runtimev1.MetricsViewSpec_SecurityV2_FieldConditionV2{
Condition: "true",
Names: mdl.MetricsViewFields,
rules = append(rules, &runtimev1.SecurityRule{
Rule: &runtimev1.SecurityRule_FieldAccess{
FieldAccess: &runtimev1.SecurityRuleFieldAccess{
Fields: mdl.MetricsViewFields,
Allow: true,
},
},
})
}
}
Expand All @@ -198,8 +225,8 @@ func (s *Server) GetProject(ctx context.Context, req *adminv1.GetProjectRequest)
runtimeauth.ReadAPI,
},
},
Attributes: attr,
Security: security,
Attributes: attr,
SecurityRules: rules,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "could not issue jwt: %s", err.Error())
Expand Down
2 changes: 2 additions & 0 deletions admin/server/runtime_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (s *Server) runtimeProxyForOrgAndProject(w http.ResponseWriter, r *http.Req
if err != nil {
return httputil.Error(http.StatusInternalServerError, err)
}
} else if claims.OwnerType() == auth.OwnerTypeService {
attr = map[string]any{"admin": true}
}

jwt, err = s.issuer.NewToken(runtimeauth.TokenOptions{
Expand Down
Loading
Loading