Skip to content

Commit 8ec2c65

Browse files
fix tls and cached nearby check (#45)
* remove tls * cleanup and a couple tls fixes * more cleanup --------- Co-authored-by: Luke Lombardi <[email protected]>
1 parent a940ff0 commit 8ec2c65

File tree

11 files changed

+88
-32
lines changed

11 files changed

+88
-32
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/moby/sys/mountinfo v0.7.2
2121
github.com/redis/go-redis/v9 v9.5.1
2222
github.com/shirou/gopsutil v2.21.11+incompatible
23+
github.com/tj/assert v0.0.3
2324
google.golang.org/grpc v1.71.1
2425
google.golang.org/protobuf v1.36.4
2526
gotest.tools v2.2.0+incompatible
@@ -30,6 +31,7 @@ require (
3031
github.com/kr/pretty v0.3.1 // indirect
3132
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
3233
github.com/rogpeppe/go-internal v1.11.0 // indirect
34+
github.com/stretchr/testify v1.9.0 // indirect
3335
)
3436

3537
require (

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
1212
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
1313
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
1414
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
15+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
16+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1517
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
1618
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1719
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
@@ -85,6 +87,7 @@ github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBL
8587
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
8688
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
8789
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
90+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8891
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
8992
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9093
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
@@ -97,8 +100,12 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
97100
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
98101
github.com/shirou/gopsutil v2.21.11+incompatible h1:lOGOyCG67a5dv2hq5Z1BLDUqqKp3HkbjPcz5j6XMS0U=
99102
github.com/shirou/gopsutil v2.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
103+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
104+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
100105
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
101106
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
107+
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
108+
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
102109
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
103110
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
104111
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
@@ -135,6 +142,8 @@ google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojt
135142
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
136143
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
137144
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
145+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
146+
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
138147
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
139148
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
140149
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=

pkg/blobfs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ func NewFileSystem(ctx context.Context, opts BlobFsSystemOpts) (*BlobFs, error)
187187

188188
err := coordinatorClient.SetFsNode(bfs.ctx, rootID, dirMeta)
189189
if err != nil {
190-
Logger.Fatalf("Unable to create blobfs root node dir metdata: %+v", err)
190+
Logger.Errorf("Unable to create blobfs root node dir metdata: %+v", err)
191+
return nil, err
191192
}
192193
}
193194

pkg/client.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package blobcache
22

33
import (
44
"context"
5+
"crypto/tls"
56
"io"
67
"sync"
78
"time"
@@ -10,6 +11,7 @@ import (
1011
rendezvous "github.com/beam-cloud/rendezvous"
1112
"github.com/hanwen/go-fuse/v2/fuse"
1213
"google.golang.org/grpc"
14+
"google.golang.org/grpc/credentials"
1315
"google.golang.org/grpc/credentials/insecure"
1416
"google.golang.org/grpc/metadata"
1517
)
@@ -133,16 +135,17 @@ func (c *BlobCacheClient) GetNearbyHosts() ([]*BlobCacheHost, error) {
133135
}
134136

135137
func (c *BlobCacheClient) addHost(host *BlobCacheHost) error {
136-
transportCredentials := grpc.WithTransportCredentials(insecure.NewCredentials())
137-
138-
// TODO: revisit TLS here for external addresses, for now since we're using the internal (private) addresses, it should only use TLS for the coordinator
139-
140138
addr := host.Addr
141-
142139
if host.PrivateAddr != "" {
143140
addr = host.PrivateAddr
144141
}
145142

143+
transportCredentials := grpc.WithTransportCredentials(insecure.NewCredentials())
144+
if isTLSEnabled(addr) {
145+
h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
146+
transportCredentials = grpc.WithTransportCredentials(h2creds)
147+
}
148+
146149
var dialOpts = []grpc.DialOption{
147150
transportCredentials,
148151
grpc.WithContextDialer(DialWithTimeout),

pkg/config.default.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ global:
3939
grpcMessageSizeBytes: 1000000000
4040
debugMode: false
4141
prettyLogs: false
42-
tlsEnabled: false
4342
sources: []

pkg/coordinator_remote.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ type CoordinatorClientRemote struct {
1818
}
1919

2020
func NewCoordinatorClientRemote(cfg BlobCacheGlobalConfig, token string) (CoordinatorClient, error) {
21-
transportCredentials := grpc.WithTransportCredentials(insecure.NewCredentials())
21+
addr := cfg.CoordinatorHost
2222

23-
isTLS := cfg.TLSEnabled
24-
if isTLS {
23+
transportCredentials := grpc.WithTransportCredentials(insecure.NewCredentials())
24+
if isTLSEnabled(addr) {
2525
h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
2626
transportCredentials = grpc.WithTransportCredentials(h2creds)
2727
}
2828

29-
addr := cfg.CoordinatorHost
30-
3129
var dialOpts = []grpc.DialOption{
3230
transportCredentials,
3331
grpc.WithContextDialer(DialWithTimeout),

pkg/discovery.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package blobcache
22

33
import (
44
"context"
5+
"crypto/tls"
56
"fmt"
67
"sync"
78
"time"
89

910
proto "github.com/beam-cloud/blobcache-v2/proto"
1011
"google.golang.org/grpc"
12+
"google.golang.org/grpc/credentials"
1113
"google.golang.org/grpc/credentials/insecure"
1214
)
1315

@@ -68,28 +70,27 @@ func (d *DiscoveryClient) discoverHosts(ctx context.Context) ([]*BlobCacheHost,
6870
mu := sync.Mutex{}
6971

7072
for _, host := range hosts {
71-
if host.PrivateAddr != "" {
72-
// Don't try to get the state on peers we're already aware of
73-
if d.hostMap.Get(host.HostId) != nil {
74-
continue
75-
}
73+
// Don't try to get the state on peers we're already aware of
74+
if d.hostMap.Get(host.HostId) != nil {
75+
continue
76+
}
7677

77-
wg.Add(1)
78-
go func(addr string) {
79-
defer wg.Done()
78+
wg.Add(1)
79+
go func(addr string) {
80+
defer wg.Done()
8081

81-
hostState, err := d.GetHostState(ctx, host)
82-
if err != nil {
83-
return
84-
}
82+
hostState, err := d.GetHostState(ctx, host)
83+
if err != nil {
84+
return
85+
}
8586

86-
mu.Lock()
87-
filteredHosts = append(filteredHosts, hostState)
88-
mu.Unlock()
87+
mu.Lock()
88+
filteredHosts = append(filteredHosts, hostState)
89+
mu.Unlock()
90+
91+
Logger.Debugf("Added host with private address to map: %s", hostState.PrivateAddr)
92+
}(host.Addr)
8993

90-
Logger.Debugf("Added host with private address to map: %s", hostState.PrivateAddr)
91-
}(host.Addr)
92-
}
9394
}
9495

9596
wg.Wait()
@@ -98,7 +99,18 @@ func (d *DiscoveryClient) discoverHosts(ctx context.Context) ([]*BlobCacheHost,
9899

99100
// GetHostState attempts to connect to the gRPC service and verifies its availability
100101
func (d *DiscoveryClient) GetHostState(ctx context.Context, host *BlobCacheHost) (*BlobCacheHost, error) {
102+
addr := host.Addr
103+
104+
if host.PrivateAddr != "" {
105+
addr = host.PrivateAddr
106+
}
107+
101108
transportCredentials := grpc.WithTransportCredentials(insecure.NewCredentials())
109+
if isTLSEnabled(addr) {
110+
h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
111+
transportCredentials = grpc.WithTransportCredentials(h2creds)
112+
}
113+
102114

103115
var dialOpts = []grpc.DialOption{
104116
transportCredentials,

pkg/network.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ func getDefaultInterface() (string, error) {
3030
return "", fmt.Errorf("default route not found")
3131
}
3232

33+
func isTLSEnabled(addr string) bool {
34+
_, port, err := net.SplitHostPort(addr)
35+
if err != nil {
36+
return false
37+
}
38+
return port == "443"
39+
}
40+
3341
func GetPublicIpAddr() (string, error) {
3442
resp, err := http.Get("https://api.ipify.org?format=text")
3543
if err != nil {

pkg/network_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package blobcache
2+
3+
import (
4+
"testing"
5+
6+
"github.com/tj/assert"
7+
)
8+
9+
func Test_IsTLSEnabled(t *testing.T) {
10+
assert.True(t, isTLSEnabled("localhost:443"))
11+
assert.False(t, isTLSEnabled("localhost:2049"))
12+
assert.True(t, isTLSEnabled("127.0.0.1:443"))
13+
assert.False(t, isTLSEnabled("127.0.0.1:2049"))
14+
}

pkg/storage.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,18 @@ func (cas *ContentAddressableStorage) Add(ctx context.Context, hash string, cont
162162
}
163163

164164
func (cas *ContentAddressableStorage) Exists(hash string) bool {
165-
_, exists := cas.cache.GetTTL(hash)
165+
var exists bool = false
166+
167+
_, exists = cas.cache.GetTTL(hash)
168+
if !exists {
169+
exists, err := os.Stat(filepath.Join(cas.diskCacheDir, hash))
170+
if err != nil {
171+
return false
172+
}
173+
174+
return exists.IsDir()
175+
}
176+
166177
return exists
167178
}
168179

0 commit comments

Comments
 (0)