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
18 changes: 18 additions & 0 deletions core/cli/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ func runE(cmd *cobra.Command, _ []string) error {
return err
}

token, err := tui.StringPrompt("enter permify token (optional)", "", config.CliConfig.Token)
if err != nil {
return err
}

certPath, err := tui.StringPrompt("enter cert path (optional)", "", config.CliConfig.CertPath)
if err != nil {
return err
}

certKey, err := tui.StringPrompt("enter cert key (optional)", "", config.CliConfig.CertKey)
if err != nil {
return err
}

resp, err := client.New(url)

// Todo: Implement pagination
Expand All @@ -124,6 +139,9 @@ func runE(cmd *cobra.Command, _ []string) error {
}
config.CliConfig.PermifyURL = url
config.CliConfig.Tenant = tenantIds[tenant]
config.CliConfig.Token = token
config.CliConfig.CertPath = certPath
config.CliConfig.CertKey = certKey
err = config.Write()
if err != nil {
logger.Log.Error(err)
Expand Down
24 changes: 22 additions & 2 deletions core/client/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
package client

import (
"crypto/tls"
"fmt"

"github.com/Permify/permify-cli/core/config"
permify "github.com/Permify/permify-go/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)

// New initializes a new permify client
func New(endpoint string) (*permify.Client, error) {
var opts []grpc.DialOption

if config.CliConfig.CertPath != "" && config.CliConfig.CertKey != "" {
cert, err := tls.LoadX509KeyPair(config.CliConfig.CertPath, config.CliConfig.CertKey)
if err != nil {
return nil, fmt.Errorf("failed to load x509 key pair: %w", err)
}
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert},
})))
} else if config.CliConfig.SslEnabled {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

client, err := permify.NewClient(
permify.Config{
Endpoint: endpoint,
},
// Todo: Implement secure call with tls certificate
grpc.WithTransportCredentials(insecure.NewCredentials()),
opts...,
)
return client, err
}
13 changes: 13 additions & 0 deletions core/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"

"github.com/Permify/permify-cli/core/config"
"github.com/Permify/permify-cli/core/logger"
)

Expand Down Expand Up @@ -40,6 +41,9 @@ func Get(host, path string, query map[string]string, respModel interface{}) erro
logger.Log.Error("failed to create request object")
return err
}
if config.CliConfig.Token != "" {
req.Header.Add("Authorization", "Bearer "+config.CliConfig.Token)
}
q := req.URL.Query()
for k, v := range query {
q.Add(k, v)
Expand Down Expand Up @@ -86,6 +90,9 @@ func Post(host, path string, query map[string]string, requestModel, respModel in
logger.Log.Error("failed to create request")
return err
}
if config.CliConfig.Token != "" {
req.Header.Add("Authorization", "Bearer "+config.CliConfig.Token)
}
req.Header.Add("Content-Type", "application/json")
q := req.URL.Query()
for k, v := range query {
Expand Down Expand Up @@ -124,6 +131,9 @@ func Delete(host, path string, query map[string]string, respModel interface{}) e
logger.Log.Error("failed to create request object")
return err
}
if config.CliConfig.Token != "" {
req.Header.Add("Authorization", "Bearer "+config.CliConfig.Token)
}
req.Header.Add("Content-Type", "application/json")
q := req.URL.Query()
for k, v := range query {
Expand Down Expand Up @@ -166,6 +176,9 @@ func Put(host, path string, query map[string]string, requestParams, respModel in
logger.Log.Error("failed to create request")
return err
}
if config.CliConfig.Token != "" {
req.Header.Add("Authorization", "Bearer "+config.CliConfig.Token)
}
req.Header.Add("Content-Type", "application/json")
q := req.URL.Query()
for k, v := range query {
Expand Down
3 changes: 3 additions & 0 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type ProfileConfigs struct {
type CoreConfig struct {
PermifyURL string `yaml:"permify_url"`
Tenant string `yaml:"tenant"`
Token string `yaml:"token"`
CertPath string `yaml:"cert_path"`
CertKey string `yaml:"cert_key"`
SslEnabled bool `yaml:"-"`
}

Expand Down