This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Josh Kim <[email protected]>
- Loading branch information
Showing
8 changed files
with
318 additions
and
6 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
127 changes: 127 additions & 0 deletions
127
plank/pkg/server/auth_provider_manager/auth_provider_manager.go
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,127 @@ | ||
package auth_provider_manager | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"sync" | ||
) | ||
|
||
type AuthProviderType int | ||
|
||
const ( | ||
STOMPAuthProviderType AuthProviderType = iota | ||
RESTAuthProviderType | ||
|
||
AUTH_PROVIDER_MANAGER_STORE = "AuthProviderManager" | ||
AUTH_PROVIDER_MANAGER_STORE_INIT_EVENT = "INIT" | ||
AUTH_PROVIDER_MANAGER_INSTANCE_KEY = "instance" | ||
) | ||
|
||
var authProviderMgrInstance AuthProviderManager | ||
|
||
type AuthProviderManager interface { | ||
GetRESTAuthProvider(uri string) (*RESTAuthProvider, error) | ||
GetSTOMPAuthProvider() (*STOMPAuthProvider, error) | ||
SetRESTAuthProvider(regex *regexp.Regexp, provider *RESTAuthProvider) int | ||
SetSTOMPAuthProvider(provider *STOMPAuthProvider) error | ||
DeleteRESTAuthProvider(idx int) error | ||
DeleteSTOMPAuthProvider() error | ||
} | ||
|
||
type regexpRESTAuthProviderPair struct { | ||
regexp *regexp.Regexp | ||
restAuthProvider *RESTAuthProvider | ||
} | ||
|
||
type authProviderManager struct { | ||
stompAuthProvider *STOMPAuthProvider | ||
uriPatternRestAuthProviderPairs []*regexpRESTAuthProviderPair | ||
mu sync.Mutex | ||
} | ||
|
||
type AuthProviderNotFoundError struct {} | ||
|
||
func (e *AuthProviderNotFoundError) Error() string { | ||
return fmt.Sprintf("no auth provider was found at the given location/name") | ||
} | ||
|
||
type AuthError struct { | ||
Code int | ||
Message string | ||
} | ||
func (e *AuthError) Error() string { | ||
return fmt.Sprintf("authentication/authorization error (%d): %s", e.Code, e.Message) | ||
} | ||
|
||
func (a *authProviderManager) GetRESTAuthProvider(uri string) (*RESTAuthProvider, error) { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
|
||
// perform regex tests to find the first matching REST auth provider | ||
for _, pair := range a.uriPatternRestAuthProviderPairs { | ||
if pair.regexp.Match([]byte(uri)) { | ||
return pair.restAuthProvider, nil | ||
} | ||
} | ||
return nil, &AuthProviderNotFoundError{} | ||
} | ||
|
||
func (a *authProviderManager) GetSTOMPAuthProvider() (*STOMPAuthProvider, error) { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
if a.stompAuthProvider == nil { | ||
return nil, &AuthProviderNotFoundError{} | ||
} | ||
return a.stompAuthProvider, nil | ||
} | ||
|
||
func (a *authProviderManager) SetRESTAuthProvider(regex *regexp.Regexp, provider *RESTAuthProvider) int { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
a.uriPatternRestAuthProviderPairs = append(a.uriPatternRestAuthProviderPairs, ®expRESTAuthProviderPair{ | ||
regexp: regex, | ||
restAuthProvider: provider, | ||
}) | ||
|
||
return len(a.uriPatternRestAuthProviderPairs)-1 | ||
} | ||
|
||
func (a *authProviderManager) SetSTOMPAuthProvider(provider *STOMPAuthProvider) error { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
a.stompAuthProvider = provider | ||
return nil | ||
} | ||
|
||
func (a *authProviderManager) DeleteRESTAuthProvider(idx int) error { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
if idx < 0 || idx > len(a.uriPatternRestAuthProviderPairs)-1 { | ||
return fmt.Errorf("no REST auth provider exists at index %d", idx) | ||
} | ||
borderLeft := idx | ||
borderRight := idx+1 | ||
if idx > 0 { | ||
borderLeft-- | ||
} | ||
a.uriPatternRestAuthProviderPairs = append(a.uriPatternRestAuthProviderPairs[:borderLeft], a.uriPatternRestAuthProviderPairs[borderRight:]...) | ||
return nil | ||
} | ||
|
||
func (a *authProviderManager) DeleteSTOMPAuthProvider() error { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
a.stompAuthProvider = nil | ||
return nil | ||
} | ||
|
||
func GetAuthProviderManager() AuthProviderManager { | ||
if authProviderMgrInstance == nil { | ||
authProviderMgrInstance = &authProviderManager{} | ||
} | ||
return authProviderMgrInstance | ||
} | ||
|
||
func DestroyAuthProviderManager() { | ||
authProviderMgrInstance = nil | ||
} |
69 changes: 69 additions & 0 deletions
69
plank/pkg/server/auth_provider_manager/rest_auth_provider.go
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,69 @@ | ||
package auth_provider_manager | ||
|
||
import ( | ||
"github.com/vmware/transport-go/plank/utils" | ||
"net/http" | ||
"sort" | ||
"sync" | ||
) | ||
|
||
type HttpRequestValidatorFn func(request *http.Request) *AuthError | ||
|
||
type httpRequestValidatorRule struct { | ||
name string | ||
priority int | ||
validatorFn HttpRequestValidatorFn | ||
} | ||
|
||
type RESTAuthProvider struct { | ||
rules map[string]*httpRequestValidatorRule | ||
rulesByPriority []*httpRequestValidatorRule | ||
mu sync.Mutex | ||
} | ||
|
||
func (ap *RESTAuthProvider) Validate(request *http.Request) *AuthError { | ||
ap.mu.Lock() | ||
defer ap.mu.Unlock() | ||
defer func() { | ||
if r := recover(); r != nil { | ||
utils.Log.Errorln(r) | ||
} | ||
}() | ||
|
||
for _, rule := range ap.rulesByPriority { | ||
err := rule.validatorFn(request) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ap *RESTAuthProvider) AddRule(name string, priority int, validatorFn HttpRequestValidatorFn) { | ||
ap.mu.Lock() | ||
defer ap.mu.Unlock() | ||
|
||
rule := &httpRequestValidatorRule{ | ||
name: name, | ||
priority: priority, | ||
validatorFn: validatorFn, | ||
} | ||
ap.rules[name] = rule | ||
ap.rulesByPriority = append(ap.rulesByPriority, rule) | ||
sort.SliceStable(ap.rulesByPriority, func(i, j int) bool { | ||
return ap.rulesByPriority[i].priority < ap.rulesByPriority[j].priority | ||
}) | ||
} | ||
|
||
func (ap *RESTAuthProvider) Reset() { | ||
ap.mu.Lock() | ||
defer ap.mu.Unlock() | ||
ap.rules = make(map[string]*httpRequestValidatorRule) | ||
ap.rulesByPriority = make([]*httpRequestValidatorRule, 0) | ||
} | ||
|
||
func NewRESTAuthProvider() *RESTAuthProvider { | ||
ap := &RESTAuthProvider{} | ||
ap.Reset() | ||
return ap | ||
} |
77 changes: 77 additions & 0 deletions
77
plank/pkg/server/auth_provider_manager/stomp_auth_provider.go
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,77 @@ | ||
package auth_provider_manager | ||
|
||
import ( | ||
"github.com/go-stomp/stomp/frame" | ||
"github.com/vmware/transport-go/plank/utils" | ||
"sort" | ||
"sync" | ||
) | ||
|
||
type StompFrameValidatorFn func(fr *frame.Frame) *AuthError | ||
|
||
type stompFrameValidatorRule struct { | ||
msgFrameType string | ||
priority int | ||
validatorFn StompFrameValidatorFn | ||
} | ||
|
||
type STOMPAuthProvider struct { | ||
rules map[string][]*stompFrameValidatorRule | ||
mu sync.Mutex | ||
} | ||
|
||
func (ap *STOMPAuthProvider) Validate(fr *frame.Frame) error { | ||
ap.mu.Lock() | ||
defer ap.mu.Unlock() | ||
defer func() { | ||
if r := recover(); r != nil { | ||
utils.Log.Errorln(r) | ||
} | ||
}() | ||
|
||
rules, found := ap.rules[fr.Command] | ||
// if no rule was found let the request pass through | ||
if !found { | ||
return nil | ||
} | ||
|
||
for _, rule := range rules { | ||
err := rule.validatorFn(fr) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (ap *STOMPAuthProvider) AddRule(types []string, priority int, validatorFn StompFrameValidatorFn) { | ||
ap.mu.Lock() | ||
defer ap.mu.Unlock() | ||
|
||
for _, typ := range types { | ||
rule := &stompFrameValidatorRule{ | ||
msgFrameType: typ, | ||
priority: priority, | ||
validatorFn: validatorFn, | ||
} | ||
if _, ok := ap.rules[typ]; !ok { | ||
ap.rules[typ] = make([]*stompFrameValidatorRule, 0) | ||
} | ||
ap.rules[typ] = append(ap.rules[typ], rule) | ||
sort.SliceStable(ap.rules[typ], func(i, j int) bool { | ||
return ap.rules[typ][i].priority < ap.rules[typ][j].priority | ||
}) | ||
} | ||
} | ||
|
||
func (ap *STOMPAuthProvider) Reset() { | ||
ap.mu.Lock() | ||
defer ap.mu.Unlock() | ||
ap.rules = make(map[string][]*stompFrameValidatorRule) | ||
} | ||
|
||
func NewSTOMPAuthProvider() *STOMPAuthProvider { | ||
return &STOMPAuthProvider{ | ||
rules: make(map[string][]*stompFrameValidatorRule), | ||
} | ||
} |
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.