-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable fetching plugins from github repository #1970
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3f2c50d
This PR adds the following changes:
jimil749 f67c8cf
fix: update url check
jimil749 dc107e0
add comments
jimil749 2978c97
Updates:
jimil749 48f3310
add comment
jimil749 f575cbe
add err check in rpc_user
jimil749 28e9a1d
add license
jimil749 b0bd1e2
change cookie value to reva
jimil749 13ac310
update URL check and make handshake-config global
jimil749 a784864
add comments
jimil749 3bd3d6f
added github repo example
jimil749 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Enable fetching plugins from github | ||
|
||
This PR introduces new feature of fetching plugin from github | ||
|
||
https://github.com/cs3org/reva/pull/1970 |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,56 @@ | ||
// 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 plugin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" | ||
ctxpkg "github.com/cs3org/reva/pkg/ctx" | ||
) | ||
|
||
// Ctx represents context to be passed to the plugins | ||
type Ctx struct { | ||
User *userpb.User | ||
Token string | ||
} | ||
|
||
// GetContextStruct retrieves context KV pairs and stores it into Ctx | ||
func GetContextStruct(ctx context.Context) (*Ctx, error) { | ||
var ok bool | ||
ctxVal := &Ctx{} | ||
ctxVal.User, ok = ctxpkg.ContextGetUser(ctx) | ||
if !ok { | ||
return nil, fmt.Errorf("cannot get user context") | ||
} | ||
ctxVal.Token, ok = ctxpkg.ContextGetToken(ctx) | ||
if !ok { | ||
return nil, fmt.Errorf("cannot get token context") | ||
} | ||
return ctxVal, nil | ||
} | ||
|
||
// SetContext sets the context | ||
func SetContext(ctxStruct *Ctx) context.Context { | ||
ctx := context.Background() | ||
ctx = ctxpkg.ContextSetUser(ctx, ctxStruct.User) | ||
ctx = ctxpkg.ContextSetToken(ctx, ctxStruct.Token) | ||
return ctx | ||
} |
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 |
---|---|---|
|
@@ -20,13 +20,15 @@ package plugin | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
|
||
"github.com/cs3org/reva/pkg/errtypes" | ||
"github.com/hashicorp/go-getter" | ||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/go-plugin" | ||
) | ||
|
@@ -39,24 +41,27 @@ type RevaPlugin struct { | |
|
||
const dirname = "/var/tmp/reva" | ||
|
||
var isAlphaNum = regexp.MustCompile(`^[A-Za-z0-9]+$`).MatchString | ||
var isAlphaNum = regexp.MustCompile(`^[A-Za-z0-9_-]+$`).MatchString | ||
var isURL = regexp.MustCompile(`^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$`).MatchString | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added this check, works for any URL:
|
||
|
||
// Kill kills the plugin process | ||
func (plug *RevaPlugin) Kill() { | ||
plug.Client.Kill() | ||
} | ||
|
||
var handshake = plugin.HandshakeConfig{ | ||
// Handshake contains plugin Handshake configuration | ||
var Handshake = plugin.HandshakeConfig{ | ||
ProtocolVersion: 1, | ||
MagicCookieKey: "BASIC_PLUGIN", | ||
MagicCookieValue: "hello", | ||
MagicCookieValue: "reva", | ||
} | ||
|
||
func compile(pluginType string, path string) (string, error) { | ||
var errb bytes.Buffer | ||
binaryPath := filepath.Join(dirname, "bin", pluginType, filepath.Base(path)) | ||
command := fmt.Sprintf("go build -o %s %s", binaryPath, path) | ||
cmd := exec.Command("bash", "-c", command) | ||
cmd.Dir = path | ||
cmd.Stderr = &errb | ||
err := cmd.Run() | ||
if err != nil { | ||
|
@@ -65,7 +70,7 @@ func compile(pluginType string, path string) (string, error) { | |
return binaryPath, nil | ||
} | ||
|
||
// checkDir checks and compiles plugin if the configuration points to a directory. | ||
// checkDirAndCompile checks and compiles plugin if the configuration points to a directory. | ||
func checkDirAndCompile(pluginType, driver string) (string, error) { | ||
bin := driver | ||
file, err := os.Stat(driver) | ||
|
@@ -82,24 +87,65 @@ func checkDirAndCompile(pluginType, driver string) (string, error) { | |
return bin, nil | ||
} | ||
|
||
// Load loads the plugin using the hashicorp go-plugin system | ||
func Load(pluginType, driver string) (*RevaPlugin, error) { | ||
// downloadPlugin downloads the plugin and stores it into local filesystem | ||
func downloadAndCompilePlugin(pluginType, driver string) (string, error) { | ||
destination := fmt.Sprintf("%s/ext/%s/%s", dirname, pluginType, filepath.Base(driver)) | ||
client := &getter.Client{ | ||
Ctx: context.Background(), | ||
Dst: destination, | ||
Src: driver, | ||
Mode: getter.ClientModeDir, | ||
} | ||
if err := client.Get(); err != nil { | ||
return "", err | ||
} | ||
bin, err := compile(pluginType, destination) | ||
if err != nil { | ||
return "", nil | ||
} | ||
return bin, nil | ||
} | ||
|
||
// isValidURL tests a string to determine if it is a well-structure URL | ||
func isValidURL(driver string) bool { | ||
return isURL(driver) | ||
} | ||
|
||
func fetchBinary(pluginType, driver string) (string, error) { | ||
var bin string | ||
jimil749 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var err error | ||
if isAlphaNum(driver) { | ||
jimil749 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, errtypes.NotFound(driver) | ||
return "", errtypes.NotFound(driver) | ||
} | ||
|
||
if isValidURL(driver) { | ||
bin, err = downloadAndCompilePlugin(pluginType, driver) | ||
if err != nil { | ||
return "", err | ||
} | ||
} else { | ||
bin, err = checkDirAndCompile(pluginType, driver) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
bin, err := checkDirAndCompile(pluginType, driver) | ||
return bin, nil | ||
} | ||
|
||
// Load loads the plugin using the hashicorp go-plugin system | ||
func Load(pluginType, driver string) (*RevaPlugin, error) { | ||
bin, err := fetchBinary(pluginType, driver) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logger := hclog.New(&hclog.LoggerOptions{ | ||
Name: "plugin", | ||
Output: os.Stdout, | ||
Level: hclog.Trace, | ||
}) | ||
|
||
client := plugin.NewClient(&plugin.ClientConfig{ | ||
HandshakeConfig: handshake, | ||
HandshakeConfig: Handshake, | ||
Plugins: PluginMap, | ||
Cmd: exec.Command(bin), | ||
AllowedProtocols: []plugin.Protocol{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this example, but it would only work once cs3org/reva-auth-json-plugin#1 is merged.