-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,007 additions
and
62 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,5 @@ | ||
Enhancement: Add support for runtime plugins | ||
|
||
This PR introduces a new plugin package, that allows loading external plugins into Reva at runtime. The hashicorp go-plugin framework was used to facilitate the plugin loading and communication. | ||
|
||
https://github.com/cs3org/reva/pull/1861 |
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,162 @@ | ||
// Copyright 2018-2021 CERN | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"strings" | ||
|
||
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
"github.com/cs3org/reva/pkg/errtypes" | ||
"github.com/cs3org/reva/pkg/user" | ||
"github.com/hashicorp/go-plugin" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// Manager is a real implementation of Manager interface. | ||
type Manager struct { | ||
users []*userpb.User | ||
} | ||
|
||
type config struct { | ||
Users string `mapstructure:"users"` | ||
} | ||
|
||
func (c *config) init() { | ||
if c.Users == "" { | ||
c.Users = "/etc/revad/users.json" | ||
} | ||
} | ||
|
||
func parseConfig(m map[string]interface{}) (*config, error) { | ||
c := &config{} | ||
if err := mapstructure.Decode(m, c); err != nil { | ||
return nil, err | ||
} | ||
c.init() | ||
return c, nil | ||
} | ||
|
||
// Configure initializes the manager struct based on the configurations. | ||
func (m *Manager) Configure(ml map[string]interface{}) error { | ||
c, err := parseConfig(ml) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := ioutil.ReadFile(c.Users) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
users := []*userpb.User{} | ||
|
||
err = json.Unmarshal(f, &users) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m.users = users | ||
|
||
return nil | ||
} | ||
|
||
// GetUser returns the user based on the uid. | ||
func (m *Manager) GetUser(ctx context.Context, uid *userpb.UserId) (*userpb.User, error) { | ||
for _, u := range m.users { | ||
if (u.Id.GetOpaqueId() == uid.OpaqueId || u.Username == uid.OpaqueId) && (uid.Idp == "" || uid.Idp == u.Id.GetIdp()) { | ||
return u, nil | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
// GetUserByClaim returns user based on the claim | ||
func (m *Manager) GetUserByClaim(ctx context.Context, claim, value string) (*userpb.User, error) { | ||
for _, u := range m.users { | ||
if userClaim, err := extractClaim(u, claim); err == nil && value == userClaim { | ||
return u, nil | ||
} | ||
} | ||
return nil, errtypes.NotFound(value) | ||
} | ||
|
||
func extractClaim(u *userpb.User, claim string) (string, error) { | ||
switch claim { | ||
case "mail": | ||
return u.Mail, nil | ||
case "username": | ||
return u.Username, nil | ||
case "uid": | ||
if u.Opaque != nil && u.Opaque.Map != nil { | ||
if uidObj, ok := u.Opaque.Map["uid"]; ok { | ||
if uidObj.Decoder == "plain" { | ||
return string(uidObj.Value), nil | ||
} | ||
} | ||
} | ||
} | ||
return "", errors.New("json: invalid field") | ||
} | ||
|
||
// TODO(jfd) search Opaque? compare sub? | ||
func userContains(u *userpb.User, query string) bool { | ||
query = strings.ToLower(query) | ||
return strings.Contains(strings.ToLower(u.Username), query) || strings.Contains(strings.ToLower(u.DisplayName), query) || | ||
strings.Contains(strings.ToLower(u.Mail), query) || strings.Contains(strings.ToLower(u.Id.OpaqueId), query) | ||
} | ||
|
||
// FindUsers returns the user based on the query | ||
func (m *Manager) FindUsers(ctx context.Context, query string) ([]*userpb.User, error) { | ||
users := []*userpb.User{} | ||
for _, u := range m.users { | ||
if userContains(u, query) { | ||
users = append(users, u) | ||
} | ||
} | ||
return users, nil | ||
} | ||
|
||
// GetUserGroups returns the user groups | ||
func (m *Manager) GetUserGroups(ctx context.Context, uid *userpb.UserId) ([]string, error) { | ||
user, err := m.GetUser(ctx, uid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return user.Groups, nil | ||
} | ||
|
||
// Handshake hashicorp go-plugin handshake | ||
var Handshake = plugin.HandshakeConfig{ | ||
ProtocolVersion: 1, | ||
MagicCookieKey: "BASIC_PLUGIN", | ||
MagicCookieValue: "hello", | ||
} | ||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeConfig{ | ||
HandshakeConfig: Handshake, | ||
Plugins: map[string]plugin.Plugin{ | ||
"userprovider": &user.ProviderPlugin{Impl: &Manager{}}, | ||
}, | ||
}) | ||
} |
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,15 @@ | ||
[shared] | ||
gatewaysvc = "localhost:19000" | ||
plugin = true | ||
|
||
[grpc] | ||
address = "0.0.0.0:19000" | ||
|
||
[grpc.services.gateway] | ||
userprovidersvc = "localhost:19000" | ||
|
||
[grpc.services.userprovider] | ||
driver = "./json" | ||
|
||
[grpc.services.userprovider.drivers.json] | ||
users = "users.demo.json" |
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,102 @@ | ||
[ | ||
{ | ||
"id": { | ||
"opaque_id": "4c510ada-c86b-4815-8820-42cdf82c3d51", | ||
"idp": "cernbox.cern.ch" | ||
}, | ||
"username": "einstein", | ||
"secret": "relativity", | ||
"mail": "[email protected]", | ||
"display_name": "Albert Einstein", | ||
"groups": ["sailing-lovers", "violin-haters", "physics-lovers"], | ||
"opaque": { | ||
"map": { | ||
"gid": { | ||
"_comment": "decodes to 987", | ||
"decoder":"plain", | ||
"value":"OTg3" | ||
}, | ||
"uid":{ | ||
"_comment": "decodes to 123", | ||
"decoder":"plain", | ||
"value":"MTIz" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"id": { | ||
"opaque_id": "f7fbf8c8-139b-4376-b307-cf0a8c2d0d9c", | ||
"idp": "cesnet.cz" | ||
}, | ||
"username": "marie", | ||
"secret": "radioactivity", | ||
"mail": "[email protected]", | ||
"display_name": "Marie Curie", | ||
"groups": ["radium-lovers", "polonium-lovers", "physics-lovers"], | ||
"opaque": { | ||
"map": { | ||
"gid": { | ||
"_comment": "decodes to 987", | ||
"decoder":"plain", | ||
"value":"OTg3" | ||
}, | ||
"uid":{ | ||
"_comment": "decodes to 456", | ||
"decoder":"plain", | ||
"value":"NDU2" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"id": { | ||
"opaque_id": "932b4540-8d16-481e-8ef4-588e4b6b151c", | ||
"idp": "example.org" | ||
}, | ||
"username": "richard", | ||
"secret": "superfluidity", | ||
"mail": "[email protected]", | ||
"display_name": "Richard Feynman", | ||
"groups": ["quantum-lovers", "philosophy-haters", "physics-lovers"], | ||
"opaque": { | ||
"map": { | ||
"gid": { | ||
"_comment": "decodes to 135", | ||
"decoder":"plain", | ||
"value":"MTM1" | ||
}, | ||
"uid":{ | ||
"_comment": "decodes to 246", | ||
"decoder":"plain", | ||
"value":"MjQ2" | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"id": { | ||
"opaque_id": "932b4522-139b-4815-8ef4-42cdf82c3d51", | ||
"idp": "example.com" | ||
}, | ||
"username": "test", | ||
"secret": "test", | ||
"mail": "[email protected]", | ||
"display_name": "Test Testman", | ||
"groups": ["quantum-lovers", "philosophy-haters", "physics-lovers"], | ||
"opaque": { | ||
"map": { | ||
"gid": { | ||
"_comment": "decodes to 135", | ||
"decoder":"plain", | ||
"value":"MTM1" | ||
}, | ||
"uid":{ | ||
"_comment": "decodes to 468", | ||
"decoder":"plain", | ||
"value":"NDY4" | ||
} | ||
} | ||
} | ||
} | ||
] |
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.