Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion plugins/binance/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ func APIKey() schema.CredentialType {
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryEnvVarPair(defaultEnvVarMapping)}
Importer: importer.TryAll(
importer.TryEnvVarPair(map[string]sdk.FieldName{
"BINANCE_API_KEY": fieldname.APIKey,
"BINANCE_SECRET_KEY": fieldname.APISecret,
}),
importer.TryEnvVarPair(map[string]sdk.FieldName{
"BINANCE_API_KEY": fieldname.APIKey,
"BINANCE_API_SECRET": fieldname.APISecret,
}),
)}
}

// binance-cli v2 reads the secret from BINANCE_SECRET_KEY, while v1 reads BINANCE_API_SECRET,
// so both are provisioned to support either version.
var defaultEnvVarMapping = map[string]sdk.FieldName{
"BINANCE_API_KEY": fieldname.APIKey,
"BINANCE_SECRET_KEY": fieldname.APISecret,
"BINANCE_API_SECRET": fieldname.APISecret,
}
48 changes: 47 additions & 1 deletion plugins/binance/api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestAPIKeyProvisioner(t *testing.T) {
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"BINANCE_API_KEY": "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
"BINANCE_SECRET_KEY": "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
"BINANCE_API_SECRET": "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
},
},
Expand All @@ -27,18 +28,63 @@ func TestAPIKeyProvisioner(t *testing.T) {

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"environment": {
"binance-cli v2 environment": {
Environment: map[string]string{
"BINANCE_API_KEY": "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
"BINANCE_SECRET_KEY": "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
fieldname.APISecret: "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
},
},
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
},
},
},
},
"binance-cli v1 environment": {
Environment: map[string]string{
"BINANCE_API_KEY": "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
"BINANCE_API_SECRET": "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
},
},
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
fieldname.APISecret: "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
},
},
},
},
"both secret env vars with different values": {
Environment: map[string]string{
"BINANCE_API_KEY": "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
"BINANCE_SECRET_KEY": "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
"BINANCE_API_SECRET": "Qx7Vb2NwLk9RtYp4HsMc6JdZf8GaUe3XiOn5TrWq1KyBv0PlCm2AzSj4DEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
fieldname.APISecret: "2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qjThmEycY2J0RgJgNNrWQBqEXAMPLE",
},
},
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "jThmEycY2J0RgJgNNrWQBq2raPzKvxCkcwPQFk8AuWUu5QxQSWaItIB1qEXAMPLE",
fieldname.APISecret: "Qx7Vb2NwLk9RtYp4HsMc6JdZf8GaUe3XiOn5TrWq1KyBv0PlCm2AzSj4DEXAMPLE",
},
},
},
},
})
Expand Down
2 changes: 2 additions & 0 deletions plugins/binance/binance_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func BinanceCLI() schema.Executable {
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotForCommand("profile"),
needsauth.NotForCommand("completion"),
needsauth.NotWhenContainsArgs("t"),
needsauth.NotWhenContainsArgs("i"),
needsauth.NotWhenContainsArgs("book"),
Expand Down
48 changes: 48 additions & 0 deletions plugins/binance/binance_cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package binance

import (
"testing"

"github.com/1Password/shell-plugins/sdk/plugintest"
)

func TestBinanceCLINeedsAuth(t *testing.T) {
plugintest.TestNeedsAuth(t, BinanceCLI().NeedsAuth, map[string]plugintest.NeedsAuthCase{
"no without args": {
Args: []string{},
ExpectedNeedsAuth: false,
},
"no for --help": {
Args: []string{"--help"},
ExpectedNeedsAuth: false,
},
"no for --version": {
Args: []string{"--version"},
ExpectedNeedsAuth: false,
},
"no for v1 public market data command": {
Args: []string{"book", "bnbusdt"},
ExpectedNeedsAuth: false,
},
"yes for v1 order command": {
Args: []string{"buy", "-s", "BNBUSDT", "-t", "LIMIT", "-q", "0.05", "-p", "350", "-f", "GTC"},
ExpectedNeedsAuth: true,
},
"no for v2 profile command": {
Args: []string{"profile", "list"},
ExpectedNeedsAuth: false,
},
"no for v2 completion command": {
Args: []string{"completion", "zsh"},
ExpectedNeedsAuth: false,
},
"yes for v2 signed command": {
Args: []string{"spot", "get-account"},
ExpectedNeedsAuth: true,
},
"yes for a profile named after an exempt command": {
Args: []string{"spot", "get-account", "--profile", "profile"},
ExpectedNeedsAuth: true,
},
})
}
Loading