Skip to content

feat: improve RBAC preconditions for Insights endpoint #8794

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

Merged
merged 8 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Address PR comments
  • Loading branch information
mtojek committed Jul 31, 2023
commit a5e01a82263846f3c318bdf0ee956488d530dfcc
8 changes: 2 additions & 6 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) {
})
if err != nil {
if httpapi.Is404Error(err) {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: "Template not found or forbidden access.",
})
httpapi.ResourceNotFound(rw)
return
}
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down Expand Up @@ -218,9 +216,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
return nil
}, nil)
if httpapi.Is404Error(err) {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: "Template not found or forbidden access.",
})
httpapi.ResourceNotFound(rw)
return
}
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -482,6 +483,9 @@ func TestTemplateInsights_RBAC(t *testing.T) {
TemplateIDs: templateIDs,
})
require.Error(t, err)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
})
})
}
Expand Down Expand Up @@ -582,6 +586,9 @@ func TestUserLatencyInsights_RBAC(t *testing.T) {
TemplateIDs: templateIDs,
})
require.Error(t, err)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
})
})
}
Expand Down