-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom authentication and authorization implementations to avoid …
…spawning new processes (#254) Purpose To avoid spawning new processes for requests received by docker auth endpoint. To allow developers to add their own plugins to docker auth. Approach Add new custom authentication and authorization implementations by implementing the existing authentication and authorization interfaces. Therefore the developers can add their own plugins with their program logics. Test environment go version: go1.12.5 darwin/amd64 OS: Mac OS 10.14.5
- Loading branch information
1 parent
b89dec9
commit bfb1517
Showing
5 changed files
with
215 additions
and
13 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,81 @@ | ||
/* | ||
Copyright 2019 Cesanta Software Ltd. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package authn | ||
|
||
import ( | ||
"fmt" | ||
"plugin" | ||
|
||
"github.com/cesanta/glog" | ||
) | ||
|
||
type PluginAuthnConfig struct { | ||
PluginPath string `yaml:"plugin_path"` | ||
} | ||
|
||
func lookupSymbol(cfg *PluginAuthnConfig) (Authenticator, error) { | ||
// load module | ||
plug, err := plugin.Open(cfg.PluginPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while loading authn plugin: %v", err) | ||
} | ||
|
||
// look up for Authn | ||
symAuthen, err := plug.Lookup("Authn") | ||
if err != nil { | ||
return nil, fmt.Errorf("error while loading authn exporting the variable: %v", err) | ||
} | ||
|
||
// assert that loaded symbol is of a desired type | ||
var authn Authenticator | ||
authn, ok := symAuthen.(Authenticator) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected type from module symbol. Unable to cast Authn module") | ||
} | ||
return authn, nil | ||
} | ||
|
||
func (c *PluginAuthnConfig) Validate() error { | ||
_, err := lookupSymbol(c) | ||
return err | ||
} | ||
|
||
type PluginAuthn struct { | ||
cfg *PluginAuthnConfig | ||
Authn Authenticator | ||
} | ||
|
||
func (c *PluginAuthn) Authenticate(user string, password PasswordString) (bool, Labels, error) { | ||
// use the plugin | ||
return c.Authn.Authenticate(user, password) | ||
} | ||
|
||
func (c *PluginAuthn) Stop() { | ||
} | ||
|
||
func (c *PluginAuthn) Name() string { | ||
return "plugin auth" | ||
} | ||
|
||
func NewPluginAuthn(cfg *PluginAuthnConfig) (*PluginAuthn, error) { | ||
glog.Infof("Plugin authenticator: %s", cfg) | ||
authn, err := lookupSymbol(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PluginAuthn{Authn: authn}, 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,81 @@ | ||
/* | ||
Copyright 2019 Cesanta Software Ltd. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package authz | ||
|
||
import ( | ||
"fmt" | ||
"plugin" | ||
|
||
"github.com/cesanta/glog" | ||
) | ||
|
||
type PluginAuthzConfig struct { | ||
PluginPath string `yaml:"plugin_path"` | ||
} | ||
|
||
func lookupSymbol(cfg *PluginAuthzConfig) (Authorizer, error) { | ||
// load module | ||
plug, err := plugin.Open(cfg.PluginPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error while loading authz plugin: %v", err) | ||
} | ||
|
||
// look up for Authz | ||
symAuthen, err := plug.Lookup("Authz") | ||
if err != nil { | ||
return nil, fmt.Errorf("error while loading authz exporting the variable: %v", err) | ||
} | ||
|
||
// assert that loaded symbol is of a desired type | ||
var authz Authorizer | ||
authz, ok := symAuthen.(Authorizer) | ||
if !ok { | ||
return nil, fmt.Errorf("unexpected type from module symbol. Unable to cast Authz module") | ||
} | ||
return authz, nil | ||
} | ||
|
||
func (c *PluginAuthzConfig) Validate() error { | ||
_, err := lookupSymbol(c) | ||
return err | ||
} | ||
|
||
type PluginAuthz struct { | ||
cfg *PluginAuthzConfig | ||
Authz Authorizer | ||
} | ||
|
||
func (c *PluginAuthz) Stop() { | ||
} | ||
|
||
func (c *PluginAuthz) Name() string { | ||
return "plugin authz" | ||
} | ||
|
||
func NewPluginAuthzAuthorizer(cfg *PluginAuthzConfig) (*PluginAuthz, error) { | ||
glog.Infof("Plugin authorization: %s", cfg) | ||
authz, err := lookupSymbol(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PluginAuthz{Authz: authz}, nil | ||
} | ||
|
||
func (c *PluginAuthz) Authorize(ai *AuthRequestInfo) ([]string, error) { | ||
// use the plugin | ||
return c.Authz.Authorize(ai) | ||
} |
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