Skip to content

Commit dac6262

Browse files
csweichelroboquat
authored andcommitted
[gpctl] Support tls-path and host for imagebuilds
1 parent 13dd222 commit dac6262

File tree

1 file changed

+71
-30
lines changed

1 file changed

+71
-30
lines changed

dev/gpctl/cmd/imagebuilds.go

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ package cmd
66

77
import (
88
"context"
9+
"crypto/tls"
10+
"crypto/x509"
911
"fmt"
12+
"io/ioutil"
1013
"net"
14+
"path/filepath"
1115

1216
"github.com/spf13/cobra"
1317
"golang.org/x/xerrors"
@@ -29,56 +33,93 @@ var imagebuildsCmd = &cobra.Command{
2933
func init() {
3034
imagebuildsCmd.PersistentFlags().StringP("tls", "t", "", "TLS certificate when connecting to a secured gRPC endpoint")
3135
imagebuildsCmd.PersistentFlags().Bool("mk3", true, "use image-builder mk3")
36+
imagebuildsCmd.PersistentFlags().String("host", "", "dial a host directly")
37+
imagebuildsCmd.PersistentFlags().String("tls-path", "", "TLS certificate when connecting to a secured gRPC endpoint")
3238

3339
rootCmd.AddCommand(imagebuildsCmd)
3440
}
3541

3642
func getImagebuildsClient(ctx context.Context) (*grpc.ClientConn, api.ImageBuilderClient, error) {
37-
cfg, namespace, err := getKubeconfig()
38-
if err != nil {
39-
return nil, nil, err
40-
}
41-
clientSet, err := kubernetes.NewForConfig(cfg)
42-
if err != nil {
43-
return nil, nil, err
44-
}
43+
host, _ := imagebuildsCmd.PersistentFlags().GetString("host")
44+
if host == "" {
45+
cfg, namespace, err := getKubeconfig()
46+
if err != nil {
47+
return nil, nil, err
48+
}
49+
clientSet, err := kubernetes.NewForConfig(cfg)
50+
if err != nil {
51+
return nil, nil, err
52+
}
4553

46-
comp := "image-builder"
47-
if mk3, _ := imagebuildsCmd.PersistentFlags().GetBool("mk3"); mk3 {
48-
comp = "image-builder-mk3"
49-
}
54+
comp := "image-builder"
55+
if mk3, _ := imagebuildsCmd.PersistentFlags().GetBool("mk3"); mk3 {
56+
comp = "image-builder-mk3"
57+
}
5058

51-
freePort, err := GetFreePort()
52-
if err != nil {
53-
return nil, nil, err
54-
}
59+
freePort, err := GetFreePort()
60+
if err != nil {
61+
return nil, nil, err
62+
}
5563

56-
port := fmt.Sprintf("%d:8080", freePort)
57-
podName, err := util.FindAnyPodForComponent(clientSet, namespace, comp)
58-
if err != nil {
59-
return nil, nil, err
60-
}
61-
readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port)
62-
select {
63-
case <-readychan:
64-
case err := <-errchan:
65-
return nil, nil, err
66-
case <-ctx.Done():
67-
return nil, nil, ctx.Err()
64+
port := fmt.Sprintf("%d:8080", freePort)
65+
podName, err := util.FindAnyPodForComponent(clientSet, namespace, comp)
66+
if err != nil {
67+
return nil, nil, err
68+
}
69+
readychan, errchan := util.ForwardPort(ctx, cfg, namespace, podName, port)
70+
select {
71+
case <-readychan:
72+
case err := <-errchan:
73+
return nil, nil, err
74+
case <-ctx.Done():
75+
return nil, nil, ctx.Err()
76+
}
77+
host = fmt.Sprintf("localhost:%d", freePort)
6878
}
6979

7080
secopt := grpc.WithInsecure()
71-
cert, _ := workspacesCmd.Flags().GetString("tls")
81+
cert, _ := imagebuildsCmd.Flags().GetString("tls")
7282
if cert != "" {
7383
creds, err := credentials.NewClientTLSFromFile(cert, "")
7484
if err != nil {
7585
return nil, nil, xerrors.Errorf("could not load tls cert: %w", err)
7686
}
7787

88+
secopt = grpc.WithTransportCredentials(creds)
89+
} else if fn, _ := imagebuildsCmd.Flags().GetString("tls-path"); fn != "" {
90+
crt, err := ioutil.ReadFile(filepath.Join(fn, "tls.crt"))
91+
if err != nil {
92+
return nil, nil, err
93+
}
94+
key, err := ioutil.ReadFile(filepath.Join(fn, "tls.key"))
95+
if err != nil {
96+
return nil, nil, err
97+
}
98+
cert, err := tls.X509KeyPair(crt, key)
99+
if err != nil {
100+
return nil, nil, err
101+
}
102+
103+
ca, err := ioutil.ReadFile(filepath.Join(fn, "ca.crt"))
104+
if err != nil {
105+
return nil, nil, err
106+
}
107+
certPool := x509.NewCertPool()
108+
certPool.AppendCertsFromPEM(ca)
109+
110+
creds := credentials.NewTLS(&tls.Config{
111+
Certificates: []tls.Certificate{cert},
112+
RootCAs: certPool,
113+
ServerName: "ws-manager",
114+
})
115+
if err != nil {
116+
return nil, nil, xerrors.Errorf("could not load tls cert: %w", err)
117+
}
118+
78119
secopt = grpc.WithTransportCredentials(creds)
79120
}
80121

81-
conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", freePort), secopt, util.WithClientUnaryInterceptor())
122+
conn, err := grpc.Dial(host, secopt, util.WithClientUnaryInterceptor())
82123
if err != nil {
83124
return nil, nil, err
84125
}

0 commit comments

Comments
 (0)