Repository navigation
feat(admin): create and delete alerts and reports on behalf of a user #10232
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
base: nishant/ai-alerts
Are you sure you want to change the base?
Changes from all commits
71ed78a
4c2fcd4
df400fb
e620362
8883105
a891cb2
2459b49
b6c94e0
365f652
7f91a46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "regexp" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/rilldata/rill/admin/database" | ||
| "github.com/rilldata/rill/admin/server/auth" | ||
| adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // userForPrimaryDeployment checks that the caller is the project's primary deployment, and returns the user it acts for with their permissions on the project. | ||
| // The deployment is trusted to pass the user of its session, as for GetAlertMeta, GetReportMeta and GetConnectorOAuthToken. | ||
| // Other deployments are rejected, because alerts and reports created from the UI live in the prod environment, which only the primary deployment reads. | ||
| func (s *Server) userForPrimaryDeployment(ctx context.Context, proj *database.Project, userID string) (*database.User, *adminv1.ProjectPermissions, error) { | ||
| claims := auth.GetClaims(ctx) | ||
| if claims.OwnerType() != auth.OwnerTypeDeployment || proj.PrimaryDeploymentID == nil || *proj.PrimaryDeploymentID != claims.OwnerID() { | ||
| return nil, nil, status.Error(codes.PermissionDenied, "only the project's primary deployment can act for a user") | ||
| } | ||
|
|
||
| // Only users, not services, magic tokens or embed users, own the resources this creates. | ||
| if _, err := uuid.Parse(userID); err != nil { | ||
| return nil, nil, status.Error(codes.PermissionDenied, "can only act for a user") | ||
| } | ||
| user, err := s.admin.DB.FindUser(ctx, userID) | ||
| if err != nil { | ||
| if errors.Is(err, database.ErrNotFound) { | ||
| return nil, nil, status.Error(codes.PermissionDenied, "can only act for a user") | ||
| } | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| orgPerms, err := s.admin.OrganizationPermissionsForUser(ctx, proj.OrganizationID, user.ID) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| projPerms, err := s.admin.ProjectPermissionsForUser(ctx, proj.ID, user.ID, orgPerms) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| // Like the user's own requests, requests for a user who lost access to the project are rejected, including deletes of their own resources. | ||
| if !projPerms.ReadProd { | ||
| return nil, nil, status.Error(codes.PermissionDenied, "user does not have access to the project") | ||
| } | ||
| return user, projPerms, nil | ||
| } | ||
|
|
||
| // slackDirectMessageDestination matches the destinations other than channels that chat.postMessage takes as a channel: | ||
| // "@name", and user and direct message IDs, which are upper case, while channel names are lower case. | ||
| var slackDirectMessageDestination = regexp.MustCompile(`^@|^[UWD][A-Z0-9]+$`) | ||
|
|
||
| // recipientsForUser checks the recipients of an alert or report that the AI creates for a user, and returns its email recipients. | ||
| // The AI can be steered by the data and tools it reads, so what it creates can only reach the project's members and its Slack workspace: | ||
| // the email recipients default to the user when there are no other recipients, other email recipients and Slack users must be members of the project, | ||
| // Slack channels are allowed, since they're in the Slack workspace the project's admin configured, but not direct messages to other people, | ||
| // and Slack webhooks, which can be any URL, are rejected. | ||
| func (s *Server) recipientsForUser(ctx context.Context, proj *database.Project, user *database.User, emails, slackUsers, slackChannels, slackWebhooks []string) ([]string, error) { | ||
| if len(slackWebhooks) > 0 { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Partly fixed in 7f91a46: |
||
| return nil, status.Error(codes.InvalidArgument, "the AI can't send to Slack webhooks, since they can be any URL") | ||
| } | ||
| for _, channel := range slackChannels { | ||
| if slackDirectMessageDestination.MatchString(strings.TrimPrefix(channel, "#")) { | ||
| return nil, status.Errorf(codes.InvalidArgument, "%q is not a Slack channel: the AI can only send to people as Slack users, who must be members of the project", channel) | ||
| } | ||
| } | ||
| if len(emails) == 0 && len(slackUsers) == 0 && len(slackChannels) == 0 { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A Slack-only alert or report can now be created in a project with no Slack setup, with no email fallback. The user gets a success response and delivery fails later in
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return []string{user.Email}, nil | ||
| } | ||
|
|
||
| for _, recipient := range slices.Concat(emails, slackUsers) { | ||
| if strings.EqualFold(recipient, user.Email) { | ||
| continue | ||
| } | ||
| _, memberID, err := s.getAttributesForProjectMember(ctx, recipient, proj.OrganizationID, proj.ID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if memberID == "" { | ||
| return nil, status.Errorf(codes.InvalidArgument, "%s is not a member of the project, and the AI can only send to project members", recipient) | ||
| } | ||
| } | ||
| return emails, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we check
projPerms.ReadProdhere? The regularDeleteAlertandDeleteReportendpoints both check current production access before checking ownership. The new delete handlers only check ownership, so their behavior diverges after a user's project access is revoked.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8883105:
userForPrimaryDeploymentnow rejects users withoutReadProd, so all four RPCs, deletes included, match the regular endpoints; a new subtest covers a user removed from the project.