Skip to content

Commit

Permalink
Merge pull request #7395 from TheThingsNetwork/fix/cli-tls
Browse files Browse the repository at this point in the history
Pass custom CA to grpc dial option
  • Loading branch information
KrishnaIyer authored Nov 19, 2024
2 parents 50bc235 + 51670f6 commit 9fb31df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/ttn-lw-cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ func preRun(tasks ...func() error) func(cmd *cobra.Command, args []string) error
}
rootCAs.AppendCertsFromPEM(pemBytes)
http.DefaultTransport.(*http.Transport).TLSClientConfig.RootCAs = rootCAs
if err = api.AddCA(pemBytes); err != nil {
return err
}
}

// OAuth
Expand Down
19 changes: 19 additions & 0 deletions cmd/ttn-lw-cli/internal/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package api
import (
"context"
"crypto/tls"
"crypto/x509"
"sync"
"time"

Expand Down Expand Up @@ -134,6 +135,24 @@ var (
conns = make(map[string]*grpc.ClientConn)
)

// AddCA adds the CA certificate file.
func AddCA(pemBytes []byte) (err error) {
if tlsConfig == nil {
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
rootCAs := tlsConfig.RootCAs
if rootCAs == nil {
if rootCAs, err = x509.SystemCertPool(); err != nil {
rootCAs = x509.NewCertPool()
}
}
rootCAs.AppendCertsFromPEM(pemBytes)
tlsConfig.RootCAs = rootCAs
return nil
}

// Dial dials a gRPC connection to the target.
func Dial(ctx context.Context, target string) (*grpc.ClientConn, error) {
connMu.Lock()
Expand Down

0 comments on commit 9fb31df

Please sign in to comment.