Skip to content

Commit

Permalink
Support Azure account key via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Jun 2, 2021
1 parent 1c0c69a commit b2233cf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
9 changes: 8 additions & 1 deletion abs/replica_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ func (c *ReplicaClient) Init(ctx context.Context) (err error) {
return nil
}

// Read account key from environment, if available.
accountKey := c.AccountKey
if accountKey == "" {
accountKey = os.Getenv("LITESTREAM_AZURE_ACCOUNT_KEY")
}

// Authenticate to ACS.
credential, err := azblob.NewSharedKeyCredential(c.AccountName, c.AccountKey)
credential, err := azblob.NewSharedKeyCredential(c.AccountName, accountKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -487,6 +493,7 @@ func (itr *walSegmentIterator) fetch() error {
}
marker = resp.NextMarker

println("dbg/wal.fetch", len(resp.Segment.BlobItems))
for _, item := range resp.Segment.BlobItems {
key := path.Base(item.Name)
index, offset, err := litestream.ParseWALSegmentPath(key)
Expand Down
31 changes: 16 additions & 15 deletions cmd/litestream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,36 +502,37 @@ func newABSReplicaClientFromConfig(c *ReplicaConfig, r *litestream.Replica) (_ *
return nil, fmt.Errorf("cannot specify url & bucket for abs replica")
}

bucket, path := c.Bucket, c.Path
// Build replica.
client := abs.NewReplicaClient()
client.AccountName = c.AccountName
client.AccountKey = c.AccountKey
client.Bucket = c.Bucket
client.Path = c.Path
client.Endpoint = c.Endpoint

// Apply settings from URL, if specified.
if c.URL != "" {
_, uhost, upath, err := ParseReplicaURL(c.URL)
u, err := url.Parse(c.URL)
if err != nil {
return nil, err
}

// Only apply URL parts to field that have not been overridden.
if path == "" {
path = upath
if client.AccountName == "" && u.User != nil {
client.AccountName = u.User.Username()
}
if bucket == "" {
bucket = uhost
if client.Bucket == "" {
client.Bucket = u.Host
}
if client.Path == "" {
client.Path = strings.TrimPrefix(path.Clean(u.Path), "/")
}
}

// Ensure required settings are set.
if bucket == "" {
if client.Bucket == "" {
return nil, fmt.Errorf("bucket required for abs replica")
}

// Build replica.
client := abs.NewReplicaClient()
client.AccountName = c.AccountName
client.AccountKey = c.AccountKey
client.Bucket = bucket
client.Path = path
client.Endpoint = c.Endpoint
return client, nil
}

Expand Down

0 comments on commit b2233cf

Please sign in to comment.