|
| 1 | +/* This file is part of VoltDB. |
| 2 | + * Copyright (C) 2022 Volt Active Data Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with VoltDB. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +/* |
| 21 | + * Program testing how go handles TLS handshake and server certificate validation. |
| 22 | + * Note: couldn't use main_test.go in same directory because it can't be used under GoLand debugger. |
| 23 | + * |
| 24 | + * Examples: |
| 25 | + * go run . -h |
| 26 | + * go run . -pem=/Users/rdykiel/ssl/server/volt.certstore.pem |
| 27 | + * go run . -altserver=voltdb.com -pem=/Users/rdykiel/ssl/server/volt.certstore.pem |
| 28 | + * go run . -altserver=foo.voltdb.com -pem=/Users/rdykiel/ssl/server/volt.certstore.pem |
| 29 | + * go run . -altserver=goo.voltdb.com -pem=/Users/rdykiel/ssl/server/volt.certstore.pem |
| 30 | + * go run . -insecure |
| 31 | + * |
| 32 | + * Certificate validation: if insecure = false, go expects to find server or altServer in the |
| 33 | + * SubjectAlternativeName extension of the PEM certificate, e.g.: |
| 34 | + * |
| 35 | + * 1: ObjectId: 2.5.29.17 Criticality=false |
| 36 | + * SubjectAlternativeName [ |
| 37 | + * IPAddress: 127.0.0.1 |
| 38 | + * DNSName: voltdb.com |
| 39 | + * DNSName: foo.voltdb.com |
| 40 | + * DNSName: bar.voltdb.com |
| 41 | + * ] |
| 42 | + */ |
| 43 | + |
| 44 | +import ( |
| 45 | + "crypto/tls" |
| 46 | + "database/sql/driver" |
| 47 | + "errors" |
| 48 | + "flag" |
| 49 | + "github.com/VoltDB/voltdb-client-go/voltdbclient" |
| 50 | + "github.com/kr/pretty" |
| 51 | + "log" |
| 52 | +) |
| 53 | + |
| 54 | +func tlsConnect(server string, altServer string, pemPath string, insecure bool) { |
| 55 | + log.Printf("Connecting to %s: altServer %s, pemPath %s, insecure %t", |
| 56 | + server, altServer, pemPath, insecure) |
| 57 | + |
| 58 | + // Keep tlsConfig nil unless altServer is provided: in this case we request to |
| 59 | + // validate the alternate name instead of the server name |
| 60 | + var tlsConfig *tls.Config |
| 61 | + if altServer != "" { |
| 62 | + tlsConfig = &tls.Config{ |
| 63 | + InsecureSkipVerify: insecure, |
| 64 | + ServerName: altServer, |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + conn, err := voltdbclient.OpenTLSConn(server, voltdbclient.ClientConfig{ |
| 69 | + PEMPath: pemPath, |
| 70 | + TLSConfig: tlsConfig, |
| 71 | + InsecureSkipVerify: insecure, |
| 72 | + ConnectTimeout: 60000000, |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + log.Fatal(err) |
| 76 | + } else if conn == nil { |
| 77 | + log.Fatal(errors.New("no connection")) |
| 78 | + } else { |
| 79 | + log.Printf("connected to: %s", server) |
| 80 | + } |
| 81 | + |
| 82 | + var params []driver.Value |
| 83 | + for _, s := range []interface{}{"PAUSE_CHECK", int32(0)} { |
| 84 | + params = append(params, s) |
| 85 | + } |
| 86 | + |
| 87 | + vr, err := conn.Query("@Statistics", params) |
| 88 | + if err != nil { |
| 89 | + log.Fatal(err) |
| 90 | + } |
| 91 | + pretty.Print(vr) |
| 92 | + log.Println() |
| 93 | + log.Printf("SUCCESS") |
| 94 | + //defer conn.Close() |
| 95 | +} |
| 96 | + |
| 97 | +func main() { |
| 98 | + server := flag.String("server", "127.0.0.1", "Server name or IP address") |
| 99 | + altServer := flag.String("altserver", "", "Server name to validate instead of server") |
| 100 | + pemPath := flag.String("pem", "", "Full path to PEM file") |
| 101 | + insecure := flag.Bool("insecure", false, "If true, bypass validation") |
| 102 | + |
| 103 | + flag.Parse() |
| 104 | + |
| 105 | + tlsConnect(*server, *altServer, *pemPath, *insecure) |
| 106 | +} |
0 commit comments