context: Add authenticated HTTP credentials (#892)

* gRPC: context overide

* exchanges: continue update

* exchange: Update context handling
*Add setter methods for API credentials
*Shift credentials functionality to its own file in exchanges package
*Add tests
*Refactor function DeployCredentialsToContext for library usage
*Add function to process credential metadata from API boundary to internal use context value.
*Add OTP rpc handling

* exchanges: reverts to old style in GetFeeByType, reverts some code I accidently deleted. Plus things and other. XD

* template: update

* exchanges: fix linter issues

* REMOVE THAT AWESOME NEW LINE!

* gct: fix some tests

* I cant spell :(

* exchanges/gctscript: fix more tests

* coinnut: fix tests

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* exchanges/gctcli: stop applying empty credentials

* fix linters

* exchanges: add test

* rpceserver: actually check error for errors

* rpcserver: fix up tests

* Update exchanges/credentials.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* exchanges/creds: move tests to corresponding files, add protection and segration for Credentials struct & ptr values

* exchanges/creds: allow subaccount to override default credentials via gRPC

* exchanges/credentials: don't return nil in GetCredentials

* creds: spelling

* exchanges: fix glorious NITS!

* credentials: Add in test and refactor IsEmpty method.

* credentials: change type positioning (glorious)

* exchange_template: Fix template changes

* DOCS: Refresh

* docs: fix spelling

* DOCS: fix alignment and add package

* DOCS: ALIGN!

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2022-03-21 13:58:08 +11:00
committed by GitHub
parent 58b9f8b9ec
commit 09fa2f236a
122 changed files with 3006 additions and 2126 deletions

View File

@@ -4925,7 +4925,7 @@ func getFuturesPositions(c *cli.Context) error {
var getCollateralCommand = &cli.Command{
Name: "getcollateral",
Usage: "returns total collateral for an exchange asset, with optional per currency breakdown",
ArgsUsage: "<exchange> <asset> <calculateoffline> <includebreakdown> <includezerovalues> <subaccount>",
ArgsUsage: "<exchange> <asset> <calculateoffline> <includebreakdown> <includezerovalues>",
Action: getCollateral,
Flags: []cli.Flag{
&cli.StringFlag{
@@ -4953,11 +4953,6 @@ var getCollateralCommand = &cli.Command{
Aliases: []string{"z"},
Usage: "include collateral values that are zero",
},
&cli.StringFlag{
Name: "subaccount",
Aliases: []string{"s"},
Usage: "the subaccount to retrieve collateral data from, depending on individual exchange support",
},
},
}
@@ -5015,13 +5010,6 @@ func getCollateral(c *cli.Context) error {
}
}
var subAccount string
if c.IsSet("subaccount") {
subAccount = c.String("subaccount")
} else if c.Args().Get(5) != "" {
subAccount = c.Args().Get(5)
}
conn, cancel, err := setupClient(c)
if err != nil {
return err
@@ -5033,7 +5021,6 @@ func getCollateral(c *cli.Context) error {
&gctrpc.GetCollateralRequest{
Exchange: exchangeName,
Asset: assetType,
SubAccount: subAccount,
IncludeBreakdown: includeBreakdown,
CalculateOffline: calculateOffline,
IncludeZeroValues: includeZeroValues,

View File

@@ -12,11 +12,13 @@ import (
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/core"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/gctrpc/auth"
"github.com/thrasher-corp/gocryptotrader/signaler"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)
var (
@@ -26,6 +28,7 @@ var (
pairDelimiter string
certPath string
timeout time.Duration
exchangeCreds exchange.Credentials
)
const defaultTimeout = time.Second * 30
@@ -53,6 +56,10 @@ func setupClient(c *cli.Context) (*grpc.ClientConn, context.CancelFunc, error) {
var cancel context.CancelFunc
c.Context, cancel = context.WithTimeout(c.Context, timeout)
if !exchangeCreds.IsEmpty() {
flag, values := exchangeCreds.GetMetaData()
c.Context = metadata.AppendToOutgoingContext(c.Context, flag, values)
}
conn, err := grpc.DialContext(c.Context, host, opts...)
return conn, cancel, err
}
@@ -100,6 +107,36 @@ func main() {
Usage: "the default context timeout value for requests",
Destination: &timeout,
},
&cli.StringFlag{
Name: "apikey",
Usage: "override config API key for request",
Destination: &exchangeCreds.Key,
},
&cli.StringFlag{
Name: "apisecret",
Usage: "override config API Secret for request",
Destination: &exchangeCreds.Secret,
},
&cli.StringFlag{
Name: "apisubaccount",
Usage: "override config API sub account for request",
Destination: &exchangeCreds.SubAccount,
},
&cli.StringFlag{
Name: "apiclientid",
Usage: "override config API client ID for request",
Destination: &exchangeCreds.ClientID,
},
&cli.StringFlag{
Name: "apipemkey",
Usage: "override config API PEM key for request",
Destination: &exchangeCreds.PEMKey,
},
&cli.StringFlag{
Name: "apionetimepassword",
Usage: "override config API One Time Password (OTP) for request",
Destination: &exchangeCreds.OneTimePassword,
},
}
app.Commands = []*cli.Command{
getInfoCommand,