|
| 1 | +package aeon |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "crypto/x509" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/apex/log" |
| 14 | + |
| 15 | + "github.com/tarantool/go-prompt" |
| 16 | + "github.com/tarantool/tt/cli/aeon/cmd" |
| 17 | + "github.com/tarantool/tt/cli/aeon/pb" |
| 18 | + "github.com/tarantool/tt/cli/connector" |
| 19 | + "google.golang.org/grpc" |
| 20 | + "google.golang.org/grpc/credentials" |
| 21 | + "google.golang.org/grpc/credentials/insecure" |
| 22 | +) |
| 23 | + |
| 24 | +// Client structure with parameters for gRPC connection to Aeon. |
| 25 | +type Client struct { |
| 26 | + title string |
| 27 | + conn *grpc.ClientConn |
| 28 | + client pb.SQLServiceClient |
| 29 | +} |
| 30 | + |
| 31 | +func makeAddress(ctx cmd.ConnectCtx) string { |
| 32 | + if ctx.Network == connector.UnixNetwork { |
| 33 | + if strings.HasPrefix(ctx.Address, "@") { |
| 34 | + return "unix-abstract:" + (ctx.Address)[1:] |
| 35 | + } |
| 36 | + return "unix:" + ctx.Address |
| 37 | + } |
| 38 | + return ctx.Address |
| 39 | +} |
| 40 | + |
| 41 | +func getCertificate(args cmd.Ssl) (tls.Certificate, error) { |
| 42 | + if args.CertFile == "" && args.KeyFile == "" { |
| 43 | + return tls.Certificate{}, nil |
| 44 | + } |
| 45 | + tls_cert, err := tls.LoadX509KeyPair(args.CertFile, args.KeyFile) |
| 46 | + if err != nil { |
| 47 | + return tls_cert, fmt.Errorf("could not load client key pair: %w", err) |
| 48 | + } |
| 49 | + return tls_cert, nil |
| 50 | +} |
| 51 | + |
| 52 | +func getTlsConfig(args cmd.Ssl) (*tls.Config, error) { |
| 53 | + if args.CaFile == "" { |
| 54 | + return &tls.Config{ |
| 55 | + ClientAuth: tls.NoClientCert, |
| 56 | + }, nil |
| 57 | + } |
| 58 | + |
| 59 | + ca, err := os.ReadFile(args.CaFile) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to read CA file: %w", err) |
| 62 | + } |
| 63 | + certPool := x509.NewCertPool() |
| 64 | + if !certPool.AppendCertsFromPEM(ca) { |
| 65 | + return nil, errors.New("failed to append CA data") |
| 66 | + } |
| 67 | + cert, err := getCertificate(args) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed get certificate: %w", err) |
| 70 | + } |
| 71 | + return &tls.Config{ |
| 72 | + Certificates: []tls.Certificate{cert}, |
| 73 | + ClientAuth: tls.RequireAndVerifyClientCert, |
| 74 | + RootCAs: certPool, |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func getDialOpts(ctx cmd.ConnectCtx) (grpc.DialOption, error) { |
| 79 | + var creds credentials.TransportCredentials |
| 80 | + if ctx.Transport == cmd.TransportSsl { |
| 81 | + config, err := getTlsConfig(ctx.Ssl) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("not tls config: %w", err) |
| 84 | + } |
| 85 | + creds = credentials.NewTLS(config) |
| 86 | + } else { |
| 87 | + creds = insecure.NewCredentials() |
| 88 | + } |
| 89 | + return grpc.WithTransportCredentials(creds), nil |
| 90 | +} |
| 91 | + |
| 92 | +// NewAeonHandler create new grpc connection to Aeon server. |
| 93 | +func NewAeonHandler(ctx cmd.ConnectCtx) (*Client, error) { |
| 94 | + c := Client{title: ctx.Address} |
| 95 | + target := makeAddress(ctx) |
| 96 | + // var err error |
| 97 | + opt, err := getDialOpts(ctx) |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("%w", err) |
| 100 | + } |
| 101 | + c.conn, err = grpc.NewClient(target, opt) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("fail to dial: %w", err) |
| 104 | + } |
| 105 | + if err := c.ping(); err == nil { |
| 106 | + log.Infof("Aeon responses at %q", target) |
| 107 | + } else { |
| 108 | + return nil, fmt.Errorf("can't ping to Aeon at %q: %w", target, err) |
| 109 | + } |
| 110 | + |
| 111 | + c.client = pb.NewSQLServiceClient(c.conn) |
| 112 | + return &c, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (c *Client) ping() error { |
| 116 | + log.Infof("Start ping aeon server") |
| 117 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 118 | + defer cancel() |
| 119 | + |
| 120 | + diag := pb.NewDiagServiceClient(c.conn) |
| 121 | + _, err := diag.Ping(ctx, &pb.PingRequest{}) |
| 122 | + if err != nil { |
| 123 | + log.Warnf("Aeon ping %s", err) |
| 124 | + } |
| 125 | + return err |
| 126 | +} |
| 127 | + |
| 128 | +// Title implements console.Handler interface. |
| 129 | +func (c *Client) Title() string { |
| 130 | + return c.title |
| 131 | +} |
| 132 | + |
| 133 | +// Validate implements console.Handler interface. |
| 134 | +func (c *Client) Validate(input string) bool { |
| 135 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 136 | + defer cancel() |
| 137 | + |
| 138 | + check, err := c.client.SQLCheck(ctx, &pb.SQLRequest{Query: input}) |
| 139 | + if err != nil { |
| 140 | + log.Warnf("Aeon validate %s\nFor request: %q", err, input) |
| 141 | + return false |
| 142 | + } |
| 143 | + |
| 144 | + return check.Status == pb.SQLCheckStatus_SQL_QUERY_VALID |
| 145 | +} |
| 146 | + |
| 147 | +// Execute implements console.Handler interface. |
| 148 | +func (c *Client) Execute(input string) any { |
| 149 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 150 | + defer cancel() |
| 151 | + |
| 152 | + resp, err := c.client.SQL(ctx, &pb.SQLRequest{Query: input}) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + return parseSQLResponse(resp) |
| 157 | +} |
| 158 | + |
| 159 | +// Stop implements console.Handler interface. |
| 160 | +func (c *Client) Close() { |
| 161 | + c.conn.Close() |
| 162 | +} |
| 163 | + |
| 164 | +// Complete implements console.Handler interface. |
| 165 | +func (c *Client) Complete(input prompt.Document) []prompt.Suggest { |
| 166 | + // TODO: waiting until there is support from Aeon side. |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +// parseSQLResponse returns result as table in map. |
| 171 | +// Where keys is name of columns. And body is array of values. |
| 172 | +// On any issue return an error. |
| 173 | +func parseSQLResponse(resp *pb.SQLResponse) any { |
| 174 | + if resp.Error != nil { |
| 175 | + return ResultError{resp.Error} |
| 176 | + } |
| 177 | + if resp.TupleFormat == nil { |
| 178 | + return ResultType{} |
| 179 | + } |
| 180 | + res := ResultType{ |
| 181 | + names: make([]string, len(resp.TupleFormat.Names)), |
| 182 | + rows: make([]ResultRow, len(resp.Tuples)), |
| 183 | + } |
| 184 | + for i, n := range resp.TupleFormat.Names { |
| 185 | + res.names[i] = n |
| 186 | + res.rows[i] = make([]any, 0, len(resp.TupleFormat.Names)) |
| 187 | + } |
| 188 | + |
| 189 | + for r, row := range resp.Tuples { |
| 190 | + for _, v := range row.Fields { |
| 191 | + val, err := decodeValue(v) |
| 192 | + if err != nil { |
| 193 | + return fmt.Errorf("tuple %d can't decode %s: %w", r, v.String(), err) |
| 194 | + } |
| 195 | + res.rows[r] = append(res.rows[r], val) |
| 196 | + } |
| 197 | + } |
| 198 | + return res |
| 199 | +} |
0 commit comments