• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / deployments / 898138042

pending completion
898138042

Pull #867

gitlab-ci

kjaskiewiczz
chore: check database version only once

Signed-off-by: Krzysztof Jaskiewicz <krzysztof.jaskiewicz@northern.tech>
Pull Request #867: feat: make releases persistent in the database

94 of 258 new or added lines in 5 files covered. (36.43%)

55 existing lines in 5 files now uncovered.

7187 of 9193 relevant lines covered (78.18%)

34.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.62
/storage/s3/options.go
1
// Copyright 2022 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package s3
16

17
import (
18
        "crypto/tls"
19
        "net/http"
20
        "time"
21

22
        "github.com/aws/aws-sdk-go-v2/aws"
23
        "github.com/aws/aws-sdk-go-v2/service/s3"
24
        validation "github.com/go-ozzo/ozzo-validation/v4"
25

26
        "github.com/mendersoftware/deployments/storage"
27
)
28

29
const (
30
        kib = 1024
31
        mib = kib * 1024
32

33
        DefaultBufferSize = 10 * mib
34
        DefaultExpire     = 15 * time.Minute
35
)
36

37
var (
38
        validAtLeast5MiB = validation.Min(MultipartMinSize).
39
                Error("must be at least 5MiB")
40
)
41

42
type Options struct {
43
        // StaticCredentials that overrides AWS config.
44
        StaticCredentials *StaticCredentials `json:"auth"`
45

46
        // Region where the bucket lives
47
        Region *string
48
        // ContentType of the uploaded objects
49
        ContentType *string
50
        // FilenameSuffix adds the suffix to the content-disposition for object downloads>
51
        FilenameSuffix *string
52
        // ExternalURI is the URI used for signing requests.
53
        ExternalURI *string
54
        // URI is the URI for the s3 API.
55
        URI *string
56

57
        // ForcePathStyle encodes bucket in the API path.
58
        ForcePathStyle bool
59
        // UseAccelerate enables s3 Accelerate
60
        UseAccelerate bool
61

62
        // DefaultExpire is the fallback presign expire duration
63
        // (defaults to 15min).
64
        DefaultExpire *time.Duration
65
        // BufferSize sets the buffer size allocated for uploads.
66
        // This implicitly sets the upper limit for upload size:
67
        // BufferSize * 10000 (defaults to: 5MiB).
68
        BufferSize *int
69
}
70

71
func NewOptions(opts ...*Options) *Options {
1✔
72
        defaultBufferSize := DefaultBufferSize
1✔
73
        ret := &Options{
1✔
74
                BufferSize: &defaultBufferSize,
1✔
75
        }
1✔
76
        for _, opt := range opts {
2✔
77
                if opt.StaticCredentials != nil {
2✔
78
                        ret.StaticCredentials = opt.StaticCredentials
1✔
79
                }
1✔
80
                if opt.Region != nil {
2✔
81
                        ret.Region = opt.Region
1✔
82
                }
1✔
83
                if opt.ContentType != nil {
2✔
84
                        ret.ContentType = opt.ContentType
1✔
85
                }
1✔
86
                if opt.ExternalURI != nil {
2✔
87
                        ret.ExternalURI = opt.ExternalURI
1✔
88
                }
1✔
89
                if opt.URI != nil {
2✔
90
                        ret.URI = opt.URI
1✔
91
                }
1✔
92
                if opt.ForcePathStyle != ret.ForcePathStyle {
2✔
93
                        ret.ForcePathStyle = opt.ForcePathStyle
1✔
94
                }
1✔
95
                if opt.UseAccelerate != ret.UseAccelerate {
1✔
UNCOV
96
                        ret.UseAccelerate = opt.UseAccelerate
×
UNCOV
97
                }
×
98
                if opt.DefaultExpire != nil {
1✔
UNCOV
99
                        ret.DefaultExpire = opt.DefaultExpire
×
UNCOV
100
                }
×
101
                if opt.BufferSize != nil {
2✔
102
                        ret.BufferSize = opt.BufferSize
1✔
103
                }
1✔
104
        }
105
        return ret
1✔
106
}
107

108
func (opts Options) Validate() error {
1✔
109
        return validation.ValidateStruct(&opts,
1✔
110
                validation.Field(&opts.StaticCredentials),
1✔
111
                validation.Field(&opts.BufferSize, validAtLeast5MiB),
1✔
112
        )
1✔
113
}
1✔
114

115
func (opts *Options) SetStaticCredentials(key, secret, sessionToken string) *Options {
1✔
116
        opts.StaticCredentials = &StaticCredentials{
1✔
117
                Key:    key,
1✔
118
                Secret: secret,
1✔
119
                Token:  sessionToken,
1✔
120
        }
1✔
121
        return opts
1✔
122
}
1✔
123

124
func (opts *Options) SetRegion(region string) *Options {
1✔
125
        opts.Region = &region
1✔
126
        return opts
1✔
127
}
1✔
128

129
func (opts *Options) SetContentType(contentType string) *Options {
1✔
130
        opts.ContentType = &contentType
1✔
131
        return opts
1✔
132
}
1✔
133

UNCOV
134
func (opts *Options) SetFilenameSuffix(suffix string) *Options {
×
UNCOV
135
        opts.FilenameSuffix = &suffix
×
UNCOV
136
        return opts
×
UNCOV
137
}
×
138

139
func (opts *Options) SetExternalURI(externalURI string) *Options {
1✔
140
        opts.ExternalURI = &externalURI
1✔
141
        return opts
1✔
142
}
1✔
143

144
func (opts *Options) SetURI(URI string) *Options {
1✔
145
        opts.URI = &URI
1✔
146
        return opts
1✔
147
}
1✔
148

149
func (opts *Options) SetForcePathStyle(forcePathStyle bool) *Options {
1✔
150
        opts.ForcePathStyle = forcePathStyle
1✔
151
        return opts
1✔
152
}
1✔
153

154
func (opts *Options) SetUseAccelerate(useAccelerate bool) *Options {
1✔
155
        opts.UseAccelerate = useAccelerate
1✔
156
        return opts
1✔
157
}
1✔
158

UNCOV
159
func (opts *Options) SetDefaultExpire(defaultExpire time.Duration) *Options {
×
UNCOV
160
        opts.DefaultExpire = &defaultExpire
×
UNCOV
161
        return opts
×
UNCOV
162
}
×
163

164
func (opts *Options) SetBufferSize(bufferSize int) *Options {
1✔
165
        opts.BufferSize = &bufferSize
1✔
166
        return opts
1✔
167
}
1✔
168

169
func (opts *Options) toS3Options() (
170
        clientOpts func(*s3.Options),
171
        presignOpts func(*s3.PresignOptions),
172
) {
1✔
173
        clientOpts = func(s3Opts *s3.Options) {
2✔
174
                if opts.StaticCredentials != nil {
2✔
175
                        s3Opts.Credentials = *opts.StaticCredentials
1✔
176
                }
1✔
177
                if opts.Region != nil {
2✔
178
                        s3Opts.Region = *opts.Region
1✔
179
                }
1✔
180
                if opts.URI != nil {
2✔
181
                        endpointURI := *opts.URI
1✔
182
                        s3Opts.EndpointResolver = s3.EndpointResolverFromURL(endpointURI,
1✔
183
                                func(ep *aws.Endpoint) {
2✔
184
                                        ep.HostnameImmutable = true
1✔
185
                                },
1✔
186
                        )
187
                }
188
                s3Opts.UsePathStyle = opts.ForcePathStyle
1✔
189
                s3Opts.UseAccelerate = opts.UseAccelerate
1✔
190
                s3Opts.HTTPClient = &http.Client{
1✔
191
                        Transport: &http.Transport{
1✔
192
                                TLSClientConfig: &tls.Config{
1✔
193
                                        RootCAs: storage.GetRootCAs(),
1✔
194
                                },
1✔
195
                        },
1✔
196
                }
1✔
197
        }
198

199
        expires := DefaultExpire
1✔
200
        if opts.DefaultExpire != nil {
1✔
UNCOV
201
                expires = *opts.DefaultExpire
×
UNCOV
202
        }
×
203
        presignOpts = func(s3Opts *s3.PresignOptions) {
2✔
204
                s3.WithPresignExpires(expires)(s3Opts)
1✔
205
                if opts.ExternalURI != nil {
2✔
206
                        presignURL := *opts.ExternalURI
1✔
207
                        resolver := s3.EndpointResolverFromURL(presignURL,
1✔
208
                                func(ep *aws.Endpoint) {
2✔
209
                                        ep.HostnameImmutable = opts.ForcePathStyle
1✔
210
                                },
1✔
211
                        )
212
                        s3.WithPresignClientFromClientOptions(
1✔
213
                                s3.WithEndpointResolver(resolver),
1✔
214
                        )(s3Opts)
1✔
215
                }
216
        }
217
        return clientOpts, presignOpts
1✔
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc