-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restrict access to all tables by default
for #10
- Loading branch information
Showing
5 changed files
with
116 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSecurityNegativeCases(t *testing.T) { | ||
t.Run("Unauthorized", func(t *testing.T) { | ||
tc := createTestContextWithHMACTokenAuth(t) | ||
defer tc.CleanUp(t) | ||
|
||
tc.authToken = "" // disable auth | ||
client := tc.Client() | ||
_, _, err := client.From("test").Select("id", "", false).Execute() | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Unauthorized") | ||
}) | ||
|
||
t.Run("TableAccessRestricted", func(t *testing.T) { | ||
tc := createTestContextWithHMACTokenAuth(t) | ||
defer tc.CleanUp(t) | ||
|
||
client := tc.Client() | ||
_, _, err := client.From(tableNameMigrations).Select("id", "", false).Execute() | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Access Restricted") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// TODO: generally speaking, we need a fine-grained RBAC system. | ||
|
||
type ServerSecurityOptions struct { | ||
// EnabledTableOrViews list of table or view names that are accessible (read & write). | ||
EnabledTableOrViews []string | ||
} | ||
|
||
func (opts *ServerSecurityOptions) bindCLIFlags(fs *pflag.FlagSet) { | ||
fs.StringSliceVar( | ||
&opts.EnabledTableOrViews, | ||
"--security-allow-table", | ||
[]string{}, | ||
"list of table or view names that are accessible (read & write)", | ||
) | ||
} | ||
|
||
func (opts *ServerSecurityOptions) defaults() error { | ||
return nil | ||
} | ||
|
||
func (opts *ServerSecurityOptions) createTableOrViewAccessCheckMiddleware( | ||
responseErr func(w http.ResponseWriter, err error), | ||
routeVarTableOrView string, | ||
) func(http.Handler) http.Handler { | ||
accesibleTableOrViews := make(map[string]struct{}) | ||
for _, t := range opts.EnabledTableOrViews { | ||
accesibleTableOrViews[t] = struct{}{} | ||
} | ||
fmt.Println(accesibleTableOrViews) | ||
|
||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
target := chi.URLParam(req, routeVarTableOrView) | ||
|
||
if _, ok := accesibleTableOrViews[target]; !ok { | ||
responseErr(w, ErrAccessRestricted) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, req) | ||
}) | ||
} | ||
} |