File: adapter.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (114 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gcs

import (
	"fmt"
	"net/http"
	"net/url"
	"time"

	"cloud.google.com/go/storage"
	"github.com/sirupsen/logrus"

	"gitlab.com/gitlab-org/gitlab-runner/cache"
	"gitlab.com/gitlab-org/gitlab-runner/common"
)

type signedURLGenerator func(bucket string, name string, opts *storage.SignedURLOptions) (string, error)

type gcsAdapter struct {
	timeout    time.Duration
	config     *common.CacheGCSConfig
	objectName string

	generateSignedURL   signedURLGenerator
	credentialsResolver credentialsResolver
}

func (a *gcsAdapter) GetDownloadURL() *url.URL {
	return a.presignURL(http.MethodGet, "")
}

func (a *gcsAdapter) GetUploadURL() *url.URL {
	return a.presignURL(http.MethodPut, "application/octet-stream")
}

func (a *gcsAdapter) GetUploadHeaders() http.Header {
	return nil
}

func (a *gcsAdapter) GetGoCloudURL() *url.URL {
	return nil
}

func (a *gcsAdapter) GetUploadEnv() map[string]string {
	return nil
}

func (a *gcsAdapter) presignURL(method string, contentType string) *url.URL {
	err := a.credentialsResolver.Resolve()
	if err != nil {
		logrus.Errorf("error while resolving GCS credentials: %v", err)
		return nil
	}

	credentials := a.credentialsResolver.Credentials()

	var privateKey []byte
	if credentials.PrivateKey != "" {
		privateKey = []byte(credentials.PrivateKey)
	}

	if a.config.BucketName == "" {
		logrus.Error("BucketName can't be empty")
		return nil
	}

	rawURL, err := a.generateSignedURL(a.config.BucketName, a.objectName, &storage.SignedURLOptions{
		GoogleAccessID: credentials.AccessID,
		PrivateKey:     privateKey,
		Method:         method,
		Expires:        time.Now().Add(a.timeout),
		ContentType:    contentType,
	})
	if err != nil {
		logrus.Errorf("error while generating GCS pre-signed URL: %v", err)
		return nil
	}

	URL, err := url.Parse(rawURL)
	if err != nil {
		logrus.Errorf("error while parsing generated URL: %v", err)
		return nil
	}

	return URL
}

func New(config *common.CacheConfig, timeout time.Duration, objectName string) (cache.Adapter, error) {
	gcs := config.GCS
	if gcs == nil {
		return nil, fmt.Errorf("missing GCS configuration")
	}

	cr, err := credentialsResolverInitializer(gcs)
	if err != nil {
		return nil, fmt.Errorf("error while initializing GCS credentials resolver: %w", err)
	}

	a := &gcsAdapter{
		config:              gcs,
		timeout:             timeout,
		objectName:          objectName,
		generateSignedURL:   storage.SignedURL,
		credentialsResolver: cr,
	}

	return a, nil
}

func init() {
	err := cache.Factories().Register("gcs", New)
	if err != nil {
		panic(err)
	}
}