-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
plugins/gRPC: fix issues with reserved keywords in response data #3881
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c43358
plugins/gRPC: fix issues with reserved keywords in response data
927b9b3
Add the path raw file for mock plugin
f65e823
Fix panic when special paths is nil
e36df4c
Add tests for Listing and raw requests from plugins
ffa311c
Add json.Number case when decoding the status
b69da20
Bump the version required for gRPC defaults
7ec9de9
Fix test for gRPC version check
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package http | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/vault/api" | ||
bplugin "github.com/hashicorp/vault/builtin/plugin" | ||
"github.com/hashicorp/vault/helper/logbridge" | ||
"github.com/hashicorp/vault/helper/pluginutil" | ||
"github.com/hashicorp/vault/logical" | ||
"github.com/hashicorp/vault/logical/plugin" | ||
"github.com/hashicorp/vault/logical/plugin/mock" | ||
"github.com/hashicorp/vault/physical/inmem" | ||
"github.com/hashicorp/vault/vault" | ||
) | ||
|
||
func getPluginClusterAndCore(t testing.TB, logger *logbridge.Logger) (*vault.TestCluster, *vault.TestClusterCore) { | ||
inmha, err := inmem.NewInmemHA(nil, logger.LogxiLogger()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
coreConfig := &vault.CoreConfig{ | ||
Physical: inmha, | ||
LogicalBackends: map[string]logical.Factory{ | ||
"plugin": bplugin.Factory, | ||
}, | ||
} | ||
|
||
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{ | ||
HandlerFunc: Handler, | ||
RawLogger: logger, | ||
}) | ||
cluster.Start() | ||
|
||
cores := cluster.Cores | ||
core := cores[0] | ||
|
||
os.Setenv(pluginutil.PluginCACertPEMEnv, cluster.CACertPEMFile) | ||
|
||
vault.TestWaitActive(t, core.Core) | ||
vault.TestAddTestPlugin(t, core.Core, "mock-plugin", "TestPlugin_PluginMain") | ||
|
||
// Mount the mock plugin | ||
err = core.Client.Sys().Mount("mock", &api.MountInput{ | ||
Type: "plugin", | ||
PluginName: "mock-plugin", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return cluster, core | ||
} | ||
|
||
func TestPlugin_PluginMain(t *testing.T) { | ||
if os.Getenv(pluginutil.PluginVaultVersionEnv) == "" { | ||
return | ||
} | ||
|
||
caPEM := os.Getenv(pluginutil.PluginCACertPEMEnv) | ||
if caPEM == "" { | ||
t.Fatal("CA cert not passed in") | ||
} | ||
|
||
args := []string{"--ca-cert=" + caPEM} | ||
|
||
apiClientMeta := &pluginutil.APIClientMeta{} | ||
flags := apiClientMeta.FlagSet() | ||
flags.Parse(args) | ||
|
||
tlsConfig := apiClientMeta.GetTLSConfig() | ||
tlsProviderFunc := pluginutil.VaultPluginTLSProvider(tlsConfig) | ||
|
||
factoryFunc := mock.FactoryType(logical.TypeLogical) | ||
|
||
err := plugin.Serve(&plugin.ServeOpts{ | ||
BackendFactoryFunc: factoryFunc, | ||
TLSProviderFunc: tlsProviderFunc, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Fatal("Why are we here") | ||
} | ||
|
||
func TestPlugin_MockList(t *testing.T) { | ||
logger := logbridge.NewLogger(hclog.New(&hclog.LoggerOptions{ | ||
Mutex: &sync.Mutex{}, | ||
})) | ||
cluster, core := getPluginClusterAndCore(t, logger) | ||
defer cluster.Cleanup() | ||
|
||
_, err := core.Client.Logical().Write("mock/kv/foo", map[string]interface{}{ | ||
"bar": "baz", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
keys, err := core.Client.Logical().List("mock/kv/") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if keys.Data["keys"].([]interface{})[0].(string) != "foo" { | ||
t.Fatal(keys) | ||
} | ||
|
||
_, err = core.Client.Logical().Write("mock/kv/zoo", map[string]interface{}{ | ||
"bar": "baz", | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
keys, err = core.Client.Logical().List("mock/kv/") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if keys.Data["keys"].([]interface{})[0].(string) != "foo" || keys.Data["keys"].([]interface{})[1].(string) != "zoo" { | ||
t.Fatal(keys) | ||
} | ||
} | ||
|
||
func TestPlugin_MockRawResponse(t *testing.T) { | ||
logger := logbridge.NewLogger(hclog.New(&hclog.LoggerOptions{ | ||
Mutex: &sync.Mutex{}, | ||
})) | ||
cluster, core := getPluginClusterAndCore(t, logger) | ||
defer cluster.Cleanup() | ||
|
||
resp, err := core.Client.RawRequest(core.Client.NewRequest("GET", "/v1/mock/raw")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(body[:]) != "Response" { | ||
t.Fatal("bad body") | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
t.Fatal("bad status") | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package mock | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/vault/logical" | ||
"github.com/hashicorp/vault/logical/framework" | ||
) | ||
|
||
// pathRaw is used to test raw responses. | ||
func pathRaw(b *backend) *framework.Path { | ||
return &framework.Path{ | ||
Pattern: "raw", | ||
Callbacks: map[logical.Operation]framework.OperationFunc{ | ||
logical.ReadOperation: b.pathRawRead, | ||
}, | ||
} | ||
} | ||
|
||
func (b *backend) pathRawRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { | ||
return &logical.Response{ | ||
Data: map[string]interface{}{ | ||
logical.HTTPContentType: "text/plain", | ||
logical.HTTPRawBody: []byte("Response"), | ||
logical.HTTPStatusCode: 200, | ||
}, | ||
}, 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
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.
For safety, you should add a
json.Number
check in here too, in case something has already serialized the data for some reason. It would be handled transparently by the JSON encoder for the response but would trip this here.