Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Better cache value for custom cert
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed Aug 19, 2021
1 parent 4add550 commit 256dd5c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cmd/sql-proxy-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -68,30 +69,42 @@ func realMain() error {

var certSource proxy.CertSource
var err error
var instance string

if *token != "" || (*serviceToken != "" && *serviceTokenName != "") {
if *orgName == "" || *dbName == "" || *branchName == "" {
return errors.New("--org, --database or --branch is not set with a token")
}
instance = fmt.Sprintf("%s/%s/%s", *orgName, *dbName, *branchName)

certSource, err = newRemoteCertSource(*token, *serviceToken, *serviceTokenName)
if err != nil {
return err
}
}

if *remoteHost != "" && *clientCertPath != "" && *clientKeyPath != "" {
certSource, err = newLocalCertSource(*clientCertPath, *clientKeyPath, *remoteHost, *remotePort)
localCertSource, err := newLocalCertSource(*clientCertPath, *clientKeyPath, *remoteHost, *remotePort)
if err != nil {
return err
}
certSource = localCertSource
cert, err := x509.ParseCertificate(localCertSource.cert.Certificate[0])
if err != nil {
return err
}
instance = cert.Subject.String()
}

if certSource == nil {
return fmt.Errorf("no configuration found, need either a token and org / datbase / branch parameters or separate specified certificate source and remote host")
return errors.New("no configuration found, need either a token and org / datbase / branch parameters or separate specified certificate source and remote host")
}

p, err := proxy.NewClient(proxy.Options{
CertSource: certSource,
LocalAddr: net.JoinHostPort(*host, *port),
RemoteAddr: net.JoinHostPort(*remoteHost, strconv.Itoa(*remotePort)),
Instance: fmt.Sprintf("%s/%s/%s", *orgName, *dbName, *branchName),
Instance: instance,
})
if err != nil {
return fmt.Errorf("couldn't create proxy client: %s", err)
Expand Down

0 comments on commit 256dd5c

Please sign in to comment.