Skip to content

Commit 1f37ca1

Browse files
committed
Cleanup some code
1 parent e6696cc commit 1f37ca1

File tree

12 files changed

+51
-145
lines changed

12 files changed

+51
-145
lines changed

coderd/authorize.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66

77
"github.com/google/uuid"
8-
98
"golang.org/x/xerrors"
109

1110
"cdr.dev/slog"

coderd/coderdtest/authorize.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import (
99
"strings"
1010
"testing"
1111

12-
"github.com/coder/coder/coderd/rbac/regosql"
13-
1412
"github.com/go-chi/chi/v5"
1513
"github.com/stretchr/testify/assert"
1614
"github.com/stretchr/testify/require"
1715
"golang.org/x/xerrors"
1816

1917
"github.com/coder/coder/coderd"
2018
"github.com/coder/coder/coderd/rbac"
19+
"github.com/coder/coder/coderd/rbac/regosql"
2120
"github.com/coder/coder/codersdk"
2221
"github.com/coder/coder/provisioner/echo"
2322
"github.com/coder/coder/provisionersdk/proto"

coderd/database/modelqueries.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/coder/coder/coderd/rbac/regosql"
9-
8+
"github.com/google/uuid"
109
"github.com/lib/pq"
10+
"golang.org/x/xerrors"
1111

1212
"github.com/coder/coder/coderd/rbac"
13+
"github.com/coder/coder/coderd/rbac/regosql"
14+
)
1315

14-
"github.com/google/uuid"
15-
"golang.org/x/xerrors"
16+
const (
17+
authorizedQueryPlaceholder = "-- @authorize_filter"
1618
)
1719

1820
// customQuerier encompasses all non-generated queries.
@@ -38,9 +40,13 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
3840
return nil, xerrors.Errorf("compile authorized filter: %w", err)
3941
}
4042

41-
filter := strings.Replace(getTemplatesWithFilter, "-- @authorize_filter", fmt.Sprintf(" AND %s", authorizedFilter), 1)
43+
filtered, err := insertAuthorizedFilter(getTemplatesWithFilter, fmt.Sprintf(" AND %s", authorizedFilter))
44+
if err != nil {
45+
return nil, xerrors.Errorf("insert authorized filter: %w", err)
46+
}
47+
4248
// The name comment is for metric tracking
43-
query := fmt.Sprintf("-- name: GetAuthorizedTemplates :many\n%s", filter)
49+
query := fmt.Sprintf("-- name: GetAuthorizedTemplates :many\n%s", filtered)
4450
rows, err := q.db.QueryContext(ctx, query,
4551
arg.Deleted,
4652
arg.OrganizationID,
@@ -183,9 +189,13 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
183189

184190
// In order to properly use ORDER BY, OFFSET, and LIMIT, we need to inject the
185191
// authorizedFilter between the end of the where clause and those statements.
186-
filter := strings.Replace(getWorkspaces, "-- @authorize_filter", fmt.Sprintf(" AND %s", authorizedFilter), 1)
192+
filtered, err := insertAuthorizedFilter(getWorkspaces, fmt.Sprintf(" AND %s", authorizedFilter))
193+
if err != nil {
194+
return nil, xerrors.Errorf("insert authorized filter: %w", err)
195+
}
196+
187197
// The name comment is for metric tracking
188-
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s", filter)
198+
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s", filtered)
189199
rows, err := q.db.QueryContext(ctx, query,
190200
arg.Deleted,
191201
arg.Status,
@@ -241,8 +251,12 @@ func (q *sqlQuerier) GetAuthorizedUserCount(ctx context.Context, arg GetFiltered
241251
return -1, xerrors.Errorf("compile authorized filter: %w", err)
242252
}
243253

244-
filter := strings.Replace(getFilteredUserCount, "-- @authorize_filter", fmt.Sprintf(" AND %s", authorizedFilter), 1)
245-
query := fmt.Sprintf("-- name: GetAuthorizedUserCount :one\n%s", filter)
254+
filtered, err := insertAuthorizedFilter(getFilteredUserCount, fmt.Sprintf(" AND %s", authorizedFilter))
255+
if err != nil {
256+
return -1, xerrors.Errorf("insert authorized filter: %w", err)
257+
}
258+
259+
query := fmt.Sprintf("-- name: GetAuthorizedUserCount :one\n%s", filtered)
246260
row := q.db.QueryRowContext(ctx, query,
247261
arg.Deleted,
248262
arg.Search,
@@ -253,3 +267,11 @@ func (q *sqlQuerier) GetAuthorizedUserCount(ctx context.Context, arg GetFiltered
253267
err = row.Scan(&count)
254268
return count, err
255269
}
270+
271+
func insertAuthorizedFilter(query string, replaceWith string) (string, error) {
272+
if !strings.Contains(query, authorizedQueryPlaceholder) {
273+
return "", xerrors.Errorf("query does not contain authorized replace string, this is not an authorized query")
274+
}
275+
filtered := strings.Replace(query, authorizedQueryPlaceholder, replaceWith, 1)
276+
return filtered, nil
277+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package database
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestIsAuthorizedQuery(t *testing.T) {
10+
t.Parallel()
11+
12+
query := `SELECT true;`
13+
_, err := insertAuthorizedFilter(query, "")
14+
require.ErrorContains(t, err, "does not contain authorized replace string", "ensure replace string")
15+
}

coderd/rbac/authz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package rbac
33
import (
44
"context"
55
_ "embed"
6-
"github.com/coder/coder/coderd/rbac/regosql"
76
"sync"
87

98
"github.com/open-policy-agent/opa/rego"
109
"go.opentelemetry.io/otel/attribute"
1110
"go.opentelemetry.io/otel/trace"
1211
"golang.org/x/xerrors"
1312

13+
"github.com/coder/coder/coderd/rbac/regosql"
1414
"github.com/coder/coder/coderd/tracing"
1515
)
1616

coderd/rbac/input.json

Lines changed: 0 additions & 120 deletions
This file was deleted.

coderd/rbac/input2.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

coderd/rbac/memprofile.out

-25.4 KB
Binary file not shown.

coderd/rbac/partial.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package rbac
33
import (
44
"context"
55

6-
"github.com/coder/coder/coderd/rbac/regosql"
7-
"golang.org/x/xerrors"
8-
96
"github.com/open-policy-agent/opa/ast"
107
"github.com/open-policy-agent/opa/rego"
8+
"golang.org/x/xerrors"
119

10+
"github.com/coder/coder/coderd/rbac/regosql"
1211
"github.com/coder/coder/coderd/tracing"
1312
)
1413

coderd/rbac/partial.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

coderd/rbac/profile.out

-46.5 KB
Binary file not shown.

coderd/rbac/rbac.test

-24 MB
Binary file not shown.

0 commit comments

Comments
 (0)