Debian Codegraph @

buildkit/cache/remotecache/azblob/utils.go

raw
1 package azblob
2
3 import (
4 "context"
5 "net/url"
6 "os"
7 "path/filepath"
8 "strings"
9 "time"
10
11 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
12 "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
13 "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
14 "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
15 "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
16 digest "github.com/opencontainers/go-digest"
17 "github.com/pkg/errors"
18 )
19
20 const (
21 attrSecretAccessKey = "secret_access_key"
22 attrAccountName = "account_name"
23 attrAccountURL = "account_url"
24 attrPrefix = "prefix"
25 attrManifestsPrefix = "manifests_prefix"
26 attrBlobsPrefix = "blobs_prefix"
27 attrName = "name"
28 attrContainer = "container"
29 IOConcurrency = 4
30 IOChunkSize = 32 * 1024 * 1024
31 )
32
33 type Config struct {
34 AccountURL string
35 Container string
36 Prefix string
37 ManifestsPrefix string
38 BlobsPrefix string
39 Names []string
40 AccountName string
41 secretAccessKey string
42 }
43
44 func getConfig(attrs map[string]string) (*Config, error) {
45 accountURLString, ok := attrs[attrAccountURL]
46 if !ok {
47 accountURLString, ok = os.LookupEnv("BUILDKIT_AZURE_STORAGE_ACCOUNT_URL")
48 if !ok {
49 return &Config{}, errors.New("either ${BUILDKIT_AZURE_STORAGE_ACCOUNT_URL} or account_url attribute is required for azblob cache")
50 }
51 }
52
53 accountURL, err := url.Parse(accountURLString)
54 if err != nil {
55 return &Config{}, errors.Wrap(err, "azure storage account url provided is not a valid url")
56 }
57
58 accountName, ok := attrs[attrAccountName]
59 if !ok {
60 accountName, ok = os.LookupEnv("BUILDKIT_AZURE_STORAGE_ACCOUNT_NAME")
61 if !ok {
62 accountName = strings.Split(accountURL.Hostname(), ".")[0]
63 }
64 }
65 if accountName == "" {
66 return &Config{}, errors.New("unable to retrieve account name from account url or ${BUILDKIT_AZURE_STORAGE_ACCOUNT_NAME} or account_name attribute for azblob cache")
67 }
68
69 container, ok := attrs[attrContainer]
70 if !ok {
71 container, ok = os.LookupEnv("BUILDKIT_AZURE_STORAGE_CONTAINER")
72 if !ok {
73 container = "buildkit-cache"
74 }
75 }
76
77 prefix, ok := attrs[attrPrefix]
78 if !ok {
79 prefix, _ = os.LookupEnv("BUILDKIT_AZURE_STORAGE_PREFIX")
80 }
81
82 manifestsPrefix, ok := attrs[attrManifestsPrefix]
83 if !ok {
84 manifestsPrefix = "manifests"
85 }
86
87 blobsPrefix, ok := attrs[attrBlobsPrefix]
88 if !ok {
89 blobsPrefix = "blobs"
90 }
91
92 names := []string{"buildkit"}
93 name, ok := attrs[attrName]
94 if ok {
95 splittedNames := strings.Split(name, ";")
96 if len(splittedNames) > 0 {
97 names = splittedNames
98 }
99 }
100
101 secretAccessKey := attrs[attrSecretAccessKey]
102
103 config := Config{
104 AccountURL: accountURLString,
105 AccountName: accountName,
106 Container: container,
107 Prefix: prefix,
108 Names: names,
109 ManifestsPrefix: manifestsPrefix,
110 BlobsPrefix: blobsPrefix,
111 secretAccessKey: secretAccessKey,
112 }
113
114 return &config, nil
115 }
116
117 func createContainerClient(ctx context.Context, config *Config) (*container.Client, error) {
118 var client *azblob.Client
119 if config.secretAccessKey != "" {
120 sharedKey, err := azblob.NewSharedKeyCredential(config.AccountName, config.secretAccessKey)
121 if err != nil {
122 return nil, errors.Wrap(err, "failed to create shared key")
123 }
124 client, err = azblob.NewClientWithSharedKeyCredential(config.AccountURL, sharedKey, &azblob.ClientOptions{})
125 if err != nil {
126 return nil, errors.Wrap(err, "failed to created service client from shared key")
127 }
128 } else {
129 cred, err := azidentity.NewDefaultAzureCredential(nil)
130 if err != nil {
131 return nil, errors.Wrap(err, "failed to create default azure credentials")
132 }
133
134 client, err = azblob.NewClient(config.AccountURL, cred, &azblob.ClientOptions{})
135 if err != nil {
136 return nil, errors.Wrap(err, "failed to create service client")
137 }
138 }
139
140 ctx, cnclFn := context.WithCancelCause(ctx)
141 ctx, _ = context.WithTimeoutCause(ctx, time.Second*60, errors.WithStack(context.DeadlineExceeded)) //nolint:govet
142 defer cnclFn(errors.WithStack(context.Canceled))
143
144 containerClient := client.ServiceClient().NewContainerClient(config.Container)
145
146 _, err := containerClient.GetProperties(ctx, &container.GetPropertiesOptions{})
147 if err == nil {
148 return containerClient, nil
149 }
150
151 if bloberror.HasCode(err, bloberror.ContainerNotFound) {
152 ctx, cnclFn := context.WithCancelCause(ctx)
153 ctx, _ = context.WithTimeoutCause(ctx, time.Minute*5, errors.WithStack(context.DeadlineExceeded)) //nolint:govet
154 defer cnclFn(errors.WithStack(context.Canceled))
155 _, err := containerClient.Create(ctx, &container.CreateOptions{})
156 if err != nil {
157 return nil, errors.Wrapf(err, "failed to create cache container %s", config.Container)
158 }
159 return containerClient, nil
160 }
161
162 return nil, errors.Wrapf(err, "failed to get properties of cache container %s", config.Container)
163 }
164
165 func manifestKey(config *Config, name string) string {
166 key := filepath.Join(config.Prefix, config.ManifestsPrefix, name)
167 return key
168 }
169
170 func blobKey(config *Config, digest digest.Digest) string {
171 key := filepath.Join(config.Prefix, config.BlobsPrefix, digest.String())
172 return key
173 }
174
175 func blobExists(ctx context.Context, containerClient *container.Client, blobKey string) (bool, error) {
176 blobClient := containerClient.NewBlobClient(blobKey)
177 ctx, cnclFn := context.WithCancelCause(ctx)
178 ctx, _ = context.WithTimeoutCause(ctx, time.Second*60, errors.WithStack(context.DeadlineExceeded)) //nolint:govet
179 defer cnclFn(errors.WithStack(context.Canceled))
180 _, err := blobClient.GetProperties(ctx, &blob.GetPropertiesOptions{})
181 if err == nil {
182 return true, nil
183 }
184
185 if bloberror.HasCode(err, bloberror.BlobNotFound) {
186 return false, nil
187 }
188
189 return false, errors.Wrapf(err, "failed to check blob %s existence", blobKey)
190 }