Support Azure account key via environment variable

This commit is contained in:
Ben Johnson
2021-06-02 13:38:39 -06:00
parent 1c0c69a5ab
commit b2233cf4de
2 changed files with 34 additions and 26 deletions

View File

@@ -56,8 +56,14 @@ func (c *ReplicaClient) Init(ctx context.Context) (err error) {
return nil return nil
} }
// Read account key from environment, if available.
accountKey := c.AccountKey
if accountKey == "" {
accountKey = os.Getenv("LITESTREAM_AZURE_ACCOUNT_KEY")
}
// Authenticate to ACS. // Authenticate to ACS.
credential, err := azblob.NewSharedKeyCredential(c.AccountName, c.AccountKey) credential, err := azblob.NewSharedKeyCredential(c.AccountName, accountKey)
if err != nil { if err != nil {
return err return err
} }
@@ -487,6 +493,7 @@ func (itr *walSegmentIterator) fetch() error {
} }
marker = resp.NextMarker marker = resp.NextMarker
println("dbg/wal.fetch", len(resp.Segment.BlobItems))
for _, item := range resp.Segment.BlobItems { for _, item := range resp.Segment.BlobItems {
key := path.Base(item.Name) key := path.Base(item.Name)
index, offset, err := litestream.ParseWALSegmentPath(key) index, offset, err := litestream.ParseWALSegmentPath(key)

View File

@@ -502,36 +502,37 @@ func newABSReplicaClientFromConfig(c *ReplicaConfig, r *litestream.Replica) (_ *
return nil, fmt.Errorf("cannot specify url & bucket for abs replica") return nil, fmt.Errorf("cannot specify url & bucket for abs replica")
} }
bucket, path := c.Bucket, c.Path
// Apply settings from URL, if specified.
if c.URL != "" {
_, uhost, upath, err := ParseReplicaURL(c.URL)
if err != nil {
return nil, err
}
// Only apply URL parts to field that have not been overridden.
if path == "" {
path = upath
}
if bucket == "" {
bucket = uhost
}
}
// Ensure required settings are set.
if bucket == "" {
return nil, fmt.Errorf("bucket required for abs replica")
}
// Build replica. // Build replica.
client := abs.NewReplicaClient() client := abs.NewReplicaClient()
client.AccountName = c.AccountName client.AccountName = c.AccountName
client.AccountKey = c.AccountKey client.AccountKey = c.AccountKey
client.Bucket = bucket client.Bucket = c.Bucket
client.Path = path client.Path = c.Path
client.Endpoint = c.Endpoint client.Endpoint = c.Endpoint
// Apply settings from URL, if specified.
if c.URL != "" {
u, err := url.Parse(c.URL)
if err != nil {
return nil, err
}
if client.AccountName == "" && u.User != nil {
client.AccountName = u.User.Username()
}
if client.Bucket == "" {
client.Bucket = u.Host
}
if client.Path == "" {
client.Path = strings.TrimPrefix(path.Clean(u.Path), "/")
}
}
// Ensure required settings are set.
if client.Bucket == "" {
return nil, fmt.Errorf("bucket required for abs replica")
}
return client, nil return client, nil
} }