|
| 1 | +// |
| 2 | +// Copyright 2023 PayPal Inc. |
| 3 | +// |
| 4 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +// contributor license agreements. See the NOTICE file distributed with |
| 6 | +// this work for additional information regarding copyright ownership. |
| 7 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +// (the "License"); you may not use this file except in compliance with |
| 9 | +// the License. You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cli |
| 21 | + |
| 22 | +import ( |
| 23 | + "crypto/tls" |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "net" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/paypal/junodb/pkg/logging" |
| 30 | + "github.com/paypal/junodb/pkg/logging/cal" |
| 31 | +) |
| 32 | + |
| 33 | +func TLSInitialized() bool { |
| 34 | + return false |
| 35 | +} |
| 36 | + |
| 37 | +func Dial(addr string, timeout time.Duration, getTLSConfig func() *tls.Config) (conn net.Conn, err error) { |
| 38 | + var tlsConn *tls.Conn |
| 39 | + |
| 40 | + if getTLSConfig == nil { |
| 41 | + return nil, errors.New("Unable to get TLS config") |
| 42 | + } |
| 43 | + timeStart := time.Now() |
| 44 | + tlsCfg := getTLSConfig() |
| 45 | + if tlsCfg == nil { |
| 46 | + err = errors.New("Unable to get TLS config") |
| 47 | + } else { |
| 48 | + dialer := &net.Dialer{Timeout: timeout} |
| 49 | + tlsConn, err = tls.DialWithDialer(dialer, "tcp", addr, tlsCfg) |
| 50 | + conn = tlsConn |
| 51 | + if tlsConn == nil && err == nil { |
| 52 | + err = errors.New("Connect failed.") |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if !cal.IsEnabled() { |
| 57 | + return conn, err |
| 58 | + } |
| 59 | + |
| 60 | + // Cal logging |
| 61 | + status := cal.StatusSuccess |
| 62 | + b := logging.NewKVBuffer() |
| 63 | + if err != nil { |
| 64 | + status = cal.StatusError |
| 65 | + b.Add([]byte("err"), err.Error()) |
| 66 | + } else { |
| 67 | + b.Add([]byte("ssl"), getConnectionState(tlsConn)) |
| 68 | + } |
| 69 | + |
| 70 | + cal.AtomicTransaction(cal.TxnTypeConnect, addr, status, |
| 71 | + time.Since(timeStart), b.Bytes()) |
| 72 | + |
| 73 | + return conn, err |
| 74 | +} |
| 75 | + |
| 76 | +func getConnectionState(c *tls.Conn) string { |
| 77 | + if c == nil { |
| 78 | + return "" |
| 79 | + } |
| 80 | + |
| 81 | + st := c.ConnectionState() |
| 82 | + rid := 0 |
| 83 | + if st.DidResume { |
| 84 | + rid = 1 |
| 85 | + } |
| 86 | + msg := fmt.Sprintf("GoTLS:%s:%s:ssl_r=%d", getVersionName(st.Version), |
| 87 | + tls.CipherSuiteName(st.CipherSuite), rid) |
| 88 | + |
| 89 | + return msg |
| 90 | +} |
| 91 | + |
| 92 | +func getVersionName(ver uint16) string { |
| 93 | + switch ver { |
| 94 | + case tls.VersionSSL30: |
| 95 | + return "SSLv3" |
| 96 | + case tls.VersionTLS10: |
| 97 | + return "TLSv1" |
| 98 | + case tls.VersionTLS11: |
| 99 | + return "TLSv1.1" |
| 100 | + case tls.VersionTLS12: |
| 101 | + return "TLSv1.2" |
| 102 | + case tls.VersionTLS13: |
| 103 | + return "TLSv1.3" |
| 104 | + default: |
| 105 | + return "" |
| 106 | + } |
| 107 | +} |
0 commit comments