Skip to content

Commit 42b6f34

Browse files
authored
Add basic functionality for downloading the Astra secure connect bundle (#69)
This adds new `--token` and `--database-id` flags to allow the proxy to connect to Astra w/o having to provide a secure connect bundle file. This is much more user friendly and makes the k8s configuration more concise. Fixes: #47
1 parent f6c7734 commit 42b6f34

File tree

5 files changed

+214
-4
lines changed

5 files changed

+214
-4
lines changed

astra/bundle.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@ package astra
1616

1717
import (
1818
"archive/zip"
19+
"bytes"
20+
"context"
1921
"crypto/tls"
2022
"crypto/x509"
2123
"encoding/json"
24+
"errors"
2225
"fmt"
2326
"io"
2427
"io/ioutil"
28+
"net/http"
2529
"runtime"
30+
"time"
31+
32+
"github.com/datastax/astra-client-go/v2/astra"
2633
)
2734

35+
const URL = "https://api.astra.datastax.com"
36+
2837
type Bundle struct {
2938
tlsConfig *tls.Config
3039
host string
@@ -85,6 +94,68 @@ func LoadBundleZipFromPath(path string) (*Bundle, error) {
8594
return LoadBundleZip(&reader.Reader)
8695
}
8796

97+
func LoadBundleZipFromURL(url, databaseID, token string, timeout time.Duration) (*Bundle, error) {
98+
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(timeout))
99+
defer cancel()
100+
101+
credsURL, err := generateSecureBundleURLWithResponse(url, databaseID, token, ctx)
102+
if err != nil {
103+
return nil, fmt.Errorf("error generating secure bundle zip URLs: %v", err)
104+
}
105+
resp, err := http.Get(credsURL.DownloadURL)
106+
107+
defer resp.Body.Close()
108+
109+
body, err := readAllWithTimeout(resp.Body, ctx)
110+
if err != nil {
111+
return nil, fmt.Errorf("error downloading bundle zip: %v", err)
112+
}
113+
114+
reader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
115+
if err != nil {
116+
return nil, fmt.Errorf("error creating zip reader for bundle zip: %v", err)
117+
}
118+
119+
return LoadBundleZip(reader)
120+
}
121+
122+
func readAllWithTimeout(r io.Reader, ctx context.Context) (bytes []byte, err error) {
123+
ch := make(chan struct{})
124+
125+
go func() {
126+
bytes, err = ioutil.ReadAll(r)
127+
close(ch)
128+
}()
129+
130+
select {
131+
case <-ch:
132+
case <-ctx.Done():
133+
return nil, errors.New("timeout reading data")
134+
}
135+
136+
return bytes, err
137+
}
138+
139+
func generateSecureBundleURLWithResponse(url, databaseID, token string, ctx context.Context) (*astra.CredsURL, error) {
140+
client, err := astra.NewClientWithResponses(url, func(c *astra.Client) error {
141+
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
142+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
143+
return nil
144+
})
145+
return nil
146+
})
147+
if err != nil {
148+
return nil, err
149+
}
150+
res, err := client.GenerateSecureBundleURLWithResponse(ctx, astra.DatabaseIdParam(databaseID))
151+
152+
if res.StatusCode() != http.StatusOK {
153+
return nil, fmt.Errorf("unable to generate bundle urls, failed with status code %d", res.StatusCode())
154+
}
155+
156+
return res.JSON200, nil
157+
}
158+
88159
func (b *Bundle) Host() string {
89160
return b.host
90161
}

astra/bundle_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ import (
2424
"encoding/asn1"
2525
"encoding/json"
2626
"encoding/pem"
27-
"github.com/stretchr/testify/assert"
28-
"github.com/stretchr/testify/require"
2927
"io"
3028
"io/ioutil"
3129
"math/big"
3230
"net"
3331
"sync"
3432
"testing"
3533
"time"
34+
35+
"github.com/stretchr/testify/assert"
36+
"github.com/stretchr/testify/require"
3637
)
3738

3839
func TestLoadBundleZip(t *testing.T) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.16
44

55
require (
66
github.com/alecthomas/kong v0.2.17
7-
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521184019-c5ad59b459ec
7+
github.com/datastax/astra-client-go/v2 v2.2.9 // indirect
88
github.com/datastax/go-cassandra-native-protocol v0.0.0-20211124104234-f6aea54fa801
99
github.com/hashicorp/golang-lru v0.5.4
1010
github.com/stretchr/testify v1.7.0

0 commit comments

Comments
 (0)