-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add authorization for the API layer
Signed-off-by: Donnie Adams <[email protected]>
- Loading branch information
Showing
17 changed files
with
459 additions
and
362 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package authn | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/otto8-ai/otto8/pkg/api/authz" | ||
"k8s.io/apiserver/pkg/authentication/authenticator" | ||
"k8s.io/apiserver/pkg/authentication/user" | ||
) | ||
|
||
type Anonymous struct { | ||
} | ||
|
||
func (n Anonymous) AuthenticateRequest(*http.Request) (*authenticator.Response, bool, error) { | ||
return &authenticator.Response{ | ||
User: &user.DefaultInfo{ | ||
UID: "anonymous", | ||
Name: "anonymous", | ||
Groups: []string{authz.UnauthenticatedGroup}, | ||
}, | ||
}, true, nil | ||
} |
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,29 @@ | ||
package authn | ||
|
||
import ( | ||
"net/http" | ||
|
||
"k8s.io/apiserver/pkg/authentication/authenticator" | ||
"k8s.io/apiserver/pkg/authentication/user" | ||
) | ||
|
||
type Authenticator struct { | ||
authenticator authenticator.Request | ||
} | ||
|
||
func NewAuthenticator(authenticator authenticator.Request) *Authenticator { | ||
return &Authenticator{ | ||
authenticator: authenticator, | ||
} | ||
} | ||
|
||
func (a *Authenticator) Authenticate(req *http.Request) (user.Info, error) { | ||
resp, ok, err := a.authenticator.AuthenticateRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !ok { | ||
panic("authentication should always succeed") | ||
} | ||
return resp.User, nil | ||
} |
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,21 @@ | ||
package authn | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/otto8-ai/otto8/pkg/api/authz" | ||
"k8s.io/apiserver/pkg/authentication/authenticator" | ||
"k8s.io/apiserver/pkg/authentication/user" | ||
) | ||
|
||
type NoAuth struct { | ||
} | ||
|
||
func (n NoAuth) AuthenticateRequest(*http.Request) (*authenticator.Response, bool, error) { | ||
return &authenticator.Response{ | ||
User: &user.DefaultInfo{ | ||
Name: "nobody", | ||
Groups: []string{authz.AdminGroup, authz.AuthenticatedGroup}, | ||
}, | ||
}, true, nil | ||
} |
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,92 @@ | ||
package authz | ||
|
||
import ( | ||
"net/http" | ||
"slices" | ||
|
||
"k8s.io/apiserver/pkg/authentication/user" | ||
) | ||
|
||
const ( | ||
AdminGroup = "admin" | ||
AuthenticatedGroup = "authenticated" | ||
UnauthenticatedGroup = "unauthenticated" | ||
|
||
// anyGroup is an internal group that allows access to any group | ||
anyGroup = "*" | ||
) | ||
|
||
type Authorizer struct { | ||
rules []rule | ||
} | ||
|
||
func NewAuthorizer() *Authorizer { | ||
return &Authorizer{ | ||
rules: defaultRules(), | ||
} | ||
} | ||
|
||
func (a *Authorizer) Authorize(req *http.Request, user user.Info) bool { | ||
userGroups := user.GetGroups() | ||
for _, r := range a.rules { | ||
if r.group == anyGroup || slices.Contains(userGroups, r.group) { | ||
if _, pattern := r.mux.Handler(req); pattern != "" { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
type rule struct { | ||
group string | ||
mux *http.ServeMux | ||
} | ||
|
||
func defaultRules() []rule { | ||
var ( | ||
rules []rule | ||
f = (*fake)(nil) | ||
) | ||
|
||
// Build admin mux, admins can assess any URL | ||
adminMux := http.NewServeMux() | ||
adminMux.Handle("/", f) | ||
|
||
rules = append(rules, rule{ | ||
group: AdminGroup, | ||
mux: adminMux, | ||
}) | ||
|
||
// Build mux that anyone can access | ||
anyMux := http.NewServeMux() | ||
anyMux.Handle("POST /api/webhooks/{id}", f) | ||
|
||
anyMux.Handle("GET /api/token-request/{id}", f) | ||
anyMux.Handle("POST /api/token-request", f) | ||
anyMux.Handle("GET /api/token-request/{id}/{service}", f) | ||
|
||
anyMux.Handle("GET /api/auth-providers", f) | ||
anyMux.Handle("GET /api/auth-providers/{slug}", f) | ||
|
||
anyMux.Handle("GET /api/oauth/start/{id}/{service}", f) | ||
anyMux.Handle("/api/oauth/redirect/{service}", f) | ||
|
||
anyMux.Handle("GET /api/app-oauth/authorize/{id}", f) | ||
anyMux.Handle("GET /api/app-oauth/refresh/{id}", f) | ||
anyMux.Handle("GET /api/app-oauth/callback/{id}", f) | ||
anyMux.Handle("GET /api/app-oauth/get-token", f) | ||
|
||
rules = append(rules, rule{ | ||
group: anyGroup, | ||
mux: anyMux, | ||
}) | ||
|
||
return rules | ||
} | ||
|
||
// fake is a fake handler that does nothing | ||
type fake struct{} | ||
|
||
func (f *fake) ServeHTTP(http.ResponseWriter, *http.Request) {} |
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
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
Oops, something went wrong.