Skip to content

Commit

Permalink
fix: pactus shell support basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Jun 30, 2024
1 parent 08559b4 commit b0f3afc
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions cmd/shell/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"context"

"encoding/base64"
"fmt"
"github.com/NathanBaulch/protoc-gen-cobra/client"
"github.com/NathanBaulch/protoc-gen-cobra/naming"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/www/grpc/basicauth"
pb "github.com/pactus-project/pactus/www/grpc/gen/go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

const (
Expand All @@ -19,23 +18,20 @@ const (
)

func main() {
var (
username string
password string
)

Check warning on line 24 in cmd/shell/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/shell/main.go#L21-L24

Added lines #L21 - L24 were not covered by tests

rootCmd := &cobra.Command{
Use: "shell",
Short: "Pactus Shell",
Long: `pactus-shell is a command line tool for interacting with the Pactus blockchain using gRPC`,
}

auth := &basicauth.BasicAuth{}

client.RegisterFlagBinder(func(fs *pflag.FlagSet, namer naming.Namer) {
fs.StringVar(&auth.Username, namer("auth-username"), "", "username for gRPC basic authentication")
fs.StringVar(&auth.Password, namer("auth-password"), "", "password for gRPC basic authentication")
})

client.RegisterPreDialer(func(_ context.Context, opts *[]grpc.DialOption) error {
*opts = append(*opts, grpc.WithPerRPCCredentials(auth))

return nil
fs.StringVar(&username, namer("auth-username"), "", "username for gRPC basic authentication")
fs.StringVar(&password, namer("auth-password"), "", "password for gRPC basic authentication")

Check warning on line 34 in cmd/shell/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/shell/main.go#L33-L34

Added lines #L33 - L34 were not covered by tests
})

changeDefaultParameters := func(c *cobra.Command) *cobra.Command {
Expand All @@ -48,6 +44,16 @@ func main() {
return c
}

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {

Check failure on line 47 in cmd/shell/main.go

View workflow job for this annotation

GitHub Actions / build-linux

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 47 in cmd/shell/main.go

View workflow job for this annotation

GitHub Actions / linting

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
if username != "" && password != "" {
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
md := metadata.Pairs("authorization", "Basic "+auth)
ctx := metadata.NewOutgoingContext(cmd.Context(), md)
cmd.SetContext(ctx)

Check warning on line 52 in cmd/shell/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/shell/main.go#L47-L52

Added lines #L47 - L52 were not covered by tests
}
return nil

Check failure on line 54 in cmd/shell/main.go

View workflow job for this annotation

GitHub Actions / build-linux

return with no blank line before (nlreturn)

Check failure on line 54 in cmd/shell/main.go

View workflow job for this annotation

GitHub Actions / linting

return with no blank line before (nlreturn)

Check warning on line 54 in cmd/shell/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/shell/main.go#L54

Added line #L54 was not covered by tests
}

rootCmd.AddCommand(changeDefaultParameters(pb.BlockchainClientCommand()))
rootCmd.AddCommand(changeDefaultParameters(pb.NetworkClientCommand()))
rootCmd.AddCommand(changeDefaultParameters(pb.TransactionClientCommand()))
Expand Down

0 comments on commit b0f3afc

Please sign in to comment.