Skip to content

Commit 3e7d8b1

Browse files
committed
Auto-enable TLS for access nodes on port 443
1 parent 88729a7 commit 3e7d8b1

6 files changed

Lines changed: 29 additions & 9 deletions

File tree

internal/accounts/create-interactive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func createInteractive(
5959
privateFile := accounts.PrivateKeyFile(name, "")
6060

6161
// create new gateway based on chosen network
62-
gw, err := gateway.NewGrpcGateway(selectedNetwork)
62+
gw, err := gateway.NewGrpcGateway(selectedNetwork, util.GRPCDialOptionForHost(selectedNetwork.Host))
6363
if err != nil {
6464
return nil, err
6565
}

internal/accounts/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func validateAccountOnNetwork(account *accounts.Account, network *config.Network
213213
var gw gateway.Gateway
214214
var err error
215215

216-
gw, err = gateway.NewGrpcGateway(*network)
216+
gw, err = gateway.NewGrpcGateway(*network, util.GRPCDialOptionForHost(network.Host))
217217

218218
if err != nil {
219219
result.Error = fmt.Sprintf("Failed to create gateway: %v", err)

internal/command/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func createGateway(network config.Network) (gateway.Gateway, error) {
193193
return gateway.NewSecureGrpcGateway(network)
194194
}
195195

196-
return gateway.NewGrpcGateway(network)
196+
return gateway.NewGrpcGateway(network, util.GRPCDialOptionForHost(network.Host))
197197
}
198198

199199
// resolveHost from the flags provided.

internal/dependencymanager/dependencyinstaller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ func NewDependencyInstaller(logger output.Logger, state *flowkit.State, saveStat
167167
return nil, fmt.Errorf("cannot use both --update and --skip-update-prompts flags together")
168168
}
169169

170-
emulatorGateway, err := gateway.NewGrpcGateway(config.EmulatorNetwork)
170+
emulatorGateway, err := gateway.NewGrpcGateway(config.EmulatorNetwork, util.GRPCDialOptionForHost(config.EmulatorNetwork.Host))
171171
if err != nil {
172172
return nil, fmt.Errorf("error creating emulator gateway: %v", err)
173173
}
174174

175-
testnetGateway, err := gateway.NewGrpcGateway(config.TestnetNetwork)
175+
testnetGateway, err := gateway.NewGrpcGateway(config.TestnetNetwork, util.GRPCDialOptionForHost(config.TestnetNetwork.Host))
176176
if err != nil {
177177
return nil, fmt.Errorf("error creating testnet gateway: %v", err)
178178
}
179179

180-
mainnetGateway, err := gateway.NewGrpcGateway(config.MainnetNetwork)
180+
mainnetGateway, err := gateway.NewGrpcGateway(config.MainnetNetwork, util.GRPCDialOptionForHost(config.MainnetNetwork.Host))
181181
if err != nil {
182182
return nil, fmt.Errorf("error creating mainnet gateway: %v", err)
183183
}

internal/mcp/mcp.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"github.com/onflow/flowkit/v2"
3131
"github.com/onflow/flowkit/v2/config"
3232
"github.com/onflow/flowkit/v2/gateway"
33+
34+
"github.com/onflow/flow-cli/internal/util"
3335
)
3436

3537
var Cmd = &cobra.Command{
@@ -128,5 +130,5 @@ func createGateway(state *flowkit.State, network string) (gateway.Gateway, error
128130
if net.Key != "" {
129131
return gateway.NewSecureGrpcGateway(*net)
130132
}
131-
return gateway.NewGrpcGateway(*net)
133+
return gateway.NewGrpcGateway(*net, util.GRPCDialOptionForHost(net.Host))
132134
}

internal/util/util.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package util
2121
import (
2222
"bytes"
2323
"context"
24+
"crypto/tls"
2425
"encoding/hex"
2526
"fmt"
2627
"net"
@@ -38,6 +39,7 @@ import (
3839
flowGo "github.com/onflow/flow-go/model/flow"
3940
flowaccess "github.com/onflow/flow/protobuf/go/flow/access"
4041
grpcOpts "google.golang.org/grpc"
42+
"google.golang.org/grpc/credentials"
4143
"google.golang.org/grpc/credentials/insecure"
4244

4345
emulatorUtils "github.com/onflow/flow-emulator/utils"
@@ -74,7 +76,7 @@ func IsAddressValidForNetwork(address flow.Address, networkName string) bool {
7476
// by querying the access node to get the actual chain ID
7577
func ValidateAddressForNetwork(address flow.Address, network *config.Network) error {
7678
// Create a grpc client to query the network
77-
client, err := grpc.NewBaseClient(network.Host, grpcOpts.WithTransportCredentials(insecure.NewCredentials()))
79+
client, err := grpc.NewBaseClient(network.Host, TransportCredentialForHost(network.Host))
7880
if err != nil {
7981
return fmt.Errorf("failed to connect to access node: %w", err)
8082
}
@@ -244,6 +246,22 @@ func AddFlowEntriesToCursorIgnore(targetDir string, loader flowkit.ReaderWriter)
244246
return addEntriesToIgnoreFile(cursorIgnorePath, flowEntries, loader)
245247
}
246248

249+
// TransportCredentialForHost returns TLS credentials using system CA certificates
250+
// if the host uses port 443, or insecure credentials otherwise.
251+
func TransportCredentialForHost(host string) grpcOpts.DialOption {
252+
_, port, err := net.SplitHostPort(host)
253+
if err == nil && port == "443" {
254+
return grpcOpts.WithTransportCredentials(credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12}))
255+
}
256+
return grpcOpts.WithTransportCredentials(insecure.NewCredentials())
257+
}
258+
259+
// GRPCDialOptionForHost returns a grpcAccess.ClientOption that configures
260+
// TLS using system CA certificates for port 443 hosts, or insecure credentials otherwise.
261+
func GRPCDialOptionForHost(host string) grpc.ClientOption {
262+
return grpc.WithGRPCDialOptions(TransportCredentialForHost(host))
263+
}
264+
247265
// GetAddressNetwork returns the chain ID for an address.
248266
func GetAddressNetwork(address flow.Address) (flow.ChainID, error) {
249267
networks := []flow.ChainID{
@@ -282,7 +300,7 @@ func GetChainIDFromHost(host string) (flowGo.ChainID, error) {
282300

283301
conn, err := grpcOpts.NewClient(
284302
host,
285-
grpcOpts.WithTransportCredentials(insecure.NewCredentials()),
303+
TransportCredentialForHost(host),
286304
emulatorUtils.DefaultGRPCRetryInterceptor(),
287305
)
288306
if err != nil {

0 commit comments

Comments
 (0)