Skip to content

Commit

Permalink
Use TLS in Client by default, add --insecure flag
Browse files Browse the repository at this point in the history
  • Loading branch information
slntopp committed Dec 1, 2021
1 parent 472459b commit f2abbcc
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var accountCmd = &cobra.Command{
Use: "account",
Short: "Manage accounts, prints info about current by default",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := account.MakeAccountsServiceClientOrFail()
ctx, client := account.MakeAccountsServiceClientOrFail(cmd)

res, err := client.Get(ctx, &accountspb.GetRequest{
Id: "me",
Expand Down
2 changes: 1 addition & 1 deletion cmd/account/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var CreateCmd = &cobra.Command{
Long: "Authorization data flags must be given('auth-type', 'auth-data')",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeAccountsServiceClientOrFail()
ctx, client := MakeAccountsServiceClientOrFail(cmd)

authType, _ := cmd.Flags().GetString("auth-type")
authData, _ := cmd.Flags().GetStringSlice("auth-data")
Expand Down
2 changes: 1 addition & 1 deletion cmd/account/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var DeleteCmd = &cobra.Command{
Short: "Delete NoCloud Account",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeAccountsServiceClientOrFail()
ctx, client := MakeAccountsServiceClientOrFail(cmd)
res, err := client.Delete(ctx, &accountspb.DeleteRequest{
Id: args[0],
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/account/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var GetCmd = &cobra.Command{
Short: "Get NoCloud Account Data",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeAccountsServiceClientOrFail()
ctx, client := MakeAccountsServiceClientOrFail(cmd)
res, err := client.Get(ctx, &accountspb.GetRequest{
Id: args[0],
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/account/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var ListCmd = &cobra.Command{
Short: "List NoCloud Accounts",
Long: `Add namespace UUID after list command, to filter accounts by namespace`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeAccountsServiceClientOrFail()
ctx, client := MakeAccountsServiceClientOrFail(cmd)
request := accountspb.ListRequest{}
if len(args) > 0 {
request.Namespace = &args[0]
Expand Down
12 changes: 10 additions & 2 deletions cmd/account/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@ package account

import (
"context"
"crypto/tls"
"fmt"
"os"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/slntopp/nocloud/pkg/accounting/accountspb"
pb "github.com/slntopp/nocloud/pkg/api/apipb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

func MakeAccountsServiceClientOrFail() (context.Context, pb.AccountsServiceClient){
func MakeAccountsServiceClientOrFail(cmd *cobra.Command) (context.Context, pb.AccountsServiceClient){
host := viper.Get("nocloud")
if host == nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic("Host is unset")
}

conn, err := grpc.Dial(host.(string), grpc.WithInsecure())
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
opt := grpc.WithTransportCredentials(creds)
if r, _ := cmd.Flags().GetBool("insecure"); r {
opt = grpc.WithInsecure()
}
conn, err := grpc.Dial(host.(string), opt)
if err != nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/account/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var UpdateCmd = &cobra.Command{
Long: "In order to execute request you must change at least one field.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeAccountsServiceClientOrFail()
ctx, client := MakeAccountsServiceClientOrFail(cmd)

updated := false
req := accountspb.Account{
Expand Down
12 changes: 10 additions & 2 deletions cmd/health/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,31 @@ package health

import (
"context"
"crypto/tls"
"fmt"
"os"

pb "github.com/slntopp/nocloud/pkg/api/apipb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

func MakeHealthServiceClientOrFail() (context.Context, pb.HealthServiceClient){
func MakeHealthServiceClientOrFail(cmd *cobra.Command) (context.Context, pb.HealthServiceClient){
host := viper.Get("nocloud")
if host == nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic("Host is unset")
}

conn, err := grpc.Dial(host.(string), grpc.WithInsecure())
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
opt := grpc.WithTransportCredentials(creds)
if r, _ := cmd.Flags().GetBool("insecure"); r {
opt = grpc.WithInsecure()
}
conn, err := grpc.Dial(host.(string), opt)
if err != nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/health/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var ProbeCmd = &cobra.Command{
Short: "Do health probe",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeHealthServiceClientOrFail()
ctx, client := MakeHealthServiceClientOrFail(cmd)
var res *pb.ProbeResponse
var err error
switch args[0] {
Expand Down
9 changes: 8 additions & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package cmd

import (
"context"
"crypto/tls"
"fmt"

"github.com/slntopp/nocloud/pkg/accounting/accountspb"
pb "github.com/slntopp/nocloud/pkg/api/apipb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

// loginCmd represents the login command
Expand All @@ -33,7 +35,12 @@ var loginCmd = &cobra.Command{
Long: `Generate Auth Token in NoCloud API and store it in CLI config.`,
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := grpc.Dial(args[0], grpc.WithInsecure())
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
opt := grpc.WithTransportCredentials(creds)
if r, _ := cmd.Flags().GetBool("insecure"); r {
opt = grpc.WithInsecure()
}
conn, err := grpc.Dial(args[0], opt)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/namespace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var CreateCmd = &cobra.Command{
Short: "Create NoCloud Namespace",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeNamespacesServiceClientOrFail()
ctx, client := MakeNamespacesServiceClientOrFail(cmd)

req := namespacespb.CreateRequest{
Title: args[0],
Expand Down
2 changes: 1 addition & 1 deletion cmd/namespace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var DeleteCmd = &cobra.Command{
Short: "Delete NoCloud Namespace",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeNamespacesServiceClientOrFail()
ctx, client := MakeNamespacesServiceClientOrFail(cmd)
res, err := client.Delete(ctx, &namespacespb.DeleteRequest{
Id: args[0],
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/namespace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var ListCmd = &cobra.Command{
Use: "list [[flags]]",
Short: "List NoCloud Namespaces",
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeNamespacesServiceClientOrFail()
ctx, client := MakeNamespacesServiceClientOrFail(cmd)
request := namespacespb.ListRequest{}

d, _ := cmd.Flags().GetInt32("depth")
Expand Down
12 changes: 10 additions & 2 deletions cmd/namespace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@ package namespace

import (
"context"
"crypto/tls"
"fmt"
"os"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/slntopp/nocloud/pkg/accounting/namespacespb"
pb "github.com/slntopp/nocloud/pkg/api/apipb"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

func MakeNamespacesServiceClientOrFail() (context.Context, pb.NamespacesServiceClient){
func MakeNamespacesServiceClientOrFail(cmd *cobra.Command) (context.Context, pb.NamespacesServiceClient){
host := viper.Get("nocloud")
if host == nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic("Host is unset")
}

conn, err := grpc.Dial(host.(string), grpc.WithInsecure())
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
opt := grpc.WithTransportCredentials(creds)
if r, _ := cmd.Flags().GetBool("insecure"); r {
opt = grpc.WithInsecure()
}
conn, err := grpc.Dial(host.(string), opt)
if err != nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic(err)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func init() {

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.default.nocloud.yaml)")
rootCmd.PersistentFlags().Bool("json", false, "Print output as json")
rootCmd.PersistentFlags().Bool("insecure", false, "Use WithInsecure instead of TLS")
rootCmd.PersistentFlags().Bool("verbose", false, "Print additional info related to the CLI itself")
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/services/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var CreateCmd = &cobra.Command{
return err
}

ctx, client := MakeServicesServiceClientOrFail()
ctx, client := MakeServicesServiceClientOrFail(cmd)
request := pb.CreateRequest{Service: &service, Namespace: namespace}
res, err := client.Create(ctx, &request)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/services/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var DownCmd = &cobra.Command{
Short: "NoCloud Service Down",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeServicesServiceClientOrFail()
ctx, client := MakeServicesServiceClientOrFail(cmd)
request := pb.DownRequest{Id: args[0]}
_, err := client.Down(ctx, &request)
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/services/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var GetCmd = &cobra.Command{
Short: "Get NoCloud Service",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeServicesServiceClientOrFail()
ctx, client := MakeServicesServiceClientOrFail(cmd)
request := pb.GetRequest{Id: args[0]}
res, err := client.Get(ctx, &request)

Expand Down
2 changes: 1 addition & 1 deletion cmd/services/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var ListCmd = &cobra.Command{
Short: "List NoCloud Services",
Long: `Add namespace UUID after list command, to filter services by namespace`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeServicesServiceClientOrFail()
ctx, client := MakeServicesServiceClientOrFail(cmd)
request := pb.ListRequest{}
if len(args) > 0 {
request.Namespace = &args[0]
Expand Down
12 changes: 10 additions & 2 deletions cmd/services/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package services

import (
"context"
"crypto/tls"
"fmt"
"os"

Expand All @@ -25,19 +26,26 @@ import (
spb "github.com/slntopp/nocloud/pkg/services/proto"
"gopkg.in/yaml.v2"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

func MakeServicesServiceClientOrFail() (context.Context, pb.ServicesServiceClient){
func MakeServicesServiceClientOrFail(cmd *cobra.Command) (context.Context, pb.ServicesServiceClient){
host := viper.Get("nocloud")
if host == nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic("Host is unset")
}

conn, err := grpc.Dial(host.(string), grpc.WithInsecure())
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
opt := grpc.WithTransportCredentials(creds)
if r, _ := cmd.Flags().GetBool("insecure"); r {
opt = grpc.WithInsecure()
}
conn, err := grpc.Dial(host.(string), opt)
if err != nil {
fmt.Fprintln(os.Stderr, "Error setting connection up")
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/services/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var TestCmd = &cobra.Command{
return err
}

ctx, client := MakeServicesServiceClientOrFail()
ctx, client := MakeServicesServiceClientOrFail(cmd)
request := pb.CreateRequest{Service: &service, Namespace: namespace}
res, err := client.TestConfig(ctx, &request)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions cmd/services/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
"github.com/spf13/cobra"
)

func SelectDeployPoliciesInteractive(ctx context.Context, client apipb.ServicesServiceClient, id string) (res map[string]string, err error) {
func SelectDeployPoliciesInteractive(ctx context.Context, cmd *cobra.Command, client apipb.ServicesServiceClient, id string) (res map[string]string, err error) {
service, err := client.Get(ctx, &pb.GetRequest{Id: id})
if err != nil {
return nil, err
}
ctx, spClient := sp.MakeServicesProviderServiceClientOrFail()
ctx, spClient := sp.MakeServicesProviderServiceClientOrFail(cmd)
sps, err := spClient.List(ctx, &sppb.ListRequest{})
if err != nil {
return nil, err
Expand Down Expand Up @@ -72,7 +72,7 @@ var UpCmd = &cobra.Command{
Short: "NoCloud Service Up",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, client := MakeServicesServiceClientOrFail()
ctx, client := MakeServicesServiceClientOrFail(cmd)

req := pb.UpRequest{Id: args[0]}
if rulesJson, _ := cmd.Flags().GetString("rules"); rulesJson != "" {
Expand All @@ -87,7 +87,7 @@ var UpCmd = &cobra.Command{
json.Unmarshal(rulesJson, &req.DeployPolicies)
} else {
fmt.Println("Nothing given, selecting in interactive mode")
r, err := SelectDeployPoliciesInteractive(ctx, client, args[0])
r, err := SelectDeployPoliciesInteractive(ctx, cmd, client, args[0])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/sp/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var CreateCmd = &cobra.Command{
return err
}

ctx, client := MakeServicesProviderServiceClientOrFail()
ctx, client := MakeServicesProviderServiceClientOrFail(cmd)
res, err := client.Create(ctx, &request)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/sp/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var GetCmd = &cobra.Command{
Short: "Get NoCloud Services Providers",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeServicesProviderServiceClientOrFail()
ctx, client := MakeServicesProviderServiceClientOrFail(cmd)
request := pb.GetRequest{Id: args[0]}
res, err := client.Get(ctx, &request)

Expand Down
2 changes: 1 addition & 1 deletion cmd/sp/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var ListCmd = &cobra.Command{
Short: "List NoCloud Services Providers",
Long: `Add namespace UUID after list command, to filter services by namespace`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, client := MakeServicesProviderServiceClientOrFail()
ctx, client := MakeServicesProviderServiceClientOrFail(cmd)
request := pb.ListRequest{}
res, err := client.List(ctx, &request)

Expand Down
Loading

0 comments on commit f2abbcc

Please sign in to comment.